work in progress
This commit is contained in:
parent
e0f3c7d2ff
commit
548b167968
70
src/main.c
70
src/main.c
|
@ -13,6 +13,7 @@
|
||||||
#include "matrix-math.h"
|
#include "matrix-math.h"
|
||||||
|
|
||||||
#define STATUS_INTERVAL 0.5
|
#define STATUS_INTERVAL 0.5
|
||||||
|
#define PI 3.14159f
|
||||||
|
|
||||||
GLuint program;
|
GLuint program;
|
||||||
|
|
||||||
|
@ -80,15 +81,6 @@ void updateStatusDisplay() {
|
||||||
|
|
||||||
void init(void) {
|
void init(void) {
|
||||||
|
|
||||||
mat4 t = {
|
|
||||||
1, 2, 3, 4,
|
|
||||||
5, 6, 7, 8,
|
|
||||||
9, 10, 11, 12,
|
|
||||||
13, 14, 15, 16
|
|
||||||
};
|
|
||||||
|
|
||||||
mat4Print(t);
|
|
||||||
|
|
||||||
INFO("Compiling Shaders...");
|
INFO("Compiling Shaders...");
|
||||||
|
|
||||||
// create and compile vertex shader
|
// create and compile vertex shader
|
||||||
|
@ -144,23 +136,14 @@ void init(void) {
|
||||||
|
|
||||||
GLfloat vertices[] = {
|
GLfloat vertices[] = {
|
||||||
// X // Y
|
// X // Y
|
||||||
-0.35f, 0.6f,
|
-0.5f, -0.5f, -0.5f,
|
||||||
-0.2f, 0.6f,
|
-0.5f, -0.5f, 0.5f,
|
||||||
-0.2f, 0.1f,
|
-0.5f, 0.5f, -0.5f,
|
||||||
0.2f, 0.1f,
|
-0.5f, 0.5f, 0.5f,
|
||||||
0.2f, 0.6f,
|
0.5f, -0.5f, -0.5f,
|
||||||
0.35f, 0.6f,
|
0.5f, -0.5f, 0.5f,
|
||||||
0.35f, -0.6f,
|
0.5f, 0.5f, -0.5f,
|
||||||
0.2f, -0.6f,
|
0.5f, 0.5f, 0.5f,
|
||||||
0.2f, -0.1f,
|
|
||||||
-0.2f, -0.1f,
|
|
||||||
-0.2f, -0.6f,
|
|
||||||
-0.35f, -0.6f,
|
|
||||||
|
|
||||||
-0.35f, -0.7f,
|
|
||||||
0.35f, -0.7f,
|
|
||||||
0.35f, -0.9f,
|
|
||||||
-0.35f, -0.9f
|
|
||||||
};
|
};
|
||||||
|
|
||||||
GLuint restart = 128;
|
GLuint restart = 128;
|
||||||
|
@ -168,10 +151,8 @@ void init(void) {
|
||||||
glPrimitiveRestartIndex(restart);
|
glPrimitiveRestartIndex(restart);
|
||||||
|
|
||||||
GLuint indices[] = {
|
GLuint indices[] = {
|
||||||
1, 0, 10, 11, restart,
|
0, 1, 2,
|
||||||
2, 9, 3, 8, restart,
|
0, 2, 5
|
||||||
5, 4, 6, 7, restart,
|
|
||||||
12, 15, 13, 14, restart
|
|
||||||
};
|
};
|
||||||
|
|
||||||
DEBUG("Creating vertext buffer");
|
DEBUG("Creating vertext buffer");
|
||||||
|
@ -190,10 +171,10 @@ void init(void) {
|
||||||
// vertex position data
|
// vertex position data
|
||||||
glVertexAttribPointer(
|
glVertexAttribPointer(
|
||||||
0, // shader location
|
0, // shader location
|
||||||
2, // number of values to read
|
3, // number of values to read
|
||||||
GL_FLOAT, // type of value
|
GL_FLOAT, // type of value
|
||||||
GL_FALSE, // if values are normalised
|
GL_FALSE, // if values are normalised
|
||||||
2 * sizeof(GLfloat), // stride - distance between vertices
|
3 * sizeof(GLfloat), // stride - distance between vertices
|
||||||
0 // start offset
|
0 // start offset
|
||||||
);
|
);
|
||||||
glEnableVertexAttribArray(0);
|
glEnableVertexAttribArray(0);
|
||||||
|
@ -210,7 +191,7 @@ void init(void) {
|
||||||
glClearColor(0.1f, 0.1f, 0.1f, 1.0f);
|
glClearColor(0.1f, 0.1f, 0.1f, 1.0f);
|
||||||
glViewport(0, 0, initialWindowWidth, initialWindowHeight);
|
glViewport(0, 0, initialWindowWidth, initialWindowHeight);
|
||||||
|
|
||||||
glEnable(GL_CULL_FACE);
|
// glEnable(GL_CULL_FACE);
|
||||||
glFrontFace(GL_CCW);
|
glFrontFace(GL_CCW);
|
||||||
|
|
||||||
initialiseStatusDisplay();
|
initialiseStatusDisplay();
|
||||||
|
@ -223,18 +204,33 @@ GLfloat currentHue = 0.0f;
|
||||||
void draw(void) {
|
void draw(void) {
|
||||||
updateStatusDisplay();
|
updateStatusDisplay();
|
||||||
|
|
||||||
currentHue += 0.001f;
|
currentHue += 0.005f;
|
||||||
if (currentHue > 1.0) currentHue = 0.0f;
|
if (currentHue > 1.0) currentHue = 0.0f;
|
||||||
|
|
||||||
glClear(GL_COLOR_BUFFER_BIT);
|
glClear(GL_COLOR_BUFFER_BIT);
|
||||||
glUseProgram(program);
|
glUseProgram(program);
|
||||||
|
|
||||||
GLuint positionLocation = glGetUniformLocation(program, "uPosition");
|
mat4 transformMatrix;
|
||||||
glUniform2f(positionLocation, cos(currentHue * 2 * M_PI) * 0.2f, sin(currentHue * 2 * M_PI) * 0.2f);
|
vec3 position = {0.5f, 0.5f, 0.0f};
|
||||||
|
mat4Identity(transformMatrix);
|
||||||
|
// mat4RotateZ(transformMatrix, transformMatrix, currentHue * PI * 2);
|
||||||
|
// mat4RotateY(transformMatrix, transformMatrix, currentHue * PI * 2);
|
||||||
|
|
||||||
|
mat4 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);
|
||||||
|
|
||||||
|
GLuint transformLocation = glGetUniformLocation(program, "uTransform");
|
||||||
|
glUniformMatrix4fv(transformLocation, 1, GL_FALSE, transformMatrix);
|
||||||
|
|
||||||
|
GLuint viewLocation = glGetUniformLocation(program, "uView");
|
||||||
|
glUniformMatrix4fv(viewLocation, 1, GL_FALSE, viewMatrix);
|
||||||
|
|
||||||
glBindVertexArray(vertexArrayObject);
|
glBindVertexArray(vertexArrayObject);
|
||||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBuffer);
|
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBuffer);
|
||||||
glDrawElements(GL_TRIANGLE_STRIP, 20, GL_UNSIGNED_INT, 0);
|
glDrawElements(GL_TRIANGLES, 20, GL_UNSIGNED_INT, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
void framebuffer_size_callback(GLFWwindow* window, int width, int height) {
|
void framebuffer_size_callback(GLFWwindow* window, int width, int height) {
|
||||||
|
|
|
@ -65,3 +65,107 @@ void mat4Print(mat4 m) {
|
||||||
printf("\n");
|
printf("\n");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void mat4Translate(mat4 out, mat4 in, vec3 v) {
|
||||||
|
mat4 T;
|
||||||
|
mat4Identity(T);
|
||||||
|
|
||||||
|
T[12] = v[0];
|
||||||
|
T[13] = v[1];
|
||||||
|
T[14] = v[2];
|
||||||
|
|
||||||
|
mat4Multiply(out, T, in);
|
||||||
|
}
|
||||||
|
void mat4RotateX(mat4 out, mat4 in, GLfloat a) {
|
||||||
|
mat4 T;
|
||||||
|
mat4Identity(T);
|
||||||
|
|
||||||
|
T[5] = cos(a);
|
||||||
|
T[6] = sin(a);
|
||||||
|
T[9] = -sin(a);
|
||||||
|
T[10] = cos(a);
|
||||||
|
|
||||||
|
mat4Multiply(out, T, in);
|
||||||
|
}
|
||||||
|
|
||||||
|
void mat4RotateY(mat4 out, mat4 in, GLfloat a) {
|
||||||
|
mat4 T;
|
||||||
|
mat4Identity(T);
|
||||||
|
|
||||||
|
T[0] = cos(a);
|
||||||
|
T[2] = -sin(a);
|
||||||
|
T[8] = sin(a);
|
||||||
|
T[10] = cos(a);
|
||||||
|
|
||||||
|
mat4Multiply(out, T, in);
|
||||||
|
}
|
||||||
|
|
||||||
|
void mat4RotateZ(mat4 out, mat4 in, GLfloat a) {
|
||||||
|
mat4 T;
|
||||||
|
mat4Identity(T);
|
||||||
|
|
||||||
|
T[0] = cos(a);
|
||||||
|
T[1] = sin(a);
|
||||||
|
T[4] = -sin(a);
|
||||||
|
T[5] = cos(a);
|
||||||
|
|
||||||
|
mat4Multiply(out, T, in);
|
||||||
|
}
|
||||||
|
|
||||||
|
void vec3Subtract(vec3 out, vec3 a, vec3 b) {
|
||||||
|
out[0] = a[0] - b[0];
|
||||||
|
out[1] = a[1] - b[1];
|
||||||
|
out[2] = a[2] - b[2];
|
||||||
|
}
|
||||||
|
|
||||||
|
void vec3CrossProduct(vec3 out, vec3 a, vec3 b) {
|
||||||
|
vec3 result;
|
||||||
|
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];
|
||||||
|
|
||||||
|
out[0] = result[0];
|
||||||
|
out[1] = result[1];
|
||||||
|
out[2] = result[2];
|
||||||
|
}
|
||||||
|
|
||||||
|
void vec3Normalise(vec3 out, vec3 in) {
|
||||||
|
GLfloat length = sqrt(in[0] * in[0] + in[1] * in[1] + in[2] * in[2]);
|
||||||
|
out[0] = in[0] / length;
|
||||||
|
out[1] = in[1] / length;
|
||||||
|
out[2] = in[2] / length;
|
||||||
|
}
|
||||||
|
|
||||||
|
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) {
|
||||||
|
vec3 n;
|
||||||
|
vec3 u;
|
||||||
|
vec3 v;
|
||||||
|
|
||||||
|
vec3Subtract(n, eye, center);
|
||||||
|
|
||||||
|
vec3CrossProduct(u, up, n);
|
||||||
|
|
||||||
|
vec3CrossProduct(v, n, u);
|
||||||
|
|
||||||
|
mat4Identity(out);
|
||||||
|
|
||||||
|
out[0] = u[0];
|
||||||
|
out[1] = v[0];
|
||||||
|
out[2] = n[0];
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
|
@ -3,14 +3,12 @@
|
||||||
|
|
||||||
#include <GL/gl.h>
|
#include <GL/gl.h>
|
||||||
|
|
||||||
// TODO: figure out better type definition to avoid constant casting to GLfloat*
|
|
||||||
// possibly typedef float mat4[16];
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* !!! ALL matrices are in column major
|
* !!! ALL matrices are in column major
|
||||||
*/
|
*/
|
||||||
|
|
||||||
typedef GLfloat vec4[4];
|
typedef GLfloat vec4[4];
|
||||||
|
typedef GLfloat vec3[3];
|
||||||
|
|
||||||
typedef GLfloat mat4[16];
|
typedef GLfloat mat4[16];
|
||||||
|
|
||||||
|
@ -19,5 +17,11 @@ extern void mat4Copy(mat4 src, mat4 dst);
|
||||||
extern void mat4Empty(mat4 mat);
|
extern void mat4Empty(mat4 mat);
|
||||||
extern void mat4Multiply(mat4 result, mat4 A, mat4 B);
|
extern void mat4Multiply(mat4 result, mat4 A, mat4 B);
|
||||||
extern void mat4Print(mat4 m);
|
extern void mat4Print(mat4 m);
|
||||||
|
extern void mat4Translate(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);
|
||||||
|
|
||||||
#endif
|
#endif
|
|
@ -1,10 +1,11 @@
|
||||||
#version 330 core
|
#version 330 core
|
||||||
layout (location = 0) in vec2 aPosition;
|
layout (location = 0) in vec3 aPosition;
|
||||||
uniform vec2 uPosition;
|
uniform mat4 uTransform;
|
||||||
|
uniform mat4 uView;
|
||||||
|
|
||||||
out vec3 vertexColor;
|
out vec3 vertexColor;
|
||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
vertexColor = vec3(1.0, 0.1, 0.1);
|
vertexColor = vec3(1.0, 0.1, 0.1);
|
||||||
gl_Position = vec4(uPosition + aPosition, 0.0, 1.0);
|
gl_Position = uView * uTransform * vec4(aPosition, 1.0);
|
||||||
}
|
}
|
Loading…
Reference in New Issue