Orthographic camera, viewpoint, correct aspect ratio

This commit is contained in:
Antoine 2021-05-19 17:59:05 +02:00
parent 57b6be084d
commit e9935403b6
7 changed files with 47 additions and 14 deletions

View File

@ -2,9 +2,11 @@
layout (location = 0) in vec3 aPos; layout (location = 0) in vec3 aPos;
uniform mat4 projection;
uniform mat4 view;
uniform mat4 transform; uniform mat4 transform;
void main() void main()
{ {
gl_Position = transform * vec4(aPos, 1.0); gl_Position = projection * view * transform * vec4(aPos, 1.0);
} }

View File

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

View File

@ -5,10 +5,12 @@ layout (location = 2) in vec2 aTexCoord;
out vec2 texCoord; out vec2 texCoord;
uniform mat4 projection;
uniform mat4 view;
uniform mat4 transform; uniform mat4 transform;
void main() void main()
{ {
gl_Position = transform * vec4(aPos, 1.0); gl_Position = projection * view * transform * vec4(aPos, 1.0);
texCoord = aTexCoord; texCoord = aTexCoord;
} }

View File

@ -7,11 +7,13 @@ layout (location = 2) in vec2 aTexCoord;
out vec2 texCoord; out vec2 texCoord;
out vec4 color; out vec4 color;
uniform mat4 projection;
uniform mat4 view;
uniform mat4 transform; uniform mat4 transform;
void main() void main()
{ {
gl_Position = transform * vec4(aPos, 1.0); gl_Position = projection * view * transform * vec4(aPos, 1.0);
color = vec4(aColor, 1.0f); color = vec4(aColor, 1.0f);
texCoord = aTexCoord; texCoord = aTexCoord;
} }

View File

@ -1,11 +1,7 @@
package engine; package engine;
import engine.math.Primitive; import engine.math.*;
import engine.math.Vector3f; import engine.object.*;
import engine.object.ObjectGl;
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;
@ -31,6 +27,8 @@ public class Engine {
public Engine() { public Engine() {
this.running = false; this.running = false;
this.objectsGl = new ArrayList<>(); this.objectsGl = new ArrayList<>();
ObjectGl.projection = Matrix4f.orthographic(-10.0f, 10.0f, -10.0f * 9.0f / 16.0f, 10.0f * 9.0f / 16.0f, 0.1f, 100.0f);
ObjectGl.view = Matrix4f.translate(new Vector3f(0.0f,0.0f,1.0f));
} }
/** /**
@ -70,7 +68,7 @@ public class Engine {
glfwSetFramebufferSizeCallback(window, resizeWindow); glfwSetFramebufferSizeCallback(window, resizeWindow);
glEnable(GL_DEPTH_TEST); // Z-Buffer glEnable(GL_DEPTH_TEST); // Z-Buffer plus z est petit plus l'objet est proche de la camera limite à 0.1f au dela l'objet disparait
glEnable(GL_BLEND); // Transparence glEnable(GL_BLEND); // Transparence
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
@ -141,16 +139,34 @@ public class Engine {
// Add objects to render // Add objects to render
List<String> path = new ArrayList<>(); List<String> path = new ArrayList<>();
path.add("textures/awesomeface.png"); 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); ObjectGl smiley = new ObjectGlTex(-0.5f,0.5f,0.0f,1.0f,1.0f, path, Primitive.stdTexWrap);
engine.add_objectGl(smiley);
ObjectGl smiley2 = new ObjectGlTex(-0.5f,0.5f,0.0f,1.0f,1.0f, path, Primitive.stdTexWrap);
engine.add_objectGl(smiley2);
smiley2.translate(new Vector3f(0.5f,0.0f,10.0f));
long timer = System.currentTimeMillis();
int frame = 0;
while(engine.isRunning()){ while(engine.isRunning()){
// Game logic should fit here // Game logic should fit here
smiley.rotateY(1.0f);
smiley2.rotateY(0.8f);
//essential part v //essential part v
engine.update(); engine.update();
engine.render(); engine.render();
frame++;
if (System.currentTimeMillis() - timer > 1000) {
timer += 1000;
System.out.println("FPS: " + frame);
frame = 0;
}
if(engine.shouldClose()) engine.setRunning(false); if(engine.shouldClose()) engine.setRunning(false);
} }
} }

View File

@ -15,6 +15,9 @@ public class ObjectGl {
protected Shader shader; protected Shader shader;
protected Matrix4f transform; protected Matrix4f transform;
public static Matrix4f projection;
public static Matrix4f view;
public ObjectGl(){ public ObjectGl(){
} }
@ -51,7 +54,11 @@ public class ObjectGl {
public void render(){ public void render(){
this.shader.enable(); this.shader.enable();
this.shader.setUniformMat4f("projection", projection);
this.shader.setUniformMat4f("view", view);
this.shader.setUniformMat4f("transform", this.transform); this.shader.setUniformMat4f("transform", this.transform);
this.vertexArray.render(); this.vertexArray.render();
this.shader.disable(); this.shader.disable();
} }

View File

@ -35,6 +35,8 @@ public class ObjectGlTex extends ObjectGl{
public void render() { public void render() {
this.shader.enable(); this.shader.enable();
this.shader.setUniformMat4f("projection", projection);
this.shader.setUniformMat4f("view", view);
this.shader.setUniformMat4f("transform", this.transform); this.shader.setUniformMat4f("transform", this.transform);
for (Texture t : textures){ for (Texture t : textures){