diff --git a/u04/Makefile b/u04/Makefile new file mode 100644 index 0000000..d974b17 --- /dev/null +++ b/u04/Makefile @@ -0,0 +1,21 @@ +GLEW_LIBS=$(shell pkgconf glew --libs) +GLFW_LIBS=$(shell pkgconf glfw3 --libs) + +OBJ = main.o + +cg1.out: $(OBJ) vertexShader.c fragmentShader.c + 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/u04/fragmentShader.c b/u04/fragmentShader.c new file mode 100644 index 0000000..d089f85 --- /dev/null +++ b/u04/fragmentShader.c @@ -0,0 +1,16 @@ +unsigned char fragmentShader_glsl[] = { + 0x23, 0x76, 0x65, 0x72, 0x73, 0x69, 0x6f, 0x6e, 0x20, 0x33, 0x33, 0x30, + 0x20, 0x63, 0x6f, 0x72, 0x65, 0x0a, 0x69, 0x6e, 0x20, 0x66, 0x6c, 0x6f, + 0x61, 0x74, 0x20, 0x66, 0x72, 0x61, 0x67, 0x6d, 0x65, 0x6e, 0x74, 0x56, + 0x61, 0x6c, 0x75, 0x65, 0x3b, 0x0a, 0x76, 0x6f, 0x69, 0x64, 0x20, 0x6d, + 0x61, 0x69, 0x6e, 0x28, 0x29, 0x20, 0x7b, 0x0a, 0x09, 0x67, 0x6c, 0x5f, + 0x46, 0x72, 0x61, 0x67, 0x43, 0x6f, 0x6c, 0x6f, 0x72, 0x20, 0x3d, 0x20, + 0x76, 0x65, 0x63, 0x34, 0x28, 0x6d, 0x69, 0x78, 0x28, 0x76, 0x65, 0x63, + 0x33, 0x28, 0x31, 0x2e, 0x30, 0x2c, 0x20, 0x30, 0x2e, 0x30, 0x2c, 0x20, + 0x30, 0x2e, 0x30, 0x29, 0x2c, 0x20, 0x76, 0x65, 0x63, 0x33, 0x28, 0x30, + 0x2e, 0x30, 0x2c, 0x20, 0x31, 0x2e, 0x30, 0x2c, 0x20, 0x30, 0x2e, 0x30, + 0x29, 0x2c, 0x20, 0x66, 0x72, 0x61, 0x67, 0x6d, 0x65, 0x6e, 0x74, 0x56, + 0x61, 0x6c, 0x75, 0x65, 0x29, 0x2c, 0x20, 0x31, 0x2e, 0x30, 0x29, 0x3b, + 0x0a, 0x7d +}; +unsigned int fragmentShader_glsl_len = 146; diff --git a/u04/fragmentShader.glsl b/u04/fragmentShader.glsl new file mode 100644 index 0000000..f2a1bd3 --- /dev/null +++ b/u04/fragmentShader.glsl @@ -0,0 +1,5 @@ +#version 330 core +in float fragmentValue; +void main() { + gl_FragColor = vec4(mix(vec3(1.0, 0.0, 0.0), vec3(0.0, 1.0, 0.0), fragmentValue), 1.0); +} \ No newline at end of file diff --git a/u04/main.c b/u04/main.c new file mode 100644 index 0000000..a2b07ba --- /dev/null +++ b/u04/main.c @@ -0,0 +1,425 @@ +#include + +#include +#include + +#include "vertexShader.c" +#include "fragmentShader.c" + +#include +#include +#include + +/** + * 0.15 0.25 + * | | + * + * 0__1 8__9 ___0.6 + * | | | | + * | | | | + * | 4_____6 | ___0.05 + * | 5_____7 | + * | | | | + * | | | | + * 2__3 10_11 + * + * 12__________14 __-0.75 + * 13__________15 __-0.85 + * + */ + +#define RESTART 345678 + + +typedef struct { + GLfloat x; + GLfloat y; + GLfloat value; +} Point; + +GLuint program; +GLuint vao; +GLuint indicesBufferObject; + +GLfloat step = 0.0f; + +GLuint indices[] = {0, 1, 2, 3, RESTART, 4, 6, 5, 7, RESTART, 8, 9, 10, 11, RESTART, 12, 14, 13, 15}; + + + +// 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); +} + +// 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); +} + +// MULTIPLY TWO 4x4 MATRICES +void multiply(GLfloat* A, GLfloat* B, GLfloat* out) { + multiplyAny(A, B, out, 4, 4, 4); +} + +// MULTIPLY 4x4 MATRIX WITH VEC4 +void multiplyV(GLfloat* M, GLfloat* v, GLfloat* out) { + multiplyAny(M, v, out, 4, 4, 1); +} + +// 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); +} + +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); + } + + // DEFINE POINTS OF LOGO + size_t size = sizeof(Point) * 16; + Point* corners = (Point *) malloc(size); + + corners[0].x = -0.25f; + corners[0].y = 0.6f; + corners[0].value = 0.0625f; + + corners[1].x = -0.15f; + corners[1].y = 0.6f; + corners[1].value = 0.125f; + + corners[2].x = -0.25f; + corners[2].y = -0.6f; + corners[2].value = 0.1875f; + + corners[3].x = -0.15f; + corners[3].y = -0.6f; + corners[3].value = 0.25f; + + corners[4].x = -0.15f; + corners[4].y = 0.05f; + corners[4].value = 0.3125f; + + corners[5].x = -0.15f; + corners[5].y = -0.05f; + corners[5].value = 0.375f; + + corners[6].x = 0.15f; + corners[6].y = 0.05f; + corners[6].value = 0.4375f; + + corners[7].x = 0.15f; + corners[7].y = -0.05f; + corners[7].value = 0.5f; + + corners[8].x = 0.15f; + corners[8].y = 0.6f; + corners[8].value = 0.5625f; + + corners[9].x = 0.25f; + corners[9].y = 0.6f; + corners[9].value = 0.625f; + + corners[10].x = 0.15f; + corners[10].y = -0.6f; + corners[10].value = 0.6875f; + + corners[11].x = 0.25f; + corners[11].y = -0.6f; + corners[11].value = 0.75f; + + corners[12].x = -0.25f; + corners[12].y = -0.75f; + corners[12].value = 0.8125f; + + corners[13].x = -0.25f; + corners[13].y = -0.85f; + corners[13].value = 0.875f; + + corners[14].x = 0.25f; + corners[14].y = -0.75f; + corners[14].value = 0.9375f; + + corners[15].x = 0.25f; + corners[15].y = -0.85f; + corners[15].value = 1.0f; + + GLuint triangleVertexBufferObject; + glGenBuffers(1, &triangleVertexBufferObject); + glBindBuffer(GL_ARRAY_BUFFER, triangleVertexBufferObject); + glBufferData(GL_ARRAY_BUFFER, size, corners, GL_STATIC_DRAW); + glBindBuffer(GL_ARRAY_BUFFER, 0); + + free(corners); + + + // create vertex array object + glGenVertexArrays(1, &vao); + glBindVertexArray(vao); + glBindBuffer(GL_ARRAY_BUFFER, triangleVertexBufferObject); + glVertexAttribPointer( + 0, + 2, + GL_FLOAT, + GL_FALSE, + sizeof(Point), + 0 + ); + glEnableVertexAttribArray(0); + glVertexAttribPointer( + 1, + 1, + GL_FLOAT, + GL_FALSE, + sizeof(Point), + (GLvoid*)(2 * sizeof(GLfloat)) + ); + glEnableVertexAttribArray(1); + 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, &indicesBufferObject); + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indicesBufferObject); + glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW); + + + glClearColor(0.2f, 0.3f, 0.3f, 1.0f); +} + +void draw(void) { + glClear(GL_COLOR_BUFFER_BIT); + glUseProgram(program); + glBindVertexArray(vao); + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indicesBufferObject); + + step += 0.005f; + if (step > 1.0f) step -= 1.0f; + + GLfloat M[16]; + GLfloat scaleFactor = sin(step * 3.14159f * 2) * 0.2f + 1.0f; + + GLfloat scaleBy[4] = {scaleFactor, scaleFactor, 1.0f, 1.0f}; + + GLfloat translateBy[4] = {sin(step * 3.14159f * 4.0f), 0.0f, 0.0f, 0.0f}; + + identity(M); + rotateZ(M, M, step * 3.14159f * 2); + scale(M, M, scaleBy); + translate(M, M, translateBy); + + glUniformMatrix4fv(glGetUniformLocation(program, "transformation"), 1, GL_FALSE, M); + + glDrawElements(GL_TRIANGLE_STRIP, sizeof(indices) / sizeof(GLuint), GL_UNSIGNED_INT, NULL); +} + +void framebuffer_size_callback(GLFWwindow *window, int width, int height) { + glViewport(0, 0, width, height); +} + +int main(void) { + + // GLfloat A[16]; + // GLfloat B[16]; + // GLfloat v[] = {1.0f, 2.0f, 3.0f}; + // GLfloat angle = 1.5f; + + // identity(A); + // identity(B); + // multiplyAny(A, B, A, 4, 4, 4); + + // translate(A, A, v); + // scale(A, A, v); + // rotateZ(A, A, angle); + + + // // DEBUG + // // PRINT COLUMN MAJOR MATRIX + // for (int i = 0; i < 16; i++) { + // if (i != 0 && i % 4 == 0) printf("\n"); + + // // cursed stuff to print column major form + // printf("%.2f ", A[i % 4 * 4 + i / 4]); + // } + // printf("\n"); + // // ------ + + // 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/u04/vertexShader.c b/u04/vertexShader.c new file mode 100644 index 0000000..845ea63 --- /dev/null +++ b/u04/vertexShader.c @@ -0,0 +1,25 @@ +unsigned char vertexShader_glsl[] = { + 0x23, 0x76, 0x65, 0x72, 0x73, 0x69, 0x6f, 0x6e, 0x20, 0x33, 0x33, 0x30, + 0x20, 0x63, 0x6f, 0x72, 0x65, 0x0a, 0x6c, 0x61, 0x79, 0x6f, 0x75, 0x74, + 0x20, 0x28, 0x6c, 0x6f, 0x63, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x20, 0x3d, + 0x20, 0x30, 0x29, 0x20, 0x69, 0x6e, 0x20, 0x76, 0x65, 0x63, 0x32, 0x20, + 0x61, 0x50, 0x6f, 0x73, 0x69, 0x74, 0x69, 0x6f, 0x6e, 0x3b, 0x0a, 0x6c, + 0x61, 0x79, 0x6f, 0x75, 0x74, 0x20, 0x28, 0x6c, 0x6f, 0x63, 0x61, 0x74, + 0x69, 0x6f, 0x6e, 0x20, 0x3d, 0x20, 0x31, 0x29, 0x20, 0x69, 0x6e, 0x20, + 0x66, 0x6c, 0x6f, 0x61, 0x74, 0x20, 0x76, 0x65, 0x72, 0x74, 0x65, 0x78, + 0x56, 0x61, 0x6c, 0x75, 0x65, 0x3b, 0x0a, 0x75, 0x6e, 0x69, 0x66, 0x6f, + 0x72, 0x6d, 0x20, 0x6d, 0x61, 0x74, 0x34, 0x20, 0x74, 0x72, 0x61, 0x6e, + 0x73, 0x66, 0x6f, 0x72, 0x6d, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x3b, 0x0a, + 0x6f, 0x75, 0x74, 0x20, 0x66, 0x6c, 0x6f, 0x61, 0x74, 0x20, 0x66, 0x72, + 0x61, 0x67, 0x6d, 0x65, 0x6e, 0x74, 0x56, 0x61, 0x6c, 0x75, 0x65, 0x3b, + 0x0a, 0x76, 0x6f, 0x69, 0x64, 0x20, 0x6d, 0x61, 0x69, 0x6e, 0x28, 0x29, + 0x20, 0x7b, 0x0a, 0x09, 0x66, 0x72, 0x61, 0x67, 0x6d, 0x65, 0x6e, 0x74, + 0x56, 0x61, 0x6c, 0x75, 0x65, 0x20, 0x3d, 0x20, 0x76, 0x65, 0x72, 0x74, + 0x65, 0x78, 0x56, 0x61, 0x6c, 0x75, 0x65, 0x3b, 0x0a, 0x09, 0x67, 0x6c, + 0x5f, 0x50, 0x6f, 0x73, 0x69, 0x74, 0x69, 0x6f, 0x6e, 0x20, 0x3d, 0x20, + 0x74, 0x72, 0x61, 0x6e, 0x73, 0x66, 0x6f, 0x72, 0x6d, 0x61, 0x74, 0x69, + 0x6f, 0x6e, 0x20, 0x2a, 0x20, 0x76, 0x65, 0x63, 0x34, 0x28, 0x61, 0x50, + 0x6f, 0x73, 0x69, 0x74, 0x69, 0x6f, 0x6e, 0x2c, 0x20, 0x30, 0x2e, 0x30, + 0x2c, 0x20, 0x31, 0x2e, 0x30, 0x29, 0x3b, 0x0a, 0x7d +}; +unsigned int vertexShader_glsl_len = 261; diff --git a/u04/vertexShader.glsl b/u04/vertexShader.glsl new file mode 100644 index 0000000..c7344b2 --- /dev/null +++ b/u04/vertexShader.glsl @@ -0,0 +1,9 @@ +#version 330 core +layout (location = 0) in vec2 aPosition; +layout (location = 1) in float vertexValue; +uniform mat4 transformation; +out float fragmentValue; +void main() { + fragmentValue = vertexValue; + gl_Position = transformation * vec4(aPosition, 0.0, 1.0); +} \ No newline at end of file