draw multiple cubes and make every side have a different colour

This commit is contained in:
Luca Conte 2025-04-22 19:29:36 +02:00
parent c5f7407199
commit 1db1b9378b
3 changed files with 75 additions and 28 deletions

View File

@ -228,47 +228,87 @@ void init(void) {
GLfloat currentHue = 0.0f; GLfloat currentHue = 0.0f;
void drawCube(vec3 position, mat4 initialModelMatrix, mat4 viewMatrix) {
mat4 modelMatrix;
mat4 modelViewMatrix;
mat4Translate(modelMatrix, initialModelMatrix, position);
// combine model and view matrix to model-view matrix
mat4Multiply(modelViewMatrix, viewMatrix, modelMatrix);
// send modelView and projection matrix to shader
GLuint modelViewLocation = glGetUniformLocation(program, "uModelView");
glUniformMatrix4fv(modelViewLocation, 1, GL_FALSE, modelViewMatrix);
// draw cube
glBindVertexArray(vertexArrayObject);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBuffer);
glDrawElements(GL_TRIANGLES, 48, GL_UNSIGNED_INT, 0);
}
void draw(void) { void draw(void) {
updateStatusDisplay(); updateStatusDisplay();
// counter for animation
currentHue += 0.0005f; currentHue += 0.0005f;
if (currentHue > 1.0) currentHue = 0.0f; if (currentHue > 1.0) currentHue = 0.0f;
// clear colour and depth buffer
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// select program
glUseProgram(program); glUseProgram(program);
mat4 transformMatrix; // build view matrix
vec3 scale = {0.2f, 0.2f, 0.2f};
vec3 position = {0.0f, 0.0f, -1.0f};
mat4Identity(transformMatrix);
mat4Scale(transformMatrix, transformMatrix, scale);
mat4RotateZ(transformMatrix, transformMatrix, currentHue * PI * 2);
mat4RotateY(transformMatrix, transformMatrix, currentHue * PI * 2);
mat4Translate(transformMatrix, transformMatrix, position);
mat4 viewMatrix; mat4 viewMatrix;
mat4Identity(viewMatrix);
vec3 cameraPos = {0.0f, 0.0f, 0.0f}; // mat4Identity(viewMatrix);
vec3 cameraPos = {0.0f, 0.0f, 2.0f};
vec3 cameraLookAt = {0.0f, 0.0f, -1.0f}; vec3 cameraLookAt = {0.0f, 0.0f, -1.0f};
vec3 cameraUp = {0.0f, 1.0f, 0.0f}; vec3 cameraUp = {0.0f, 1.0f, 0.0f};
mat4BuildLookAt(viewMatrix, cameraPos, cameraLookAt, cameraUp); mat4BuildLookAt(viewMatrix, cameraPos, cameraLookAt, cameraUp);
mat4 modelViewMatrix;
mat4Multiply(modelViewMatrix, viewMatrix, transformMatrix);
GLuint modelViewLocation = glGetUniformLocation(program, "uModelView");
glUniformMatrix4fv(modelViewLocation, 1, GL_FALSE, modelViewMatrix);
GLuint projectionLocation = glGetUniformLocation(program, "uProjection"); GLuint projectionLocation = glGetUniformLocation(program, "uProjection");
glUniformMatrix4fv(projectionLocation, 1, GL_FALSE, projectionMatrix); glUniformMatrix4fv(projectionLocation, 1, GL_FALSE, projectionMatrix);
glBindVertexArray(vertexArrayObject); // build model Matrix
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBuffer); mat4 modelMatrix;
glDrawElements(GL_TRIANGLES, 48, GL_UNSIGNED_INT, 0); vec3 scale;
vec3 position;
mat4 scaledMatrix;
mat4Identity(scaledMatrix);
vec3Set(scale, 0.2f, 0.2f, 0.2f);
mat4Scale(scaledMatrix, scaledMatrix, scale);
// draw cubes
// cube 1
vec3Set(position, -1.0f, 0.0f, 0.0f);
mat4RotateZ(modelMatrix, scaledMatrix, currentHue * PI * 4);
mat4RotateY(modelMatrix, modelMatrix, currentHue * PI * 2);
drawCube(position, modelMatrix, viewMatrix);
// cube 2
vec3Set(position, 0.0f, 0.0f, 0.0f);
mat4RotateY(modelMatrix, scaledMatrix, currentHue * PI * 4);
mat4RotateX(modelMatrix, modelMatrix, currentHue * PI * 2);
drawCube(position, modelMatrix, viewMatrix);
// cube 3
vec3Set(position, 1.0f, 0.0f, 0.0f);
mat4RotateX(modelMatrix, scaledMatrix, currentHue * PI * 4);
mat4RotateZ(modelMatrix, modelMatrix, currentHue * PI * 2);
drawCube(position, modelMatrix, viewMatrix);
} }
void framebuffer_size_callback(GLFWwindow* window, int width, int height) { void framebuffer_size_callback(GLFWwindow* window, int width, int height) {

View File

@ -1,5 +1,15 @@
#version 330 core #version 330 core
in vec3 vertexColor; vec3 colors[6] = vec3[6](
vec3(1.0, 0.5, 0.5),
vec3(0.5, 1.0, 0.5),
vec3(0.5, 0.5, 1.0),
vec3(1.0, 1.0, 0.5),
vec3(1.0, 0.5, 1.0),
vec3(0.5, 1.0, 1.0)
);
void main() { void main() {
gl_FragColor = vec4(vertexColor, 1.0); // hacky solution to give each side a different colour
int side = gl_PrimitiveID / 2;
gl_FragColor = vec4(colors[side], 1.0);
} }

View File

@ -3,9 +3,6 @@ layout (location = 0) in vec3 aPosition;
uniform mat4 uModelView; uniform mat4 uModelView;
uniform mat4 uProjection; uniform mat4 uProjection;
out vec3 vertexColor;
void main() { void main() {
vertexColor = (aPosition + vec3(1)) / 2;
gl_Position = uProjection * uModelView * vec4(aPosition, 1.0); gl_Position = uProjection * uModelView * vec4(aPosition, 1.0);
} }