Code cleanup
This commit is contained in:
parent
75cedfea0e
commit
29251ba60c
9 changed files with 178 additions and 184 deletions
|
@ -13,6 +13,7 @@
|
||||||
#include <GL/glew.h>
|
#include <GL/glew.h>
|
||||||
|
|
||||||
// Include GLFW
|
// Include GLFW
|
||||||
|
#define GLFW_INCLUDE_VULKAN
|
||||||
#include <GLFW/glfw3.h>
|
#include <GLFW/glfw3.h>
|
||||||
|
|
||||||
// Include project headers
|
// Include project headers
|
||||||
|
|
|
@ -6,7 +6,8 @@ typedef struct _model {
|
||||||
size_t bufsize;
|
size_t bufsize;
|
||||||
} Model;
|
} Model;
|
||||||
|
|
||||||
Model * model_load(const char *);
|
Model *model_load(const char *);
|
||||||
|
int model_free(Model *);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
|
@ -1,12 +1,8 @@
|
||||||
#version 330 core
|
#version 330 core
|
||||||
|
|
||||||
in float colorF;
|
|
||||||
out vec3 color;
|
out vec3 color;
|
||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
if (colorF == 0)
|
|
||||||
color = vec3(1, 1, 1);
|
color = vec3(1, 1, 1);
|
||||||
else
|
|
||||||
color = vec3(0, 0, 0);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
104
src/cx.c
104
src/cx.c
|
@ -15,7 +15,7 @@ int cx_glinit(GLFWwindow **window) {
|
||||||
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
|
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
|
||||||
|
|
||||||
// Open a window and create its OpenGL context
|
// Open a window and create its OpenGL context
|
||||||
*window = glfwCreateWindow(1280, 720, "OpenGL Clock", NULL, NULL);
|
*window = glfwCreateWindow(1280, 720, "CONTROL-X", NULL, NULL);
|
||||||
if (*window == NULL) {
|
if (*window == NULL) {
|
||||||
fprintf(stderr, "Failed to open GLFW window.\n");
|
fprintf(stderr, "Failed to open GLFW window.\n");
|
||||||
glfwTerminate();
|
glfwTerminate();
|
||||||
|
@ -44,7 +44,6 @@ int cx_glrun(GLFWwindow *window) {
|
||||||
GLuint VertexArrayID;
|
GLuint VertexArrayID;
|
||||||
GLuint programID;
|
GLuint programID;
|
||||||
|
|
||||||
|
|
||||||
glGenVertexArrays(1, &VertexArrayID);
|
glGenVertexArrays(1, &VertexArrayID);
|
||||||
glBindVertexArray(VertexArrayID);
|
glBindVertexArray(VertexArrayID);
|
||||||
|
|
||||||
|
@ -56,50 +55,49 @@ int cx_glrun(GLFWwindow *window) {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// Load model to render from file
|
||||||
Model *model;
|
Model *model;
|
||||||
model = model_load("../triangle.obj");
|
model = model_load("../triangle.obj");
|
||||||
|
|
||||||
|
// Allocate the render buffer
|
||||||
|
// GL uses this to feed the GPU
|
||||||
|
GLfloat *render_buffer;
|
||||||
|
render_buffer = malloc(model->bufsize * 4 * sizeof(GLfloat));
|
||||||
|
memcpy(render_buffer, model->object, model->bufsize * 4 * sizeof(GLfloat));
|
||||||
|
|
||||||
|
// Bind the render buffer to OpenGL
|
||||||
GLuint vertexbuffer;
|
GLuint vertexbuffer;
|
||||||
glGenBuffers(1, &vertexbuffer);
|
glGenBuffers(1, &vertexbuffer);
|
||||||
glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
|
glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
|
||||||
glBufferData(GL_ARRAY_BUFFER, model->bufsize*4*sizeof(GLfloat), model->object, GL_STATIC_DRAW);
|
glBufferData(GL_ARRAY_BUFFER, model->bufsize*4*sizeof(GLfloat), render_buffer, GL_STATIC_DRAW);
|
||||||
|
|
||||||
|
// 1rst attribute buffer : vertices
|
||||||
|
glEnableVertexAttribArray(0);
|
||||||
|
glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
|
||||||
|
glVertexAttribPointer(
|
||||||
|
0, // attribute 0 in the pipeline
|
||||||
|
4, // size
|
||||||
|
GL_FLOAT, // type
|
||||||
|
GL_FALSE, // normalized?
|
||||||
|
0, // stride
|
||||||
|
NULL // array buffer offset
|
||||||
|
);
|
||||||
|
|
||||||
|
// Remainder from cursor experiments, might be useful later
|
||||||
double xpos, ypos;
|
double xpos, ypos;
|
||||||
glfwGetCursorPos(window, &xpos, &ypos);
|
glfwGetCursorPos(window, &xpos, &ypos);
|
||||||
|
|
||||||
GLfloat *matrix;
|
GLfloat *rotation_matrix = matrix_new();
|
||||||
GLfloat *temp = matrix_new();
|
GLfloat *projection_matrix = matrix_new();
|
||||||
|
|
||||||
matrix = temp;
|
// Temporary storage of transformation results
|
||||||
time_t t = time(NULL);
|
GLfloat *temp_buffer;
|
||||||
|
GLfloat *projected_buffer;
|
||||||
|
|
||||||
temp = matrix_new();
|
projection_matrix[14] = -1.0f;
|
||||||
|
projection_matrix[0] = (GLfloat)9/16; // Widescreen FOV
|
||||||
|
|
||||||
temp[0] = cos(M_PI*2/60*(t%60));
|
|
||||||
temp[4] = -sin(M_PI*2/60*(t%60));
|
|
||||||
temp[1] = sin(M_PI*2/60*(t%60));
|
|
||||||
temp[5] = cos(M_PI*2/60*(t%60));
|
|
||||||
|
|
||||||
matrix = temp;
|
|
||||||
t /= 60;
|
|
||||||
|
|
||||||
|
|
||||||
GLfloat *projection = matrix_new();
|
|
||||||
GLfloat *buffer;
|
|
||||||
projection[14] = -1.0f;
|
|
||||||
buffer = matrix_new();
|
|
||||||
buffer[0] = (GLfloat)9/16;
|
|
||||||
temp = matrix_multip(projection, buffer);
|
|
||||||
free(buffer);
|
|
||||||
free(projection);
|
|
||||||
projection = temp;
|
|
||||||
temp = malloc(model->bufsize * 4 * sizeof(GLfloat));
|
|
||||||
buffer = malloc(model->bufsize * 4 * sizeof(GLfloat));
|
|
||||||
memcpy(temp, model->object, model->bufsize * 4 * sizeof(GLfloat));
|
|
||||||
|
|
||||||
GLfloat *orig;
|
|
||||||
orig = malloc(model->bufsize * 4 * sizeof(GLfloat));
|
|
||||||
memcpy(orig, model->object, model->bufsize * 4 * sizeof(GLfloat));
|
|
||||||
|
|
||||||
do {
|
do {
|
||||||
// Clear the screen. It's not mentioned before Tutorial 02,
|
// Clear the screen. It's not mentioned before Tutorial 02,
|
||||||
|
@ -110,39 +108,30 @@ int cx_glrun(GLFWwindow *window) {
|
||||||
glUseProgram(programID);
|
glUseProgram(programID);
|
||||||
time_t t = time(NULL);
|
time_t t = time(NULL);
|
||||||
|
|
||||||
GLfloat *temp_mat;
|
|
||||||
temp_mat = matrix_new();
|
|
||||||
|
|
||||||
temp_mat[0] = cos(M_PI*2/60*(t%60));
|
rotation_matrix[0] = cos(M_PI*2/60*(t%60));
|
||||||
temp_mat[4] = -sin(M_PI*2/60*(t%60));
|
rotation_matrix[4] = -sin(M_PI*2/60*(t%60));
|
||||||
temp_mat[1] = sin(M_PI*2/60*(t%60));
|
rotation_matrix[1] = sin(M_PI*2/60*(t%60));
|
||||||
temp_mat[5] = cos(M_PI*2/60*(t%60));
|
rotation_matrix[5] = cos(M_PI*2/60*(t%60));
|
||||||
|
|
||||||
matrix = temp_mat;
|
|
||||||
t /= 60;
|
|
||||||
|
|
||||||
for (int i = 2; i < 5; i++) {
|
// BANANA, ROH-TAH-TEH
|
||||||
GLfloat *slice;
|
temp_buffer = matrix_transform(model->object, model->bufsize, rotation_matrix);
|
||||||
slice = matrix_transform(orig, model->bufsize, matrix);
|
|
||||||
memcpy(temp, slice, model->bufsize* 4 * sizeof(GLfloat));
|
|
||||||
free(slice);
|
|
||||||
}
|
|
||||||
free(buffer);
|
|
||||||
|
|
||||||
buffer = matrix_transform(temp, model->bufsize, projection);
|
// Guess I'm just projecting.
|
||||||
memcpy(model->object, buffer, model->bufsize * 4 * sizeof(GLfloat));
|
projected_buffer = matrix_transform(temp_buffer, model->bufsize, projection_matrix);
|
||||||
|
|
||||||
GLuint vertexbuffer;
|
memcpy(render_buffer, projected_buffer, model->bufsize * 4 * sizeof(GLfloat));
|
||||||
glGenBuffers(1, &vertexbuffer);
|
free(temp_buffer);
|
||||||
glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
|
free(projected_buffer);
|
||||||
glBufferData(GL_ARRAY_BUFFER, model->bufsize * 4 * sizeof(GLfloat), model->object, GL_STATIC_DRAW);
|
|
||||||
|
|
||||||
|
glBufferData(GL_ARRAY_BUFFER, model->bufsize*4*sizeof(GLfloat), render_buffer, GL_STATIC_DRAW);
|
||||||
|
|
||||||
// 1rst attribute buffer : vertices
|
// 1rst attribute buffer : vertices
|
||||||
glEnableVertexAttribArray(0);
|
glEnableVertexAttribArray(0);
|
||||||
glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
|
glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
|
||||||
glVertexAttribPointer(
|
glVertexAttribPointer(
|
||||||
0, // attribute 0. No particular reason for 0, but must match the layout in the shader.
|
0, // attribute 0 in the pipeline
|
||||||
4, // size
|
4, // size
|
||||||
GL_FLOAT, // type
|
GL_FLOAT, // type
|
||||||
GL_FALSE, // normalized?
|
GL_FALSE, // normalized?
|
||||||
|
@ -166,8 +155,11 @@ int cx_glrun(GLFWwindow *window) {
|
||||||
!glfwWindowShouldClose(window));
|
!glfwWindowShouldClose(window));
|
||||||
|
|
||||||
// Close OpenGL window and terminate GLFW
|
// Close OpenGL window and terminate GLFW
|
||||||
free(matrix);
|
|
||||||
glfwTerminate();
|
glfwTerminate();
|
||||||
|
free(rotation_matrix);
|
||||||
|
free(projection_matrix);
|
||||||
|
model_free(model);
|
||||||
|
free(render_buffer);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
|
@ -14,5 +14,7 @@ main(void) {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
return cx_glrun(window);
|
int retval;
|
||||||
|
retval = cx_glrun(window);
|
||||||
|
return retval;
|
||||||
}
|
}
|
||||||
|
|
15
src/model.c
15
src/model.c
|
@ -8,11 +8,6 @@ model_new(size_t size) {
|
||||||
return model;
|
return model;
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
|
||||||
model_free(Model *self) {
|
|
||||||
free(self->object);
|
|
||||||
free(self);
|
|
||||||
}
|
|
||||||
|
|
||||||
Model *
|
Model *
|
||||||
model_load(const char *path) {
|
model_load(const char *path) {
|
||||||
|
@ -63,8 +58,16 @@ model_load(const char *path) {
|
||||||
}
|
}
|
||||||
model->object[i*12+j*4+3] = 1;
|
model->object[i*12+j*4+3] = 1;
|
||||||
}
|
}
|
||||||
model->bufsize = facecount*3;
|
|
||||||
}
|
}
|
||||||
|
free(vertices);
|
||||||
|
free(faces);
|
||||||
return model;
|
return model;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int
|
||||||
|
model_free(Model *self) {
|
||||||
|
free(self->object);
|
||||||
|
free(self);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
|
@ -12,7 +12,7 @@ load_code(const char *filepath, char **code) {
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
*code = malloc(256 * sizeof(char));
|
*code = malloc(1024 * sizeof(char));
|
||||||
if (code == NULL) {
|
if (code == NULL) {
|
||||||
fprintf(stderr, "Out of memory");
|
fprintf(stderr, "Out of memory");
|
||||||
return 1;
|
return 1;
|
||||||
|
|
|
@ -42,7 +42,6 @@ matrix_transform(GLfloat *vects, int vectcount,
|
||||||
|
|
||||||
result = calloc(vectcount*4, sizeof(GLfloat));
|
result = calloc(vectcount*4, sizeof(GLfloat));
|
||||||
|
|
||||||
|
|
||||||
for (int k = 0; k < vectcount; k++) {
|
for (int k = 0; k < vectcount; k++) {
|
||||||
for (int j = 0; j < 4; j++) {
|
for (int j = 0; j < 4; j++) {
|
||||||
dot_prod = 0;
|
dot_prod = 0;
|
||||||
|
|
Loading…
Reference in a new issue