Ajout d'un canal texture dans le vertexArray

This commit is contained in:
Antoine 2021-05-14 20:46:17 +02:00
parent e1cbbb85eb
commit 5d93325b3d
6 changed files with 65 additions and 19 deletions

View File

@ -1,10 +1,12 @@
#version 330 core
out vec4 FragColor;
in vec3 Color;
in vec3 ourColor;
in vec2 TexCoord;
uniform sampler2D ourTexture;
void main()
{
FragColor = vec4(Color, 1.0);
FragColor = texture(ourTexture, TexCoord) * vec4(ourColor, 1.0f);
}

View File

@ -1,12 +1,14 @@
#version 330 core
layout (location = 0) in vec3 aPos; // la variable aPos a l'attribut de position 0
layout (location = 0) in vec3 aPos;
layout (location = 1) in vec3 aColor;
layout (location = 2) in vec2 aTexCoord;
out vec3 Color;
out vec3 ourColor;
out vec2 TexCoord;
void main()
{
gl_Position = vec4(aPos, 1.0);
Color = aColor;
ourColor = aColor;
TexCoord = aTexCoord;
}

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.triangle, Primitive.triangle_indices, Primitive.triangle_couleur);
this.scene = new Scene("shaders/vert.vert", "shaders/frag.frag", Primitive.rectangle, Primitive.rectangle_indices, Primitive.rectangle_color, Primitive.rectangle_texture);
}

View File

@ -66,12 +66,18 @@ public class Primitive {
-0.5f, 0.5f, 0.0f // top left
};
public static float[] rectangle_color = new float[]{
1.0f, 0.0f, 0.0f, // top right
0.0f, 1.0f, 0.0f, // bottom right
0.0f, 0.0f, 1.0f, // bottom left
1.0f, 1.0f, 0.0f, // top left
};
public static float[] rectangle_texture = new float[]{
// positions // colors // texture coords
0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 0.0f, // top right
0.5f, -0.5f, 0.0f, 0.0f, 1.0f, 0.0f, // bottom right
-0.5f, -0.5f, 0.0f, 0.0f, 0.0f, 1.0f, // bottom left
-0.5f, 0.5f, 0.0f, 1.0f, 1.0f, 0.0f, // top left
1.0f, 1.0f,
1.0f, 0.0f,
0.0f, 0.0f,
0.0f, 1.0f
};
public static byte[] rectangle_indices = new byte[] {

View File

@ -1,13 +1,13 @@
package engine;
import static java.lang.Math.cos;
import static java.lang.Math.sin;
import static org.lwjgl.glfw.GLFW.glfwGetTime;
import static org.lwjgl.opengl.GL11.*;
public class Scene {
float[] vertices;
float[] color;
float[] texture;
Texture texture_map;
byte[] indices;
VertexArray vertexArray;
@ -20,17 +20,21 @@ public class Scene {
this.vertexArray = new VertexArray(this.vertices, this.indices);
}
public Scene(String vertPath, String fragPath, float[] vertices, byte[] indices, float[] color){
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.vertexArray = new VertexArray(this.vertices, this.indices, this.color);
this.texture_map = new Texture("textures/container.jpg");
this.texture = texture;
this.vertexArray = new VertexArray(this.vertices, this.indices, this.color, this.texture);
}
public void render(){
this.shader.enable();
this.texture_map.bind();
this.vertexArray.render();
this.texture_map.unbind();
this.shader.disable();
}
}

View File

@ -6,7 +6,7 @@ import static org.lwjgl.opengl.GL30.*;
public class VertexArray {
private int VAO ,VBO, EBO, CBO;
private int VAO ,VBO, EBO, CBO, TBO;
private int count;
public VertexArray(float[] vertices, byte[] indices){
@ -49,6 +49,30 @@ public class VertexArray {
}
public VertexArray(float[] vertices, byte[] indices, float[] color, float[] texture) {
count = indices.length;
// VERTEX ARRAY OBJECT
VAO = glGenVertexArrays();
glBindVertexArray(VAO);
glEnableVertexAttribArray(0);
// VERTEX BUFFER OBJECT
createVertexBufferObject(vertices);
// COLOR BUFFER OBJECT
createColorBufferObject(color);
// TEXTURE BUFFER OBJECT
createTextureBufferObject(texture);
EBO = glGenBuffers();
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, BufferUtils.createByteBuffer(indices), GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
}
private void createVertexBufferObject(float[] vertices){
VBO = glGenBuffers();
glBindBuffer(GL_ARRAY_BUFFER, VBO);
@ -65,6 +89,14 @@ public class VertexArray {
glEnableVertexAttribArray(1);
}
private void createTextureBufferObject(float[] texture){
TBO = glGenBuffers();
glBindBuffer(GL_ARRAY_BUFFER, TBO);
glBufferData(GL_ARRAY_BUFFER, BufferUtils.createFloatBuffer(texture), GL_STATIC_DRAW);
glVertexAttribPointer(2, 2, GL_FLOAT, false, 0, 0);
glEnableVertexAttribArray(2);
}
public void bind(){
glBindVertexArray(this.VAO);
}