diff --git a/u05-1/Makefile b/u05-1/Makefile new file mode 100644 index 0000000..25c194f --- /dev/null +++ b/u05-1/Makefile @@ -0,0 +1,21 @@ +GLEW_LIBS=$(shell pkgconf glew --libs) +GLFW_LIBS=$(shell pkgconf glfw3 --libs) + +OBJ = main.o matrixMath.o + +cg1.out: $(OBJ) vertexShader.c fragmentShader.c matrixMath.h + gcc -o $@ $(OBJ) -lm $(GLEW_LIBS) $(GLFW_LIBS) + +%Shader.c: %Shader.glsl + xxd -i $? > $@ + +main.o: vertexShader.c fragmentShader.c + +%.o: %.c + gcc -c $< + +run: cg1.out + ./cg1.out + +clean: + rm vertexShader.c fragmentShader.c cg1.out *.o \ No newline at end of file diff --git a/u05-1/fragmentShader.glsl b/u05-1/fragmentShader.glsl new file mode 100644 index 0000000..4064d63 --- /dev/null +++ b/u05-1/fragmentShader.glsl @@ -0,0 +1,5 @@ +#version 330 core +in vec3 color; +void main() { + gl_FragColor = vec4(color, 1.0); +} \ No newline at end of file diff --git a/u05-1/main.c b/u05-1/main.c new file mode 100644 index 0000000..5f3035c --- /dev/null +++ b/u05-1/main.c @@ -0,0 +1,281 @@ +#include + +#include +#include + +#include "vertexShader.c" +#include "fragmentShader.c" +#include "matrixMath.h" + +#include +#include +#include + +#define RESTART 345678 + +GLuint program; +GLuint vao; +GLuint cubeIndicesBufferObject; + +GLfloat aspectRatio = 1.0f; + +GLfloat step = 0.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 init(void) { + // create and compile vertex shader + GLchar *vertexText = malloc(vertexShader_glsl_len); + memcpy(vertexText, vertexShader_glsl, vertexShader_glsl_len); + const GLchar *vertexTextConst = vertexText; + + 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); + } + + free(vertexText); + vertexText = NULL; + vertexTextConst = NULL; + + + + // create and compile fragment shader + + GLchar *fragmentText = malloc(fragmentShader_glsl_len); + memcpy(fragmentText, fragmentShader_glsl, fragmentShader_glsl_len); + const GLchar *fragmentTextConst = fragmentText; + + 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); + } + + free(fragmentText); + fragmentText = NULL; + fragmentTextConst = NULL; + + // 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); + } + + + 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); + + + 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 draw(void) { + 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 M[16]; + identity(M); + + // GLfloat cameraPosition[3] = {sin(step * 2 * 3.14169f), 0.3f, 0.3f}; + GLfloat cameraPosition[3] = {0.6f, 0.3f, 0.3f}; + GLfloat cubePosition[3] = {0.0f, 0.0f, sin(step * 2 * 3.14159)}; + GLfloat up[3] = {0.0f, 1.0f, 0.0f}; + + + lookAt(M, cameraPosition, cubePosition, up); + + // GLfloat scaleFactor = sin(step * 3.14159f * 2) * 0.1f + 0.2f; + GLfloat scaleFactor = 0.5f; + + GLfloat aspectScale[3] = {1.0f / aspectRatio, 1.0f, 1.0f}; + GLfloat scaleBy[3] = {scaleFactor, scaleFactor, scaleFactor}; + + // rotateY(M, M, step * 3.14159f * 2); + // rotateX(M, M, step * 3.14159f * 2 + 1.0f); + // rotateZ(M, M, step * 3.14159f * 2 + 0.5f); + scale(M, M, aspectScale); + + // translate(M, M, cubePosition); + scale(M, M, scaleBy); + + glUniformMatrix4fv(glGetUniformLocation(program, "transformation"), 1, GL_FALSE, M); + + glDrawElements(GL_TRIANGLES, sizeof(cubeIndices) / sizeof(GLuint), GL_UNSIGNED_INT, NULL); +} + +void framebuffer_size_callback(GLFWwindow *window, int width, int height) { + glViewport(0, 0, width, height); + aspectRatio = (float)width / height; +} + +int main(void) { + + // GLfloat a[3]; + // GLfloat b[3]; + + // vec3Zero(a); + // vec3Zero(b); + + // a[0] = 1; + // a[1] = 2; + // a[2] = 3; + + // vec3Normalise(a, a); + + // printf("%f %f %f\n", a[0], a[1], a[2]); + // return 0; + + glfwInit(); + glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); + glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); + glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); + + GLFWwindow *window = glfwCreateWindow(600, 600, "Computergrafik 1", NULL, NULL); + + if (!window) { + printf("Failed to create window\n"); + glfwTerminate(); + return -1; + } + + + glfwSetFramebufferSizeCallback(window, framebuffer_size_callback); + glfwMakeContextCurrent(window); + + glewInit(); + + printf("OpenGL version supported by this platform (%s):\n", glGetString(GL_VERSION)); + + + init(); + + while (!glfwWindowShouldClose(window)) { + draw(); + + glfwSwapBuffers(window); + glfwPollEvents(); + } + + glfwTerminate(); + + return 0; +} \ No newline at end of file diff --git a/u05-1/matrixMath.c b/u05-1/matrixMath.c new file mode 100644 index 0000000..9550f74 --- /dev/null +++ b/u05-1/matrixMath.c @@ -0,0 +1,242 @@ +#include +#include +#include +#include +#include + +void vec3Zero(GLfloat* out) { + for (int i = 0; i < 3; i++) { + out[i] = 0; + } +} + +void vec3Add(GLfloat* out, GLfloat* a, GLfloat* b) { + for (int i = 0; i < 3; i++) { + out[i] = a[i] + b[i]; + } +} + +void vec3Multiply(GLfloat* out, GLfloat* a, GLfloat x) { + for (int i = 0; i < 3; i++) { + out[i] = a[i] * x; + } +} + +void vec3Subtract(GLfloat* out, GLfloat* a, GLfloat* b) { + vec3Multiply(out, b, -1); + vec3Add(out, a, out); +} + +void vec3Cross(GLfloat* out, GLfloat* a, GLfloat* b) { + GLfloat result[3]; + result[0] = a[1] * b[2] - a[2] * b[1]; + result[1] = a[2] * b[0] - a[0] * b[2]; + result[2] = a[0] * b[1] - a[1] * b[0]; + memcpy(out, result, sizeof(result)); +} + +GLfloat vec3Length(GLfloat* a) { + return (GLfloat)sqrt(a[0]*a[0] + a[1]*a[1] + a[2]*a[2]); +} + +GLfloat vec3Dot(GLfloat* a, GLfloat* b) { + return a[0] * b[0] + a[1] * b[1] + a[2] * b[2]; +} + +void vec3Normalise(GLfloat* out, GLfloat* a) { + vec3Multiply(out, a, 1 / vec3Length(a)); +} +// CREATE 4x4 IDENTITY MATRIX +void identity(GLfloat* out) { + for (int i = 0; i < 16; i++) { + out[i] = (i % 4 == i / 4); + } +} + +// CREATE 4x4 TRANSLATION MATRIX +void translation(GLfloat* out, GLfloat* v) { + identity(out); + for (int i = 0; i < 3; i++) { + out[3 * 4 + i] = v[i]; + } +} + +// CREATE 4x4 SCALING MATRIX +void scaling(GLfloat* out, GLfloat* v) { + identity(out); + for (int i = 0; i < 3; i++) { + out[i * 5] = v[i]; + } +} + +// 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(GLfloat* out, GLfloat angle) { + identity(out); + out[0] = cos(angle); + out[1] = sin(angle); + out[4] = -sin(angle); + out[5] = cos(angle); +} + +// CREATE 4x4 ROTATION MATRIX AROUND Y AXIS +void rotationY(GLfloat* out, GLfloat angle) { + identity(out); + out[0] = cos(angle); + out[2] = -sin(angle); + out[8] = sin(angle); + out[10] = cos(angle); +} + +// CREATE 4x4 ROTATION MATRIX AROUND Y AXIS +void rotationX(GLfloat* out, GLfloat angle) { + identity(out); + out[5] = cos(angle); + out[6] = sin(angle); + out[9] = -sin(angle); + out[10] = cos(angle); +} + +// MULTIPLY ANY TO MATRICES +void multiplyAny(GLfloat* A, GLfloat* B, GLfloat* out, 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; + // printf("%d: ", i); + for (int j = 0; j < wA; j++) { + // printf("%d : %f ", j * hA + i % hA, A[j * hA + i % hA]); + result[i] += A[j * hA + i % hA] * B[j + i / hA * wB]; + } + // printf("\n"); + } + memcpy(out, result, sizeOut * sizeof(GLfloat)); + free(result); + result = NULL; +} + +// MULTIPLY TWO 4x4 MATRICES +void multiply(GLfloat* A, GLfloat* B, GLfloat* out) { + multiplyAny(A, B, out, 4, 4, 4); +} + +// MULTIPLY in WITH TRANSLATION MATRIX OF v +void translate(GLfloat* out, GLfloat* in, GLfloat* v) { + GLfloat translationMatrix[16]; + translation(translationMatrix, v); + multiply(translationMatrix, in, out); +} + +// MULTIPLY in WITH SCALING MATRIX OF v +void scale(GLfloat* out, GLfloat* in, GLfloat* v) { + GLfloat scalingMatrix[16]; + scaling(scalingMatrix, v); + multiply(scalingMatrix, in, out); +} + +// MULTIPLY in WITH ROTATION MATRIX OF a AROUND Z AXIS +void rotateZ(GLfloat* out, GLfloat* in, GLfloat angle) { + GLfloat rotationMatrix[16]; + rotationZ(rotationMatrix, angle); + multiply(rotationMatrix, in, out); +} +// MULTIPLY in WITH ROTATION MATRIX OF a AROUND Y AXIS +void rotateY(GLfloat* out, GLfloat* in, GLfloat angle) { + GLfloat rotationMatrix[16]; + rotationY(rotationMatrix, angle); + multiply(rotationMatrix, in, out); +} +// MULTIPLY in WITH ROTATION MATRIX OF a AROUND X AXIS +void rotateX(GLfloat* out, GLfloat* in, GLfloat angle) { + GLfloat rotationMatrix[16]; + rotationX(rotationMatrix, angle); + multiply(rotationMatrix, in, out); +} + +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(GLfloat* out, GLfloat* in) { + transposeAny(out, 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(GLfloat* a) { + printAny(a, 1, 3); +} + +void mat4Print(GLfloat* m) { + printAny(m, 4, 4); +} + +void lookAt(GLfloat* out, GLfloat* eye, GLfloat* center, GLfloat* up) { + + + GLfloat n[3]; + vec3Subtract(n, eye, center); + + GLfloat u[3]; + vec3Cross(u, up, n); + + GLfloat v[3]; + vec3Cross(v, n, u); + + + vec3Normalise(n, n); + vec3Normalise(u, u); + vec3Normalise(v, v); + + + GLfloat Mr[16]; + identity(Mr); + + memcpy(&Mr[0], u, sizeof(GLfloat) * 3); + memcpy(&Mr[4], v, sizeof(GLfloat) * 3); + memcpy(&Mr[8], n, sizeof(GLfloat) * 3); + transpose(Mr, Mr); + + + GLfloat t[3]; + vec3Multiply(u, u, -1); + vec3Multiply(v, v, -1); + vec3Multiply(n, n, -1); + + + + t[0] = vec3Dot(u, eye); + t[1] = vec3Dot(v, eye); + t[2] = vec3Dot(n, eye); + + + memcpy(&Mr[12], t, sizeof(GLfloat) * 3); + + memcpy(out, Mr, sizeof(GLfloat) * 16); +} \ No newline at end of file diff --git a/u05-1/matrixMath.h b/u05-1/matrixMath.h new file mode 100644 index 0000000..f72156e --- /dev/null +++ b/u05-1/matrixMath.h @@ -0,0 +1,40 @@ +#ifndef MATRIX_MATH +#define MATRIX_MATH + +#include + +void vec3Zero(GLfloat* out); +void vec3Add(GLfloat* out, GLfloat* a, GLfloat* b); +void vec3Multiply(GLfloat* out, GLfloat* a, GLfloat x); +void vec3Subtract(GLfloat* out, GLfloat* a, GLfloat* b); +void vec3Cross(GLfloat* out, GLfloat* a, GLfloat* b); +void vec3Normalise(GLfloat* out, GLfloat* a); +GLfloat vec3Length(GLfloat* a); +GLfloat vec3Dot(GLfloat* a, GLfloat* b); + +void identity(GLfloat* out); +void translation(GLfloat* out, GLfloat* v); +void scaling(GLfloat* out, GLfloat* v); +void rotationZ(GLfloat* out, GLfloat angle); +void rotationY(GLfloat* out, GLfloat angle); +void rotationX(GLfloat* out, GLfloat angle); + +void multiplyAny(GLfloat* A, GLfloat* B, GLfloat* out, int wA, int hA, int wB); +void multiply(GLfloat* A, GLfloat* B, GLfloat* out); + +void translate(GLfloat* out, GLfloat* in, GLfloat* v); +void scale(GLfloat* out, GLfloat* in, GLfloat* v); +void rotateZ(GLfloat* out, GLfloat* in, GLfloat angle); +void rotateY(GLfloat* out, GLfloat* in, GLfloat angle); +void rotateX(GLfloat* out, GLfloat* in, GLfloat angle); + +void transposeAny(GLfloat* out, GLfloat* in, int w, int h); +void transpose(GLfloat* out, GLfloat* in); + +void printAny(GLfloat* M, int w, int h); +void vec3Print(GLfloat* a); +void mat4Print(GLfloat* m); + +void lookAt(GLfloat* out, GLfloat* eye, GLfloat* center, GLfloat* up); + +#endif \ No newline at end of file diff --git a/u05-1/vertexShader.glsl b/u05-1/vertexShader.glsl new file mode 100644 index 0000000..28c164b --- /dev/null +++ b/u05-1/vertexShader.glsl @@ -0,0 +1,8 @@ +#version 330 core +layout (location = 0) in vec3 aPosition; +uniform mat4 transformation; +out vec3 color; +void main() { + color = aPosition / 2 + vec3(0.5, 0.5, 0.5); + gl_Position = transformation * vec4(aPosition, 1.0); +} \ No newline at end of file