Code cleanup
This commit is contained in:
parent
75cedfea0e
commit
29251ba60c
9 changed files with 178 additions and 184 deletions
|
@ -13,20 +13,20 @@ include_directories(
|
|||
)
|
||||
|
||||
set(ALL_LIBS
|
||||
${OPENGL_LIBRARY}
|
||||
glfw
|
||||
GLEW
|
||||
${OPENGL_LIBRARY}
|
||||
glfw
|
||||
GLEW
|
||||
m
|
||||
)
|
||||
|
||||
set(CMAKE_C_FLAGS "-O0 -ggdb -Wall")
|
||||
|
||||
add_definitions(
|
||||
-DTW_STATIC
|
||||
-DTW_NO_LIB_PRAGMA
|
||||
-DTW_NO_DIRECT3D
|
||||
-DGLEW_STATIC
|
||||
-D_CRT_SECURE_NO_WARNINGS
|
||||
-DTW_STATIC
|
||||
-DTW_NO_LIB_PRAGMA
|
||||
-DTW_NO_DIRECT3D
|
||||
-DGLEW_STATIC
|
||||
-D_CRT_SECURE_NO_WARNINGS
|
||||
)
|
||||
|
||||
add_executable(
|
||||
|
|
|
@ -13,6 +13,7 @@
|
|||
#include <GL/glew.h>
|
||||
|
||||
// Include GLFW
|
||||
#define GLFW_INCLUDE_VULKAN
|
||||
#include <GLFW/glfw3.h>
|
||||
|
||||
// Include project headers
|
||||
|
|
|
@ -6,7 +6,8 @@ typedef struct _model {
|
|||
size_t bufsize;
|
||||
} Model;
|
||||
|
||||
Model * model_load(const char *);
|
||||
Model *model_load(const char *);
|
||||
int model_free(Model *);
|
||||
|
||||
#endif
|
||||
|
||||
|
|
|
@ -1,12 +1,8 @@
|
|||
#version 330 core
|
||||
|
||||
in float colorF;
|
||||
out vec3 color;
|
||||
|
||||
void main() {
|
||||
if (colorF == 0)
|
||||
color = vec3(1, 1, 1);
|
||||
else
|
||||
color = vec3(0, 0, 0);
|
||||
color = vec3(1, 1, 1);
|
||||
}
|
||||
|
||||
|
|
236
src/cx.c
236
src/cx.c
|
@ -1,54 +1,53 @@
|
|||
#include <cx.h>
|
||||
|
||||
int cx_glinit(GLFWwindow **window) {
|
||||
// Initialise GLFW
|
||||
if(!glfwInit()) {
|
||||
fprintf(stderr, "Failed to initialize GLFW\n");
|
||||
getchar();
|
||||
return -1;
|
||||
}
|
||||
// Initialise GLFW
|
||||
if(!glfwInit()) {
|
||||
fprintf(stderr, "Failed to initialize GLFW\n");
|
||||
getchar();
|
||||
return -1;
|
||||
}
|
||||
|
||||
glfwWindowHint(GLFW_SAMPLES, 4);
|
||||
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
|
||||
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
|
||||
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); // To make MacOS happy; should not be needed
|
||||
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
|
||||
glfwWindowHint(GLFW_SAMPLES, 4);
|
||||
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
|
||||
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
|
||||
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); // To make MacOS happy; should not be needed
|
||||
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
|
||||
|
||||
// Open a window and create its OpenGL context
|
||||
*window = glfwCreateWindow(1280, 720, "OpenGL Clock", NULL, NULL);
|
||||
if (*window == NULL) {
|
||||
fprintf(stderr, "Failed to open GLFW window.\n");
|
||||
glfwTerminate();
|
||||
return -1;
|
||||
}
|
||||
// Open a window and create its OpenGL context
|
||||
*window = glfwCreateWindow(1280, 720, "CONTROL-X", NULL, NULL);
|
||||
if (*window == NULL) {
|
||||
fprintf(stderr, "Failed to open GLFW window.\n");
|
||||
glfwTerminate();
|
||||
return -1;
|
||||
}
|
||||
|
||||
glfwMakeContextCurrent(*window);
|
||||
glfwMakeContextCurrent(*window);
|
||||
|
||||
// Initialize GLEW
|
||||
if (glewInit() != GLEW_OK) {
|
||||
fprintf(stderr, "Failed to initialize GLEW\n");
|
||||
glfwTerminate();
|
||||
return -1;
|
||||
}
|
||||
// Initialize GLEW
|
||||
if (glewInit() != GLEW_OK) {
|
||||
fprintf(stderr, "Failed to initialize GLEW\n");
|
||||
glfwTerminate();
|
||||
return -1;
|
||||
}
|
||||
|
||||
// Ensure we can capture the escape key being pressed below
|
||||
glfwSetInputMode(*window, GLFW_STICKY_KEYS, GL_TRUE);
|
||||
// Ensure we can capture the escape key being pressed below
|
||||
glfwSetInputMode(*window, GLFW_STICKY_KEYS, GL_TRUE);
|
||||
|
||||
// Dark grey background
|
||||
glClearColor(0.15f, 0.15f, 0.15f, 0.0f);
|
||||
// Dark grey background
|
||||
glClearColor(0.15f, 0.15f, 0.15f, 0.0f);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int cx_glrun(GLFWwindow *window) {
|
||||
GLuint VertexArrayID;
|
||||
GLuint programID;
|
||||
GLuint VertexArrayID;
|
||||
GLuint programID;
|
||||
|
||||
glGenVertexArrays(1, &VertexArrayID);
|
||||
glBindVertexArray(VertexArrayID);
|
||||
|
||||
glGenVertexArrays(1, &VertexArrayID);
|
||||
glBindVertexArray(VertexArrayID);
|
||||
|
||||
// Create and compile our GLSL program from the shaders
|
||||
// Create and compile our GLSL program from the shaders
|
||||
if (LoadShaders(&programID,
|
||||
"../shaders/SimpleVertexShader.vertexshader",
|
||||
"../shaders/SimpleFragmentShader.fragmentshader")) {
|
||||
|
@ -56,118 +55,111 @@ int cx_glrun(GLFWwindow *window) {
|
|||
return -1;
|
||||
}
|
||||
|
||||
|
||||
// Load model to render from file
|
||||
Model *model;
|
||||
model = model_load("../triangle.obj");
|
||||
|
||||
GLuint vertexbuffer;
|
||||
glGenBuffers(1, &vertexbuffer);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
|
||||
glBufferData(GL_ARRAY_BUFFER, model->bufsize*4*sizeof(GLfloat), model->object, GL_STATIC_DRAW);
|
||||
// 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;
|
||||
glGenBuffers(1, &vertexbuffer);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
|
||||
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;
|
||||
glfwGetCursorPos(window, &xpos, &ypos);
|
||||
|
||||
GLfloat *matrix;
|
||||
GLfloat *temp = matrix_new();
|
||||
GLfloat *rotation_matrix = matrix_new();
|
||||
GLfloat *projection_matrix = matrix_new();
|
||||
|
||||
matrix = temp;
|
||||
time_t t = time(NULL);
|
||||
// Temporary storage of transformation results
|
||||
GLfloat *temp_buffer;
|
||||
GLfloat *projected_buffer;
|
||||
|
||||
temp = matrix_new();
|
||||
|
||||
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;
|
||||
projection_matrix[14] = -1.0f;
|
||||
projection_matrix[0] = (GLfloat)9/16; // Widescreen FOV
|
||||
|
||||
|
||||
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 {
|
||||
// Clear the screen. It's not mentioned before Tutorial 02,
|
||||
do {
|
||||
// Clear the screen. It's not mentioned before Tutorial 02,
|
||||
// but it can cause flickering, so it's there nonetheless.
|
||||
glClear(GL_COLOR_BUFFER_BIT);
|
||||
glClear(GL_COLOR_BUFFER_BIT);
|
||||
|
||||
// Use our shader
|
||||
glUseProgram(programID);
|
||||
// Use our shader
|
||||
glUseProgram(programID);
|
||||
time_t t = time(NULL);
|
||||
|
||||
GLfloat *temp_mat;
|
||||
temp_mat = matrix_new();
|
||||
|
||||
temp_mat[0] = cos(M_PI*2/60*(t%60));
|
||||
temp_mat[4] = -sin(M_PI*2/60*(t%60));
|
||||
temp_mat[1] = sin(M_PI*2/60*(t%60));
|
||||
temp_mat[5] = cos(M_PI*2/60*(t%60));
|
||||
|
||||
matrix = temp_mat;
|
||||
t /= 60;
|
||||
|
||||
for (int i = 2; i < 5; i++) {
|
||||
GLfloat *slice;
|
||||
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);
|
||||
memcpy(model->object, buffer, model->bufsize * 4 * sizeof(GLfloat));
|
||||
|
||||
GLuint vertexbuffer;
|
||||
glGenBuffers(1, &vertexbuffer);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
|
||||
glBufferData(GL_ARRAY_BUFFER, model->bufsize * 4 * sizeof(GLfloat), model->object, GL_STATIC_DRAW);
|
||||
rotation_matrix[0] = cos(M_PI*2/60*(t%60));
|
||||
rotation_matrix[4] = -sin(M_PI*2/60*(t%60));
|
||||
rotation_matrix[1] = sin(M_PI*2/60*(t%60));
|
||||
rotation_matrix[5] = cos(M_PI*2/60*(t%60));
|
||||
|
||||
|
||||
// 1rst attribute buffer : vertices
|
||||
glEnableVertexAttribArray(0);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
|
||||
glVertexAttribPointer(
|
||||
0, // attribute 0. No particular reason for 0, but must match the layout in the shader.
|
||||
4, // size
|
||||
GL_FLOAT, // type
|
||||
GL_FALSE, // normalized?
|
||||
0, // stride
|
||||
NULL // array buffer offset
|
||||
);
|
||||
// BANANA, ROH-TAH-TEH
|
||||
temp_buffer = matrix_transform(model->object, model->bufsize, rotation_matrix);
|
||||
|
||||
// Guess I'm just projecting.
|
||||
projected_buffer = matrix_transform(temp_buffer, model->bufsize, projection_matrix);
|
||||
|
||||
memcpy(render_buffer, projected_buffer, model->bufsize * 4 * sizeof(GLfloat));
|
||||
free(temp_buffer);
|
||||
free(projected_buffer);
|
||||
|
||||
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
|
||||
);
|
||||
|
||||
|
||||
// Draw!
|
||||
glDrawArrays(GL_TRIANGLES, 0, model->bufsize); // 3 indices starting at 0 -> 1 triangle
|
||||
// Draw!
|
||||
glDrawArrays(GL_TRIANGLES, 0, model->bufsize); // 3 indices starting at 0 -> 1 triangle
|
||||
|
||||
glDisableVertexAttribArray(0);
|
||||
|
||||
// Swap buffers
|
||||
glfwSwapBuffers(window);
|
||||
glfwPollEvents();
|
||||
glDisableVertexAttribArray(0);
|
||||
|
||||
// Swap buffers
|
||||
glfwSwapBuffers(window);
|
||||
glfwPollEvents();
|
||||
|
||||
// Check if the ESC key was pressed or the window was closed
|
||||
usleep(1000000/60);
|
||||
} while(glfwGetKey(window, GLFW_KEY_ESCAPE) != GLFW_PRESS &&
|
||||
!glfwWindowShouldClose(window));
|
||||
} while(glfwGetKey(window, GLFW_KEY_ESCAPE) != GLFW_PRESS &&
|
||||
!glfwWindowShouldClose(window));
|
||||
|
||||
// Close OpenGL window and terminate GLFW
|
||||
free(matrix);
|
||||
glfwTerminate();
|
||||
// Close OpenGL window and terminate GLFW
|
||||
glfwTerminate();
|
||||
free(rotation_matrix);
|
||||
free(projection_matrix);
|
||||
model_free(model);
|
||||
free(render_buffer);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
|
@ -14,5 +14,7 @@ main(void) {
|
|||
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;
|
||||
}
|
||||
|
||||
void
|
||||
model_free(Model *self) {
|
||||
free(self->object);
|
||||
free(self);
|
||||
}
|
||||
|
||||
Model *
|
||||
model_load(const char *path) {
|
||||
|
@ -63,8 +58,16 @@ model_load(const char *path) {
|
|||
}
|
||||
model->object[i*12+j*4+3] = 1;
|
||||
}
|
||||
model->bufsize = facecount*3;
|
||||
}
|
||||
free(vertices);
|
||||
free(faces);
|
||||
return model;
|
||||
}
|
||||
|
||||
int
|
||||
model_free(Model *self) {
|
||||
free(self->object);
|
||||
free(self);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
|
80
src/shader.c
80
src/shader.c
|
@ -12,7 +12,7 @@ load_code(const char *filepath, char **code) {
|
|||
return 1;
|
||||
}
|
||||
|
||||
*code = malloc(256 * sizeof(char));
|
||||
*code = malloc(1024 * sizeof(char));
|
||||
if (code == NULL) {
|
||||
fprintf(stderr, "Out of memory");
|
||||
return 1;
|
||||
|
@ -31,22 +31,22 @@ load_code(const char *filepath, char **code) {
|
|||
static int
|
||||
compile_code(const char *filepath, const char *code,
|
||||
GLuint ShaderID, GLint *Result) {
|
||||
int InfoLogLength;
|
||||
// Compile Shader
|
||||
printf("Compiling shader : %s\n", filepath);
|
||||
glShaderSource(ShaderID, 1, (const char **)&code, NULL);
|
||||
glCompileShader(ShaderID);
|
||||
int InfoLogLength;
|
||||
// Compile Shader
|
||||
printf("Compiling shader : %s\n", filepath);
|
||||
glShaderSource(ShaderID, 1, (const char **)&code, NULL);
|
||||
glCompileShader(ShaderID);
|
||||
|
||||
// Check Shader
|
||||
glGetShaderiv(ShaderID, GL_COMPILE_STATUS, Result);
|
||||
glGetShaderiv(ShaderID, GL_INFO_LOG_LENGTH, &InfoLogLength);
|
||||
if (InfoLogLength > 0) {
|
||||
char *ShaderErrorMessage = malloc(InfoLogLength+1);
|
||||
glGetShaderInfoLog(ShaderID, InfoLogLength, NULL, ShaderErrorMessage);
|
||||
printf("%s\n", ShaderErrorMessage);
|
||||
// Check Shader
|
||||
glGetShaderiv(ShaderID, GL_COMPILE_STATUS, Result);
|
||||
glGetShaderiv(ShaderID, GL_INFO_LOG_LENGTH, &InfoLogLength);
|
||||
if (InfoLogLength > 0) {
|
||||
char *ShaderErrorMessage = malloc(InfoLogLength+1);
|
||||
glGetShaderInfoLog(ShaderID, InfoLogLength, NULL, ShaderErrorMessage);
|
||||
printf("%s\n", ShaderErrorMessage);
|
||||
free(ShaderErrorMessage);
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
@ -57,58 +57,58 @@ LoadShaders(GLuint *programID, const char *vertex_file_path,
|
|||
char *vertex_code = NULL;
|
||||
char *fragment_code = NULL;
|
||||
|
||||
// Create the shaders
|
||||
GLuint VertexShaderID = glCreateShader(GL_VERTEX_SHADER);
|
||||
GLuint FragmentShaderID = glCreateShader(GL_FRAGMENT_SHADER);
|
||||
// Create the shaders
|
||||
GLuint VertexShaderID = glCreateShader(GL_VERTEX_SHADER);
|
||||
GLuint FragmentShaderID = glCreateShader(GL_FRAGMENT_SHADER);
|
||||
|
||||
// Read the Vertex Shader code from the file
|
||||
// Read the Vertex Shader code from the file
|
||||
if (load_code(vertex_file_path, (char **)&vertex_code)) {
|
||||
goto end;
|
||||
}
|
||||
|
||||
// Read the Fragment Shader code from the file
|
||||
// Read the Fragment Shader code from the file
|
||||
if (load_code(fragment_file_path, (char **)&fragment_code)) {
|
||||
goto end;
|
||||
}
|
||||
|
||||
GLint Result = GL_FALSE;
|
||||
GLint Result = GL_FALSE;
|
||||
|
||||
// Compile Vertex Shader
|
||||
if (compile_code(vertex_file_path, vertex_code, VertexShaderID, &Result)) {
|
||||
goto end;
|
||||
}
|
||||
|
||||
// Compile Fragment Shader
|
||||
// Compile Fragment Shader
|
||||
if (compile_code(fragment_file_path, fragment_code, FragmentShaderID, &Result)) {
|
||||
goto end;
|
||||
}
|
||||
|
||||
// Link the program
|
||||
printf("Linking program\n");
|
||||
*programID = glCreateProgram();
|
||||
glAttachShader(*programID, VertexShaderID);
|
||||
glAttachShader(*programID, FragmentShaderID);
|
||||
glLinkProgram(*programID);
|
||||
// Link the program
|
||||
printf("Linking program\n");
|
||||
*programID = glCreateProgram();
|
||||
glAttachShader(*programID, VertexShaderID);
|
||||
glAttachShader(*programID, FragmentShaderID);
|
||||
glLinkProgram(*programID);
|
||||
|
||||
GLint InfoLogLength;
|
||||
// Check the program
|
||||
glGetProgramiv(*programID, GL_LINK_STATUS, &Result);
|
||||
glGetProgramiv(*programID, GL_INFO_LOG_LENGTH, &InfoLogLength);
|
||||
if (InfoLogLength > 0) {
|
||||
char *ProgramErrorMessage = malloc(InfoLogLength+1);
|
||||
glGetProgramInfoLog(*programID, InfoLogLength, NULL, ProgramErrorMessage);
|
||||
printf("%s\n", ProgramErrorMessage);
|
||||
}
|
||||
// Check the program
|
||||
glGetProgramiv(*programID, GL_LINK_STATUS, &Result);
|
||||
glGetProgramiv(*programID, GL_INFO_LOG_LENGTH, &InfoLogLength);
|
||||
if (InfoLogLength > 0) {
|
||||
char *ProgramErrorMessage = malloc(InfoLogLength+1);
|
||||
glGetProgramInfoLog(*programID, InfoLogLength, NULL, ProgramErrorMessage);
|
||||
printf("%s\n", ProgramErrorMessage);
|
||||
}
|
||||
|
||||
|
||||
glDetachShader(*programID, VertexShaderID);
|
||||
glDetachShader(*programID, FragmentShaderID);
|
||||
glDetachShader(*programID, VertexShaderID);
|
||||
glDetachShader(*programID, FragmentShaderID);
|
||||
|
||||
glDeleteShader(VertexShaderID);
|
||||
glDeleteShader(FragmentShaderID);
|
||||
glDeleteShader(VertexShaderID);
|
||||
glDeleteShader(FragmentShaderID);
|
||||
|
||||
// If code got here, it means it was successful
|
||||
retval = 0;
|
||||
retval = 0;
|
||||
|
||||
end:
|
||||
free(vertex_code);
|
||||
|
|
|
@ -42,7 +42,6 @@ matrix_transform(GLfloat *vects, int vectcount,
|
|||
|
||||
result = calloc(vectcount*4, sizeof(GLfloat));
|
||||
|
||||
|
||||
for (int k = 0; k < vectcount; k++) {
|
||||
for (int j = 0; j < 4; j++) {
|
||||
dot_prod = 0;
|
||||
|
|
Loading…
Reference in a new issue