A whole bunch of unrelated tweaks.
This commit is contained in:
parent
87a03582cd
commit
edfe78a36e
12 changed files with 149 additions and 62 deletions
26
3d_assets/cube.obj
Normal file
26
3d_assets/cube.obj
Normal file
|
@ -0,0 +1,26 @@
|
||||||
|
# Blender 4.1.1
|
||||||
|
# www.blender.org
|
||||||
|
mtllib cube.mtl
|
||||||
|
o Cube
|
||||||
|
v 0.370694 0.370694 -0.370694
|
||||||
|
v 0.370694 -0.370694 -0.370694
|
||||||
|
v 0.370694 0.370694 0.370694
|
||||||
|
v 0.370694 -0.370694 0.370694
|
||||||
|
v -0.370694 0.370694 -0.370694
|
||||||
|
v -0.370694 -0.370694 -0.370694
|
||||||
|
v -0.370694 0.370694 0.370694
|
||||||
|
v -0.370694 -0.370694 0.370694
|
||||||
|
s 0
|
||||||
|
usemtl Material
|
||||||
|
f 5 3 1
|
||||||
|
f 3 8 4
|
||||||
|
f 7 6 8
|
||||||
|
f 2 8 6
|
||||||
|
f 1 4 2
|
||||||
|
f 5 2 6
|
||||||
|
f 5 7 3
|
||||||
|
f 3 7 8
|
||||||
|
f 7 5 6
|
||||||
|
f 2 4 8
|
||||||
|
f 1 3 4
|
||||||
|
f 5 1 2
|
24
3d_assets/cube_rotated.obj
Normal file
24
3d_assets/cube_rotated.obj
Normal file
|
@ -0,0 +1,24 @@
|
||||||
|
# Blender 4.1.1
|
||||||
|
# www.blender.org
|
||||||
|
o Cube
|
||||||
|
v 0.732743 -0.117712 -0.338262
|
||||||
|
v 0.295571 -0.722522 0.236198
|
||||||
|
v 0.507624 0.592343 0.237987
|
||||||
|
v 0.070452 -0.012468 0.812447
|
||||||
|
v -0.070452 0.012468 -0.812447
|
||||||
|
v -0.507624 -0.592343 -0.237987
|
||||||
|
v -0.295571 0.722522 -0.236198
|
||||||
|
v -0.732743 0.117712 0.338262
|
||||||
|
s 0
|
||||||
|
f 5 3 1
|
||||||
|
f 3 8 4
|
||||||
|
f 7 6 8
|
||||||
|
f 2 8 6
|
||||||
|
f 1 4 2
|
||||||
|
f 5 2 6
|
||||||
|
f 5 7 3
|
||||||
|
f 3 7 8
|
||||||
|
f 7 5 6
|
||||||
|
f 2 4 8
|
||||||
|
f 1 3 4
|
||||||
|
f 5 1 2
|
|
@ -1,12 +1,11 @@
|
||||||
#ifndef MATRIX_H
|
#ifndef MATRIX_H
|
||||||
#define MATRIX_H
|
#define MATRIX_H
|
||||||
|
|
||||||
GLfloat *matrix_new(void);
|
float *matrix_new(void);
|
||||||
|
|
||||||
GLfloat *matrix_multip(GLfloat *, GLfloat *);
|
float *matrix_multip(float *, float *);
|
||||||
|
|
||||||
GLfloat *matrix_transform(GLfloat *, int,
|
float *matrix_transform(float *, int, float *);
|
||||||
GLfloat *);
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
|
@ -1,8 +1,9 @@
|
||||||
#version 330 core
|
#version 330 core
|
||||||
|
|
||||||
|
in vec3 colorF;
|
||||||
out vec3 color;
|
out vec3 color;
|
||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
color = vec3(1, 1, 1);
|
color = colorF;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -2,13 +2,13 @@
|
||||||
|
|
||||||
// Input vertex data, different for all executions of this shader.
|
// Input vertex data, different for all executions of this shader.
|
||||||
layout(location = 0) in vec4 position;
|
layout(location = 0) in vec4 position;
|
||||||
out float colorF;
|
layout(location = 1) in vec4 normal;
|
||||||
|
out vec3 colorF;
|
||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
if (position.z <= 0.0)
|
colorF.x = position.x;
|
||||||
colorF = 0;
|
colorF.y = position.y;
|
||||||
else
|
colorF.z = position.z;
|
||||||
colorF = 1;
|
|
||||||
|
|
||||||
gl_Position = position;
|
gl_Position = position;
|
||||||
}
|
}
|
||||||
|
|
74
src/cx.c
74
src/cx.c
|
@ -4,16 +4,17 @@ int cx_glinit(GLFWwindow **window) {
|
||||||
// Initialise GLFW
|
// Initialise GLFW
|
||||||
if(!glfwInit()) {
|
if(!glfwInit()) {
|
||||||
fprintf(stderr, "Failed to initialize GLFW\n");
|
fprintf(stderr, "Failed to initialize GLFW\n");
|
||||||
getchar();
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
glfwWindowHint(GLFW_SAMPLES, 4);
|
glfwWindowHint(GLFW_SAMPLES, 4);
|
||||||
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
|
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
|
||||||
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 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_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
|
||||||
|
|
||||||
|
// To make MacOS happy; should not be needed
|
||||||
|
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
|
||||||
|
|
||||||
// Open a window and create its OpenGL context
|
// Open a window and create its OpenGL context
|
||||||
*window = glfwCreateWindow(1280, 720, "CONTROL-X", NULL, NULL);
|
*window = glfwCreateWindow(1280, 720, "CONTROL-X", NULL, NULL);
|
||||||
if (*window == NULL) {
|
if (*window == NULL) {
|
||||||
|
@ -58,19 +59,21 @@ int cx_glrun(GLFWwindow *window) {
|
||||||
|
|
||||||
// Load model to render from file
|
// Load model to render from file
|
||||||
Model *model;
|
Model *model;
|
||||||
model = model_load("../triangle.obj");
|
model = model_load("../3d_assets/cube.obj");
|
||||||
|
|
||||||
// Allocate the render buffer
|
// Allocate the render buffer
|
||||||
// GL uses this to feed the GPU
|
// GL uses this to feed the GPU
|
||||||
GLfloat *render_buffer;
|
GLfloat *render_buffer;
|
||||||
render_buffer = malloc(model->bufsize * 4 * sizeof(GLfloat));
|
render_buffer = malloc(model->bufsize * 4 * sizeof(GLfloat));
|
||||||
memcpy(render_buffer, model->object, model->bufsize * 4 * sizeof(GLfloat));
|
memcpy(render_buffer, model->object,
|
||||||
|
model->bufsize * 4 * sizeof(GLfloat));
|
||||||
|
|
||||||
// Bind the render buffer to OpenGL
|
// 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), render_buffer, 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);
|
||||||
|
@ -89,16 +92,17 @@ int cx_glrun(GLFWwindow *window) {
|
||||||
glfwGetCursorPos(window, &xpos, &ypos);
|
glfwGetCursorPos(window, &xpos, &ypos);
|
||||||
|
|
||||||
GLfloat *rotation_matrix = matrix_new();
|
GLfloat *rotation_matrix = matrix_new();
|
||||||
GLfloat *projection_matrix = matrix_new();
|
GLfloat *translation_matrix = matrix_new();
|
||||||
|
|
||||||
|
|
||||||
// Temporary storage of transformation results
|
// Temporary storage of transformation results
|
||||||
GLfloat *temp_buffer;
|
GLfloat *temp_buffer[2];
|
||||||
GLfloat *projected_buffer;
|
|
||||||
|
|
||||||
projection_matrix[14] = -1.0f;
|
translation_matrix[3] = 0.5f;
|
||||||
projection_matrix[0] = (GLfloat)9/16; // Widescreen FOV
|
|
||||||
|
|
||||||
|
|
||||||
|
int t = 0;
|
||||||
|
|
||||||
do {
|
do {
|
||||||
// Clear the screen. It's not mentioned before Tutorial 02,
|
// Clear the screen. It's not mentioned before Tutorial 02,
|
||||||
// but it can cause flickering, so it's there nonetheless.
|
// but it can cause flickering, so it's there nonetheless.
|
||||||
|
@ -106,24 +110,23 @@ int cx_glrun(GLFWwindow *window) {
|
||||||
|
|
||||||
// Use our shader
|
// Use our shader
|
||||||
glUseProgram(programID);
|
glUseProgram(programID);
|
||||||
time_t t = time(NULL);
|
|
||||||
|
|
||||||
|
|
||||||
rotation_matrix[0] = cos(M_PI*2/60*(t%60));
|
rotation_matrix[0] = cos(M_PI*2/256*(t%256));
|
||||||
rotation_matrix[4] = -sin(M_PI*2/60*(t%60));
|
rotation_matrix[8] = -sin(M_PI*2/256*(t%256));
|
||||||
rotation_matrix[1] = sin(M_PI*2/60*(t%60));
|
rotation_matrix[2] = sin(M_PI*2/256*(t%256));
|
||||||
rotation_matrix[5] = cos(M_PI*2/60*(t%60));
|
rotation_matrix[10] = cos(M_PI*2/256*(t%256));
|
||||||
|
|
||||||
|
|
||||||
// BANANA, ROH-TAH-TEH
|
// BANANA, ROH-TAH-TEH
|
||||||
temp_buffer = matrix_transform(model->object, model->bufsize, rotation_matrix);
|
temp_buffer[0] = matrix_transform(model->object, model->bufsize, rotation_matrix);
|
||||||
|
temp_buffer[1] = matrix_transform(temp_buffer[0], model->bufsize, translation_matrix);
|
||||||
|
|
||||||
// Guess I'm just projecting.
|
// Guess I'm just projecting.
|
||||||
projected_buffer = matrix_transform(temp_buffer, model->bufsize, projection_matrix);
|
free(temp_buffer[0]);
|
||||||
|
|
||||||
memcpy(render_buffer, projected_buffer, model->bufsize * 4 * sizeof(GLfloat));
|
memcpy(render_buffer, temp_buffer[1], model->bufsize * 4 * sizeof(GLfloat));
|
||||||
free(temp_buffer);
|
free(temp_buffer[1]);
|
||||||
free(projected_buffer);
|
|
||||||
|
|
||||||
glBufferData(GL_ARRAY_BUFFER, model->bufsize*4*sizeof(GLfloat), render_buffer, GL_STATIC_DRAW);
|
glBufferData(GL_ARRAY_BUFFER, model->bufsize*4*sizeof(GLfloat), render_buffer, GL_STATIC_DRAW);
|
||||||
|
|
||||||
|
@ -149,18 +152,45 @@ int cx_glrun(GLFWwindow *window) {
|
||||||
glfwSwapBuffers(window);
|
glfwSwapBuffers(window);
|
||||||
glfwPollEvents();
|
glfwPollEvents();
|
||||||
|
|
||||||
// Check if the ESC key was pressed or the window was closed
|
temp_buffer[0] = matrix_transform(model->object, model->bufsize, translation_matrix);
|
||||||
|
|
||||||
|
t++;
|
||||||
usleep(1000000/60);
|
usleep(1000000/60);
|
||||||
|
// Check if the ESC key was pressed or the window was closed
|
||||||
} while(glfwGetKey(window, GLFW_KEY_ESCAPE) != GLFW_PRESS &&
|
} while(glfwGetKey(window, GLFW_KEY_ESCAPE) != GLFW_PRESS &&
|
||||||
!glfwWindowShouldClose(window));
|
!glfwWindowShouldClose(window));
|
||||||
|
|
||||||
// Close OpenGL window and terminate GLFW
|
// Close OpenGL window and terminate GLFW
|
||||||
glfwTerminate();
|
glfwTerminate();
|
||||||
free(rotation_matrix);
|
free(rotation_matrix);
|
||||||
free(projection_matrix);
|
|
||||||
model_free(model);
|
model_free(model);
|
||||||
free(render_buffer);
|
free(render_buffer);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
int cx_nninit(Neural_Network **nn) {
|
||||||
|
// Allocate a Neural Network
|
||||||
|
*nn = neural_new(64, 1);
|
||||||
|
if(!*nn) {
|
||||||
|
fprintf(stderr, "Failed to initialize Neural Network.\n");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Populate the neural network with sensible values.
|
||||||
|
neural_randomize(*nn);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int cx_nnrun(Neural_Network *nn) {
|
||||||
|
|
||||||
|
// Establish a neural interface.
|
||||||
|
float *input_buffer = malloc(64*sizeof(float));
|
||||||
|
float *output_buffer;
|
||||||
|
|
||||||
|
output_buffer = neural_process(nn, input_buffer);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
|
@ -10,11 +10,17 @@
|
||||||
int
|
int
|
||||||
main(void) {
|
main(void) {
|
||||||
GLFWwindow *window;
|
GLFWwindow *window;
|
||||||
|
Neural_Network *nn;
|
||||||
|
int retval;
|
||||||
|
|
||||||
|
|
||||||
if (cx_glinit(&window)) {
|
if (cx_glinit(&window)) {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
if (cx_nninit(&nn)) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
int retval;
|
|
||||||
retval = cx_glrun(window);
|
retval = cx_glrun(window);
|
||||||
return retval;
|
return retval;
|
||||||
}
|
}
|
||||||
|
|
28
src/model.c
28
src/model.c
|
@ -2,17 +2,17 @@
|
||||||
|
|
||||||
Model *
|
Model *
|
||||||
model_new(size_t size) {
|
model_new(size_t size) {
|
||||||
Model *model = calloc(1, sizeof(Model));
|
Model *self = calloc(1, sizeof(Model));
|
||||||
model->object = calloc((size ? size : 1) *4 , sizeof(GLfloat));
|
self->object = calloc((size ? size : 1) *4 , sizeof(GLfloat));
|
||||||
model->bufsize = size;
|
self->bufsize = size;
|
||||||
return model;
|
return self;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
Model *
|
Model *
|
||||||
model_load(const char *path) {
|
model_load(const char *path) {
|
||||||
Model *model;
|
Model *self;
|
||||||
GLfloat *vertices;
|
float *vertices;
|
||||||
int *faces;
|
int *faces;
|
||||||
size_t vertcount, facecount;
|
size_t vertcount, facecount;
|
||||||
char type, check;
|
char type, check;
|
||||||
|
@ -20,7 +20,7 @@ model_load(const char *path) {
|
||||||
|
|
||||||
f = fopen(path, "r");
|
f = fopen(path, "r");
|
||||||
vertices = malloc(3 * sizeof(GLfloat));
|
vertices = malloc(3 * sizeof(GLfloat));
|
||||||
faces = malloc(3*sizeof(int *));
|
faces = malloc(3*sizeof(int));
|
||||||
type = 0;
|
type = 0;
|
||||||
vertcount = 0;
|
vertcount = 0;
|
||||||
facecount = 0;
|
facecount = 0;
|
||||||
|
@ -38,8 +38,8 @@ model_load(const char *path) {
|
||||||
vertcount++;
|
vertcount++;
|
||||||
}
|
}
|
||||||
else if (type == 'f') {
|
else if (type == 'f') {
|
||||||
faces = realloc(faces, (facecount+1)*3*sizeof(GLfloat));
|
faces = realloc(faces, (facecount+1)*3*sizeof(int));
|
||||||
check = fscanf(f, "%d %d %d\n", &(faces[facecount*3]),
|
check = fscanf(f, "%i %i %i\n", &(faces[facecount*3]),
|
||||||
&(faces[facecount*3+1]),
|
&(faces[facecount*3+1]),
|
||||||
&(faces[facecount*3+2]));
|
&(faces[facecount*3+2]));
|
||||||
facecount++;
|
facecount++;
|
||||||
|
@ -50,18 +50,20 @@ model_load(const char *path) {
|
||||||
}
|
}
|
||||||
} while(check != EOF);
|
} while(check != EOF);
|
||||||
|
|
||||||
model = model_new(facecount*3);
|
self = model_new(facecount*4);
|
||||||
for (int i = 0; i < facecount; i++) {
|
for (int i = 0; i < facecount; i++) {
|
||||||
for (int j = 0; j < 3; j++) {
|
for (int j = 0; j < 3; j++) {
|
||||||
for (int k = 0; k < 3; k++) {
|
for (int k = 0; k < 3; k++) {
|
||||||
model->object[i*12+j*4+k] = vertices[(faces[i*3+j]-1)*3+k];
|
self->object[i*12+j*4+k] = vertices[(faces[i*3+j]-1)*3+k];
|
||||||
|
printf("%f, ", vertices[(faces[i*3+j]-1)*3+k]);
|
||||||
}
|
}
|
||||||
model->object[i*12+j*4+3] = 1;
|
printf("\n");
|
||||||
|
self->object[i*12+j*4+3] = 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
free(vertices);
|
free(vertices);
|
||||||
free(faces);
|
free(faces);
|
||||||
return model;
|
return self;
|
||||||
}
|
}
|
||||||
|
|
||||||
int
|
int
|
||||||
|
|
|
@ -32,7 +32,6 @@ neural_new(size_t layer_size, size_t layers) {
|
||||||
}
|
}
|
||||||
|
|
||||||
return self;
|
return self;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
|
|
28
src/tensor.c
28
src/tensor.c
|
@ -1,10 +1,10 @@
|
||||||
#include "cx.h"
|
#include "cx.h"
|
||||||
|
|
||||||
GLfloat *
|
float *
|
||||||
matrix_new() {
|
matrix_new() {
|
||||||
GLfloat *mat;
|
float *mat;
|
||||||
|
|
||||||
mat = calloc(16, sizeof(GLfloat));
|
mat = calloc(16, sizeof(float));
|
||||||
|
|
||||||
for (int i = 0; i < 4; i++) {
|
for (int i = 0; i < 4; i++) {
|
||||||
mat[i*4+i] = 1;
|
mat[i*4+i] = 1;
|
||||||
|
@ -13,10 +13,10 @@ matrix_new() {
|
||||||
return mat;
|
return mat;
|
||||||
}
|
}
|
||||||
|
|
||||||
GLfloat *
|
float *
|
||||||
matrix_multip(GLfloat *mat1, GLfloat *mat2) {
|
matrix_multip(float *mat1, float *mat2) {
|
||||||
GLfloat *result;
|
float *result;
|
||||||
GLfloat dot_prod;
|
float dot_prod;
|
||||||
|
|
||||||
result = matrix_new();
|
result = matrix_new();
|
||||||
|
|
||||||
|
@ -34,13 +34,13 @@ matrix_multip(GLfloat *mat1, GLfloat *mat2) {
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
GLfloat *
|
float *
|
||||||
matrix_transform(GLfloat *vects, int vectcount,
|
matrix_transform(float *vects, int vectcount,
|
||||||
GLfloat *mat) {
|
float *mat) {
|
||||||
GLfloat dot_prod;
|
float dot_prod;
|
||||||
GLfloat *result;
|
float *result;
|
||||||
|
|
||||||
result = calloc(vectcount*4, sizeof(GLfloat));
|
result = calloc(vectcount*4, sizeof(float));
|
||||||
|
|
||||||
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++) {
|
||||||
|
@ -51,7 +51,7 @@ matrix_transform(GLfloat *vects, int vectcount,
|
||||||
result[j+k*4] = dot_prod;
|
result[j+k*4] = dot_prod;
|
||||||
}
|
}
|
||||||
if (result[k*4+3] != 0.0f) {
|
if (result[k*4+3] != 0.0f) {
|
||||||
GLfloat div = result[k*4+3];
|
float div = result[k*4+3];
|
||||||
for (int i = 0; i < 4; i++) {
|
for (int i = 0; i < 4; i++) {
|
||||||
result[k*4+i] /= div;
|
result[k*4+i] /= div;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue