0

I am trying to use a function to render a triangle with OpenGL. The shape isn't appearing and I don't know why. I think it might be just because of the locations of the vertices.

Main method:

 int main() {


    glutInitWindowSize(400, 400);
    glutInitWindowPosition(200, 200);
    glutCreateWindow("Test");
    Initialize();
    glutDisplayFunc(Draw);
    glutMainLoop();
    return 0;
}

Initialise method:

 void Initialize() {
    glClearColor(0.1, 0.1, 0.1, 0.0);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);
}

drawTriangle function:

 void drawTriangle(int v1x, int v1y, int v2x, int v2y, int v3x, int v3y, int red, int green, int blue)
{
    //arguments are: "vertex 1 x coordinate", "vertex 1 y coordinate" etc
    glClear(GL_COLOR_BUFFER_BIT);
    glColor3f(1.0, 1.0, 1.0);

    glBegin(GL_TRIANGLES);
        glVertex2f(v1x, v1y); // v1
        glVertex2f(v2x, v2y); // v2
        glVertex2f(v3x, v3y); // v3
    glEnd();

    glFlush();
}

Draw method:

void Draw() {
    drawTriangle(3.0, 2.9, 300, 300, 100, 100, 10, 0, 0);
}
genpfault
  • 51,148
  • 11
  • 85
  • 139
  • Yes, your coordinates are way off the screen. You should make yourself familiar with how GL coordinate transforms work and what `glOrtho()` does. – derhass Jun 29 '14 at 16:38

1 Answers1

0

Your coordinates are not fitting inside the defined ortho block

replace : glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0); 
with this:     glOrtho(0.0, 400.0, 0.0, 400.0, -1.0, 1.0);

The above is a temporary fix, it will help you understand how glOrtho works. Also, you are passing float arguments into the drawTriangle function, so you must replace all the parameters from int to float

OmarAsifShaikh
  • 174
  • 1
  • 12
  • 1
    Thank you so much! I am still learning, so from now on I will know. –  Jun 30 '14 at 19:43