start of u06-2

This commit is contained in:
Luca Conte 2024-04-24 12:51:16 +02:00
parent 46f5c48493
commit ae882e31bb
11 changed files with 26796 additions and 0 deletions

22
u06-2/Makefile Normal file
View File

@ -0,0 +1,22 @@
GLEW_LIBS=$(shell pkgconf glew --libs)
GLFW_LIBS=$(shell pkgconf glfw3 --libs)
OBJ = main.o matrixMath.o transformation.o wavefrontobj.o
SHADERS = fragmentShader.c vertexShader.c
cg1.out: $(OBJ) $(SHADERS)
gcc -o $@ $(OBJ) -lm $(GLEW_LIBS) $(GLFW_LIBS)
%Shader.c: %Shader.glsl
xxd -i $? > $@
main.o: $(SHADERS) matrixMath.h transformation.h wavefrontobj.h
%.o: %.c
gcc -c $<
run: cg1.out
./cg1.out
clean:
rm $(SHADERS) $(OBJ) cg1.out

View File

@ -0,0 +1,5 @@
#version 330 core
uniform vec3 color;
void main() {
gl_FragColor = vec4(color, 1.0);
}

396
u06-2/main.c Normal file
View File

@ -0,0 +1,396 @@
#include <stdio.h>
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include "vertexShader.c"
#include "fragmentShader.c"
#include "matrixMath.h"
#include "transformation.h"
#include "wavefrontobj.h"
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include <stdbool.h>
#define RESTART 345678
GLuint program;
GLuint vao;
GLuint cubeIndicesBufferObject;
bool exitRequested = false;
GLFWwindow* window;
GLfloat aspectRatio = 1.0f;
GLfloat step = 0.0f;
const GLfloat pi = 3.14159f;
vec3 cameraPosition = {0.0f, 0.0f, 2.0f};
GLfloat cube[] = {
1.0f, 1.0f, 1.0f,
1.0f, 1.0f, -1.0f,
1.0f, -1.0f, 1.0f,
1.0f, -1.0f, -1.0f,
-1.0f, 1.0f, 1.0f,
-1.0f, 1.0f, -1.0f,
-1.0f, -1.0f, 1.0f,
-1.0f, -1.0f, -1.0f
};
GLfloat ground[] = {
1.0f, 0.0f, 1.0f,
1.0f, 0.0f, -1.0f,
-1.0f, 0.0f, 0.0f,
-1.0f, 0.0f, -1.0f
};
GLuint cubeIndices[] = {
0, 1, 2,
1, 3, 2,
1, 7, 3,
1, 5, 7,
4, 6, 5,
5, 6, 7,
0, 2, 4,
4, 2, 6,
7, 6, 3,
6, 2, 3,
4, 5, 1,
4, 1, 0
};
GLuint groundIndices[] = {
0, 1, 2,
1, 3, 2
};
void handleInputs(void) {
if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS) {
cameraPosition.z += 0.02f;
}
if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS) {
cameraPosition.z -= 0.02f;
}
if (glfwGetKey(window, GLFW_KEY_SPACE) == GLFW_PRESS) {
cameraPosition.y += 0.02f;
}
if (glfwGetKey(window, GLFW_KEY_LEFT_SHIFT) == GLFW_PRESS) {
cameraPosition.y -= 0.02f;
}
}
void keyboardHandler(GLFWwindow* window, int key, int scancode, int action, int mods) {
if (action == GLFW_PRESS) {
if (key == GLFW_KEY_ESCAPE) {
exitRequested = true;
}
}
}
void init(void) {
// create and compile vertex shader
const GLchar *vertexTextConst = vertexShader_glsl;
GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertexShader, 1, &vertexTextConst, &vertexShader_glsl_len);
glCompileShader(vertexShader);
GLint status;
glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &status);
if (!status) {
printf("Error compiling vertex shader: ");
GLchar infoLog[1024];
glGetShaderInfoLog(vertexShader, 1024, NULL, infoLog);
printf("%s",infoLog);
}
vertexTextConst = NULL;
// create and compile fragment shader
const GLchar *fragmentTextConst = fragmentShader_glsl;
GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragmentShader, 1, &fragmentTextConst, &fragmentShader_glsl_len);
glCompileShader(fragmentShader);
glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &status);
if (!status) {
printf("Error compiling fragment shader: ");
GLchar infoLog[1024];
glGetShaderInfoLog(fragmentShader, 1024, NULL, infoLog);
printf("%s",infoLog);
}
// create and link shader program
program = glCreateProgram();
glAttachShader(program, vertexShader);
glAttachShader(program, fragmentShader);
glLinkProgram(program);
glGetProgramiv(program, GL_LINK_STATUS, &status);
if (!status) {
printf("Error linking program: ");
GLchar infoLog[1024];
glGetProgramInfoLog(program, 1024, NULL, infoLog);
printf("%s",infoLog);
}
glValidateProgram(program);
glGetProgramiv(program, GL_VALIDATE_STATUS, &status);
if (!status) {
printf("Error validating program: ");
GLchar infoLog[1024];
glGetProgramInfoLog(program, 1024, NULL, infoLog);
printf("%s",infoLog);
}
ParsedObjFile teapot = readObjFile("teapot.obj");
GLuint triangleVertexBufferObject;
glGenBuffers(1, &triangleVertexBufferObject);
glBindBuffer(GL_ARRAY_BUFFER, triangleVertexBufferObject);
glBufferData(GL_ARRAY_BUFFER, sizeof(cube), cube, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
// create vertex array object
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);
glBindBuffer(GL_ARRAY_BUFFER, triangleVertexBufferObject);
glVertexAttribPointer(
0,
3,
GL_FLOAT,
GL_FALSE,
sizeof(GLfloat) * 3,
0
);
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
// ENABLE BACKFACE CULLING
glFrontFace(GL_CW);
glEnable(GL_CULL_FACE);
// ENABLE RESTARTING
glEnable(GL_PRIMITIVE_RESTART);
glPrimitiveRestartIndex(RESTART);
// DEFINE INDEX ARRAY FOR ELEMENT DRAWING
glGenBuffers(1, &cubeIndicesBufferObject);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, cubeIndicesBufferObject);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(cubeIndices), cubeIndices, GL_STATIC_DRAW);
glClearColor(0.1f, 0.1f, 0.1f, 1.0f);
}
void drawCube(vec3* position, vec3* scaleVec, vec3* rotateVec) {
mat4 modelTransformation;
identity(&modelTransformation);
scale(&modelTransformation, &modelTransformation, scaleVec);
rotateX(&modelTransformation, &modelTransformation, rotateVec->x);
rotateY(&modelTransformation, &modelTransformation, rotateVec->y);
rotateZ(&modelTransformation, &modelTransformation, rotateVec->z);
translate(&modelTransformation, &modelTransformation, position);
glUniformMatrix4fv(glGetUniformLocation(program, "modelTransformation"), 1, GL_FALSE, (GLfloat*)&modelTransformation);
glUniform3f(glGetUniformLocation(program, "color"), 1.0f, 0.0f, 0.0f);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, (void*)(0 * sizeof(GLuint)));
glUniform3f(glGetUniformLocation(program, "color"), 1.0f, 1.0f, 0.0f);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, (void*)(6 * sizeof(GLuint)));
glUniform3f(glGetUniformLocation(program, "color"), 1.0f, 1.0f, 1.0f);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, (void*)(12 * sizeof(GLuint)));
glUniform3f(glGetUniformLocation(program, "color"), 0.0f, 1.0f, 1.0f);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, (void*)(18 * sizeof(GLuint)));
glUniform3f(glGetUniformLocation(program, "color"), 0.0f, 0.0f, 1.0f);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, (void*)(24 * sizeof(GLuint)));
glUniform3f(glGetUniformLocation(program, "color"), 1.0f, 0.0f, 1.0f);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, (void*)(30 * sizeof(GLuint)));
}
void draw(void) {
handleInputs();
glClear(GL_COLOR_BUFFER_BIT);
glUseProgram(program);
glBindVertexArray(vao);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, cubeIndicesBufferObject);
step += 0.002f;
if (step > 1.0f) step -= 1.0f;
GLfloat stepi = step * pi * 2;
// ------------- MODEL TRANSFORMATION ---------------------
// SCALE -> ROTATE -> TRANSLATE
// GLfloat cubePosition[3] = {0.0f, -2.0f, 0.0f};
// GLfloat cubeScale[3] = {4.0f, 0.2f, 4.0f};
// GLfloat modelTransformation[16];
// identity(modelTransformation);
// scale(modelTransformation, modelTransformation, cubeScale);
// rotateY(modelTransformation, modelTransformation, stepi);
// rotateX(modelTransformation, modelTransformation, stepi + 1.0f);
// rotateZ(modelTransformation, modelTransformation, stepi + 0.5f);
// translate(modelTransformation, modelTransformation, cubePosition);
// ------------- VIEWING TRANSFORMATION -------------------
vec3 origin = {0.0f, 0.0f, 0.0f};
vec3 up = {0.0f, 1.0f, 0.0f};
mat4 viewingTransformation;
lookAt(&viewingTransformation, &cameraPosition, &origin, &up);
// -------------- PROJECTION TRANSFORMATION ----------------
mat4 projectionTransformation;
GLfloat near = 0.1f;
GLfloat far = 10.0f;
perspectiveProjection(&projectionTransformation, near, far);
// -------------- NORMALISATION TRANSFORMATION -------------
mat4 normalisationTransformation;
GLfloat fovy = pi / 2;
normalisedDeviceCoordinatesFov(&normalisationTransformation, fovy, aspectRatio, near, far);
mat4 globalTransformation;
identity(&globalTransformation);
multiply(&globalTransformation, &viewingTransformation, &globalTransformation);
multiply(&globalTransformation, &projectionTransformation, &globalTransformation);
multiply(&globalTransformation, &normalisationTransformation, &globalTransformation);
glUniformMatrix4fv(glGetUniformLocation(program, "globalTransformation"), 1, GL_FALSE, (GLfloat*)&globalTransformation);
vec3 position = {0.0f, -3.0f, 0.0f};
vec3 scaleVec = {3.0f, 0.1f, 3.0f};
vec3 rotateVec = {0.0f, 0.0f, 0.0f};
drawCube(&position, &scaleVec, &rotateVec);
position.y = -2.8f;
scaleVec.x = 2.0f;
scaleVec.z = 2.0f;
rotateVec.x = pi;
drawCube(&position, &scaleVec, &rotateVec);
position.y = -2.4f;
scaleVec.x = 1.0f;
scaleVec.y = 0.3f;
scaleVec.z = 1.0f;
rotateVec.x = 0.0f;
rotateVec.y = pi / 4;
drawCube(&position, &scaleVec, &rotateVec);
}
void framebuffer_size_callback(GLFWwindow *window, int width, int height) {
glViewport(0, 0, width, height);
aspectRatio = (float)width / height;
}
int main(void) {
// GLfloat test[16] = {
// 1.0f, 2.0f, 3.0f, 4.0f,
// 5.0f, 6.0f, 7.0f, 8.0f,
// 9.0f, 1.0f, 2.0f, 3.0f,
// 4.0f, 5.0f, 6.0f, 7.0f
// };
// transpose(test, test);
// GLfloat test2[16] = {
// 0.0f, 1.0f, 0.0f, 1.0f,
// 1.0f, 2.0f, 1.0f, 2.0f,
// 2.0f, 3.0f, 2.0f, 3.0f,
// 3.0f, 4.0f, 3.0f, 4.0f
// };
// transpose(test2, test2);
// multiply(test, test, test2);
// mat4Print(test);
// return 0;
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
window = glfwCreateWindow(700, 700, "Computergrafik 1", NULL, NULL);
if (!window) {
printf("Failed to create window\n");
glfwTerminate();
return -1;
}
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
glfwMakeContextCurrent(window);
// register keyboard event handler
glfwSetKeyCallback(window, keyboardHandler);
glewInit();
printf("OpenGL version supported by this platform (%s):\n", glGetString(GL_VERSION));
init();
while (!glfwWindowShouldClose(window) && !exitRequested) {
draw();
glfwSwapBuffers(window);
glfwPollEvents();
}
glfwTerminate();
return 0;
}

200
u06-2/matrixMath.c Normal file
View File

@ -0,0 +1,200 @@
#include <math.h>
#include <stdlib.h>
#include <stdio.h>
#include <GL/glew.h>
#include <string.h>
#include "matrixMath.h"
// MATRICES IN COLUMN MAJOR
void vec3Zero(vec3* out) {
for (int i = 0; i < 3; i++) {
((GLfloat*)out)[i] = 0;
}
}
void vec3Add(vec3* out, vec3* a, vec3* b) {
for (int i = 0; i < 3; i++) {
((GLfloat*)out)[i] = ((GLfloat*)a)[i] + ((GLfloat*)b)[i];
}
}
void vec3Multiply(vec3* out, vec3* a, GLfloat x) {
for (int i = 0; i < 3; i++) {
((GLfloat*)out)[i] = ((GLfloat*)a)[i] * x;
}
}
void vec3Subtract(vec3* out, vec3* a, vec3* b) {
vec3 minusB;
vec3Multiply(&minusB, b, -1);
vec3Add(out, a, &minusB);
}
void vec3Cross(vec3* out, vec3* a, vec3* b) {
vec3 result;
result.x = a->y * b->z - a->z * b->y;
result.y = a->z * b->x - a->x * b->z;
result.z = a->x * b->y - a->y * b->x;
memcpy(out, &result, sizeof(vec3));
}
GLfloat vec3Length(vec3* a) {
return (GLfloat)sqrt(a->x * a->x + a->y * a->y + a->z * a->z);
}
GLfloat vec3Dot(vec3* a, vec3* b) {
return a->x * b->x + a->y * b->y + a->z * b->z;
}
void vec3Normalise(vec3* out, vec3* a) {
vec3Multiply(out, a, 1 / vec3Length(a));
}
// CREATE 4x4 IDENTITY MATRIX
void identity(mat4* out) {
for (int i = 0; i < 16; i++) {
((GLfloat*)out)[i] = (i % 4 == i / 4);
}
}
// CREATE 4x4 TRANSLATION MATRIX
void translation(mat4* out, vec3* v) {
identity(out);
out->m03 = v->x;
out->m13 = v->y;
out->m23 = v->z;
}
// CREATE 4x4 SCALING MATRIX
void scaling(mat4* out, vec3* v) {
identity(out);
out->m00 = v->x;
out->m11 = v->y;
out->m22 = v->z;
}
// CREATE 4x4 ROTATION MATRIX AROUND Z AXIS
/* cos a -sin a 0 0
* sin a cos a 0 0
* 0 0 1 0
* 0 0 0 1
*/
void rotationZ(mat4* out, GLfloat angle) {
identity(out);
out->m00 = cos(angle);
out->m10 = sin(angle);
out->m01 = -sin(angle);
out->m11 = cos(angle);
}
// CREATE 4x4 ROTATION MATRIX AROUND Y AXIS
void rotationY(mat4* out, GLfloat angle) {
identity(out);
out->m00 = cos(angle);
out->m20 = -sin(angle);
out->m02 = sin(angle);
out->m22 = cos(angle);
}
// CREATE 4x4 ROTATION MATRIX AROUND Y AXIS
void rotationX(mat4* out, GLfloat angle) {
identity(out);
out->m11 = cos(angle);
out->m21 = sin(angle);
out->m12 = -sin(angle);
out->m22 = cos(angle);
}
// MULTIPLY ANY TO MATRICES
void multiplyAny(GLfloat* out, GLfloat* A, GLfloat* B, int wA, int hA, int wB) {
int sizeOut = hA * wB;
GLfloat* result = (GLfloat*) malloc(sizeOut * sizeof(GLfloat));
for (int i = 0; i < sizeOut; i++) {
result[i] = 0;
for (int j = 0; j < wA; j++) {
result[i] += A[j * hA + i % hA] * B[j + i / hA * wB];
}
}
memcpy(out, result, sizeOut * sizeof(GLfloat));
free(result);
result = NULL;
}
// MULTIPLY TWO 4x4 MATRICES
void multiply(mat4* out, mat4* A, mat4* B) {
multiplyAny((GLfloat*)out, (GLfloat*)A, (GLfloat*)B, 4, 4, 4);
}
// MULTIPLY in WITH TRANSLATION MATRIX OF v
void translate(mat4* out, mat4* in, vec3* v) {
mat4 translationMatrix;
translation(&translationMatrix, v);
multiply(out, &translationMatrix, in);
}
// MULTIPLY in WITH SCALING MATRIX OF v
void scale(mat4* out, mat4* in, vec3* v) {
mat4 scalingMatrix;
scaling(&scalingMatrix, v);
multiply(out, &scalingMatrix, in);
}
// MULTIPLY in WITH ROTATION MATRIX OF a AROUND Z AXIS
void rotateZ(mat4* out, mat4* in, GLfloat angle) {
mat4 rotationMatrix;
rotationZ(&rotationMatrix, angle);
multiply(out, &rotationMatrix, in);
}
// MULTIPLY in WITH ROTATION MATRIX OF a AROUND Y AXIS
void rotateY(mat4* out, mat4* in, GLfloat angle) {
mat4 rotationMatrix;
rotationY(&rotationMatrix, angle);
multiply(out, &rotationMatrix, in);
}
// MULTIPLY in WITH ROTATION MATRIX OF a AROUND X AXIS
void rotateX(mat4* out, mat4* in, GLfloat angle) {
mat4 rotationMatrix;
rotationX(&rotationMatrix, angle);
multiply(out, &rotationMatrix, in);
}
void transposeAny(GLfloat* out, GLfloat* in, int w, int h) {
int size = w * h;
GLfloat* result = (GLfloat*) malloc(size * sizeof(GLfloat));
for (int i = 0; i < size; i++) {
result[i] = in[(i % w) * h + i / w];
}
memcpy(out, result, size * sizeof(GLfloat));
free(result);
result = NULL;
}
void transpose(mat4* out, mat4* in) {
transposeAny((GLfloat*)out, (GLfloat*)in, 4, 4);
}
void printAny(GLfloat* M, int w, int h) {
GLfloat* transposed = (GLfloat*) malloc(w * h * sizeof(GLfloat));
transposeAny(transposed, M, w, h);
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
printf("%.4f ", transposed[i * w + j]);
}
printf("\n");
}
free(transposed);
transposed = NULL;
}
void vec3Print(vec3* a) {
printAny((GLfloat*)a, 1, 3);
}
void mat4Print(mat4* m) {
printAny((GLfloat*)m, 4, 4);
}

66
u06-2/matrixMath.h Normal file
View File

@ -0,0 +1,66 @@
#ifndef MATRIX_MATH
#define MATRIX_MATH
#include <GL/glew.h>
typedef struct {
GLfloat x;
GLfloat y;
GLfloat z;
} vec3;
typedef struct {
GLfloat m00;
GLfloat m10;
GLfloat m20;
GLfloat m30;
GLfloat m01;
GLfloat m11;
GLfloat m21;
GLfloat m31;
GLfloat m02;
GLfloat m12;
GLfloat m22;
GLfloat m32;
GLfloat m03;
GLfloat m13;
GLfloat m23;
GLfloat m33;
} mat4;
extern void vec3Zero(vec3* out);
extern void vec3Add(vec3* out, vec3* a, vec3* b);
extern void vec3Multiply(vec3* out, vec3* a, GLfloat x);
extern void vec3Subtract(vec3* out, vec3* a, vec3* b);
extern void vec3Cross(vec3* out, vec3* a, vec3* b);
extern void vec3Normalise(vec3* out, vec3* a);
extern GLfloat vec3Length(vec3* a);
extern GLfloat vec3Dot(vec3* a, vec3* b);
extern void identity(mat4* out);
extern void translation(mat4* out, vec3* v);
extern void scaling(mat4* out, vec3* v);
extern void rotationZ(mat4* out, GLfloat angle);
extern void rotationY(mat4* out, GLfloat angle);
extern void rotationX(mat4* out, GLfloat angle);
extern void multiplyAny(GLfloat* out, GLfloat* A, GLfloat* B, int wA, int hA, int wB);
extern void multiply(mat4* out, mat4* A, mat4* B);
extern void translate(mat4* out, mat4* in, vec3* v);
extern void scale(mat4* out, mat4* in, vec3* v);
extern void rotateZ(mat4* out, mat4* in, GLfloat angle);
extern void rotateY(mat4* out, mat4* in, GLfloat angle);
extern void rotateX(mat4* out, mat4* in, GLfloat angle);
extern void transposeAny(GLfloat* out, GLfloat* in, int w, int h);
extern void transpose(mat4* out, mat4* in);
extern void printAny(GLfloat* M, int w, int h);
extern void vec3Print(vec3* a);
extern void mat4Print(mat4* m);
#endif

25934
u06-2/teapot.obj Normal file

File diff suppressed because it is too large Load Diff

79
u06-2/transformation.c Normal file
View File

@ -0,0 +1,79 @@
#include <math.h>
#include <stdlib.h>
#include <stdio.h>
#include <GL/glew.h>
#include <string.h>
#include "matrixMath.h"
#include "transformation.h"
void lookAt(mat4* out, vec3* eye, vec3* look, vec3* up) {
vec3 n;
vec3Subtract(&n, eye, look);
vec3 u;
vec3Cross(&u, up, &n);
vec3 v;
vec3Cross(&v, &n, &u);
vec3Normalise(&n, &n);
vec3Normalise(&u, &u);
vec3Normalise(&v, &v);
mat4 Mr;
identity(&Mr);
memcpy(&Mr.m00, &u, sizeof(vec3));
memcpy(&Mr.m01, &v, sizeof(vec3));
memcpy(&Mr.m02, &n, sizeof(vec3));
transpose(&Mr, &Mr);
vec3 t;
vec3Multiply(&u, &u, -1);
vec3Multiply(&v, &v, -1);
vec3Multiply(&n, &n, -1);
t.x = vec3Dot(&u, eye);
t.y = vec3Dot(&v, eye);
t.z = vec3Dot(&n, eye);
memcpy(&Mr.m03, &t, sizeof(vec3));
memcpy(out, &Mr, sizeof(mat4));
}
void perspectiveProjection(mat4* out, GLfloat near, GLfloat far) {
identity(out);
out->m22 = 1 + (far / near);
out->m32 = - 1.0f / near;
out->m23 = far;
out->m33 = 0;
}
void normalisedDeviceCoordinates(mat4* out, GLfloat r, GLfloat l, GLfloat t, GLfloat b, GLfloat n, GLfloat f) {
identity(out);
out->m00 = 2 / (r - l);
out->m11 = 2 / (t - b);
out->m22 = -2 / (f - n);
out->m03 = - (r + l) / (r - l);
out->m13 = - (t + b) / (t - b);
out->m23 = - (f + n) / (f - n);
}
void normalisedDeviceCoordinatesFov(mat4* out, GLfloat fovy, GLfloat aspectRatio, GLfloat n, GLfloat f) {
GLfloat t = tan(fovy / 2) * n;
GLfloat r = t * aspectRatio;
normalisedDeviceCoordinates(out, r, -r, t, -t, n, f);
}

11
u06-2/transformation.h Normal file
View File

@ -0,0 +1,11 @@
#ifndef TRANSFORMATION_H
#define TRANSFORMATION_H
#include <GL/glew.h>
extern void lookAt(mat4* out, vec3* eye, vec3* look, vec3* up);
extern void perspectiveProjection(mat4* out, GLfloat near, GLfloat far);
extern void normalisedDeviceCoordinates(mat4* out, GLfloat r, GLfloat l, GLfloat t, GLfloat b, GLfloat n, GLfloat f);
extern void normalisedDeviceCoordinatesFov(mat4* out, GLfloat fovy, GLfloat aspectRatio, GLfloat n, GLfloat f);
#endif

8
u06-2/vertexShader.glsl Normal file
View File

@ -0,0 +1,8 @@
#version 330 core
layout (location = 0) in vec3 aPosition;
uniform mat4 globalTransformation;
uniform mat4 modelTransformation;
void main() {
// color = aPosition / 2 + vec3(0.5, 0.5, 0.5);
gl_Position = globalTransformation * modelTransformation * vec4(aPosition, 1.0);
}

51
u06-2/wavefrontobj.c Normal file
View File

@ -0,0 +1,51 @@
#include <GL/glew.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "wavefrontobj.h"
#define OBJ_LINE_BUFFER_SIZE 256
ParsedObjFile readObjFile(char* path) {
ParsedObjFile parsedFile;
FILE* fp = fopen(path, "r");
if (fp == NULL) {
fprintf(stderr, "File could not be opened: %s", path);
parsedFile.vertices = NULL;
parsedFile.length = 0;
}
uint numVertices = 0;
uint numVertexNormals = 0;
uint numFaces = 0;
uint numTextureCoords = 0;
char buf[OBJ_LINE_BUFFER_SIZE];
while (fgets(buf, OBJ_LINE_BUFFER_SIZE, fp)) {
if (buf[0] == 'v') {
if (buf[1] == ' ') {
numVertices++;
} else if (buf[1] == 't') {
numTextureCoords++;
} else if (buf[1] == 'n') {
numVertexNormals++;
}
}
if (buf[0] == 'f') {
numFaces++;
}
}
printf("Vertices: %d\nFaces: %d\nNormals:%d\nTextures:%d\n", numVertices, numFaces, numVertexNormals, numTextureCoords);
fclose(fp);
}
void clearParsedFile(ParsedObjFile file) {
free(file.vertices);
}

24
u06-2/wavefrontobj.h Normal file
View File

@ -0,0 +1,24 @@
#ifndef WAVEFRONTOBJ_H
#define WAVEFRONTOBJ_H
#include <GL/glew.h>
#include "matrixMath.h"
typedef struct {
vec3 position;
vec3 texture;
vec3 normal;
} vertex;
typedef struct {
vertex* vertices;
GLuint length;
} ParsedObjFile;
extern ParsedObjFile readObjFile(char* path);
extern void clearParsedFile(ParsedObjFile file);
#endif