projection
This commit is contained in:
parent
cbc94e8459
commit
0aeec51093
23
src/main.c
23
src/main.c
|
@ -24,6 +24,8 @@ GLuint indexBuffer;
|
|||
GLuint initialWindowWidth = 800;
|
||||
GLuint initialWindowHeight = 600;
|
||||
|
||||
GLfloat aspect;
|
||||
|
||||
int frameCount = 0;
|
||||
struct timespec last_time, current_time;
|
||||
|
||||
|
@ -228,24 +230,33 @@ void draw(void) {
|
|||
|
||||
mat4 transformMatrix;
|
||||
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;
|
||||
mat4Identity(viewMatrix);
|
||||
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};
|
||||
mat4BuildLookAt(viewMatrix, cameraPos, cameraLookAt, cameraUp);
|
||||
|
||||
GLuint transformLocation = glGetUniformLocation(program, "uTransform");
|
||||
glUniformMatrix4fv(transformLocation, 1, GL_FALSE, transformMatrix);
|
||||
mat4 modelViewMatrix;
|
||||
mat4Multiply(modelViewMatrix, viewMatrix, transformMatrix);
|
||||
|
||||
GLuint viewLocation = glGetUniformLocation(program, "uView");
|
||||
glUniformMatrix4fv(viewLocation, 1, GL_FALSE, viewMatrix);
|
||||
GLuint modelViewLocation = glGetUniformLocation(program, "uModelView");
|
||||
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);
|
||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBuffer);
|
||||
|
@ -254,6 +265,7 @@ void draw(void) {
|
|||
|
||||
void framebuffer_size_callback(GLFWwindow* window, int width, int height) {
|
||||
glViewport(0, 0, width, height);
|
||||
aspect = (float)width / height;
|
||||
}
|
||||
|
||||
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);
|
||||
|
||||
GLFWwindow *window = glfwCreateWindow(initialWindowWidth, initialWindowHeight, "CG1", NULL, NULL);
|
||||
aspect = (float)initialWindowWidth / initialWindowHeight;
|
||||
if (!window) {
|
||||
FATAL("Failed to open window");
|
||||
glfwTerminate();
|
||||
|
|
|
@ -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[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;
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
|
@ -23,7 +23,8 @@ extern void mat4RotateX(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 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 vec3CrossProduct(vec3 out, vec3 a, vec3 b);
|
||||
|
|
|
@ -1,11 +1,11 @@
|
|||
#version 330 core
|
||||
layout (location = 0) in vec3 aPosition;
|
||||
uniform mat4 uTransform;
|
||||
uniform mat4 uView;
|
||||
uniform mat4 uModelView;
|
||||
uniform mat4 uProjection;
|
||||
|
||||
out vec3 vertexColor;
|
||||
|
||||
void main() {
|
||||
vertexColor = vec3(1.0, 0.1, 0.1);
|
||||
gl_Position = uView * uTransform * vec4(aPosition, 1.0);
|
||||
gl_Position = uProjection * uModelView * vec4(aPosition, 1.0);
|
||||
}
|
Loading…
Reference in New Issue