Reputation: 21
For reference I'm using this guide.
I've gone over several guides including one that almost had the answer I was looking for.
I'm trying to draw an Icosahedron (an approximation on a sphere), as soon as I figure out why I'm not drawing I'll subdivide it so that I can progress from drawing with 20 triangles to 80 and then to 320 but currently I'm just trying to draw one with 20 sides.
I've gone through debugging and really can't see what's wrong with the program, all variables have the right values when they encounter a function.
Here's my program:
#define _USE_MATH_DEFINES
#define X .525731112119133606f
#define Z .850650808352039932f
#define N 0.0f
#include <glut.h>
#include <math.h>
#include <iostream>
using namespace std;
GLsizei winWidth = 1000, winHeight = 1000;
static GLfloat vdata[12][3] = {
{ -X,N,Z },{ X,N,Z },{ -X,N,-Z },{ X,N,-Z },
{ N,Z,X },{ N,Z,-X },{ N,-Z,X },{ N,-Z,-X },
{ Z,X,N },{ -Z,X, N },{ Z,-X,N },{ -Z,-X, N }
};
static GLuint tindices[20][3] = {
{ 0,4,1 },{ 0,9,4 },{ 9,5,4 },{ 4,5,8 },{ 4,8,1 },
{ 8,10,1 },{ 8,3,10 },{ 5,3,8 },{ 5,2,3 },{ 2,7,3 },
{ 7,10,3 },{ 7,6,10 },{ 7,11,6 },{ 11,0,6 },{ 0,1,6 },
{ 6,1,10 },{ 9,0,11 },{ 9,11,2 },{ 9,2,5 },{ 7,2,11 }
};
double randFrac() {
return (double)rand() / RAND_MAX;
}
void init(void){
glClearColor(1.0, 1.0, 1.0, 0.0); // White display window.
}
void drawTriangle(float* v1, float* v2, float* v3) {
glBegin(GL_TRIANGLES);
glNormal3fv(v1);
glVertex3fv(v1);
glNormal3fv(v2);
glVertex3fv(v2);
glNormal3fv(v3);
glVertex3fv(v3);
glEnd();
}
void normalize(float v[3]) {
GLfloat d = sqrt(v[0] * v[0] + v[1] * v[1] + v[1] * v[2]);
if (!d) {
cerr << "VECTOR HAS LENGTH OF ZERO";
return;
}
v[0] /= d;
v[1] /= d;
v[2] /= d;
}
void subdivide(float* v1, float* v2, float* v3, long depth) {
GLfloat v12[3],v23[3], v31[3];
if (!depth) {
drawTriangle(v1, v2, v3);
return;
}
for (GLint i = 0; i < 3; i++) {
v12[i] = (v1[i] + v2[i]) / 2.0;
v23[i] = (v2[i] + v3[i]) / 2.0;
v31[i] = (v1[i] + v3[i]) / 2.0;;
}
normalize(v12);
normalize(v23);
normalize(v31);
subdivide(v1, v12, v31, depth - 1);
subdivide(v2, v23, v12, depth - 1);
subdivide(v3, v31, v23, depth - 1);
subdivide(v12, v23, v31, depth - 1);
}
void drawIcosahedrons() {
glClear(GL_COLOR_BUFFER_BIT); // Clear display window.
glColor3f(0.0, 0.0, 1.0); // Set line color to blue.
/* Set viewing transformation. */
gluLookAt(5.0, 5.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
for (int i = 0; i < 20; i++) {
glColor3f(randFrac(), randFrac(), randFrac()); // set sides to random color
drawTriangle(&vdata[tindices[i][0]][0], &vdata[tindices[i][1]][0], &vdata[tindices[i][2]][0]);
}
}
void winReshapeFcn(GLint newWidth, GLint newHeight) {
glViewport(0, 0, newWidth, newHeight);
glMatrixMode(GL_PROJECTION);
glFrustum(-1.0, 1.0, -1.0, 1.0, 2.0, 20.0);
glMatrixMode(GL_MODELVIEW);
glClear(GL_COLOR_BUFFER_BIT);
}
void main (int argc, char** argv){
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
glutInitWindowPosition (100, 100);
glutInitWindowSize (winWidth, winHeight);
glutCreateWindow ("title");
init();
glutDisplayFunc (drawIcosahedrons);
glutReshapeFunc (winReshapeFcn);
glutMainLoop();
}
Upvotes: 0
Views: 201
Reputation: 21
My apologies everyone, I forgot to glFlush(); after I finished drawing.
Upvotes: 1