PDA

View Full Version : not drawing the triangle



WhiteSword
06-16-2017, 10:13 AM
Could anybody help me with my code, please? It's not displaying the triangle. I can't figure out what have I left or have written wrong in it.

Here's the code :



#include <GL/freeglut.h>

void reclear()
{
glClearColor(DEF_WHITE, 0.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
}

/* Called every single frame */
void display()
{
reclear();

glBegin(GL_TRIANGLES);
glColor3f(1.0, 0.0, 0.0);
glVertex3f(-1.0,-1.0, 0.0);
glVertex3f(0.0,1.0,0.0);
glVertex3f(1.0, -1.0, 0.0);
glEnd();

glutSwapBuffers();
}


/* Called whenever the window's dimensions change */
void reshape(int w, int h)
{
float w2h = (h>0)? (double)w/h: 1;
glViewport(0, 0, w, h);
}


/* Called when there's no interaction with the window and the event queue is empty */
void idle()
{
display();
}



void initDisplay(int *argc, char **argv, int width, int height)
{
glutInit(argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA);
glutInitWindowSize(width, height);
glutCreateWindow("Project L3");
glViewport(0, 0, width, height);
}



int main(int argc, char** argv)
{
initDisplay(&argc, argv, WIDTH, HEIGHT);
glutDisplayFunc(display);
glutIdleFunc(idle);
glutReshapeFunc(reshape);
glutMainLoop();
}

Dark Photon
06-16-2017, 04:15 PM
What have you done to diagnose the problem? Have you checked to see if your 1) reshape(), 2) display(), and/or 3) idle() function are being called?

Your code works fine here. But a few suggestions:



You don't need your idle() function, and you shouldn't call the display() function from elsewhere. GLUT calls your display() function when a redisplay is needed.
If you want to prompt your display() function to be called the next time GLUT returns to its mainloop, call glutPostRedisplay(). It queues a "redisplay" event. For instance, you could call glutPostRedisplay() from the end of your reshape() function, anytime you know the state of the screen needs to be updated, and (if you don't really care about burning CPU and GPU cycles and just want to have display() called as fast as possible rather than just when needed) at the end of your display() function.

WhiteSword
06-17-2017, 01:42 AM
What have you done to diagnose the problem? Have you checked to see if your 1) reshape(), 2) display(), and/or 3) idle() function are being called?


Thanks for the reply.
It's working perfectly now. Only reshape was being called. I don't know why was it so. :/