简体   繁体   中英

Rendering GL_TRIANGLES with OpenGL in C++

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM