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
|
GLEW
|
||||||
)
|
)
|
||||||
|
|
||||||
set(CMAKE_C_FLAGS "-O0 -ggdb")
|
set(CMAKE_C_FLAGS "-O0 -ggdb -lm")
|
||||||
|
|
||||||
add_definitions(
|
add_definitions(
|
||||||
-DTW_STATIC
|
-DTW_STATIC
|
||||||
|
|
|
@ -6,6 +6,7 @@
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <math.h>
|
#include <math.h>
|
||||||
|
#include <time.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
// Include GLEW
|
// Include GLEW
|
||||||
|
|
|
@ -1,12 +1,18 @@
|
||||||
#ifndef MODEL_LOADER_H
|
#ifndef MODEL_LOADER_H
|
||||||
#define MODEL_LOADER_H
|
#define MODEL_LOADER_H
|
||||||
|
|
||||||
|
typedef struct _entity {
|
||||||
|
size_t offset;
|
||||||
|
size_t size;
|
||||||
|
} Entity;
|
||||||
|
|
||||||
typedef struct _model {
|
typedef struct _model {
|
||||||
GLfloat *buffer;
|
GLfloat *object;
|
||||||
size_t bufsize;
|
size_t bufsize;
|
||||||
|
Entity *entities[5];
|
||||||
} Model;
|
} Model;
|
||||||
|
|
||||||
Model * model_load(const char *path);
|
Model * model_load(const char *);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
|
@ -1,8 +1,11 @@
|
||||||
#version 330 core
|
#version 330 core
|
||||||
|
|
||||||
out vec3 color;
|
in float colorF;
|
||||||
|
out vec3 color;
|
||||||
void main() {
|
|
||||||
color = vec3(1, 0, 0);
|
void main() {
|
||||||
|
if (colorF == 0)
|
||||||
}
|
color = vec3(1, 1, 1);
|
||||||
|
else
|
||||||
|
color = vec3(0, 0, 0);
|
||||||
|
}
|
||||||
|
|
|
@ -1,12 +1,14 @@
|
||||||
#version 330 core
|
#version 330 core
|
||||||
|
|
||||||
// Input vertex data, different for all executions of this shader.
|
// 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(){
|
|
||||||
|
void main() {
|
||||||
gl_Position.xyz = vertexPosition_modelspace;
|
if (position.z <= 0.0)
|
||||||
gl_Position.w = 1.0;
|
colorF = 0;
|
||||||
|
else
|
||||||
}
|
colorF = 1;
|
||||||
|
|
||||||
|
gl_Position = position;
|
||||||
|
}
|
||||||
|
|
74
src/clock.c
74
src/clock.c
|
@ -64,17 +64,17 @@ clock_run(GLFWwindow *window) {
|
||||||
0.0f, 0.3f, 0.0f, 1.0f
|
0.0f, 0.3f, 0.0f, 1.0f
|
||||||
};
|
};
|
||||||
Model *model;
|
Model *model;
|
||||||
model = model_load("../cube.obj");
|
model = model_load("../clock.obj");
|
||||||
|
|
||||||
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->buffer, GL_STATIC_DRAW);
|
glBufferData(GL_ARRAY_BUFFER, model->bufsize*4*sizeof(GLfloat), model->object, GL_STATIC_DRAW);
|
||||||
|
|
||||||
double xpos, ypos;
|
double xpos, ypos;
|
||||||
glfwGetCursorPos(window, &xpos, &ypos);
|
glfwGetCursorPos(window, &xpos, &ypos);
|
||||||
|
|
||||||
GLfloat **matrices = malloc(2 * sizeof(GLfloat *));
|
GLfloat **matrices = malloc(4 * sizeof(GLfloat *));
|
||||||
|
|
||||||
GLfloat *temp = matrix_new();
|
GLfloat *temp = matrix_new();
|
||||||
|
|
||||||
|
@ -84,34 +84,36 @@ clock_run(GLFWwindow *window) {
|
||||||
//temp[5] = cos(M_PI*2/60);
|
//temp[5] = cos(M_PI*2/60);
|
||||||
|
|
||||||
matrices[0] = temp;
|
matrices[0] = temp;
|
||||||
temp = matrix_new();
|
time_t t = time(NULL);
|
||||||
|
for (int i = 1; i < 4; i++) {
|
||||||
|
temp = matrix_new();
|
||||||
|
|
||||||
temp[0] = cos(M_PI*2/60/4);
|
temp[0] = cos(M_PI*2/60*(t%60));
|
||||||
temp[8] = -sin(M_PI*2/60/4);
|
temp[4] = -sin(M_PI*2/60*(t%60));
|
||||||
temp[2] = sin(M_PI*2/60/4);
|
temp[1] = sin(M_PI*2/60*(t%60));
|
||||||
temp[10] = cos(M_PI*2/60/4);
|
temp[5] = cos(M_PI*2/60*(t%60));
|
||||||
|
|
||||||
matrices[1] = temp;
|
matrices[i] = temp;
|
||||||
|
t /= 60;
|
||||||
GLfloat *mat = matrix_new();
|
|
||||||
|
|
||||||
for (int i = 1; i >= 0; i--) {
|
|
||||||
temp = matrix_multip(matrices[i], mat);
|
|
||||||
free(mat);
|
|
||||||
mat = temp;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
GLfloat *projection = matrix_new();
|
GLfloat *projection = matrix_new();
|
||||||
GLfloat *buffer;
|
GLfloat *buffer;
|
||||||
projection[14] = -1.0f;
|
projection[14] = -1.0f;
|
||||||
buffer = matrix_new();
|
buffer = matrix_new();
|
||||||
buffer[0] = (GLfloat)9/16;
|
buffer[0] = (GLfloat)9/16;
|
||||||
temp = matrix_multip(projection, buffer);
|
temp = matrix_multip(projection, buffer);
|
||||||
|
free(buffer);
|
||||||
free(projection);
|
free(projection);
|
||||||
projection = buffer;
|
projection = temp;
|
||||||
temp = malloc(model->bufsize * 4 * sizeof(GLfloat));
|
temp = malloc(model->bufsize * 4 * sizeof(GLfloat));
|
||||||
buffer = 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 {
|
do {
|
||||||
// Clear the screen. It's not mentioned before Tutorial 02,
|
// 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[6] = (float)xpos / 1280 * 2 - 1;
|
||||||
//g_vertex_buffer_data[7] = 2 - (float)ypos / 720 * 2 - 1 + 0.15;
|
//g_vertex_buffer_data[7] = 2 - (float)ypos / 720 * 2 - 1 + 0.15;
|
||||||
buffer = matrix_transform(temp, model->bufsize, mat);
|
time_t t = time(NULL);
|
||||||
free(temp);
|
for (int i = 1; i < 4; i++) {
|
||||||
temp = buffer;
|
GLfloat *temp_mat;
|
||||||
buffer = matrix_transform(buffer, model->bufsize, projection);
|
temp_mat = matrix_new();
|
||||||
memcpy(model->buffer, buffer, model->bufsize * 4 * sizeof(GLfloat));
|
|
||||||
|
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);
|
free(buffer);
|
||||||
|
|
||||||
|
buffer = matrix_transform(temp, model->bufsize, projection);
|
||||||
|
memcpy(model->object, buffer, model->bufsize * 4 * sizeof(GLfloat));
|
||||||
|
|
||||||
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->buffer, GL_STATIC_DRAW);
|
glBufferData(GL_ARRAY_BUFFER, model->bufsize * 4 * sizeof(GLfloat), model->object, GL_STATIC_DRAW);
|
||||||
|
|
||||||
|
|
||||||
// 1rst attribute buffer : vertices
|
// 1rst attribute buffer : vertices
|
||||||
glEnableVertexAttribArray(0);
|
glEnableVertexAttribArray(0);
|
||||||
|
@ -154,7 +173,8 @@ clock_run(GLFWwindow *window) {
|
||||||
NULL // array buffer offset
|
NULL // array buffer offset
|
||||||
);
|
);
|
||||||
|
|
||||||
// Draw the triangle !
|
|
||||||
|
// Draw!
|
||||||
glDrawArrays(GL_TRIANGLES, 0, model->bufsize); // 3 indices starting at 0 -> 1 triangle
|
glDrawArrays(GL_TRIANGLES, 0, model->bufsize); // 3 indices starting at 0 -> 1 triangle
|
||||||
|
|
||||||
glDisableVertexAttribArray(0);
|
glDisableVertexAttribArray(0);
|
||||||
|
@ -166,10 +186,10 @@ clock_run(GLFWwindow *window) {
|
||||||
// Check if the ESC key was pressed or the window was closed
|
// Check if the ESC key was pressed or the window was closed
|
||||||
usleep(1000000/60);
|
usleep(1000000/60);
|
||||||
} while(glfwGetKey(window, GLFW_KEY_ESCAPE) != GLFW_PRESS &&
|
} while(glfwGetKey(window, GLFW_KEY_ESCAPE) != GLFW_PRESS &&
|
||||||
glfwWindowShouldClose(window) == 0);
|
!glfwWindowShouldClose(window));
|
||||||
|
|
||||||
// Close OpenGL window and terminate GLFW
|
// 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[i]);
|
||||||
}
|
}
|
||||||
free(matrices);
|
free(matrices);
|
||||||
|
|
81
src/model.c
81
src/model.c
|
@ -3,32 +3,42 @@
|
||||||
Model *
|
Model *
|
||||||
model_new(size_t size) {
|
model_new(size_t size) {
|
||||||
Model *model = calloc(1, sizeof(Model));
|
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;
|
model->bufsize = size;
|
||||||
|
for (int i = 0; i < 5; i++) {
|
||||||
|
model->entities[i] = calloc(1, sizeof(Entity));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
model_free(Model *self) {
|
model_free(Model *self) {
|
||||||
free(self->buffer);
|
free(self->object);
|
||||||
free(self);
|
free(self);
|
||||||
|
for (int i = 0; i < 5; i++) {
|
||||||
|
free(self->entities[i]);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Model *
|
Model *
|
||||||
model_load(const char *path) {
|
model_load(const char *path) {
|
||||||
Model *model;
|
Model *model;
|
||||||
GLfloat *vertices;
|
GLfloat *vertices;
|
||||||
int *faces;
|
int **faces;
|
||||||
size_t vertcount, facecount;
|
size_t vertcount, facecount[5] = {0};
|
||||||
char type, check, *line;
|
char type, check, *buffer;
|
||||||
FILE *f;
|
FILE *f;
|
||||||
|
GLfloat colorset[3];
|
||||||
|
size_t entity_read;
|
||||||
|
|
||||||
f = fopen(path, "r");
|
f = fopen(path, "r");
|
||||||
line = malloc(255);
|
buffer = malloc(255);
|
||||||
vertices = malloc(3 * sizeof(GLfloat));
|
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;
|
type = 0;
|
||||||
vertcount = 0;
|
vertcount = 0;
|
||||||
facecount = 0;
|
|
||||||
|
|
||||||
do {
|
do {
|
||||||
check = fscanf(f, "%c", &type);
|
check = fscanf(f, "%c", &type);
|
||||||
|
@ -43,11 +53,29 @@ model_load(const char *path) {
|
||||||
vertcount++;
|
vertcount++;
|
||||||
}
|
}
|
||||||
else if (type == 'f') {
|
else if (type == 'f') {
|
||||||
faces = realloc(faces, (facecount+1)*3*sizeof(GLfloat));
|
faces[entity_read] = realloc(faces[entity_read], (facecount[entity_read]+1)*3*sizeof(GLfloat));
|
||||||
check = fscanf(f, "%d %d %d\n", &(faces[facecount*3]),
|
check = fscanf(f, "%d %d %d\n", &(faces[entity_read][facecount[entity_read]*3]),
|
||||||
&(faces[facecount*3+1]),
|
&(faces[entity_read][facecount[entity_read]*3+1]),
|
||||||
&(faces[facecount*3+2]));
|
&(faces[entity_read][facecount[entity_read]*3+2]));
|
||||||
facecount++;
|
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 {
|
else {
|
||||||
check = fscanf(f, "%*[^\n]\n", NULL);
|
check = fscanf(f, "%*[^\n]\n", NULL);
|
||||||
|
@ -55,16 +83,25 @@ model_load(const char *path) {
|
||||||
}
|
}
|
||||||
} while(check != EOF);
|
} while(check != EOF);
|
||||||
|
|
||||||
model = model_new(facecount*3);
|
int total_facecount = 0;
|
||||||
for (int i = 0; i < facecount; i++) {
|
for (int i = 0; i < 5; i++) {
|
||||||
for (int j = 0; j < 3; j++) {
|
total_facecount += facecount[i];
|
||||||
for (int k = 0; k < 3; k++) {
|
}
|
||||||
model->buffer[i*12+j*4+k] = vertices[(faces[i*3+j]-1)*3+k];
|
model = model_new(total_facecount*3);
|
||||||
}
|
int offset = 0;
|
||||||
model->buffer[i*12+j*4+3] = 1;
|
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->object[i*12+j*4+k+offset] = vertices[(faces[ent][i*3+j]-1)*3+k];
|
||||||
|
}
|
||||||
|
model->object[i*12+j*4+3+offset] = 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
offset += facecount[ent]*4*3;
|
||||||
|
model->entities[ent]->size = facecount[ent]*3;
|
||||||
}
|
}
|
||||||
|
|
||||||
return model;
|
return model;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue