projection

This commit is contained in:
Luca Conte 2025-04-20 01:26:39 +02:00
parent cbc94e8459
commit 0aeec51093
4 changed files with 47 additions and 9 deletions

View File

@ -24,6 +24,8 @@ GLuint indexBuffer;
GLuint initialWindowWidth = 800; GLuint initialWindowWidth = 800;
GLuint initialWindowHeight = 600; GLuint initialWindowHeight = 600;
GLfloat aspect;
int frameCount = 0; int frameCount = 0;
struct timespec last_time, current_time; struct timespec last_time, current_time;
@ -228,24 +230,33 @@ void draw(void) {
mat4 transformMatrix; mat4 transformMatrix;
vec3 scale = {0.2f, 0.2f, 0.2f}; vec3 scale = {0.2f, 0.2f, 0.2f};
vec3 position = {0.0f, 0.0f, -1.0f};
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);
mat4 viewMatrix; mat4 viewMatrix;
mat4Identity(viewMatrix); mat4Identity(viewMatrix);
vec3 cameraPos = {0.0f, sin(currentHue * PI * 2), 0.0f}; vec3 cameraPos = {0.0f, sin(currentHue * PI * 2), 0.0f};
vec3 cameraLookAt = {0.0f, 0.0f, -1.0f}; vec3 cameraLookAt = {0.0f, sin(currentHue * PI * 2), -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);
GLuint transformLocation = glGetUniformLocation(program, "uTransform"); mat4 modelViewMatrix;
glUniformMatrix4fv(transformLocation, 1, GL_FALSE, transformMatrix); mat4Multiply(modelViewMatrix, viewMatrix, transformMatrix);
GLuint viewLocation = glGetUniformLocation(program, "uView"); GLuint modelViewLocation = glGetUniformLocation(program, "uModelView");
glUniformMatrix4fv(viewLocation, 1, GL_FALSE, viewMatrix); glUniformMatrix4fv(modelViewLocation, 1, GL_FALSE, modelViewMatrix);
mat4 projectionMatrix;
mat4BuildPerspective(projectionMatrix, 60 * M_PI / 180, aspect, 0.1, 10);
GLuint projectionLocation = glGetUniformLocation(program, "uProjection");
glUniformMatrix4fv(projectionLocation, 1, GL_FALSE, projectionMatrix);
glBindVertexArray(vertexArrayObject); glBindVertexArray(vertexArrayObject);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBuffer); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBuffer);
@ -254,6 +265,7 @@ void draw(void) {
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;
} }
int main(int argc, char const *argv[]) int main(int argc, char const *argv[])
@ -276,6 +288,7 @@ int main(int argc, char const *argv[])
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
GLFWwindow *window = glfwCreateWindow(initialWindowWidth, initialWindowHeight, "CG1", NULL, NULL); GLFWwindow *window = glfwCreateWindow(initialWindowWidth, initialWindowHeight, "CG1", NULL, NULL);
aspect = (float)initialWindowWidth / initialWindowHeight;
if (!window) { if (!window) {
FATAL("Failed to open window"); FATAL("Failed to open window");
glfwTerminate(); glfwTerminate();

View File

@ -180,4 +180,28 @@ void mat4BuildLookAt(mat4 out, vec3 eye, vec3 look, vec3 up) {
out[1] = v[0]; out[5] = v[1]; out[9] = v[2]; out[13] = t[1]; out[1] = v[0]; out[5] = v[1]; out[9] = v[2]; out[13] = t[1];
out[2] = n[0]; out[6] = n[1]; out[10] = n[2]; out[14] = t[2]; out[2] = n[0]; out[6] = n[1]; out[10] = n[2]; out[14] = t[2];
out[3] = 0; out[7] = 0; out[11] = 0; out[15] = 1; out[3] = 0; out[7] = 0; out[11] = 0; out[15] = 1;
}
void mat4BuildProjection(mat4 out, GLfloat r, GLfloat l, GLfloat t, GLfloat b, GLfloat n, GLfloat f) {
mat4Identity(out);
out[0] = 2.0f / (r - l);
out[5] = 2.0f / (t - b);
out[8] = 1.0f / n * (r + l) / (r - l);
out[9] = 1.0f / n * (t + b) / (t - b);
out[10] = -1.0f / n * (f + n) / (f - n);
out[11] = -1.0f / n;
out[14] = - 2.0f * f / (f - n);
}
void mat4BuildPerspective(mat4 out, GLfloat fovy, GLfloat aspect, GLfloat n, GLfloat f) {
GLfloat t = n * tan(0.5f * fovy);
GLfloat b = -t;
GLfloat r = aspect * t;
GLfloat l = -r;
mat4BuildProjection(out, r, l, t, b, n, f);
} }

View File

@ -23,7 +23,8 @@ extern void mat4RotateX(mat4 out, mat4 in, GLfloat a);
extern void mat4RotateY(mat4 out, mat4 in, GLfloat a); extern void mat4RotateY(mat4 out, mat4 in, GLfloat a);
extern void mat4RotateZ(mat4 out, mat4 in, GLfloat a); extern void mat4RotateZ(mat4 out, mat4 in, GLfloat a);
extern void mat4BuildLookAt(mat4 out, vec3 eye, vec3 center, vec3 up); extern void mat4BuildLookAt(mat4 out, vec3 eye, vec3 center, vec3 up);
extern void mat4BuildProjection(mat4 out, GLfloat r, GLfloat l, GLfloat t, GLfloat b, GLfloat f, GLfloat n); extern void mat4BuildProjection(mat4 out, GLfloat r, GLfloat l, GLfloat t, GLfloat b, GLfloat n, GLfloat f);
extern void mat4BuildPerspective(mat4 out, GLfloat fovy, GLfloat aspect, GLfloat n, GLfloat f);
extern void vec3Subtract(vec3 out, vec3 a, vec3 b); extern void vec3Subtract(vec3 out, vec3 a, vec3 b);
extern void vec3CrossProduct(vec3 out, vec3 a, vec3 b); extern void vec3CrossProduct(vec3 out, vec3 a, vec3 b);

View File

@ -1,11 +1,11 @@
#version 330 core #version 330 core
layout (location = 0) in vec3 aPosition; layout (location = 0) in vec3 aPosition;
uniform mat4 uTransform; uniform mat4 uModelView;
uniform mat4 uView; uniform mat4 uProjection;
out vec3 vertexColor; out vec3 vertexColor;
void main() { void main() {
vertexColor = vec3(1.0, 0.1, 0.1); vertexColor = vec3(1.0, 0.1, 0.1);
gl_Position = uView * uTransform * vec4(aPosition, 1.0); gl_Position = uProjection * uModelView * vec4(aPosition, 1.0);
} }