P. 1
Exemplo de Um Cilindro Em CPP

Exemplo de Um Cilindro Em CPP

|Views: 603|Likes:
Published by Joaquim Pereira
exemplo de um cilindro em 3D
exemplo de um cilindro em 3D

More info:

Published by: Joaquim Pereira on Apr 10, 2011
Copyright:Attribution Non-commercial

Availability:

Read on Scribd mobile: iPhone, iPad and Android.
download as DOCX, PDF, TXT or read online from Scribd
See more
See less

03/13/2013

pdf

text

original

Exemplo de programa em C++ com biblioteca GLUT de um cilindro com visualização em 3D. O exemplo utiliza o rato e as teclas: #include <stdlib.

h> #include <GL/glut.h> #include <math.h> #define _PI_ 3.14159 #define ANG2RAD 3.14159265358979323846/360.0 float camX = 0, camY, camZ = 5; int startX, startY, tracking = 0; int alpha = 0, beta = 0, r = 5;

void changeSize(int w, int h) { // Prevent a divide by zero, when window is too short // (you cant make a window with zero width). if(h == 0) h = 1; // compute window's aspect ratio float ratio = w * 1.0 / h; // Reset the coordinate system before modifying glMatrixMode(GL_PROJECTION); glLoadIdentity(); // Set the viewport to be the entire window glViewport(0, 0, w, h); // Set the correct perspective gluPerspective(45,ratio,1,1000); // return to the model view matrix mode glMatrixMode(GL_MODELVIEW); }

/*----------------------------------------------------------------------------------Função para desenhar um cilindro como superfície de revolução parametros: lados : o número de fatias do objecto gerado

raio : raio do cilindro altura: altura do cilindro -----------------------------------------------------------------------------------*/

void cilindro(floataltura,floatraio,int lados) { float passo = 2*_PI_/lados; floatnroCiclos = lados;

//glPushMatrix(); glBegin(GL_TRIANGLE_FAN); glVertex3f(0.0f,altura/2,0.0f); for(int i=lados+1;i>0;i--){ glVertex3f(raio*cos(passo*i),altura/2,raio*sin(passo*i)); } glEnd(); //glPopMatrix(); ////glPushMatrix(); ////glTranslatef(0.0f,-altura,0.0f); glBegin(GL_TRIANGLE_FAN); glVertex3f(0.0f,-altura/2,0.0f); for(int i=0;i<=lados;i++){ glVertex3f(raio*cos(passo*i),-altura/2,raio*sin(passo*i)); } glEnd(); ////glPopMatrix(); ////glPushMatrix(); ////glTranslatef(0.0f,-altura,0.0f); glBegin(GL_TRIANGLE_STRIP); //glVertex3f(0.0f,1.0f,0.0f); for(int i=0;i<=lados;i++){ glVertex3f(raio*cos(passo*i),-altura/2,raio*sin(passo*i)); glVertex3f(raio*cos(passo*i),altura/2,raio*sin(passo*i)); } glEnd(); ////glPopMatrix();

}

void renderScene(void) {

float pos[4] = {-1.0, 1.0, 1.0, 0.0}; glClearColor(0.0f,0.0f,0.0f,0.0f); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glLoadIdentity(); gluLookAt(camX, camY, camZ, 0.0,0.0,0.0, 0.0f,1.0f,0.0f); // colocar aqui a instrução para posicionar a luz // desenho //glutWireTeapot(1); cilindro(2,1,50); // End of frame glutSwapBuffers(); }

// escrever função de processamento do teclado void processKeys(unsigned char key, int xx, int yy) { switch(key) { } }

void processMouseButtons(int button, int state, int xx, int yy) { if (state == GLUT_DOWN) { startX = xx; startY = yy; if (button == GLUT_LEFT_BUTTON) tracking = 1; else if (button == GLUT_RIGHT_BUTTON) tracking = 2; else tracking = 0; } else if (state == GLUT_UP) { if (tracking == 1) { alpha += (xx - startX);

beta += (yy - startY); } else if (tracking == 2) { r -= yy - startY; if (r < 3) r = 3.0; } tracking = 0; }

}

void processMouseMotion(int xx, int yy) { int deltaX, deltaY; int alphaAux, betaAux; int rAux; if (!tracking) return; deltaX = xx - startX; deltaY = yy - startY; if (tracking == 1) {

alphaAux = alpha + deltaX; betaAux = beta + deltaY; if (betaAux > 85.0) betaAux = 85.0; else if (betaAux < -85.0) betaAux = -85.0; rAux = r; } else if (tracking == 2) { alphaAux = alpha; betaAux = beta; rAux = r - deltaY; if (rAux< 3) rAux = 3; } camX = rAux * sin(alphaAux * 3.14 / 180.0) * cos(betaAux * 3.14 / 180.0);

camZ = rAux * cos(alphaAux * 3.14 / 180.0) * cos(betaAux * 3.14 / 180.0); camY = rAux * sin(betaAux * 3.14 / 180.0);

} void main(int argc, char **argv) { // inicialização glutInit(&argc, argv); glutInitDisplayMode(GLUT_DEPTH|GLUT_DOUBLE|GLUT_RGBA); glutInitWindowPosition(100,100); glutInitWindowSize(320,320); glutCreateWindow("CG@DI-UM");

// registo de funções glutDisplayFunc(renderScene); glutIdleFunc(renderScene); glutReshapeFunc(changeSize); // pôr aqui registo da funções do teclado e rato glutKeyboardFunc(processKeys); glutMouseFunc(processMouseButtons); glutMotionFunc(processMouseMotion);

// alguns settings para OpenGL glEnable(GL_DEPTH_TEST); glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); glEnable(GL_CULL_FACE);

// inicialização da luz

// entrar no ciclo do GLUT glutMainLoop(); }

You're Reading a Free Preview

Download
scribd
/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->