Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 3 of 3

Thread: not drawing the triangle

  1. #1
    Junior Member Newbie
    Join Date
    Jun 2017
    Posts
    5

    Unhappy not drawing the triangle

    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 :

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

  2. #2
    Senior Member OpenGL Guru Dark Photon's Avatar
    Join Date
    Oct 2004
    Location
    Druidia
    Posts
    4,123
    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:


    1. 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.
    2. 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.
    Last edited by Dark Photon; 06-16-2017 at 04:28 PM.

  3. #3
    Junior Member Newbie
    Join Date
    Jun 2017
    Posts
    5
    Quote Originally Posted by Dark Photon View Post
    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. :/

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •