PDA

View Full Version : Code to draw triangle won't work



Rockinruler
03-31-2010, 03:42 PM
Hi,
I'm just starting with OpenGL, and have appreciable knowledge in C++
I've written a very simple program to draw a triangle which isn't working.
Please kindly tell me what is wrong


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

void initRendering()
{
glEnable(GL_DEPTH_TEST);
}

void handleKey(unsigned char key, int x, int y)
{
switch(key)
case 27:
exit(0);
}

void handleResize(int w, int h)
{
glViewport(0,0,w,h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0,(double)w/(double)h,1,200);
}

void drawScene()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

glBegin(GL_TRIANGLES);

glVertex3f(5,5,-5);
glVertex3f(10,5,-5);
glVertex3f(7.5,10,-5);

glutSwapBuffers();

}

int main(int argc,char** argv)
{
using namespace std;

glutInit(&amp;argc, argv);
glutInitWindowSize(500,500);
glutInitDisplayMode( GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH );

glutCreateWindow("OpenGL window");
initRendering();

glutDisplayFunc(drawScene);
glutKeyboardFunc(handleKey);
glutReshapeFunc(handleResize);

glutMainLoop();
return 19;
}

overlay
03-31-2010, 05:08 PM
I don't see glEnd();

ZbuffeR
03-31-2010, 05:10 PM
Try these coordinates instead :

glVertex3f(5,5,-50);
glVertex3f(10,5,-50);
glVertex3f(7.5,10,-50);

Otherwise the triangle is out of the field of view.

Rockinruler
04-03-2010, 02:30 PM
Ty buffer and overlay