I want to change tetrahedron to a triangle mesh to fill up the empty space and there is a origin code I’m down so far as below:
How can I make it works?
#include <stdio.h>
#include <GL/glew.h>
#include <GL/glut.h>
#include “InitShader.h”
#include <math.h>
#define BUFFER_OFFSET( offset ) ((GLvoid*) (offset))
GLuint abuffer[2];
GLuint vertexArrayObject[2];
const int NumTimesToSubdivide = 4;
const int NumTetrahedra = 1024; // 4^5 tetrahedra
const int NumTriangles = 4*NumTetrahedra;
const int NumVertices = 3 * NumTriangles;
float theta;
float angle;
float points[NumVertices*3];
int Index = 0;
GLuint program;
void triangle( const float *a, const float *b, const float *c)
{
points[Index++] = a[0];
points[Index++] = a[1];
points[Index++] = a[2];
points[Index++] = b[0];
points[Index++] = b[1];
points[Index++] = b[2];
points[Index++] = c[0];
points[Index++] = c[1];
points[Index++] = c[2];
}
void tetra( const float *a, const float *b, const float *c, const float *d)
{
triangle( a, b, c );
triangle( a, c, d );
triangle( a, d, b );
triangle( b, d, c );
}
void divide_tetra( const float *a, const float *b, const float *c, const float *d, int count )
{
float v0[3],v1[3],v2[3],v3[3],v4[3],v5[3];
float v01[3],v11[3],v21[3],v31[3],v41[3],v51[3];
v01[0] = ( a[0] + b[0] ) / 2.0;
v01[1] = ( a[1] + b[1] ) / 2.0;
v01[2] = ( a[2] + b[2] ) / 2.0;
v11[0] = ( a[0] + c[0] ) / 2.0;
v11[1] = ( a[1] + c[1] ) / 2.0;
v11[2] = ( a[2] + c[2] ) / 2.0;
v21[0] = ( a[0] + d[0] ) / 2.0;
v21[1] = ( a[1] + d[1] ) / 2.0;
v21[2] = ( a[2] + d[2] ) / 2.0;
v31[0] = ( b[0] + c[0] ) / 2.0;
v31[1] = ( b[1] + c[1] ) / 2.0;
v31[2] = ( b[2] + c[2] ) / 2.0;
v41[0] = ( c[0] + d[0] ) / 2.0;
v41[1] = ( c[1] + d[1] ) / 2.0;
v41[2] = ( c[2] + d[2] ) / 2.0;
v51[0] = ( b[0] + d[0] ) / 2.0;
v51[1] = ( b[1] + d[1] ) / 2.0;
v51[2] = ( b[2] + d[2] ) / 2.0;
if ( count > 0 ) {
v0[0] = ( a[0] + b[0] ) / 2.0;
v0[1] = ( a[1] + b[1] ) / 2.0;
v0[2] = ( a[2] + b[2] ) / 2.0;
v1[0] = ( a[0] + c[0] ) / 2.0;
v1[1] = ( a[1] + c[1] ) / 2.0;
v1[2] = ( a[2] + c[2] ) / 2.0;
v2[0] = ( a[0] + d[0] ) / 2.0;
v2[1] = ( a[1] + d[1] ) / 2.0;
v2[2] = ( a[2] + d[2] ) / 2.0;
v3[0] = ( b[0] + c[0] ) / 2.0;
v3[1] = ( b[1] + c[1] ) / 2.0;
v3[2] = ( b[2] + c[2] ) / 2.0;
v4[0] = ( c[0] + d[0] ) / 2.0;
v4[1] = ( c[1] + d[1] ) / 2.0;
v4[2] = ( c[2] + d[2] ) / 2.0;
v5[0] = ( b[0] + d[0] ) / 2.0;
v5[1] = ( b[1] + d[1] ) / 2.0;
v5[2] = ( b[2] + d[2] ) / 2.0;
divide_tetra( a, v0, v1, v2, count - 1 );
divide_tetra( v0, b, v3, v5, count - 1 );
divide_tetra( v1, v3, c, v4, count - 1 );
divide_tetra( v2, v4, v5, d, count - 1 );
}
else {
//tetra( a, b, c, d ); // draw triangle at end of recursion
tetra( v01, v11, v31, v51 );
}
}
void init(){
float initTetra[] = {0.0, 0.0, -1.0,
0.0, 0.942809, 0.333333,
-0.816497, -0.471405, 0.333333,
0.816497, -0.471405, 0.333333};
divide_tetra(initTetra,
&(initTetra[3]),
&(initTetra[6]),
&(initTetra[9]),
NumTimesToSubdivide);
glClearColor(0.4,0.4,0.4,1.0);
// Load shaders and use the resulting shader program
program = InitShader( “vshader1.glsl”, “fshader.glsl” );
glUseProgram( program );
glEnable(GL_DEPTH_TEST);
glGenVertexArrays( 2, vertexArrayObject );
glBindVertexArray( vertexArrayObject[0] );
glGenBuffers(2, abuffer);
glBindBuffer(GL_ARRAY_BUFFER, abuffer[0]);
glBufferData(GL_ARRAY_BUFFER, sizeof(points),NULL,GL_STATIC_DRAW);
glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(points), points);
// Initialize the vertex position attribute from the vertex shader
GLuint loc = glGetAttribLocation( program, “vPos” );
glEnableVertexAttribArray( loc );
glVertexAttribPointer( loc, 3, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(0) );
glBindVertexArray(0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
}
void idle(){
theta = 0.5 * 3.14/180.0;
for (int xi=0; xi<NumVertices*3; xi+=3){
float x = cos(theta)*points[xi] - sin(theta)*points[xi+2];
float z = sin(theta)*points[xi] + cos(theta)*points[xi+2];
points[xi]=x;
points[xi+2]=z;
}
glBindBuffer(GL_ARRAY_BUFFER, abuffer[0]);
glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(points), points);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glutPostRedisplay();
}
void mydisplay()
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glBindVertexArray( vertexArrayObject[0] );
glDrawArrays(GL_TRIANGLES, 0, NumVertices);
glutSwapBuffers();
}
int main(int argc, char** argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH|GLUT_RGB);
glutCreateWindow(“Shader Simple with depth test”);
glutDisplayFunc(mydisplay);
glutIdleFunc(idle);
glewInit();
init();
glutMainLoop();
}