Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Page 1 of 2 12 LastLast
Results 1 to 10 of 11

Thread: Problem loading my graphic

  1. #1
    Junior Member Newbie
    Join Date
    Nov 2008
    Posts
    8

    Problem loading my graphic

    All I am trying to do is load up a simple polygon but all I get is a blank screen.

    Everything compiles correctly and runs but I just don't get my polygon coming up.


    I have created a window 200 x 300 and created my 2d matrix with the glOrtho2D() function.

    #include <stdlib.h>
    #include <GL/glut.h>



    void myinit(void) // simple initialisation function
    {
    glClearColor(1.0, 1.0, 1.0, 0.0) ; // sets clearing (background) colour

    glMatrixMode(GL_PROJECTION); // Projects the matrix
    gluOrtho2D(-8.8, 8.8, -8.8, 8.8); // sets the 2D projection matrix area

    }





    void polygon(void)
    {

    glColor3f(1.0,0.0,0.0); // polygon set to red

    glBegin(GL_POLYGON);
    glVertex2f(0.0, 0.0);
    glVertex2f(0.0, 3.0);
    glVertex2f(4.0, 3.0);
    glVertex2f(6.0, 1.5);
    glVertex2f(4.0, 0.0);
    glEnd();

    }




    void display(void)
    {

    glClear(GL_COLOR_BUFFER_BIT);
    glDisable(GL_DEPTH_TEST);
    glMatrixMode(GL_MODELVIEW);

    glLoadIdentity();

    glPushMatrix();

    polygon();



    //popmatrix()

    //pushmatrix()

    //translate() //moves the object
    //polygon()
    //glFlush();



    }


    main(int argc, char **argv)
    {
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
    glutInitWindowSize(200, 300);
    glutInitWindowPosition(0,0);
    glutCreateWindow("Assignment 2 polyogon");
    glutDisplayFunc(display);
    myinit();
    glutMainLoop();

    return 0 ;
    }



    Can you please help as I am struggling to understand why?

  2. #2
    Member Regular Contributor trinitrotoluene's Avatar
    Join Date
    Sep 2008
    Location
    Montérégie,Québec
    Posts
    354

    Re: Problem loading my graphic

    You lack a glutSwapBuffers at the end of your display function. You must also match the number of push and pop matrix call. And it should be good to have a reshape function: put a glViewport(...) call there with your gluOrtho2D. Then add glutReshapeFunc(reshape).

  3. #3
    Junior Member Newbie
    Join Date
    Nov 2008
    Posts
    8

    Re: Problem loading my graphic

    #include <stdlib.h>
    #include <GL/glut.h>



    void myinit(void) // simple initialisation function
    {
    glClearColor(1.0, 1.0, 1.0, 0.0) ; // sets clearing (background) colour

    glMatrixMode(GL_PROJECTION); // Projects the matrix


    }


    void polygon(void)
    {

    glColor3f(1.0,0.0,0.0); // polygon set to red

    glBegin(GL_POLYGON);
    glVertex2f(0.0, 0.0);
    glVertex2f(0.0, 3.0);
    glVertex2f(4.0, 3.0);
    glVertex2f(6.0, 1.5);
    glVertex2f(4.0, 0.0);
    glEnd();

    }


    void display(void)
    {

    glClear(GL_COLOR_BUFFER_BIT);
    glDisable(GL_DEPTH_TEST);
    glMatrixMode(GL_MODELVIEW);

    glLoadIdentity();

    glPushMatrix();

    polygon();


    //popmatrix()

    //pushmatrix()

    //translate() //moves the object
    //polygon()
    //glFlush();

    glutSwapBuffers();

    }


    void reshape(void)
    {

    glViewport-1.0, 1.0, -1.0, 1.0 );
    gluOrtho2D(-8.8, 8.8, -8.8, 8.8); // sets the 2D projection matrix area

    }


    main(int argc, char **argv)
    {
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
    glutInitWindowSize(200, 300);
    glutCreateWindow("Assignment 2 polyogon");
    glutInitWindowPosition(0,0);
    myinit();
    glutDisplayFunc(display);
    glutReshapeFunc(reshape);
    glutMainLoop();

    return 0 ;
    }

    I have made the changes you asked but all I get is an error message :

    BOX.c: In function ‘reshape’:
    BOX.c:68: error: invalid operands to binary - (have ‘void (*)(GLint, GLint, GLsizei, GLsizei)’ and ‘double’)
    BOX.c:68: error: expected ‘;’ before ‘)’ token
    BOX.c:68: error: expected statement before ‘)’ token
    BOX.c: In function ‘main’:
    BOX.c:82: warning: passing argument 1 of ‘glutReshapeFunc’ from incompatible pointer type
    make: *** [BOX] Error 1.

    Im so lost. I cant believe how hard it is to just put a shape on the screen.

    PLEASE HELP

  4. #4
    Member Regular Contributor trinitrotoluene's Avatar
    Join Date
    Sep 2008
    Location
    Montérégie,Québec
    Posts
    354

    Re: Problem loading my graphic

    The reshape function must have a prototype like this void reshape(int width,int height). Then the glViewport call should be something like this glViewport(0,0,width,height);

    Edit: In general reshape function look like this:
    Code :
    void reshape(int width,int height)
    {
      glViewport(0,0,width,height);
      glMatrixMode(GL_PROJECTION);
      glLoadIdentity();
      glOrtho2D(...)
      glMatrixMode(GL_MODELVIEW);
      glLoadIdentity();
    }

  5. #5
    Junior Member Newbie
    Join Date
    Nov 2008
    Posts
    8

    Re: Problem loading my graphic

    that worked thank you.

    I am trying to create the exact same shape but obviously move it.
    I am attempting to do this with the Matix push,pop and transform functions.

    to my understanding the glTranslate takes the x and y coordinate that I want to change it by. therefore if i were to put glTranslate(1.0,1.0) in it woul move the new shapes position up 1 on the x and y axes?

    void display(void)
    {

    glClear(GL_COLOR_BUFFER_BIT);
    glDisable(GL_DEPTH_TEST);
    glMatrixMode(GL_MODELVIEW);

    glLoadIdentity();

    glPushMatrix();

    polygon();

    glPopMatrix();

    glPushMatrix();

    glTranslate(1.0,1.0); //moves the object
    polygon();

    //glFlush();

    glutSwapBuffers();

    }

  6. #6
    Member Regular Contributor trinitrotoluene's Avatar
    Join Date
    Sep 2008
    Location
    Montérégie,Québec
    Posts
    354

    Re: Problem loading my graphic

    Yes, but glTranslatef(x,y,z) take 3 parameters, then call glTranslatef(x,y,0.0).

  7. #7
    Junior Member Newbie
    Join Date
    Nov 2008
    Posts
    8

    Re: Problem loading my graphic

    problem solved

  8. #8
    Member Regular Contributor trinitrotoluene's Avatar
    Join Date
    Sep 2008
    Location
    Montérégie,Québec
    Posts
    354

    Re: Problem loading my graphic

    When you draw a shape with GL_POLYGON, it must be convex. For drawing your C letter you must subdivide it so all sub part of the letter are convex.

    Edit:
    Code :
     
       *---------*
       | \    1  |
       |   *-----*
       |   | 
       | 2 |       
       |   *-----*
       | /   3   |
       *---------*
    So the letter C can be drawn with 3 polygons.

  9. #9
    Junior Member Newbie
    Join Date
    Nov 2008
    Posts
    8

    Re: Problem loading my graphic

    My shapes are going off the screen because my shape starts in the center of the screen. How do i change it to start in the top left???

    Is it something to do with the viewpoint?

  10. #10
    Member Regular Contributor trinitrotoluene's Avatar
    Join Date
    Sep 2008
    Location
    Montérégie,Québec
    Posts
    354

    Re: Problem loading my graphic

    First you can modify gluOrtho2D(-20.0,20.0,-20.0,20.0); to view a larger "field". Second in your drawing function, you can use gluLookAt() just after the glLoadIdentity(); at the beginning of the display function.

Posting Permissions

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