Functioning clock
This commit is contained in:
parent
d3ff1ed2ad
commit
43549c8cab
8 changed files with 1198 additions and 72 deletions
|
@ -18,7 +18,7 @@ set(ALL_LIBS
|
|||
GLEW
|
||||
)
|
||||
|
||||
set(CMAKE_C_FLAGS "-O0 -ggdb")
|
||||
set(CMAKE_C_FLAGS "-O0 -ggdb -lm")
|
||||
|
||||
add_definitions(
|
||||
-DTW_STATIC
|
||||
|
|
|
@ -6,6 +6,7 @@
|
|||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <math.h>
|
||||
#include <time.h>
|
||||
#include <unistd.h>
|
||||
|
||||
// Include GLEW
|
||||
|
|
|
@ -1,12 +1,18 @@
|
|||
#ifndef MODEL_LOADER_H
|
||||
#define MODEL_LOADER_H
|
||||
|
||||
typedef struct _entity {
|
||||
size_t offset;
|
||||
size_t size;
|
||||
} Entity;
|
||||
|
||||
typedef struct _model {
|
||||
GLfloat *buffer;
|
||||
GLfloat *object;
|
||||
size_t bufsize;
|
||||
Entity *entities[5];
|
||||
} Model;
|
||||
|
||||
Model * model_load(const char *path);
|
||||
Model * model_load(const char *);
|
||||
|
||||
#endif
|
||||
|
||||
|
|
|
@ -1,8 +1,11 @@
|
|||
#version 330 core
|
||||
|
||||
in float colorF;
|
||||
out vec3 color;
|
||||
|
||||
void main() {
|
||||
color = vec3(1, 0, 0);
|
||||
|
||||
if (colorF == 0)
|
||||
color = vec3(1, 1, 1);
|
||||
else
|
||||
color = vec3(0, 0, 0);
|
||||
}
|
||||
|
|
|
@ -1,12 +1,14 @@
|
|||
#version 330 core
|
||||
|
||||
// Input vertex data, different for all executions of this shader.
|
||||
layout(location = 0) in vec3 vertexPosition_modelspace;
|
||||
layout(location = 0) in vec4 position;
|
||||
out float colorF;
|
||||
|
||||
void main(){
|
||||
|
||||
gl_Position.xyz = vertexPosition_modelspace;
|
||||
gl_Position.w = 1.0;
|
||||
void main() {
|
||||
if (position.z <= 0.0)
|
||||
colorF = 0;
|
||||
else
|
||||
colorF = 1;
|
||||
|
||||
gl_Position = position;
|
||||
}
|
||||
|
||||
|
|
72
src/clock.c
72
src/clock.c
|
@ -64,17 +64,17 @@ clock_run(GLFWwindow *window) {
|
|||
0.0f, 0.3f, 0.0f, 1.0f
|
||||
};
|
||||
Model *model;
|
||||
model = model_load("../cube.obj");
|
||||
model = model_load("../clock.obj");
|
||||
|
||||
GLuint vertexbuffer;
|
||||
glGenBuffers(1, &vertexbuffer);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
|
||||
glBufferData(GL_ARRAY_BUFFER, model->bufsize*4*sizeof(GLfloat), model->buffer, GL_STATIC_DRAW);
|
||||
glBufferData(GL_ARRAY_BUFFER, model->bufsize*4*sizeof(GLfloat), model->object, GL_STATIC_DRAW);
|
||||
|
||||
double xpos, ypos;
|
||||
glfwGetCursorPos(window, &xpos, &ypos);
|
||||
|
||||
GLfloat **matrices = malloc(2 * sizeof(GLfloat *));
|
||||
GLfloat **matrices = malloc(4 * sizeof(GLfloat *));
|
||||
|
||||
GLfloat *temp = matrix_new();
|
||||
|
||||
|
@ -84,34 +84,36 @@ clock_run(GLFWwindow *window) {
|
|||
//temp[5] = cos(M_PI*2/60);
|
||||
|
||||
matrices[0] = temp;
|
||||
time_t t = time(NULL);
|
||||
for (int i = 1; i < 4; i++) {
|
||||
temp = matrix_new();
|
||||
|
||||
temp[0] = cos(M_PI*2/60/4);
|
||||
temp[8] = -sin(M_PI*2/60/4);
|
||||
temp[2] = sin(M_PI*2/60/4);
|
||||
temp[10] = cos(M_PI*2/60/4);
|
||||
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));
|
||||
|
||||
matrices[1] = temp;
|
||||
|
||||
GLfloat *mat = matrix_new();
|
||||
|
||||
for (int i = 1; i >= 0; i--) {
|
||||
temp = matrix_multip(matrices[i], mat);
|
||||
free(mat);
|
||||
mat = temp;
|
||||
matrices[i] = 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 = buffer;
|
||||
projection = temp;
|
||||
temp = malloc(model->bufsize * 4 * sizeof(GLfloat));
|
||||
buffer = malloc(model->bufsize * 4 * sizeof(GLfloat));
|
||||
memcpy(temp, model->buffer, 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,
|
||||
|
@ -129,18 +131,35 @@ clock_run(GLFWwindow *window) {
|
|||
|
||||
//g_vertex_buffer_data[6] = (float)xpos / 1280 * 2 - 1;
|
||||
//g_vertex_buffer_data[7] = 2 - (float)ypos / 720 * 2 - 1 + 0.15;
|
||||
buffer = matrix_transform(temp, model->bufsize, mat);
|
||||
free(temp);
|
||||
temp = buffer;
|
||||
buffer = matrix_transform(buffer, model->bufsize, projection);
|
||||
memcpy(model->buffer, buffer, model->bufsize * 4 * sizeof(GLfloat));
|
||||
time_t t = time(NULL);
|
||||
for (int i = 1; i < 4; i++) {
|
||||
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));
|
||||
|
||||
matrices[i] = temp_mat;
|
||||
t /= 60;
|
||||
}
|
||||
for (int i = 2; i < 5; i++) {
|
||||
GLfloat *slice;
|
||||
slice = matrix_transform(&(orig[model->entities[i]->offset]), model->entities[i]->size, matrices[i-1]);
|
||||
memcpy(&(temp[model->entities[i]->offset]), slice, model->entities[i]->size* 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->buffer, GL_STATIC_DRAW);
|
||||
glBufferData(GL_ARRAY_BUFFER, model->bufsize * 4 * sizeof(GLfloat), model->object, GL_STATIC_DRAW);
|
||||
|
||||
|
||||
// 1rst attribute buffer : vertices
|
||||
glEnableVertexAttribArray(0);
|
||||
|
@ -154,7 +173,8 @@ clock_run(GLFWwindow *window) {
|
|||
NULL // array buffer offset
|
||||
);
|
||||
|
||||
// Draw the triangle !
|
||||
|
||||
// Draw!
|
||||
glDrawArrays(GL_TRIANGLES, 0, model->bufsize); // 3 indices starting at 0 -> 1 triangle
|
||||
|
||||
glDisableVertexAttribArray(0);
|
||||
|
@ -166,10 +186,10 @@ clock_run(GLFWwindow *window) {
|
|||
// 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) == 0);
|
||||
!glfwWindowShouldClose(window));
|
||||
|
||||
// Close OpenGL window and terminate GLFW
|
||||
for (int i = 0; i < 2; i++) {
|
||||
for (int i = 0; i < 4; i++) {
|
||||
free(matrices[i]);
|
||||
}
|
||||
free(matrices);
|
||||
|
|
73
src/model.c
73
src/model.c
|
@ -3,32 +3,42 @@
|
|||
Model *
|
||||
model_new(size_t size) {
|
||||
Model *model = calloc(1, sizeof(Model));
|
||||
model->buffer = calloc((size ? size : 1) *4 , sizeof(GLfloat));
|
||||
model->object = calloc((size ? size : 1) *4 , sizeof(GLfloat));
|
||||
model->bufsize = size;
|
||||
for (int i = 0; i < 5; i++) {
|
||||
model->entities[i] = calloc(1, sizeof(Entity));
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
model_free(Model *self) {
|
||||
free(self->buffer);
|
||||
free(self->object);
|
||||
free(self);
|
||||
for (int i = 0; i < 5; i++) {
|
||||
free(self->entities[i]);
|
||||
}
|
||||
}
|
||||
|
||||
Model *
|
||||
model_load(const char *path) {
|
||||
Model *model;
|
||||
GLfloat *vertices;
|
||||
int *faces;
|
||||
size_t vertcount, facecount;
|
||||
char type, check, *line;
|
||||
int **faces;
|
||||
size_t vertcount, facecount[5] = {0};
|
||||
char type, check, *buffer;
|
||||
FILE *f;
|
||||
GLfloat colorset[3];
|
||||
size_t entity_read;
|
||||
|
||||
f = fopen(path, "r");
|
||||
line = malloc(255);
|
||||
buffer = malloc(255);
|
||||
vertices = malloc(3 * sizeof(GLfloat));
|
||||
faces = malloc(3 * sizeof(int));
|
||||
faces = malloc(5*sizeof(int *));
|
||||
for (int i = 0; i < 5; i++) {
|
||||
faces[i] = malloc(3 * sizeof(int));
|
||||
}
|
||||
type = 0;
|
||||
vertcount = 0;
|
||||
facecount = 0;
|
||||
|
||||
do {
|
||||
check = fscanf(f, "%c", &type);
|
||||
|
@ -43,11 +53,29 @@ model_load(const char *path) {
|
|||
vertcount++;
|
||||
}
|
||||
else if (type == 'f') {
|
||||
faces = realloc(faces, (facecount+1)*3*sizeof(GLfloat));
|
||||
check = fscanf(f, "%d %d %d\n", &(faces[facecount*3]),
|
||||
&(faces[facecount*3+1]),
|
||||
&(faces[facecount*3+2]));
|
||||
facecount++;
|
||||
faces[entity_read] = realloc(faces[entity_read], (facecount[entity_read]+1)*3*sizeof(GLfloat));
|
||||
check = fscanf(f, "%d %d %d\n", &(faces[entity_read][facecount[entity_read]*3]),
|
||||
&(faces[entity_read][facecount[entity_read]*3+1]),
|
||||
&(faces[entity_read][facecount[entity_read]*3+2]));
|
||||
facecount[entity_read]++;
|
||||
}
|
||||
else if (type == 'o') {
|
||||
check = fscanf(f, "%s\n", buffer);
|
||||
if (strstr(buffer, "Sphere")) {
|
||||
entity_read = 0;
|
||||
}
|
||||
else if (strstr(buffer, "Text")) {
|
||||
entity_read = 1;
|
||||
}
|
||||
else if (strstr(buffer, "Sec")) {
|
||||
entity_read = 2;
|
||||
}
|
||||
else if (strstr(buffer, "Min")) {
|
||||
entity_read = 3;
|
||||
}
|
||||
else if (strstr(buffer, "Hrs")) {
|
||||
entity_read = 4;
|
||||
}
|
||||
}
|
||||
else {
|
||||
check = fscanf(f, "%*[^\n]\n", NULL);
|
||||
|
@ -55,16 +83,25 @@ model_load(const char *path) {
|
|||
}
|
||||
} while(check != EOF);
|
||||
|
||||
model = model_new(facecount*3);
|
||||
for (int i = 0; i < facecount; i++) {
|
||||
int total_facecount = 0;
|
||||
for (int i = 0; i < 5; i++) {
|
||||
total_facecount += facecount[i];
|
||||
}
|
||||
model = model_new(total_facecount*3);
|
||||
int offset = 0;
|
||||
for (int ent = 0; ent < 5; ent++) {
|
||||
model->entities[ent]->offset = offset;
|
||||
for (int i = 0; i < facecount[ent]; i++) {
|
||||
for (int j = 0; j < 3; j++) {
|
||||
for (int k = 0; k < 3; k++) {
|
||||
model->buffer[i*12+j*4+k] = vertices[(faces[i*3+j]-1)*3+k];
|
||||
model->object[i*12+j*4+k+offset] = vertices[(faces[ent][i*3+j]-1)*3+k];
|
||||
}
|
||||
model->buffer[i*12+j*4+3] = 1;
|
||||
model->object[i*12+j*4+3+offset] = 1;
|
||||
}
|
||||
}
|
||||
|
||||
offset += facecount[ent]*4*3;
|
||||
model->entities[ent]->size = facecount[ent]*3;
|
||||
}
|
||||
return model;
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue