ObjectGlTex & ObjectGlTexColor now working :o)

This commit is contained in:
Antoine 2021-05-19 15:00:12 +02:00
parent 7f491ad597
commit 57b6be084d
8 changed files with 26 additions and 82 deletions

View File

@ -2,12 +2,11 @@
out vec4 FragColor; out vec4 FragColor;
in vec2 TexCoord; in vec2 texCoord;
uniform sampler2D texture1; uniform sampler2D texture1;
uniform sampler2D texture2;
void main() void main()
{ {
FragColor = mix(texture(texture1, TexCoord), texture(texture2, vec2(TexCoord)), 0.5); FragColor = texture(texture1, vec2(texCoord.y, -texCoord.x));
} }

View File

@ -1,9 +1,9 @@
#version 330 core #version 330 core
layout (location = 0) in vec3 aPos; layout (location = 0) in vec3 aPos;
layout (location = 2) in vec3 aTexCoord; layout (location = 2) in vec2 aTexCoord;
out vec3 texCoord; out vec2 texCoord;
uniform mat4 transform; uniform mat4 transform;

View File

@ -1,13 +1,13 @@
#version 330 core #version 330 core
out vec4 FragColor; out vec4 FragColor;
in vec3 ourColor; in vec4 color;
in vec2 TexCoord; in vec2 texCoord;
uniform sampler2D texture1; uniform sampler2D texture1;
uniform sampler2D texture2;
void main() void main()
{ {
FragColor = Color * mix(texture(texture1, TexCoord), texture(texture2, TexCoord), 0.5); FragColor = texture(texture1, vec2(texCoord.y, -texCoord.x)) * color;
} }

View File

@ -2,15 +2,16 @@
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 vec3 aTexCoord; layout (location = 2) in vec2 aTexCoord;
out vec3 texCoord; out vec2 texCoord;
out vec4 color;
uniform mat4 transform; uniform mat4 transform;
void main() void main()
{ {
gl_Position = transform * vec4(aPos, 1.0); gl_Position = transform * vec4(aPos, 1.0);
Color = aColor; color = vec4(aColor, 1.0f);
texCoord = aTexCoord; texCoord = aTexCoord;
} }

View File

@ -1,8 +1,11 @@
package engine; package engine;
import engine.math.Primitive;
import engine.math.Vector3f; import engine.math.Vector3f;
import engine.object.ObjectGl; import engine.object.ObjectGl;
import engine.object.ObjectGlColor; import engine.object.ObjectGlColor;
import engine.object.ObjectGlTex;
import engine.object.ObjectGlTexColor;
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;
@ -68,6 +71,10 @@ public class Engine {
glfwSetFramebufferSizeCallback(window, resizeWindow); glfwSetFramebufferSizeCallback(window, resizeWindow);
glEnable(GL_DEPTH_TEST); // Z-Buffer glEnable(GL_DEPTH_TEST); // Z-Buffer
glEnable(GL_BLEND); // Transparence
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glClearColor(0.2f, 0.3f, 0.3f, 1.0f); glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
System.out.println("OpenGL: " + glGetString(GL_VERSION)); System.out.println("OpenGL: " + glGetString(GL_VERSION));
} }
@ -132,14 +139,14 @@ public class Engine {
engine.init(); engine.init();
// Add objects to render // Add objects to render
ObjectGl cube = new ObjectGlColor(-0.5f,0.5f,0.0f,1.0f,1.0f, new float[] {0.2f, 0.2f, 0.8f}); List<String> path = new ArrayList<>();
path.add("textures/awesomeface.png");
ObjectGlTexColor cube = new ObjectGlTexColor(-0.5f,0.5f,0.0f,1.0f,1.0f, path, Primitive.stdTexWrap, new float[] {1.0f,1.0f,1.0f});
engine.add_objectGl(cube); engine.add_objectGl(cube);
while(engine.isRunning()){ while(engine.isRunning()){
// Game logic should fit here // Game logic should fit here
cube.rotateZ(1.0f);
cube.translate(new Vector3f(0.01f, 0.0f, 0.0f));
//essential part v //essential part v
engine.update(); engine.update();

View File

@ -1,67 +0,0 @@
package engine;
import engine.graphics.Shader;
import engine.graphics.Texture;
import engine.graphics.VertexArray;
import engine.math.Matrix4f;
import engine.math.Vector3f;
public class Scene {
float[] vertices;
float[] color;
float[] texture;
float[] texture2;
Texture texture_map;
Texture texture_map2;
byte[] indices;
Matrix4f transform;
VertexArray vertexArray;
Shader shader;
public Scene(String vertPath, String fragPath, float[] vertices, byte[] indices){
this.vertices = vertices;
this.indices = indices;
this.shader = new Shader(vertPath, fragPath);
this.vertexArray = new VertexArray(this.vertices, this.indices, null, null);
}
public Scene(String vertPath, String fragPath, float[] vertices, byte[] indices, float[] color, float[] texture){
this.vertices = vertices;
this.indices = indices;
this.color = color;
this.shader = new Shader(vertPath, fragPath);
this.texture_map = new Texture("textures/container.jpg", 0);
this.texture = texture;
this.texture_map2 = new Texture("textures/awesomeface.png", 1);
this.texture2 = texture;
this.vertexArray = new VertexArray(this.vertices, this.indices, this.color, this.texture);
shader.setUniform1i("texture1", 0);
shader.setUniform1i("texture2", 1);
this.transform = Matrix4f.translate(new Vector3f(-0.1f, 0.2f, 0.0f));
this.transform = this.transform.multiply(Matrix4f.rotateZ(90.0f));
shader.setUniformMat4f("transform", this.transform);
}
public void render(){
if (this.transform != null){
this.transform = this.transform.multiply(Matrix4f.rotateZ(1.0f));
this.transform = this.transform.multiply(Matrix4f.translate(new Vector3f(-0.001f, 0.0f, 0.0f)));
this.transform = this.transform.multiply(Matrix4f.rotateX(1.0f));
shader.setUniformMat4f("transform", this.transform);
}
this.shader.enable();
if (this.texture_map != null){
this.texture_map.bind();
this.texture_map2.bind();
}
this.vertexArray.render();
if (this.texture_map != null) {
this.texture_map2.unbind();
this.texture_map.unbind();
}
this.shader.disable();
}
}

View File

@ -6,6 +6,7 @@ import engine.graphics.Texture;
import engine.graphics.VertexArray; import engine.graphics.VertexArray;
import engine.math.Matrix4f; import engine.math.Matrix4f;
import java.util.ArrayList;
import java.util.List; import java.util.List;
public class ObjectGlTex extends ObjectGl{ public class ObjectGlTex extends ObjectGl{
@ -22,6 +23,7 @@ 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");
this.textures = new ArrayList<>();
int count = 0; int count = 0;
for (String path : texPath){ for (String path : texPath){
textures.add(new Texture(path, count)); textures.add(new Texture(path, count));

View File

@ -6,6 +6,7 @@ import engine.graphics.Shader;
import engine.graphics.VertexArray; import engine.graphics.VertexArray;
import engine.math.Matrix4f; import engine.math.Matrix4f;
import java.util.ArrayList;
import java.util.List; import java.util.List;
public class ObjectGlTexColor extends ObjectGlTex{ public class ObjectGlTexColor extends ObjectGlTex{
@ -16,6 +17,7 @@ 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");
this.textures = new ArrayList<>();
int count = 0; int count = 0;
for (String path : texPath){ for (String path : texPath){
textures.add(new Texture(path, count)); textures.add(new Texture(path, count));