TriangleGPURenderer
Very simplistic GPU renderer of a triangle gradient using LWJGL and some GLSL for the vertex and fragment shader files. Author: [sebastian.albu@tugraz.at]
Simple Triangle Renderer with LWJGL and GLSL
This project demonstrates a real-time rendering example using LWJGL (Lightweight Java Game Library) and GLSL shaders. It draws a simple triangle that dynamically changes colors based on time.
Features
- Real-time Rendering: Renders a triangle with OpenGL.
- GLSL Shaders: Uses vertex and fragment shaders to process geometry and apply dynamic color effects.
- GLFW Integration: Manages the window and rendering loop.
Project Structure
Source Files
File | Description |
---|---|
GpuRenderer.java |
The main rendering logic. Sets up OpenGL, compiles shaders, and manages the rendering loop. |
ShaderUtils.java |
Utility class for loading, compiling, and linking GLSL shaders. |
vertexShader.vert |
Vertex shader: Handles geometry transformation and passes color data to the fragment shader. |
fragmentShader.frag |
Fragment shader: Handles pixel-level coloring, including the real-time dynamic color-changing effect. |
GLSL Files
File | GLSL Functionality |
---|---|
vertexShader.vert |
- Transforms vertex positions to clip space. - Passes vertex colors ( vColor ) to the fragment shader. |
fragmentShader.frag |
- Receives interpolated colors from the vertex shader. - Applies time-driven color changes using uTime . |
Key GLFW/OpenGL Methods
Method | Description |
---|---|
glfwInit() |
Initializes GLFW. |
glfwCreateWindow() |
Creates a rendering window. |
glfwMakeContextCurrent() |
Sets the current OpenGL context. |
glfwSwapBuffers() |
Swaps the front and back buffers for double buffering. |
glfwPollEvents() |
Polls for window events, such as input and closing. |
glGenVertexArrays() |
Generates a Vertex Array Object (VAO) to store vertex attribute configurations. |
glGenBuffers() |
Generates a Vertex Buffer Object (VBO) for storing vertex data. |
glVertexAttribPointer() |
Defines how vertex attributes (positions, colors, etc.) are interpreted from the buffer. |
glUseProgram() |
Activates a shader program for rendering. |
glUniform1f() |
Sets a uniform float variable (uTime in this case) in the active shader program. |
How to Run the Project
Prerequisites
- Install Java 17 or higher (this project uses Java 17).
- Install Maven (required for dependency management).
- Install IntelliJ IDEA (Community or Ultimate Edition).
- Ensure you have OpenGL 3.3 or later supported on your system.
Steps to Run
-
Clone the Project: Clone the repository or download the files to your local machine.
git clone <repository-url> cd <project-folder>
-
Open in IntelliJ:
- Open IntelliJ IDEA.
- Click
File > Open
and select the project folder. - Ensure IntelliJ recognizes the
pom.xml
file and automatically loads dependencies.
-
Set the Working Directory:
- Go to
Run > Edit Configurations
. - In the Run/Debug Configurations window, select the
GpuRenderer
configuration. - Set the Working Directory to the project root (where
resources
is located).
- Go to
-
Run the Project:
- Right-click
GpuRenderer.java
in the project structure. - Select
Run GpuRenderer.main()
.
- Right-click
-
Expected Output:
- A window will open displaying a triangle.
- The triangle will dynamically change colors in real time.
File Overview
GpuRenderer.java
Handles the OpenGL rendering process:
- Initializes GLFW and OpenGL context.
- Compiles and links shaders using
ShaderUtils
. - Creates a triangle with vertex positions and colors.
- Passes time (
uTime
) to the fragment shader for dynamic effects.
ShaderUtils.java
Provides helper methods for shader management:
-
createShaderProgram()
: Compiles and links vertex and fragment shaders into a single shader program. -
readFile()
: Reads the GLSL shader files from the specified path. -
createShader()
: Compiles a GLSL shader of a specified type (vertex or fragment).
vertexShader.vert
Vertex shader:
- Converts vertex coordinates from model space to clip space (
gl_Position
). - Passes interpolated color data (
vColor
) to the fragment shader.
fragmentShader.frag
Fragment shader:
- Modulates the triangle's color based on time (
uTime
) using sine functions for smooth transitions.
Dependencies
The project uses Maven for dependency management. Key dependencies include:
- LWJGL 3: For OpenGL, GLFW, and related bindings.
pom.xml
<dependencies>
<dependency>
<groupId>org.lwjgl</groupId>
<artifactId>lwjgl</artifactId>
<version>3.3.1</version>
</dependency>
<dependency>
<groupId>org.lwjgl</groupId>
<artifactId>lwjgl-opengl</artifactId>
<version>3.3.1</version>
</dependency>
<dependency>
<groupId>org.lwjgl</groupId>
<artifactId>lwjgl-glfw</artifactId>
<version>3.3.1</version>
</dependency>
</dependencies>
Customization Ideas
- Add Mouse Interaction: Use GLFW input functions to interact with the triangle (e.g., changing its size or position).
- Dynamic Vertex Data: Modify vertex positions or colors in real-time for more dynamic visuals.
- Add More Shapes: Experiment with rendering additional shapes or complex objects.