hi,

I started learning openGl yesterday. I made a simple program that supposed to create a triangle and a square, but its not working properly, any help please?

#include<iostream>
#include<Gl/glut.h>
#include<cstdlib>
using namespace std;

void handleKeyPress(unsigned char key, int x, int y)
{
		if(key == 26)
			exit(1);
}


 int InitGL(GLvoid)
{
	
	glShadeModel(GL_SMOOTH);//Enable smootheness
	glClearColor(0.0,0.0,0.0,0.5);//Clear any color to black
	glClearDepth(1.0f);//we clear depth.
	glEnable(GL_DEPTH_BUFFER_BIT);//we turn on depthness
	glDepthFunc(GL_LEQUAL);//the type of test we will use for depth test
	glHint(GL_PERSPECTIVE_CORRECTION_HINT,GL_NICEST); // 
	glEnable(GL_COLOR_BUFFER_BIT);//we enable color buffer.
	
	return true;
}

GLvoid ReSizeGLScene(GLsizei width, GLsizei height)	
{
	if(height == 0)
		height = 1;

	glViewport(0, 0, width, height);	
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	gluPerspective(45.0f,(GLfloat)width/(GLfloat)height,0.1f,100.0f);
	
}

void disp()
{
	 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // clear color and depth to what we set it before;
	 glMatrixMode(GL_MODELVIEW);
	 glLoadIdentity();

	glTranslatef(-1.5,0.0f,-6.0f);
	
	glBegin(GL_TRIANGLES);
	
		glVertex3f(0.0f,1.0f,0.0f);
		glVertex3f(-1.0f,-1.0f,0.0f);
		glVertex3f(1.0f,-1.0f,0.0f);
		
		glEnd();

		glTranslatef(3.0f,0.0f,0.0f);

	glBegin(GL_QUADS);
		glVertex3f(-1.0f,1.0f,0.0f);
		glVertex3f(1.0f,1.0f,0.0f);
		glVertex3f(1.0f,-1.0f,0.0f);
		glVertex3f(-1.0f,-1.0f,0.0f);

	glEnd();

	glutSwapBuffers();
}


int main(int argc,char**argv)
{
		glutInit(&argc,argv);
		glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB|GLUT_DEPTH);
		glutInitWindowSize(500,500);
		glutInitWindowPosition(300,100);
		glutCreateWindow("My first polygons");
		glEnable(GL_DEPTH_TEST);
		glutDisplayFunc(disp);
		glutKeyboardFunc(handleKeyPress);
		glutReshapeFunc(ReSizeGLScene);

		glutMainLoop();

		return 0;
}

Figured out the problem. Needed to add glFlush() at the end of the drawing function.

I'll leave this up here so other can see it,but I will delete this soon.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.