added ObjectGlTexColor, tweaked shaders

This commit is contained in:
Antoine 2021-05-19 05:37:45 +02:00
parent 58e94314cc
commit 7f491ad597
10 changed files with 51 additions and 33 deletions

View File

@ -1,10 +1,10 @@
#version 330 core #version 330 core
in vec3 Color; in vec3 color;
out vec4 FragColor; out vec4 FragColor;
void main() void main()
{ {
FragColor = vec4(Color, 1.0f); FragColor = vec4(color, 1.0f);
} }

View File

@ -3,10 +3,12 @@
layout (location = 0) in vec3 aPos; layout (location = 0) in vec3 aPos;
layout (location = 1) in vec3 aColor; layout (location = 1) in vec3 aColor;
out vec3 Color; out vec3 color;
uniform mat4 transform;
void main() void main()
{ {
gl_Position = vec4(aPos, 1.0); gl_Position = transform * vec4(aPos, 1.0);
Color = aColor; color = aColor;
} }

View File

@ -1,10 +1,13 @@
#version 330 core #version 330 core
in vec3 Color;
out vec4 FragColor; out vec4 FragColor;
in vec2 TexCoord;
uniform sampler2D texture1;
uniform sampler2D texture2;
void main() void main()
{ {
FragColor = vec4(Color, 1.0f); FragColor = mix(texture(texture1, TexCoord), texture(texture2, vec2(TexCoord)), 0.5);
} }

View File

@ -1,12 +1,14 @@
#version 330 core #version 330 core
layout (location = 0) in vec3 aPos; layout (location = 0) in vec3 aPos;
layout (location = 1) in vec3 aColor; layout (location = 2) in vec3 aTexCoord;
out vec3 Color; out vec3 texCoord;
uniform mat4 transform;
void main() void main()
{ {
gl_Position = vec4(aPos, 1.0); gl_Position = transform * vec4(aPos, 1.0);
Color = aColor; texCoord = aTexCoord;
} }

View File

@ -3,12 +3,11 @@ out vec4 FragColor;
in vec3 ourColor; in vec3 ourColor;
in vec2 TexCoord; in vec2 TexCoord;
in vec4 position;
uniform sampler2D texture1; uniform sampler2D texture1;
uniform sampler2D texture2; uniform sampler2D texture2;
void main() void main()
{ {
FragColor = mix(texture(texture1, TexCoord), texture(texture2, vec2(TexCoord.x, -TexCoord.y)), position.x * position.y); FragColor = Color * mix(texture(texture1, TexCoord), texture(texture2, TexCoord), 0.5);
} }

View File

@ -1,18 +1,16 @@
#version 330 core #version 330 core
layout (location = 0) in vec3 aPos; layout (location = 0) in vec3 aPos;
layout (location = 1) in vec3 aColor; layout (location = 1) in vec3 aColor;
layout (location = 2) in vec2 aTexCoord; layout (location = 2) in vec3 aTexCoord;
out vec3 ourColor; out vec3 texCoord;
out vec2 TexCoord;
out vec4 position;
uniform mat4 transform; uniform mat4 transform;
void main() void main()
{ {
gl_Position = transform * vec4(aPos, 1.0); gl_Position = transform * vec4(aPos, 1.0);
ourColor = aColor; Color = aColor;
TexCoord = aTexCoord; texCoord = aTexCoord;
position = gl_Position;
} }

View File

@ -2,6 +2,7 @@ package engine;
import engine.math.Vector3f; import engine.math.Vector3f;
import engine.object.ObjectGl; import engine.object.ObjectGl;
import engine.object.ObjectGlColor;
import org.lwjgl.glfw.GLFWFramebufferSizeCallback; import org.lwjgl.glfw.GLFWFramebufferSizeCallback;
import org.lwjgl.glfw.GLFWVidMode; import org.lwjgl.glfw.GLFWVidMode;
import org.lwjgl.opengl.GL; import org.lwjgl.opengl.GL;
@ -131,7 +132,7 @@ public class Engine {
engine.init(); engine.init();
// Add objects to render // Add objects to render
ObjectGl cube = new ObjectGl(-0.5f,0.5f,0.0f,1.0f,1.0f); ObjectGl cube = new ObjectGlColor(-0.5f,0.5f,0.0f,1.0f,1.0f, new float[] {0.2f, 0.2f, 0.8f});
engine.add_objectGl(cube); engine.add_objectGl(cube);
while(engine.isRunning()){ while(engine.isRunning()){

View File

@ -6,18 +6,10 @@ import engine.graphics.VertexArray;
import engine.math.Matrix4f; import engine.math.Matrix4f;
public class ObjectGlColor extends ObjectGl{ public class ObjectGlColor extends ObjectGl{
public ObjectGlColor(float x, float y, float z, float h, float w, float[] color) { public ObjectGlColor(float x, float y, float z, float h, float w, float[] color) {
super(); super();
this.vertexArray = new VertexArray(Primitive.createRectangle(x, y, z, h, w), Primitive.rectangle_indices, color, null); this.vertexArray = new VertexArray(Primitive.createRectangle(x, y, z, h, w), Primitive.rectangle_indices, color, null);
this.transform = Matrix4f.identity(); this.transform = Matrix4f.identity();
this.shader = new Shader("shaders/ObjectGlColor/vert.glsl","shaders/ObjectGlColor/frag.glsl"); this.shader = new Shader("shaders/ObjectGlColor/vert.glsl","shaders/ObjectGlColor/frag.glsl");
} }
@Override
public void render() {
this.shader.enable();
this.vertexArray.render();
this.shader.disable();
}
} }

View File

@ -10,7 +10,7 @@ import java.util.List;
public class ObjectGlTex extends ObjectGl{ public class ObjectGlTex extends ObjectGl{
List<Texture> textures; protected List<Texture> textures;
public ObjectGlTex(){ public ObjectGlTex(){
@ -22,13 +22,29 @@ public class ObjectGlTex extends ObjectGl{
this.transform = Matrix4f.identity(); this.transform = Matrix4f.identity();
this.shader = new Shader("shaders/ObjectGlTex/vert.glsl","shaders/ObjectGlTex/frag.glsl"); this.shader = new Shader("shaders/ObjectGlTex/vert.glsl","shaders/ObjectGlTex/frag.glsl");
// TODO tex int count = 0;
for (String path : texPath){
textures.add(new Texture(path, count));
count++;
}
} }
@Override @Override
public void render() { public void render() {
this.shader.enable(); this.shader.enable();
this.shader.setUniformMat4f("transform", this.transform);
for (Texture t : textures){
t.bind();
}
this.vertexArray.render(); this.vertexArray.render();
for (Texture t : textures){
t.unbind();
}
this.shader.disable(); this.shader.disable();
} }
} }

View File

@ -1,5 +1,6 @@
package engine.object; package engine.object;
import engine.graphics.Texture;
import engine.math.Primitive; import engine.math.Primitive;
import engine.graphics.Shader; import engine.graphics.Shader;
import engine.graphics.VertexArray; import engine.graphics.VertexArray;
@ -15,6 +16,10 @@ public class ObjectGlTexColor extends ObjectGlTex{
this.transform = Matrix4f.identity(); this.transform = Matrix4f.identity();
this.shader = new Shader("shaders/ObjectGlTexColor/vert.glsl","shaders/ObjectGlTexColor/frag.glsl"); this.shader = new Shader("shaders/ObjectGlTexColor/vert.glsl","shaders/ObjectGlTexColor/frag.glsl");
// TODO Create texture int count = 0;
for (String path : texPath){
textures.add(new Texture(path, count));
count++;
}
} }
} }