Setup for test on R4
This commit is contained in:
7
Makefile
7
Makefile
@@ -1,6 +1,7 @@
|
|||||||
BIN = manigraph
|
BIN = manigraph
|
||||||
|
|
||||||
OBJ = \
|
OBJ = \
|
||||||
|
src/surface.o \
|
||||||
src/context.o \
|
src/context.o \
|
||||||
src/texture.o \
|
src/texture.o \
|
||||||
src/window.o \
|
src/window.o \
|
||||||
@@ -13,7 +14,7 @@ OBJ = \
|
|||||||
CFLAGS = \
|
CFLAGS = \
|
||||||
-I./ext/cglm/include \
|
-I./ext/cglm/include \
|
||||||
-I./ext/glfw/include \
|
-I./ext/glfw/include \
|
||||||
-Wall -Wno-unused-function -std=c99 \
|
-Wall -Wno-unused-function -std=c99 \
|
||||||
|
|
||||||
WAYLAND-LIB = \
|
WAYLAND-LIB = \
|
||||||
xdg-shell \
|
xdg-shell \
|
||||||
@@ -47,10 +48,12 @@ windows: $(OBJ) glfw.dll
|
|||||||
glfw.dll:
|
glfw.dll:
|
||||||
$(CC) -fPIC -shared -D_GLFW_WIN32 -D_GLFW_BUILD_DLL ./ext/glfw/src/*.c -o $@ -lgdi32
|
$(CC) -fPIC -shared -D_GLFW_WIN32 -D_GLFW_BUILD_DLL ./ext/glfw/src/*.c -o $@ -lgdi32
|
||||||
|
|
||||||
|
src/main.o: src/data/shaders.h src/data/cube.h src/data/axis.h
|
||||||
|
|
||||||
# LINUX
|
# LINUX
|
||||||
linux-x11: $(OBJ)
|
linux-x11: $(OBJ)
|
||||||
$(MAKE) BKN=_GLFW_X11 libglfw.so
|
$(MAKE) BKN=_GLFW_X11 libglfw.so
|
||||||
$(CC) -o $(BIN) $(OBJ) -L. -lGLEW -lGL -lglfw -lm
|
$(CC) -o $(BIN) $(OBJ) -L. -lGLEW -lGL -lglfw -lm
|
||||||
|
|
||||||
linux-wayland: $(OBJ)
|
linux-wayland: $(OBJ)
|
||||||
$(MAKE) BKN=_GLFW_WAYLAND libglfw.so
|
$(MAKE) BKN=_GLFW_WAYLAND libglfw.so
|
||||||
|
|||||||
@@ -6,6 +6,7 @@
|
|||||||
#undef F
|
#undef F
|
||||||
#undef G
|
#undef G
|
||||||
#undef H
|
#undef H
|
||||||
|
|
||||||
#define A -2.0,-0.05,-0.05,
|
#define A -2.0,-0.05,-0.05,
|
||||||
#define B -2.0,-0.05, 0.05,
|
#define B -2.0,-0.05, 0.05,
|
||||||
#define C -2.0, 0.05,-0.05,
|
#define C -2.0, 0.05,-0.05,
|
||||||
|
|||||||
@@ -1,8 +1,14 @@
|
|||||||
const char * vs =
|
const char * vs =
|
||||||
"#version 330 core\n"
|
"#version 330 core\n"
|
||||||
|
|
||||||
"layout (location = 0) in vec3 aPos;"
|
"layout (location = 0) in float aPos_x;"
|
||||||
"layout (location = 1) in vec3 aNormal;"
|
"layout (location = 1) in float aPos_y;"
|
||||||
|
"layout (location = 2) in float aPos_z;"
|
||||||
|
"layout (location = 3) in float aPos_w;"
|
||||||
|
"layout (location = 4) in float aNormal_x;"
|
||||||
|
"layout (location = 5) in float aNormal_y;"
|
||||||
|
"layout (location = 6) in float aNormal_z;"
|
||||||
|
"layout (location = 7) in float aNormal_w;"
|
||||||
|
|
||||||
"uniform float idx;"
|
"uniform float idx;"
|
||||||
"uniform mat4 fix;"
|
"uniform mat4 fix;"
|
||||||
@@ -16,12 +22,30 @@ const char * vs =
|
|||||||
"void main()"
|
"void main()"
|
||||||
"{"
|
"{"
|
||||||
" index=idx;"
|
" index=idx;"
|
||||||
|
" vec3 aNormal = vec3(aNormal_x,aNormal_y,aNormal_z);"
|
||||||
|
" vec3 aPos = vec3(aPos_x,aPos_y,aPos_z);"
|
||||||
" Normal = mat3(transpose(inverse(rot*mdl))) * aNormal;"
|
" Normal = mat3(transpose(inverse(rot*mdl))) * aNormal;"
|
||||||
" gl_Position = fix * rot * mdl * vec4( aPos, 1.0 );\n"
|
" gl_Position = fix * rot * mdl * vec4( aPos, 1.0 );\n"
|
||||||
" FragPos = vec3( rot * mdl * vec4(aPos, 1.0));"
|
" FragPos = vec3( rot * mdl * vec4(aPos, 1.0));"
|
||||||
"}";
|
"}";
|
||||||
|
|
||||||
|
|
||||||
|
const char * fs_plain =
|
||||||
|
"#version 330 core\n"
|
||||||
|
|
||||||
|
"uniform sampler2DArray palette;"
|
||||||
|
|
||||||
|
"in float index;"
|
||||||
|
"out vec4 FragColor;"
|
||||||
|
"in vec3 Normal;"
|
||||||
|
"in vec3 FragPos;"
|
||||||
|
|
||||||
|
"void main()"
|
||||||
|
"{"
|
||||||
|
" FragColor = texture( palette, vec3( 0, 0, index ) ).rgba;"
|
||||||
|
"}";
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
const char * fs =
|
const char * fs =
|
||||||
"#version 330 core\n"
|
"#version 330 core\n"
|
||||||
|
|||||||
16
src/input.c
16
src/input.c
@@ -1,6 +1,7 @@
|
|||||||
#include "main.h"
|
#include "main.h"
|
||||||
#include <cglm/quat.h>
|
#include <cglm/quat.h>
|
||||||
#include <GLFW/glfw3.h>
|
#include <GLFW/glfw3.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
#define ANGLE ((float)0x01/0xff*2*GLM_PI)
|
#define ANGLE ((float)0x01/0xff*2*GLM_PI)
|
||||||
|
|
||||||
@@ -17,6 +18,17 @@ vec3 axis[3] =
|
|||||||
{0, 0, 1},
|
{0, 0, 1},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
void __key_callback(GLFWwindow * window, int key, int scancode, int action, int mods )
|
||||||
|
{
|
||||||
|
if( action != GLFW_PRESS)
|
||||||
|
return;
|
||||||
|
|
||||||
|
if( GLFW_KEY_0 <= key && key <= GLFW_KEY_9 )
|
||||||
|
{
|
||||||
|
printf("%d\n", key-GLFW_KEY_0 );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void __window_callback(GLFWwindow * window, int w, int h)
|
void __window_callback(GLFWwindow * window, int w, int h)
|
||||||
{
|
{
|
||||||
int m;
|
int m;
|
||||||
@@ -37,8 +49,8 @@ void __mouse_callback(GLFWwindow* window, int button, int action, int mods)
|
|||||||
return;
|
return;
|
||||||
|
|
||||||
glfwGetCursorPos(window, &xpos, &ypos);
|
glfwGetCursorPos(window, &xpos, &ypos);
|
||||||
glReadPixels((int)xpos, (int)(window_height - ypos), 1, 1, GL_GREEN, GL_UNSIGNED_BYTE, &green_value);
|
glReadPixels((int)xpos, (int)(window_height - ypos), 1, 1, GL_GREEN,
|
||||||
|
GL_UNSIGNED_BYTE, &green_value);
|
||||||
|
|
||||||
switch(green_value)
|
switch(green_value)
|
||||||
{
|
{
|
||||||
|
|||||||
105
src/main.c
105
src/main.c
@@ -1,14 +1,16 @@
|
|||||||
#include "main.h"
|
#include "main.h"
|
||||||
#include "data/cube.h"
|
|
||||||
#include "data/axis.h"
|
#include "data/axis.h"
|
||||||
#include "data/shaders.h"
|
#include "data/shaders.h"
|
||||||
#include <GL/glew.h>
|
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
#include <GL/glew.h>
|
||||||
#include <cglm/vec3.h>
|
#include <cglm/vec3.h>
|
||||||
|
#include <cglm/vec4.h>
|
||||||
|
|
||||||
#define WIDTH 512
|
#define WIDTH 512
|
||||||
#define HEIGHT 512
|
#define HEIGHT 512
|
||||||
|
|
||||||
|
unsigned char coordanate[4] = {0,1,2,3};
|
||||||
|
|
||||||
unsigned char palette[] =
|
unsigned char palette[] =
|
||||||
{
|
{
|
||||||
16,
|
16,
|
||||||
@@ -20,8 +22,9 @@ unsigned char palette[] =
|
|||||||
|
|
||||||
void calc_normal(float* v1, float* v2, float* v3,float* normal, unsigned char n)
|
void calc_normal(float* v1, float* v2, float* v3,float* normal, unsigned char n)
|
||||||
{
|
{
|
||||||
|
float alpha;
|
||||||
vec4 lado1, lado2;
|
vec4 lado1, lado2;
|
||||||
vec4 u1, u2;
|
vec4 u1, u2, proy;
|
||||||
|
|
||||||
switch (n)
|
switch (n)
|
||||||
{
|
{
|
||||||
@@ -35,21 +38,20 @@ void calc_normal(float* v1, float* v2, float* v3,float* normal, unsigned char n)
|
|||||||
return;
|
return;
|
||||||
|
|
||||||
case 4:
|
case 4:
|
||||||
|
#if 0
|
||||||
glm_vec4_sub(v2, v1, lado1);
|
glm_vec4_sub(v2, v1, lado1);
|
||||||
glm_vec4_sub(v3, v1, lado2);
|
glm_vec4_sub(v3, v1, lado2);
|
||||||
|
|
||||||
glm_vec4_copy(lado1, u1);
|
glm_vec4_copy(lado1, u1);
|
||||||
|
|
||||||
float alfa = glm_vec4_dot(lado2, u1) / glm_vec4_dot(u1, u1);
|
alpha = glm_vec4_dot(lado2, u1) / glm_vec4_dot(u1, u1);
|
||||||
vec4 proy;
|
glm_vec4_scale(u1, alpha, proy);
|
||||||
glm_vec4_scale(u1, alfa, proy);
|
|
||||||
glm_vec4_sub(lado2, proy, u2);
|
glm_vec4_sub(lado2, proy, u2);
|
||||||
|
|
||||||
glm_vec4_normalize(u2);
|
glm_vec4_normalize(u2);
|
||||||
glm_vec4_copy(u2, normal);
|
glm_vec4_copy(u2, normal);
|
||||||
|
#endif
|
||||||
default:
|
return;
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -58,24 +60,25 @@ float * fill_normal( float * d, unsigned char m )
|
|||||||
float * n;
|
float * n;
|
||||||
n = malloc( (*d+1)*sizeof(float));
|
n = malloc( (*d+1)*sizeof(float));
|
||||||
*n = *d;
|
*n = *d;
|
||||||
for (int i = 0; i < *d; i += 9)
|
for (int i = 0; i < *d; i += 3*m)
|
||||||
{
|
{
|
||||||
|
|
||||||
vec3 norm_vec;
|
vec4 norm_vec;
|
||||||
calc_normal((d+1)+i, (d+1)+i+3, (d+1)+i+6, norm_vec, m);
|
|
||||||
|
calc_normal((d+1)+i, (d+1)+i+m, (d+1)+i+2*m, norm_vec, m);
|
||||||
glm_vec3_copy( norm_vec, (n+1)+i );
|
glm_vec3_copy( norm_vec, (n+1)+i );
|
||||||
glm_vec3_copy( norm_vec, (n+1)+i+3 );
|
glm_vec3_copy( norm_vec, (n+1)+i+m );
|
||||||
glm_vec3_copy( norm_vec, (n+1)+i+6 );
|
glm_vec3_copy( norm_vec, (n+1)+i+2*m );
|
||||||
}
|
}
|
||||||
return n;
|
return n;
|
||||||
}
|
}
|
||||||
|
|
||||||
const char * wname = "manigraph: manifold grapher";
|
const char * wname = "manigraph: manifold grapher";
|
||||||
|
float * generate_surface();
|
||||||
|
|
||||||
int main( void )
|
int main( void )
|
||||||
{
|
{
|
||||||
float * n_cube, *n_axis;
|
id_t shader, texture, shader_plain;
|
||||||
id_t shader, texture;
|
|
||||||
mesh_t m_cube, m_axis;
|
mesh_t m_cube, m_axis;
|
||||||
window_t window;
|
window_t window;
|
||||||
|
|
||||||
@@ -87,63 +90,83 @@ int main( void )
|
|||||||
|
|
||||||
glewInit();
|
glewInit();
|
||||||
|
|
||||||
if( !( shader = create_shader() ) )
|
|
||||||
goto error_shader;
|
|
||||||
|
|
||||||
gload_program( shader, vs, VERTEX );
|
|
||||||
gload_program( shader, fs, FRAGMENT );
|
|
||||||
use_shader( shader );
|
|
||||||
|
|
||||||
load_fix_matrix( shader, (float)WIDTH/HEIGHT );
|
|
||||||
|
|
||||||
n_cube = fill_normal( d_cube, 3 );
|
|
||||||
n_axis = fill_normal( d_axis, 3 );
|
|
||||||
|
|
||||||
if( !( m_cube = create_mesh( d_cube, n_cube ) ) )
|
|
||||||
goto error_mesh_cube;
|
|
||||||
|
|
||||||
if( !( m_axis = create_mesh( d_axis, n_axis ) ) )
|
|
||||||
goto error_mesh_axis;
|
|
||||||
|
|
||||||
free( n_cube );
|
|
||||||
free( n_axis );
|
|
||||||
|
|
||||||
texture=create_palette_texture( palette );
|
texture=create_palette_texture( palette );
|
||||||
use_texture( texture );
|
use_texture( texture );
|
||||||
|
|
||||||
|
if( !( shader = create_shader() ) )
|
||||||
|
goto error_shader;
|
||||||
|
gload_program( shader, vs, VERTEX );
|
||||||
|
gload_program( shader, fs, FRAGMENT );
|
||||||
|
|
||||||
|
if( !( shader_plain = create_shader() ) )
|
||||||
|
goto error_shader_plain;
|
||||||
|
gload_program( shader_plain, vs, VERTEX );
|
||||||
|
gload_program( shader_plain, fs_plain, FRAGMENT );
|
||||||
|
|
||||||
|
load_fix_matrix( shader, (float)WIDTH/HEIGHT );
|
||||||
|
load_fix_matrix( shader_plain, (float)WIDTH/HEIGHT );
|
||||||
|
|
||||||
|
/* Fill m_cube */
|
||||||
|
{
|
||||||
|
float * n_cube, *d_cube;
|
||||||
|
d_cube = generate_surface(16);
|
||||||
|
n_cube = fill_normal( d_cube, 4 );
|
||||||
|
if( !( m_cube = create_mesh( d_cube, n_cube, coordanate, 4 ) ) )
|
||||||
|
goto error_mesh_cube;
|
||||||
|
free( n_cube );
|
||||||
|
free( d_cube);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Fill m_axis */
|
||||||
|
{
|
||||||
|
float * n_axis;
|
||||||
|
n_axis = fill_normal( d_axis, 3 );
|
||||||
|
if( !( m_axis = create_mesh( d_axis, n_axis, coordanate, 3 ) ) )
|
||||||
|
goto error_mesh_axis;
|
||||||
|
free( n_axis );
|
||||||
|
}
|
||||||
|
|
||||||
while( is_open_window( window ) )
|
while( is_open_window( window ) )
|
||||||
{
|
{
|
||||||
quat_t q;
|
quat_t q;
|
||||||
|
|
||||||
q=poll_input( window );
|
q=poll_input( window );
|
||||||
load_rot_matrix( shader, q );
|
load_rot_matrix( shader, q );
|
||||||
|
load_rot_matrix( shader_plain, q );
|
||||||
|
|
||||||
clean_context();
|
clean_context();
|
||||||
|
|
||||||
#ifndef DEBUG
|
#ifndef DEBUG
|
||||||
load_mdl_matrix( shader, 0, 0 );
|
load_mdl_matrix( shader_plain, 0, 0 );
|
||||||
draw_mesh( m_axis );
|
draw_mesh( m_axis );
|
||||||
load_mdl_matrix( shader, 1, 1 );
|
load_mdl_matrix( shader_plain, 1, 1 );
|
||||||
draw_mesh( m_axis );
|
draw_mesh( m_axis );
|
||||||
load_mdl_matrix( shader, 2, 2 );
|
load_mdl_matrix( shader_plain, 2, 2 );
|
||||||
draw_mesh( m_axis );
|
draw_mesh( m_axis );
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
load_mdl_matrix( shader, 0, 3 );
|
load_mdl_matrix( shader, 0, 3 );
|
||||||
draw_mesh( m_cube );
|
draw_mesh( m_cube );
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
destroy_texture( texture );
|
|
||||||
destroy_mesh( m_axis );
|
destroy_mesh( m_axis );
|
||||||
destroy_mesh( m_cube );
|
destroy_mesh( m_cube );
|
||||||
|
destroy_shader( shader_plain );
|
||||||
destroy_shader( shader );
|
destroy_shader( shader );
|
||||||
|
destroy_texture( texture );
|
||||||
close_window( window );
|
close_window( window );
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
error_mesh_axis:
|
error_mesh_axis:
|
||||||
destroy_mesh( m_cube );
|
destroy_mesh( m_cube );
|
||||||
error_mesh_cube:
|
error_mesh_cube:
|
||||||
|
destroy_shader( shader_plain );
|
||||||
|
error_shader_plain:
|
||||||
destroy_shader( shader );
|
destroy_shader( shader );
|
||||||
error_shader:
|
error_shader:
|
||||||
close_window( window );
|
close_window( window );
|
||||||
|
destroy_texture( texture );
|
||||||
error_window:
|
error_window:
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -22,7 +22,7 @@ int is_open_window(window_t window);
|
|||||||
|
|
||||||
void close_window(window_t window);
|
void close_window(window_t window);
|
||||||
|
|
||||||
mesh_t create_mesh( float * d, float * n);
|
mesh_t create_mesh( float * d, float * n, unsigned char * coordanate, unsigned char m );
|
||||||
|
|
||||||
void destroy_mesh(mesh_t p);
|
void destroy_mesh(mesh_t p);
|
||||||
|
|
||||||
|
|||||||
27
src/mesh.c
27
src/mesh.c
@@ -7,30 +7,43 @@ struct obj
|
|||||||
unsigned int vertex, vao, n_vbo, d_vbo;
|
unsigned int vertex, vao, n_vbo, d_vbo;
|
||||||
};
|
};
|
||||||
|
|
||||||
mesh_t create_mesh( float * d, float * n )
|
mesh_t create_mesh( float * d, float * n, unsigned char * coordanate, unsigned char m )
|
||||||
{
|
{
|
||||||
struct obj * p;
|
struct obj * p;
|
||||||
|
|
||||||
p=malloc(sizeof(struct obj));
|
p=malloc(sizeof(struct obj));
|
||||||
|
|
||||||
p->vertex=(*d)/3;
|
p->vertex=(*d)/m;
|
||||||
|
|
||||||
glGenVertexArrays( 1, &p->vao );
|
glGenVertexArrays( 1, &p->vao );
|
||||||
|
|
||||||
glGenBuffers( 1, &p->d_vbo );
|
glGenBuffers( 1, &p->d_vbo );
|
||||||
glBindVertexArray( p->vao );
|
glBindVertexArray( p->vao );
|
||||||
glBindBuffer( GL_ARRAY_BUFFER, p->d_vbo );
|
glBindBuffer( GL_ARRAY_BUFFER, p->d_vbo );
|
||||||
glBufferData( GL_ARRAY_BUFFER, p->vertex*3*sizeof(float), d+1,
|
glBufferData( GL_ARRAY_BUFFER, p->vertex*m*sizeof(float), d+1,
|
||||||
GL_STATIC_DRAW );
|
GL_STATIC_DRAW );
|
||||||
glVertexAttribPointer( 0,3,GL_FLOAT, 0, 3*sizeof(float), NULL );
|
glVertexAttribPointer( 0,1,GL_FLOAT, 0, m*sizeof(float), (float*)(coordanate[0]*sizeof(float)) );
|
||||||
glEnableVertexAttribArray(0);
|
glEnableVertexAttribArray(0);
|
||||||
|
glVertexAttribPointer( 1,1,GL_FLOAT, 0, m*sizeof(float), (float*)(coordanate[1]*sizeof(float)) );
|
||||||
|
glEnableVertexAttribArray(1);
|
||||||
|
glVertexAttribPointer( 2,1,GL_FLOAT, 0, m*sizeof(float), (float*)(coordanate[2]*sizeof(float)) );
|
||||||
|
glEnableVertexAttribArray(2);
|
||||||
|
glVertexAttribPointer( 3,1,GL_FLOAT, 0, m*sizeof(float), (float*)(coordanate[3]*sizeof(float)) );
|
||||||
|
glEnableVertexAttribArray(3);
|
||||||
|
|
||||||
glGenBuffers( 1, &p->n_vbo );
|
glGenBuffers( 1, &p->n_vbo );
|
||||||
glBindBuffer( GL_ARRAY_BUFFER, p->n_vbo );
|
glBindBuffer( GL_ARRAY_BUFFER, p->n_vbo );
|
||||||
glBufferData( GL_ARRAY_BUFFER, p->vertex*3*sizeof(float), n+1,
|
glBufferData( GL_ARRAY_BUFFER, p->vertex*m*sizeof(float), n+1,
|
||||||
GL_STATIC_DRAW );
|
GL_STATIC_DRAW );
|
||||||
glVertexAttribPointer( 1,3,GL_FLOAT, 0, 3*sizeof(float), NULL );
|
glVertexAttribPointer( 4,1,GL_FLOAT, 0, m*sizeof(float), (float*)(coordanate[0]*sizeof(float)) );
|
||||||
glEnableVertexAttribArray(1);
|
glEnableVertexAttribArray(4);
|
||||||
|
glVertexAttribPointer( 5,1,GL_FLOAT, 0, m*sizeof(float), (float*)(coordanate[1]*sizeof(float)) );
|
||||||
|
glEnableVertexAttribArray(5);
|
||||||
|
glVertexAttribPointer( 6,1,GL_FLOAT, 0, m*sizeof(float), (float*)(coordanate[2]*sizeof(float)) );
|
||||||
|
glEnableVertexAttribArray(6);
|
||||||
|
glVertexAttribPointer( 7,1,GL_FLOAT, 0, m*sizeof(float), (float*)(coordanate[3]*sizeof(float)) );
|
||||||
|
glEnableVertexAttribArray(7);
|
||||||
|
|
||||||
|
|
||||||
return p;
|
return p;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -37,8 +37,8 @@ unsigned char gload_program( unsigned int program, const char * src,
|
|||||||
shader = glCreateShader(type[i]);
|
shader = glCreateShader(type[i]);
|
||||||
glShaderSource( shader, 1, (const GLchar **)&src, ((void*)0 ));
|
glShaderSource( shader, 1, (const GLchar **)&src, ((void*)0 ));
|
||||||
glCompileShader(shader);
|
glCompileShader(shader);
|
||||||
glGetShaderiv( shader, GL_COMPILE_STATUS, &status );
|
|
||||||
|
|
||||||
|
glGetShaderiv( shader, GL_COMPILE_STATUS, &status );
|
||||||
|
|
||||||
if( !status )
|
if( !status )
|
||||||
{
|
{
|
||||||
|
|||||||
93
src/surface.c
Normal file
93
src/surface.c
Normal file
@@ -0,0 +1,93 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <math.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
#ifndef M_PI
|
||||||
|
#define M_PI 3.14159265358979323846
|
||||||
|
#endif
|
||||||
|
|
||||||
|
typedef void(*function_t)(float*,int, int, int);
|
||||||
|
|
||||||
|
void mobius(float *d_surface, int i, int j, int grid_size)
|
||||||
|
{
|
||||||
|
const float width = 0.5;
|
||||||
|
float u = (2*M_PI) * ((float)i/grid_size );
|
||||||
|
float v = (2*width) * ((float)j/grid_size ) - width;
|
||||||
|
|
||||||
|
d_surface[0] = cos(u) + v * cos(u / 2) * cos(u);
|
||||||
|
d_surface[1] = sin(u) + v * cos(u / 2) * sin(u);
|
||||||
|
d_surface[2] = v * sin(u / 2);
|
||||||
|
}
|
||||||
|
|
||||||
|
void toro(float *d_surface, int i, int j, int grid_size)
|
||||||
|
{
|
||||||
|
float u = (2*M_PI) * ((float)i/grid_size );
|
||||||
|
float v = (2*M_PI) * ((float)j/grid_size );
|
||||||
|
|
||||||
|
d_surface[0] = (1 + 0.5*cos(v))*cos(u);
|
||||||
|
d_surface[1] = (1 + 0.5*cos(v))*sin(u);
|
||||||
|
d_surface[2] = 0.5*sin(v);
|
||||||
|
}
|
||||||
|
|
||||||
|
void klein(float *d_surface, int i, int j, int grid_size)
|
||||||
|
{
|
||||||
|
float u = (2*M_PI) * ((float)i/grid_size );
|
||||||
|
float v = (2*M_PI) * ((float)j/grid_size );
|
||||||
|
|
||||||
|
d_surface[0] = (0.5*cos(v) + 0.5) * cos(u);
|
||||||
|
d_surface[1] = (0.5*cos(v) + 0.5) * sin(u);
|
||||||
|
d_surface[2] = sin(v) * cos(u/2);
|
||||||
|
d_surface[3] = sin(v)*sin(u/2);
|
||||||
|
}
|
||||||
|
|
||||||
|
float * generate_surface(int grid_size)
|
||||||
|
{
|
||||||
|
unsigned char m = 4;
|
||||||
|
long size = grid_size*grid_size*2*9*m;
|
||||||
|
function_t f = klein;
|
||||||
|
float * d_surface;
|
||||||
|
int k=0;
|
||||||
|
|
||||||
|
d_surface = malloc((size+1)*sizeof(float));
|
||||||
|
d_surface[0] = size;
|
||||||
|
|
||||||
|
for (int i = 0; i < grid_size; i++)
|
||||||
|
{
|
||||||
|
for (int j = 0; j < grid_size; j++)
|
||||||
|
{
|
||||||
|
// triangle 1, front
|
||||||
|
f(&d_surface[k + 1], i, j, grid_size);
|
||||||
|
k+=m;
|
||||||
|
f(&d_surface[k + 1], i + 1, j, grid_size);
|
||||||
|
k+=m;
|
||||||
|
f(&d_surface[k + 1], i + 1, j + 1, grid_size);
|
||||||
|
k+=m;
|
||||||
|
|
||||||
|
// triangle 1, back
|
||||||
|
f(&d_surface[k + 1], i + 1, j + 1, grid_size);
|
||||||
|
k+=m;
|
||||||
|
f(&d_surface[k + 1], i + 1, j, grid_size);
|
||||||
|
k+=m;
|
||||||
|
f(&d_surface[k + 1], i, j, grid_size);
|
||||||
|
k+=m;
|
||||||
|
|
||||||
|
// triangle 2, font
|
||||||
|
f(&d_surface[k + 1], i, j, grid_size);
|
||||||
|
k+=m;
|
||||||
|
f(&d_surface[k + 1], i, j + 1, grid_size);
|
||||||
|
k+=m;
|
||||||
|
f(&d_surface[k + 1], i + 1, j + 1, grid_size);
|
||||||
|
k+=m;
|
||||||
|
|
||||||
|
// triangle 2, back
|
||||||
|
f(&d_surface[k + 1], i + 1, j + 1, grid_size);
|
||||||
|
k+=m;
|
||||||
|
f(&d_surface[k + 1], i, j + 1, grid_size);
|
||||||
|
k+=m;
|
||||||
|
f(&d_surface[k + 1], i, j, grid_size);
|
||||||
|
k+=m;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return d_surface;
|
||||||
|
}
|
||||||
@@ -4,12 +4,11 @@
|
|||||||
void __window_callback(GLFWwindow *, int, int);
|
void __window_callback(GLFWwindow *, int, int);
|
||||||
void __mouse_callback(GLFWwindow *, int, int, int);
|
void __mouse_callback(GLFWwindow *, int, int, int);
|
||||||
void __scroll_callback(GLFWwindow *, double, double);
|
void __scroll_callback(GLFWwindow *, double, double);
|
||||||
|
void __key_callback(GLFWwindow *, int, int, int, int);
|
||||||
|
|
||||||
window_t init_window(unsigned int w, unsigned int h, const char * name)
|
window_t init_window(unsigned int w, unsigned int h, const char * name)
|
||||||
{
|
{
|
||||||
void * window;
|
void * window;
|
||||||
void __mouse_callback(GLFWwindow *window, int button, int action, int mods);
|
|
||||||
void __scroll_callback(GLFWwindow *window, double xoffset, double yoffset);
|
|
||||||
|
|
||||||
if( !glfwInit() )
|
if( !glfwInit() )
|
||||||
return NULL;
|
return NULL;
|
||||||
@@ -29,6 +28,7 @@ window_t init_window(unsigned int w, unsigned int h, const char * name)
|
|||||||
glfwSetWindowSizeCallback(window, __window_callback);
|
glfwSetWindowSizeCallback(window, __window_callback);
|
||||||
glfwSetMouseButtonCallback((GLFWwindow*)window, __mouse_callback);
|
glfwSetMouseButtonCallback((GLFWwindow*)window, __mouse_callback);
|
||||||
glfwSetScrollCallback((GLFWwindow*)window, __scroll_callback);
|
glfwSetScrollCallback((GLFWwindow*)window, __scroll_callback);
|
||||||
|
glfwSetKeyCallback((GLFWwindow*)window, __key_callback);
|
||||||
|
|
||||||
__window_callback( window, w, h );
|
__window_callback( window, w, h );
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user