solid cube

This commit is contained in:
Luca Conte 2025-04-20 02:04:02 +02:00
parent 0aeec51093
commit 5046266906
2 changed files with 34 additions and 24 deletions

View File

@ -25,6 +25,7 @@ GLuint initialWindowWidth = 800;
GLuint initialWindowHeight = 600; GLuint initialWindowHeight = 600;
GLfloat aspect; GLfloat aspect;
mat4 projectionMatrix;
int frameCount = 0; int frameCount = 0;
struct timespec last_time, current_time; struct timespec last_time, current_time;
@ -81,6 +82,11 @@ void updateStatusDisplay() {
} }
} }
void recalculateProjectionMatrix() {
mat4BuildPerspective(projectionMatrix, 60 * M_PI / 180, aspect, 0.1, 10);
DEBUG("Recalculating Projection Matrix");
}
void init(void) { void init(void) {
INFO("Compiling Shaders..."); INFO("Compiling Shaders...");
@ -153,24 +159,23 @@ void init(void) {
glPrimitiveRestartIndex(restart); glPrimitiveRestartIndex(restart);
GLuint indices[] = { GLuint indices[] = {
0, 1, 0, 1, 2,
0, 2, 1, 2, 3,
0, 4,
1, 3, 0, 4, 1,
1, 5, 5, 4, 1,
2, 3, 5, 1, 7,
2, 6, 3, 1, 7,
3, 7, 7, 5, 6,
4, 5, 6,
4, 5, 4, 0, 6,
4, 6, 2, 0, 6,
5, 7, 2, 3, 6,
7, 3, 6
6, 7
}; };
DEBUG("Creating vertext buffer"); DEBUG("Creating vertext buffer");
@ -212,8 +217,12 @@ void init(void) {
// glEnable(GL_CULL_FACE); // glEnable(GL_CULL_FACE);
glFrontFace(GL_CCW); glFrontFace(GL_CCW);
glEnable(GL_DEPTH_TEST);
initialiseStatusDisplay(); initialiseStatusDisplay();
recalculateProjectionMatrix();
INFO("--- Initialisation done ---"); INFO("--- Initialisation done ---");
} }
@ -225,7 +234,7 @@ void draw(void) {
currentHue += 0.0005f; currentHue += 0.0005f;
if (currentHue > 1.0) currentHue = 0.0f; if (currentHue > 1.0) currentHue = 0.0f;
glClear(GL_COLOR_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glUseProgram(program); glUseProgram(program);
mat4 transformMatrix; mat4 transformMatrix;
@ -234,38 +243,39 @@ void draw(void) {
mat4Identity(transformMatrix); mat4Identity(transformMatrix);
mat4Scale(transformMatrix, transformMatrix, scale); mat4Scale(transformMatrix, transformMatrix, scale);
// mat4RotateZ(transformMatrix, transformMatrix, currentHue * PI * 2); mat4RotateZ(transformMatrix, transformMatrix, currentHue * PI * 2);
// mat4RotateY(transformMatrix, transformMatrix, currentHue * PI * 2); mat4RotateY(transformMatrix, transformMatrix, currentHue * PI * 2);
mat4Translate(transformMatrix, transformMatrix, position); mat4Translate(transformMatrix, transformMatrix, position);
mat4 viewMatrix; mat4 viewMatrix;
mat4Identity(viewMatrix); mat4Identity(viewMatrix);
vec3 cameraPos = {0.0f, sin(currentHue * PI * 2), 0.0f}; vec3 cameraPos = {0.0f, 0.0f, 0.0f};
vec3 cameraLookAt = {0.0f, sin(currentHue * PI * 2), -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; mat4 modelViewMatrix;
mat4Multiply(modelViewMatrix, viewMatrix, transformMatrix); mat4Multiply(modelViewMatrix, viewMatrix, transformMatrix);
GLuint modelViewLocation = glGetUniformLocation(program, "uModelView"); GLuint modelViewLocation = glGetUniformLocation(program, "uModelView");
glUniformMatrix4fv(modelViewLocation, 1, GL_FALSE, modelViewMatrix); glUniformMatrix4fv(modelViewLocation, 1, GL_FALSE, modelViewMatrix);
mat4 projectionMatrix;
mat4BuildPerspective(projectionMatrix, 60 * M_PI / 180, aspect, 0.1, 10);
GLuint projectionLocation = glGetUniformLocation(program, "uProjection"); GLuint projectionLocation = glGetUniformLocation(program, "uProjection");
glUniformMatrix4fv(projectionLocation, 1, GL_FALSE, projectionMatrix); glUniformMatrix4fv(projectionLocation, 1, GL_FALSE, projectionMatrix);
glBindVertexArray(vertexArrayObject); glBindVertexArray(vertexArrayObject);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBuffer); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBuffer);
glDrawElements(GL_LINES, 24, GL_UNSIGNED_INT, 0); glDrawElements(GL_TRIANGLES, 48, GL_UNSIGNED_INT, 0);
} }
void framebuffer_size_callback(GLFWwindow* window, int width, int height) { void framebuffer_size_callback(GLFWwindow* window, int width, int height) {
glViewport(0, 0, width, height); glViewport(0, 0, width, height);
aspect = (float)width / height; aspect = (float)width / height;
recalculateProjectionMatrix();
} }
int main(int argc, char const *argv[]) int main(int argc, char const *argv[])

View File

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