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.
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With