Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Draw a triangle with OpenGL

Tags:

c++

opengl

Write a C++ program which will draw a triangle having vertices at (300,210), (340,215) and (320,250). Center of the triangle lies at (320,240).

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

void display(void)
{
    glClearColor(1,1,0,0);

    glClear(GL_COLOR_BUFFER_BIT);

    glBegin(GL_TRIANGLES);

    glColor3f(0.5,0,0);

    glVertex2f(300.0,210.0);
    glVertex2f(340.0,215.0);
    glVertex2f(320.0,250.0);

    glEnd();

    glFlush();
}

int main(int argc, char *argv[])
{
    glutInit(&argc, argv);
    glutInitWindowSize(640,500);
    glutInitWindowPosition(1,1);

    glutCreateWindow("Triangle");

    glutDisplayFunc(display);


    glutMainLoop();

    return EXIT_SUCCESS;
}

Issue triangle isn't appearing only a yellow screen appears.

like image 759
Waleed Tariq Avatar asked Feb 04 '23 15:02

Waleed Tariq


1 Answers

Your program needs an appropriate view/projection matrix. glOrtho(0, 640, 480, 0, -1, 1) should do the trick. Ideally it should be called with MatrixMode set to GL_PROJECTION.

like image 130
Bartek Banachewicz Avatar answered Feb 19 '23 14:02

Bartek Banachewicz