Computação Gráfica I

Trabalho Prático

Exercícios



1. Introdução à OpenGL


(um simples triangulo em OpenGL)

Baixe o arquivo zip contendo o exemplo e execute no seu PC.

Fonte:

#include <GL/glut.h>


void display(void)
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); // Clear the colour and depth buffer

glLoadIdentity(); // Clear matrix stack

glTranslatef(0,0,-10);

glColor3f(1,0,0);
glBegin(GL_TRIANGLES);
glVertex3f(0,0,0);
glVertex3f(1,0,0);
glVertex3f(0,1,0);
glEnd();

glFlush(); // Makes sure that we output the model to the graphics card
glutSwapBuffers();
glutPostRedisplay();
}

// Called when a key is pressed
void key(unsigned char k, int x, int y)
{
if( k == 'q' ) exit(0);
}

void reshape(int width,int height)
{
glViewport(0,0,width,height); // Reset The Current Viewport

glMatrixMode(GL_PROJECTION); // Select The Projection Matrix
glLoadIdentity(); // Reset The Projection Matrix

// Calculate The Aspect Ratio Of The Window
gluPerspective(45.0f,(float)640/(float)480,0.1f,1000.0f);
// Always keeps the same aspect as a 640 wide and 480 high window

glMatrixMode(GL_MODELVIEW); // Select The Modelview Matrix
glLoadIdentity(); // Reset The Modelview Matrix
}

void init()
{
glClearColor(0.2,0,0.5,0);
glClearDepth(1.0); // Enables Clearing Of The Depth Buffer
glDepthFunc(GL_LESS); // The Type Of Depth Test To Do
glEnable(GL_DEPTH_TEST); // Enables Depth Testing
glShadeModel(GL_SMOOTH); // Enables Smooth Color Shading
}

int main(int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB|GLUT_DOUBLE); // We want rgb display functionality
glutInitWindowSize(640,480); // Set the window dimensions
glutInitWindowPosition(0,0); // Set the window starting point
glutCreateWindow("El Triangulo del Futuro"); // Set the caption and launch the window

init();

// Last things before rendering starts
glutDisplayFunc(display); // This will be called every frame
glutReshapeFunc(reshape); // Reshape the window when something changes
glutKeyboardFunc(key); // Callback for input

glutMainLoop(); // Starts the main program

// We will not reach this point unless exit(0) is called (see function keyCB)

return 0;
}