PDA

View Full Version : Problem with Icosahedron Example



Adam_MTech
03-10-2014, 12:59 AM
Hello,

I followed an example from the OpenGL programming book(red book) on drawing an icosahedron, but I am only getting a black screen when I run my code.
I am using GLFW for window handling. I tried to follow the example as closely as possible.

Any suggestions as to how this might be fixed and/or a diagnosis of the problem would be much appreciated
I'm not using any real lighting or shading yet, so pay no attention to the calls to shading functions.


static void error_callback(int error, const char* description)
{
fputs(description, stderr);
}

static void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods)
{
if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
glfwSetWindowShouldClose(window, GL_TRUE);
}


int main(int argc, char* argv[])
{
GLFWwindow* window;
glfwSetErrorCallback(error_callback);

if (!glfwInit())
exit(EXIT_FAILURE);

window = glfwCreateWindow(800, 600, WINDOW_NAME, NULL, NULL);
if (!window)
{
glfwTerminate();
exit(EXIT_FAILURE);
}

glfwMakeContextCurrent(window);
glfwSetKeyCallback(window, key_callback);
glfwSwapInterval(1);
glShadeModel(GL_SMOOTH); //Enable smooth shading
glClearDepth(1.0f);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL); //Depth test function to use
//glEnable(GL_COLOR_MATERIAL);
//glEnableClientState(GL_VERTEX_ARRAY);
//glFrontFace(GL_CW);
//glDisable(GL_CULL_FACE);
//glPolygonMode( GL_FRONT_AND_BACK, GL_LINE); //set to draw wirefram
#define X 0.525731112119133606
#define Z 0.850650808352039932

static GLfloat vdata[12][3] = { (-X, 0.0, Z), (X, 0.0, Z), (-X, 0.0, -Z), (X, 0.0, -Z),
(0.0, Z, X), (0.0, Z, -X), (0.0, -Z, X), (0.0, -Z, -X),
(Z, X, 0.0), (-Z, X, 0.0), (Z, -X, 0.0), (-Z, -X, 0.0)
};

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)
};



while (!glfwWindowShouldClose(window)) //NEW window loop goes here. I am in control of the loop now
{
float ratio;
int width, height;
glfwGetFramebufferSize(window, &width, &height);
if ( height == 0)
height = 1;

ratio = width / (float) height;
glViewport(0, 0, width, height);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0, ratio, 1.0, 100.0);
gluLookAt(0.0,0.0,1.5,0.0,0.0,0.0,0.0,1.0,0.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
//glTranslatef(0.0,0.0,-10.0);
//gluLookAt(0.0,0.0,1.0,0.0,0.0,0.25,0.0,0.0,1.0);
//glRotatef((float) glfwGetTime() * 50.f, 0.f, 0.f, 1.f);
//glRotatef((float) glfwGetTime() * 50.f, 0.f, 1.f, 0.f);

//glColor3f(1.0,0.0,0.0);

glBegin(GL_TRIANGLES);
for (int i = 0; i < 20; i++)
{
glColor3f((i+0.0)/19.0,(i+0.0)/19.0,(i+0.0)/19.0);
glVertex3fv(&vdata[ tindices[i][0] ] [0]);
glVertex3fv(&vdata[ tindices[i][1] ] [0]);
glVertex3fv(&vdata[ tindices[i][2] ] [0]);

/*glVertex3f(i/200.0,0.0,i/200.0); Test Draw - This works, unlike the icosahedron
glVertex3f(i/200.0 + 0.1, 0.0, i/200.0);
glVertex3f(i/200.0 + 0.05, 0.1, i/200.0);*/
}
glEnd();

glfwSwapBuffers(window);
glfwPollEvents();
}

glfwDestroyWindow(window);


glfwTerminate(); //Done using glfw
exit(EXIT_SUCCESS);
}