Render rectangle with color interpolation
This commit is contained in:
parent
76e268e9ed
commit
8d76c8bcf0
@ -1,6 +1,7 @@
|
|||||||
package core.engine;
|
package core.engine;
|
||||||
|
|
||||||
import core.engine.model.RawModel;
|
import core.engine.model.RawModel;
|
||||||
|
import core.engine.shader.StaticShader;
|
||||||
import org.lwjgl.Version;
|
import org.lwjgl.Version;
|
||||||
import org.lwjgl.glfw.GLFWErrorCallback;
|
import org.lwjgl.glfw.GLFWErrorCallback;
|
||||||
import org.lwjgl.glfw.GLFWVidMode;
|
import org.lwjgl.glfw.GLFWVidMode;
|
||||||
@ -102,6 +103,7 @@ public class Engine {
|
|||||||
|
|
||||||
// Set the clear color
|
// Set the clear color
|
||||||
glClearColor(1.0f, 0.0f, 0.0f, 0.0f);
|
glClearColor(1.0f, 0.0f, 0.0f, 0.0f);
|
||||||
|
StaticShader shader = new StaticShader();
|
||||||
float[] vertices = {
|
float[] vertices = {
|
||||||
-0.5f, 0.5f, 0f,
|
-0.5f, 0.5f, 0f,
|
||||||
-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.
|
// the window or has pressed the ESCAPE key.
|
||||||
while ( !glfwWindowShouldClose(window) ) {
|
while ( !glfwWindowShouldClose(window) ) {
|
||||||
renderer.prepare();
|
renderer.prepare();
|
||||||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // clear the framebuffer
|
shader.start();
|
||||||
renderer.render(model);
|
renderer.render(model);
|
||||||
|
shader.stop();
|
||||||
glfwSwapBuffers(window); // swap the color buffers
|
glfwSwapBuffers(window); // swap the color buffers
|
||||||
|
|
||||||
// Poll for window events. The key callback above will only be
|
// Poll for window events. The key callback above will only be
|
||||||
// invoked during this call.
|
// invoked during this call.
|
||||||
glfwPollEvents();
|
glfwPollEvents();
|
||||||
}
|
}
|
||||||
|
shader.cleanUp();
|
||||||
loader.cleanUp();
|
loader.cleanUp();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -5,10 +5,12 @@ import org.lwjgl.opengl.GL11;
|
|||||||
import org.lwjgl.opengl.GL20;
|
import org.lwjgl.opengl.GL20;
|
||||||
import org.lwjgl.opengl.GL30;
|
import org.lwjgl.opengl.GL30;
|
||||||
|
|
||||||
|
import static org.lwjgl.opengl.GL11C.*;
|
||||||
|
|
||||||
public class Renderer {
|
public class Renderer {
|
||||||
|
|
||||||
public void prepare() {
|
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);
|
GL11.glClearColor(1,0,0,1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
77
client/src/main/java/core/engine/shader/ShaderProgram.java
Normal file
77
client/src/main/java/core/engine/shader/ShaderProgram.java
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
15
client/src/main/java/core/engine/shader/StaticShader.java
Normal file
15
client/src/main/java/core/engine/shader/StaticShader.java
Normal 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");
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,9 @@
|
|||||||
|
#version 400 core
|
||||||
|
|
||||||
|
in vec3 color;
|
||||||
|
|
||||||
|
out vec4 out_Color;
|
||||||
|
|
||||||
|
void main(void) {
|
||||||
|
out_Color = vec4(color, 1.0);
|
||||||
|
}
|
11
client/src/main/java/core/engine/shader/vertexShader.glsl
Normal file
11
client/src/main/java/core/engine/shader/vertexShader.glsl
Normal 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);
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user