Render rectangle with color interpolation

This commit is contained in:
Sebastian 2023-08-04 08:39:05 +02:00
parent 76e268e9ed
commit 8d76c8bcf0
6 changed files with 120 additions and 3 deletions

View File

@ -1,6 +1,7 @@
package core.engine;
import core.engine.model.RawModel;
import core.engine.shader.StaticShader;
import org.lwjgl.Version;
import org.lwjgl.glfw.GLFWErrorCallback;
import org.lwjgl.glfw.GLFWVidMode;
@ -102,6 +103,7 @@ public class Engine {
// Set the clear color
glClearColor(1.0f, 0.0f, 0.0f, 0.0f);
StaticShader shader = new StaticShader();
float[] vertices = {
-0.5f, 0.5f, 0f,
-0.5f, -0.5f, 0f,
@ -117,15 +119,16 @@ public class Engine {
// the window or has pressed the ESCAPE key.
while ( !glfwWindowShouldClose(window) ) {
renderer.prepare();
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // clear the framebuffer
shader.start();
renderer.render(model);
shader.stop();
glfwSwapBuffers(window); // swap the color buffers
// Poll for window events. The key callback above will only be
// invoked during this call.
glfwPollEvents();
}
shader.cleanUp();
loader.cleanUp();
}

View File

@ -5,10 +5,12 @@ import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.GL20;
import org.lwjgl.opengl.GL30;
import static org.lwjgl.opengl.GL11C.*;
public class Renderer {
public void prepare() {
GL11.glClear(GL11.GL_COLOR_BUFFER_BIT);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // clear the framebuffer
GL11.glClearColor(1,0,0,1);
}

View File

@ -0,0 +1,77 @@
package core.engine.shader;
import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.GL20;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
public abstract class ShaderProgram {
private int programID;
private int vertexShaderID;
private int fragmentShaderID;
public ShaderProgram(String vertexFile, String fragmentFile) {
vertexShaderID = loadShader(vertexFile, GL20.GL_VERTEX_SHADER);
fragmentShaderID = loadShader(fragmentFile, GL20.GL_FRAGMENT_SHADER);
programID = GL20.glCreateProgram();
GL20.glAttachShader(programID, vertexShaderID);
GL20.glAttachShader(programID, fragmentShaderID);
GL20.glLinkProgram(programID);
GL20.glValidateProgram(programID);
bindAttributes();
}
public void start() {
GL20.glUseProgram(programID);
}
public void stop() {
GL20.glUseProgram(0);
}
public void cleanUp() {
stop();
GL20.glDetachShader(programID, vertexShaderID);
GL20.glDetachShader(programID, fragmentShaderID);
GL20.glDeleteShader(vertexShaderID);
GL20.glDeleteShader(fragmentShaderID);
GL20.glDeleteProgram(programID);
}
protected abstract void bindAttributes();
protected void bindAttribute(int attribute, String variableName) {
GL20.glBindAttribLocation(programID, attribute, variableName);
}
private static int loadShader(String file, int type) {
StringBuilder shadersource = new StringBuilder();
try {
BufferedReader bufferedReader = new BufferedReader(new FileReader(file));
String line;
while((line = bufferedReader.readLine()) != null) {
shadersource.append(line).append(System.lineSeparator());
}
bufferedReader.close();
} catch (IOException e) {
System.err.println("Could not read file!");
e.printStackTrace();
System.exit(-1);
}
int shaderID = GL20.glCreateShader(type);
GL20.glShaderSource(shaderID, shadersource);
GL20.glCompileShader(shaderID);
if(GL20.glGetShaderi(shaderID, GL20.GL_COMPILE_STATUS) == GL11.GL_FALSE) {
System.out.println(GL20.glGetShaderInfoLog(shaderID));
System.err.println("Could not compile Shader");
System.exit(-1);
}
return shaderID;
}
}

View File

@ -0,0 +1,15 @@
package core.engine.shader;
public class StaticShader extends ShaderProgram{
private static final String VERTEX_FILE = "src/main/java/core/engine/shader/vertexShader.glsl";
private static final String FRAGMENT_FILE = "src/main/java/core/engine/shader/fragmentShader.glsl";
public StaticShader() {
super(VERTEX_FILE, FRAGMENT_FILE);
}
@Override
protected void bindAttributes() {
super.bindAttribute(0, "position");
}
}

View File

@ -0,0 +1,9 @@
#version 400 core
in vec3 color;
out vec4 out_Color;
void main(void) {
out_Color = vec4(color, 1.0);
}

View File

@ -0,0 +1,11 @@
#version 400 core
in vec3 position;
out vec3 color;
void main(void) {
gl_Position = vec4(position, 1.0);
color = vec3(position.x+0.5, 1.0, position.y+0.5);
}