setUniform4f in Shader

This commit is contained in:
Antoine 2021-05-14 15:34:08 +02:00
parent e4c1c80e4a
commit 049e735e1a
6 changed files with 59 additions and 2 deletions

View File

@ -1,7 +1,10 @@
#version 330 core
uniform float time;
out vec4 FragColor;
void main()
{
FragColor = vec4(1.0f, 0.5f, 0.2f, 1.0f);
FragColor = vec4(sin(time), sin(time/2), sin(time/4), 1.0f);
}

View File

@ -1,4 +1,7 @@
#version 330 core
uniform float time;
layout (location = 0) in vec3 aPos;
void main()

View File

@ -63,7 +63,7 @@ public class Engine {
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
System.out.println("OpenGL: " + glGetString(GL_VERSION));
this.scene = new Scene("shaders/vert.vert", "shaders/frag.frag", Primitive.rectangle, Primitive.rectangle_indices);
this.scene = new Scene("shaders/vert.vert", "shaders/frag.frag", Primitive.triangle, Primitive.triangle_indices);
}

View File

@ -2,6 +2,33 @@ package engine;
public class Primitive {
public float[] vertices;
public byte[] indices;
public Primitive(Vector3f vertex1, Vector3f vertex2, Vector3f vertex3){
vertices = new float[]{
vertex1.x, vertex1.y, vertex1.z,
vertex2.x, vertex2.y, vertex2.z,
vertex3.x, vertex3.y, vertex3.z
};
indices = new byte[]{
0, 1, 2
};
}
public Primitive(Vector3f vertex1, Vector3f vertex2, Vector3f vertex3, Vector3f vertex4){
vertices = new float[]{
vertex1.x, vertex1.y, vertex1.z,
vertex2.x, vertex2.y, vertex2.z,
vertex3.x, vertex3.y, vertex3.z,
vertex4.x, vertex4.y, vertex4.z
};
indices = new byte[]{
0, 1, 2,
1, 2, 3
};
}
public static float[] triangle = new float[] {
0.0f, 0.5f, 0.0f,
0.5f, -0.5f, 0.0f,
@ -12,6 +39,20 @@ public class Primitive {
0, 1, 2
};
public static float[] two_triangle = new float[] {
0.0f, 0.5f, 0.0f,
0.5f, -0.5f, 0.0f,
-0.5f, -0.5f, 0.0f,
-0.8f, -0.8f, 0.0f,
0.8f, -0.8f, 0.0f,
0.8f, -0.9f, 0.0f,
};
public static byte[] two_triangle_indices = new byte[]{
0, 1, 2,
3, 4, 5
};
public static float[] rectangle = new float[] {
0.5f, 0.5f, 0.0f, // top right
0.5f, -0.5f, 0.0f, // bottom right

View File

@ -1,5 +1,9 @@
package engine;
import static java.lang.Math.cos;
import static java.lang.Math.sin;
import static org.lwjgl.glfw.GLFW.glfwGetTime;
public class Scene {
float[] vertices;
@ -17,6 +21,7 @@ public class Scene {
public void render(){
this.shader.enable();
this.shader.setUniform1f("time", (float)glfwGetTime());
this.vertexArray.render();
this.shader.disable();
}

View File

@ -48,6 +48,11 @@ public class Shader {
glUniform3f(getUniform(name), vector.x, vector.y, vector.z);
}
public void setUniform4f(String name, float x, float y, float z, float w) {
if (!enabled) enable();
glUniform4f(getUniform(name), x, y, z, w);
}
public void setUniformMat4f(String name, Matrix4f matrix){
if (!enabled) enable();
glUniformMatrix4fv(getUniform(name), false, matrix.toFloatBuffer());