Intro_Triang_Cuad.c



// DIBUJO DE UN TRIANGULO Y UN CUADRO EN PERSPECTIVA

#include <GL/glut.h>

// Objetos con oclusiOn
// Hay que activar el Z-Buffer. Es un arreglo que guarda la profundidad (Z) para todos los pixeles
void display(void)
{
	glClearColor (0.0, 0.0, 0.0, 0.0);
	glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);	// (1 de 3) borra la pantalla e inicializa el Z-Buffer
	
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	//glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0);
	glMatrixMode(GL_MODELVIEW);
	gluPerspective(60,1,1,50);
	glTranslatef(0,0,-2);		// Alejamos el cuadro del observador 2 unidades en Z
	
	// Activacion del Z-Buffer		(2 de 3)
	glDepthFunc(GL_LEQUAL);		// Decimos que un pixel se dibuje si su Z es mAs cercana al observador
	glEnable(GL_DEPTH_TEST);	// Se habilita la comprobaciOn de la progundidad en el dibujado
	glClearDepth(1);		// Cada vez que se borre el buffer de profundidad se inicialice en 1.0
	
	glBegin(GL_QUADS);
		glColor3f(1,0,0); glVertex3f(-0.5,0.5,-0.5);
		glColor3f(0,1,0); glVertex3f(-0.5,-0.5,0.5);
		glColor3f(0,0,1); glVertex3f(0.5,-0.5,0.5);
		glColor3f(0,1,1); glVertex3f(0.5,0.5,-0.5);
	glEnd();
	glBegin(GL_TRIANGLES);
		glColor3f(1,0,0); glVertex3f(0.0,0.5,0.0);
		glColor3f(0,1,0); glVertex3f(-0.7,-0.5,0.0);
		glColor3f(0,0,1); glVertex3f(0.7,-0.5,0.0);
	glEnd();

	glFlush ();
	sleep(5);
	exit(0);
}

void inicializa (void)
{
	// inicializa los valores de la vista
	glLoadIdentity();
}




int main(int argc, char** argv)
{
	glutInit(&argc, argv);
	glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);	// (3 de 3) Se reserva espacio para el Z-Buffer
	glutInitWindowSize (500, 500);
	glutInitWindowPosition (20, 20);
	glutCreateWindow ("Triangulo y cuadro");
	//inicializa ();
	glutDisplayFunc(display);
	glutMainLoop();
	return 0;
}