diff --git a/src/main.c b/src/main.c index 43e8f04..bd45394 100644 --- a/src/main.c +++ b/src/main.c @@ -136,14 +136,14 @@ void init(void) { GLfloat vertices[] = { // X // Y - -0.5f, -0.5f, -0.5f, - -0.5f, -0.5f, 0.5f, - -0.5f, 0.5f, -0.5f, - -0.5f, 0.5f, 0.5f, - 0.5f, -0.5f, -0.5f, - 0.5f, -0.5f, 0.5f, - 0.5f, 0.5f, -0.5f, - 0.5f, 0.5f, 0.5f, + 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, }; GLuint restart = 128; @@ -151,8 +151,24 @@ void init(void) { glPrimitiveRestartIndex(restart); GLuint indices[] = { - 0, 1, 2, - 0, 2, 5 + 0, 1, + 0, 2, + 0, 4, + + 1, 3, + 1, 5, + + 2, 3, + 2, 6, + + 3, 7, + + 4, 5, + 4, 6, + + 5, 7, + + 6, 7 }; DEBUG("Creating vertext buffer"); @@ -204,23 +220,26 @@ GLfloat currentHue = 0.0f; void draw(void) { updateStatusDisplay(); - currentHue += 0.005f; + currentHue += 0.0005f; if (currentHue > 1.0) currentHue = 0.0f; glClear(GL_COLOR_BUFFER_BIT); glUseProgram(program); mat4 transformMatrix; - vec3 position = {0.5f, 0.5f, 0.0f}; + vec3 scale = {0.2f, 0.2f, 0.2f}; mat4Identity(transformMatrix); - // mat4RotateZ(transformMatrix, transformMatrix, currentHue * PI * 2); - // mat4RotateY(transformMatrix, transformMatrix, currentHue * PI * 2); + mat4Scale(transformMatrix, transformMatrix, scale); + + mat4RotateZ(transformMatrix, transformMatrix, currentHue * PI * 2); + mat4RotateY(transformMatrix, transformMatrix, currentHue * PI * 2); mat4 viewMatrix; + mat4Identity(viewMatrix); vec3 cameraPos = {0.0f, sin(currentHue * PI * 2), 0.0f}; vec3 cameraLookAt = {0.0f, 0.0f, -1.0f}; vec3 cameraUp = {0.0f, 1.0f, 0.0f}; - mat4BuildLookAt(viewMatrix, cameraPos, cameraLookAt, cameraUp); + // mat4BuildLookAt(viewMatrix, cameraPos, cameraLookAt, cameraUp); GLuint transformLocation = glGetUniformLocation(program, "uTransform"); glUniformMatrix4fv(transformLocation, 1, GL_FALSE, transformMatrix); @@ -230,7 +249,7 @@ void draw(void) { glBindVertexArray(vertexArrayObject); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBuffer); - glDrawElements(GL_TRIANGLES, 20, GL_UNSIGNED_INT, 0); + glDrawElements(GL_LINES, 24, GL_UNSIGNED_INT, 0); } void framebuffer_size_callback(GLFWwindow* window, int width, int height) { diff --git a/src/matrix-math.c b/src/matrix-math.c index ef1f763..688b904 100644 --- a/src/matrix-math.c +++ b/src/matrix-math.c @@ -76,6 +76,18 @@ void mat4Translate(mat4 out, mat4 in, vec3 v) { mat4Multiply(out, T, in); } + +void mat4Scale(mat4 out, mat4 in, vec3 v) { + mat4 T; + mat4Identity(T); + + T[0] = v[0]; + T[5] = v[1]; + T[10] = v[2]; + + mat4Multiply(out, T, in); +} + void mat4RotateX(mat4 out, mat4 in, GLfloat a) { mat4 T; mat4Identity(T); @@ -130,42 +142,42 @@ void vec3CrossProduct(vec3 out, vec3 a, vec3 b) { } void vec3Normalise(vec3 out, vec3 in) { - GLfloat length = sqrt(in[0] * in[0] + in[1] * in[1] + in[2] * in[2]); + GLfloat length = vec3Length(in); out[0] = in[0] / length; out[1] = in[1] / length; out[2] = in[2] / length; } +GLfloat vec3Length(vec3 in) { + return sqrt(in[0] * in[0] + in[1] * in[1] + in[2] * in[2]); +} + GLfloat vec3DotProduct(vec3 a, vec3 b) { return a[0] * b[0] + a[1] * b[1] + a[2] * b[2]; } -void mat4BuildLookAt(mat4 out, vec3 eye, vec3 center, vec3 up) { +void mat4BuildLookAt(mat4 out, vec3 eye, vec3 look, vec3 up) { vec3 n; vec3 u; vec3 v; + vec3 t; - vec3Subtract(n, eye, center); + vec3Subtract(n, eye, look); vec3CrossProduct(u, up, n); vec3CrossProduct(v, n, u); - mat4Identity(out); + vec3Normalise(n, n); + vec3Normalise(u, u); + vec3Normalise(v, v); - out[0] = u[0]; - out[1] = v[0]; - out[2] = n[0]; + t[0] = - vec3DotProduct(u, eye); + t[1] = - vec3DotProduct(v, eye); + t[2] = - vec3DotProduct(n, eye); - out[4] = u[1]; - out[5] = v[1]; - out[6] = n[1]; - - out[8] = u[2]; - out[9] = v[2]; - out[10] = n[2]; - - out[12] = - vec3DotProduct(u, eye); - out[13] = - vec3DotProduct(v, eye); - out[14] = - vec3DotProduct(n, eye); + out[0] = u[0]; out[4] = u[1]; out[8] = u[2]; out[12] = t[0]; + 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; } \ No newline at end of file diff --git a/src/matrix-math.h b/src/matrix-math.h index 3b38b2a..875675c 100644 --- a/src/matrix-math.h +++ b/src/matrix-math.h @@ -18,10 +18,17 @@ extern void mat4Empty(mat4 mat); extern void mat4Multiply(mat4 result, mat4 A, mat4 B); extern void mat4Print(mat4 m); extern void mat4Translate(mat4 out, mat4 in, vec3 v); +extern void mat4Scale(mat4 out, mat4 in, vec3 v); 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 vec3Subtract(vec3 out, vec3 a, vec3 b); +extern void vec3CrossProduct(vec3 out, vec3 a, vec3 b); +extern void vec3Normalise(vec3 out, vec3 in); +extern GLfloat vec3Length(vec3 in); +extern GLfloat vec3DotProduct(vec3 a, vec3 b); + #endif \ No newline at end of file