Added scaling operation in Matrix4f

This commit is contained in:
Antoine 2021-05-18 19:01:18 +02:00
parent 2aa23512be
commit 4abc87597d
6 changed files with 98 additions and 7 deletions

10
shaders/frag.glsl Normal file
View File

@ -0,0 +1,10 @@
#version 330 core
out vec4 FragColor;
in vec4 position;
in vec4 Color;
void main()
{
FragColor = Color;
}

12
shaders/vert.glsl Normal file
View File

@ -0,0 +1,12 @@
#version 330 core
layout (location = 0) in vec3 aPos;
layout (location = 1) in vec3 aColor;
out vec4 position;
out vec4 Color;
void main()
{
gl_Position = vec4(aPos, 1.0);
Color = vec4(aColor, 1.0f);
}

View File

@ -21,6 +21,7 @@ public class Engine {
private long window;
private Scene scene;
private Scene scene2;
private boolean running;
@ -65,6 +66,7 @@ public class Engine {
System.out.println("OpenGL: " + glGetString(GL_VERSION));
this.scene = new Scene("shaders/vert.vert", "shaders/frag.frag", Primitive.rectangle, Primitive.rectangle_indices, Primitive.rectangle_color, Primitive.rectangle_texture);
this.scene2 = new Scene("shaders/vert.glsl", "shaders/frag.frag", Primitive.triangle, Primitive.triangle_indices);
}
@ -76,6 +78,7 @@ public class Engine {
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); //SWAP avec le precedent
scene.render();
scene2.render();
int error = glGetError();
if (error != GL_NO_ERROR) System.out.println(error);
glfwSwapBuffers(window); //Envoie le buffer vers le moniteur

50
src/engine/ObjectGl.java Normal file
View File

@ -0,0 +1,50 @@
package engine;
import engine.graphics.VertexArray;
import engine.graphics.Shader;
import engine.math.Matrix4f;
import engine.math.Vector3f;
public class ObjectGl {
private VertexArray vertexArray;
private Shader shader;
private Matrix4f transform;
public ObjectGl(float x, float y, float h, float w){
// TODO Create a rectangle
this.transform = Matrix4f.identity();
}
public void resetTransform(){
this.transform = Matrix4f.identity();
}
public void translate(Vector3f vec){
this.transform = this.transform.multiply(Matrix4f.translate(vec));
}
public void scale(Vector3f vec){
this.transform = this.transform.multiply(Matrix4f.scale(vec));
}
public void rotateX(float angle){
this.transform = this.transform.multiply(Matrix4f.rotateX(angle));
}
public void rotateY(float angle){
this.transform = this.transform.multiply(Matrix4f.rotateY(angle));
}
public void rotateZ(float angle){
this.transform = this.transform.multiply(Matrix4f.rotateZ(angle));
}
public void render(){
this.shader.enable();
this.vertexArray.render();
this.shader.disable();
}
}

View File

@ -46,15 +46,22 @@ public class Scene {
}
public void render(){
this.transform = this.transform.multiply(Matrix4f.rotateZ(1.0f));
this.transform = this.transform.multiply(Matrix4f.translate(new Vector3f(-0.001f, 0.0f, 0.0f)));
shader.setUniformMat4f("transform", this.transform);
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();
this.texture_map.bind();
this.texture_map2.bind();
if (this.texture_map != null){
this.texture_map.bind();
this.texture_map2.bind();
}
this.vertexArray.render();
this.texture_map2.unbind();
this.texture_map.unbind();
if (this.texture_map != null) {
this.texture_map2.unbind();
this.texture_map.unbind();
}
this.shader.disable();
}
}

View File

@ -48,6 +48,15 @@ public class Matrix4f {
return result;
}
public static Matrix4f scale(Vector3f vector){
Matrix4f result= identity();
result.elements[0 + 0*4] = vector.x;
result.elements[1 + 1*4] = vector.y;
result.elements[2 + 2*4] = vector.z;
return result;
}
public static Matrix4f rotateX(float angle){
Matrix4f result = identity();
float r = (float) Math.toRadians(angle);