Ajout d'un canal couleur dans le vertexArray

This commit is contained in:
Antoine 2021-05-14 18:47:05 +02:00
parent 049e735e1a
commit e1cbbb85eb
8 changed files with 133 additions and 14 deletions

View File

@ -1,10 +1,10 @@
#version 330 core
uniform float time;
out vec4 FragColor;
in vec3 Color;
void main()
{
FragColor = vec4(sin(time), sin(time/2), sin(time/4), 1.0f);
FragColor = vec4(Color, 1.0);
}

View File

@ -1,10 +1,12 @@
#version 330 core
uniform float time;
layout (location = 0) in vec3 aPos; // la variable aPos a l'attribut de position 0
layout (location = 1) in vec3 aColor;
layout (location = 0) in vec3 aPos;
out vec3 Color;
void main()
{
gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0);
gl_Position = vec4(aPos, 1.0);
Color = aColor;
}

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

View File

@ -35,6 +35,12 @@ public class Primitive {
-0.5f, -0.5f, 0.0f,
};
public static float[] triangle_couleur = new float[] {
1.0f, 0.0f, 0.0f,
0.0f, 1.0f, 0.0f,
0.0f, 0.0f, 1.0f
};
public static byte[] triangle_indices = new byte[]{
0, 1, 2
};
@ -60,6 +66,14 @@ public class Primitive {
-0.5f, 0.5f, 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
};
public static byte[] rectangle_indices = new byte[] {
0, 1, 3,
1, 2, 3

View File

@ -7,6 +7,7 @@ import static org.lwjgl.glfw.GLFW.glfwGetTime;
public class Scene {
float[] vertices;
float[] color;
byte[] indices;
VertexArray vertexArray;
@ -19,9 +20,16 @@ public class Scene {
this.vertexArray = new VertexArray(this.vertices, this.indices);
}
public Scene(String vertPath, String fragPath, float[] vertices, byte[] indices, float[] color){
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);
}
public void render(){
this.shader.enable();
this.shader.setUniform1f("time", (float)glfwGetTime());
this.vertexArray.render();
this.shader.disable();
}

61
src/engine/Texture.java Normal file
View File

@ -0,0 +1,61 @@
package engine;
import java.awt.image.BufferedImage;
import java.io.FileInputStream;
import java.io.IOException;
import javax.imageio.ImageIO;
import static org.lwjgl.opengl.GL11.*;
import static org.lwjgl.opengl.GL30.*;
public class Texture {
private int width, height;
private int texture;
public Texture(String path) {
texture = load(path);
}
private int load(String path) {
int[] pixels = null;
try {
BufferedImage image = ImageIO.read(new FileInputStream(path));
width = image.getWidth();
height = image.getHeight();
pixels = new int[width * height];
image.getRGB(0, 0, width, height, pixels, 0, width);
} catch (IOException e) {
e.printStackTrace();
}
int[] data = new int[width * height];
for (int i = 0; i < width * height; i++) {
int a = (pixels[i] & 0xff000000) >> 24;
int r = (pixels[i] & 0xff0000) >> 16;
int g = (pixels[i] & 0xff00) >> 8;
int b = (pixels[i] & 0xff);
data[i] = a << 24 | b << 16 | g << 8 | r;
}
int result = glGenTextures();
glBindTexture(GL_TEXTURE_2D, result);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, BufferUtils.createIntBuffer(data));
glGenerateMipmap(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, 0);
return result;
}
public void bind() {
glBindTexture(GL_TEXTURE_2D, texture);
}
public void unbind() {
glBindTexture(GL_TEXTURE_2D, 0);
}
}

View File

@ -6,7 +6,7 @@ import static org.lwjgl.opengl.GL30.*;
public class VertexArray {
private int VAO ,VBO, EBO;
private int VAO ,VBO, EBO, CBO;
private int count;
public VertexArray(float[] vertices, byte[] indices){
@ -16,11 +16,7 @@ public class VertexArray {
glBindVertexArray(VAO);
// VERTEX BUFFER OBJECT
VBO = glGenBuffers();
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, BufferUtils.createFloatBuffer(vertices), GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, false, 0, 0);
glEnableVertexAttribArray(0);
createVertexBufferObject(vertices);
EBO = glGenBuffers();
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
@ -31,6 +27,44 @@ public class VertexArray {
}
public VertexArray(float[] vertices, byte[] indices, float[] color) {
count = indices.length;
// VERTEX ARRAY OBJECT
VAO = glGenVertexArrays();
glBindVertexArray(VAO);
glEnableVertexAttribArray(0);
// VERTEX BUFFER OBJECT
createVertexBufferObject(vertices);
// COLOR BUFFER OBJECT
createColorBufferObject(color);
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);
glBufferData(GL_ARRAY_BUFFER, BufferUtils.createFloatBuffer(vertices), GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, false, 0, 0);
glEnableVertexAttribArray(0);
}
private void createColorBufferObject(float[] color){
CBO = glGenBuffers();
glBindBuffer(GL_ARRAY_BUFFER, CBO);
glBufferData(GL_ARRAY_BUFFER, BufferUtils.createFloatBuffer(color), GL_STATIC_DRAW);
glVertexAttribPointer(1, 3, GL_FLOAT, false, 0, 0);
glEnableVertexAttribArray(1);
}
public void bind(){
glBindVertexArray(this.VAO);
}

BIN
textures/container.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 181 KiB