Files
manigraph/src/mesh.c
2024-10-22 19:26:10 -06:00

80 lines
1.7 KiB
C
Executable File

#include "main.h"
#include <GL/glew.h>
#include <stdlib.h>
struct obj
{
unsigned int vertex, vao, n_vbo, d_vbo;
};
/*
In this function we load all the vertex and normal datas on two
diferents buffers, so we can access the coordanates that we want
to display using the layout location in GLSL.
This trick can be done with glVertexAttribPointer.
*/
mesh_t create_mesh( float * d, float * n, unsigned char * coordanate, unsigned char m )
{
unsigned char i;
struct obj * p;
p=malloc(sizeof(struct obj));
p->vertex=(*d)/m;
glGenVertexArrays( 1, &p->vao );
glGenBuffers( 1, &p->d_vbo );
glBindVertexArray( p->vao );
glBindBuffer( GL_ARRAY_BUFFER, p->d_vbo );
glBufferData( GL_ARRAY_BUFFER, p->vertex*m*sizeof(float), d+1,
GL_STATIC_DRAW );
for( i=0; i<4; ++i )
{
glVertexAttribPointer( i,1,GL_FLOAT, 0, m*sizeof(float),
(float*)(coordanate[i]*sizeof(float)) );
glEnableVertexAttribArray(i);
}
glGenBuffers( 1, &p->n_vbo );
glBindBuffer( GL_ARRAY_BUFFER, p->n_vbo );
glBufferData( GL_ARRAY_BUFFER, p->vertex*m*sizeof(float), n+1,
GL_STATIC_DRAW );
for( i=0; i<4; ++i )
{
glVertexAttribPointer( i+4,1,GL_FLOAT, 0, m*sizeof(float),
(float*)(coordanate[i]*sizeof(float)) );
glEnableVertexAttribArray(i+4);
}
return p;
}
void destroy_mesh( mesh_t p )
{
struct obj * obj ;
obj = p;
glDeleteVertexArrays( 1, &obj->vao );
glDeleteBuffers( 1, &obj->d_vbo );
glDeleteBuffers( 1, &obj->n_vbo );
free( p );
}
void draw_mesh( mesh_t p )
{
struct obj * obj=p;
glBindVertexArray( obj->vao );
#ifdef DEBUG
{
int i;
for( i=0; i<obj->vertex;i+=3 )
glDrawArrays(GL_LINE_LOOP, i, 3 );
}
#else
glDrawArrays(GL_TRIANGLES, 0, obj->vertex );
#endif
}