EJEMPLOS OPENGL

13
// Traslacion de un Objeto en 3 Dimensiones #include <GL/glut.h> #define ancho 500 #define alto 375 #define profundidad 400 void DibujaObjeto3D(); void EjesXYZ(); void tecladoEspecial(int tecla, int x, int y); int posx=0, posy=0; int angulo=15; float colorr=1, colorg=0, colorb=0; int main(int argc, char** argv) { glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE); glutInitWindowPosition(100, 0); glutInitWindowSize(ancho, alto); glutCreateWindow("Figuras GLUT"); glOrtho(-(ancho/2), (ancho/2), -(alto/2), (alto/2), -profundidad, profundidad); glClearColor(1, 1, 1, 0); glutDisplayFunc(DibujaObjeto3D); glutSpecialFunc(tecladoEspecial); glutMainLoop(); return 0; } void DibujaObjeto3D() { glClear(GL_COLOR_BUFFER_BIT); EjesXYZ(); glPushMatrix(); glTranslatef(posx, posy, 0); //glRotatef(angulo, 1, 1, 1); //glRotatef(0, 1, 1, 1); glColor3f(0,0,1); //glutWireCone(50, 200, 18, 18); glutWireTeapot(100); glPopMatrix(); glutSwapBuffers(); } void EjesXYZ() { glColor3f(1.0, 0.0, 0.0); glBegin(GL_LINES); glVertex3f(-ancho/2, 0, 0);

Transcript of EJEMPLOS OPENGL

// Traslacion de un Objeto en 3 Dimensiones#include <GL/glut.h>#define ancho 500#define alto 375#define profundidad 400void DibujaObjeto3D();void EjesXYZ();void tecladoEspecial(int tecla, int x, int y);int posx=0, posy=0;int angulo=15;float colorr=1, colorg=0, colorb=0;int main(int argc, char** argv) {glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE);glutInitWindowPosition(100, 0);glutInitWindowSize(ancho, alto);glutCreateWindow("Figuras GLUT");glOrtho(-(ancho/2), (ancho/2), -(alto/2), (alto/2), -profundidad, profundidad);glClearColor(1, 1, 1, 0);glutDisplayFunc(DibujaObjeto3D);glutSpecialFunc(tecladoEspecial);glutMainLoop();return 0;}void DibujaObjeto3D() {glClear(GL_COLOR_BUFFER_BIT);EjesXYZ();glPushMatrix();glTranslatef(posx, posy, 0);//glRotatef(angulo, 1, 1, 1);//glRotatef(0, 1, 1, 1);glColor3f(0,0,1);//glutWireCone(50, 200, 18, 18);glutWireTeapot(100);glPopMatrix();glutSwapBuffers();}void EjesXYZ() {glColor3f(1.0, 0.0, 0.0);glBegin(GL_LINES);

glVertex3f(-ancho/2, 0, 0);

glVertex3f(ancho/2, 0, 0);glVertex3f(0, alto/2, 0);glVertex3f(0, -alto/2, 0);glEnd();}void tecladoEspecial(int tecla, int x, int y) {

switch(tecla) {case GLUT_KEY_UP : posy++;break;case GLUT_KEY_DOWN : posy--;break;case GLUT_KEY_RIGHT : posx++;break;case GLUT_KEY_LEFT : posx--;break;}glutPostRedisplay();

}

// Escalado de un Objeto3D#include <GL/glut.h>#define ancho 500#define alto 375#define profundidad 400void DibujaObjeto3D();void EjesXYZ();void tecladoEspecial(int tecla, int x, int y);float esc=1;int main(int argc, char** argv) {glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE);glutInitWindowPosition(100, 0);glutInitWindowSize(ancho, alto);glutCreateWindow("Escala de un Objeto 3D");glOrtho(-(ancho/2), (ancho/2), -(alto/2), (alto/2), -profundidad, profundidad);glClearColor(1, 1, 1, 0);glutDisplayFunc(DibujaObjeto3D);glutSpecialFunc(tecladoEspecial);glutMainLoop();return 0;}void DibujaObjeto3D() {glClear(GL_COLOR_BUFFER_BIT);EjesXYZ();glPushMatrix();glScalef(esc, esc, esc);glColor3f(1,0,0);glutWireTorus(20, 80, 18, 18);glPopMatrix();glutSwapBuffers();}void EjesXYZ() {glColor3f(0.9, 0.9, 0.9);glBegin(GL_LINES);glVertex3f(-ancho/2, 0, 0);glVertex3f(ancho/2, 0, 0);glVertex3f(0, alto/2, 0);glVertex3f(0, -alto/2, 0);glEnd();

}

void tecladoEspecial(int tecla, int x, int y) {switch(tecla) {case GLUT_KEY_PAGE_UP : esc=esc*1.01;break;case GLUT_KEY_PAGE_DOWN : esc=esc*0.99;break;}

glutPostRedisplay();}

// Rota un objeto 3d (esferera)#include <GL/glut.h>#define ancho 500#define alto 375#define p 400void DibujaObjeto3D();void EjesXYZ();void tecladoNormal(unsigned char tecla, int x, int y);void texto(int x, int y, char *palabra);int angulo=35;int main(int argc, char** argv) {glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE);glutInitWindowPosition(100, 0);glutInitWindowSize(ancho, alto);glutCreateWindow("Dibuja un objeto en 3D");glOrtho(-(ancho/2), (ancho/2), -(alto/2), (alto/2), -p, p);glClearColor(1, 1, 1, 0);glutDisplayFunc(DibujaObjeto3D);glutKeyboardFunc(tecladoNormal);

glutMainLoop();return 0;}void DibujaObjeto3D() {glClear(GL_COLOR_BUFFER_BIT);EjesXYZ();glPushMatrix();glRotatef(angulo, 1, 1, 1);glRotatef(45, 1, 1, 1);glColor3f(0, 0, 1);glutWireSphere(100, 18, 18);glPopMatrix();texto(-ancho/2+82,alto/2-20,"ENRICOTI");glutSwapBuffers();}void EjesXYZ() {glColor3f(0.9, 0.9, 0.9);glBegin(GL_LINES);

glVertex3f(-ancho/2, 0, 0);

glVertex3f(ancho/2, 0, 0);glVertex3f(0, alto/2, 0);glVertex3f(0, -alto/2, 0);glEnd();}void tecladoNormal(unsigned char tecla, int x, int y) {switch(tecla) {case ’s’:case ’S’:case 27: exit(0);break;case ’r’:case ’R’: angulo++;break;}glutPostRedisplay();}void texto(int x, int y, char *palabra) {int i;glColor3f(0,0,1);glRasterPos2f(x, y);for (i = 0; palabra[i]; i++)glutBitmapCharacter(GLUT_BITMAP_HELVETICA_12, palabra[i]);

}

//Escalado de un objeto con teclado y ratón#include <gl/glut.h>#include <stdio.h>#include <math.h>#define ancho 300#define alto 300#define profundidad 200float esc=1;void dibujarEscala(){glClear(GL_COLOR_BUFFER_BIT);glPushMatrix();glScalef( esc, esc, esc );glutWireTeapot( 50.0 );glPopMatrix();glutSwapBuffers();}void TecladoEspecial(int tecla, int X, int Y)

{switch(tecla){case GLUT_KEY_PAGE_UP : esc = esc * 1.01; break;case GLUT_KEY_PAGE_DOWN: esc = esc * 0.99; break;}glutPostRedisplay();}void ratonEscala(int Button, int State, int x, int y ){glutPostRedisplay();}void moverEscala( int x, int y ){if( y > (alto/2) ){esc = esc * 1.01;}else{esc = esc * 0.99;

}

glutPostRedisplay();}int main(int argc, char **argv[]){glutInitDisplayMode( GLUT_RGBA|GLUT_DOUBLE );glutInitWindowPosition(0,0);glutInitWindowSize(ancho,alto);glutCreateWindow("Escalado de un Objeto 3D Raton - Teclado");glOrtho(-(ancho/2),(ancho/2),-(alto/2),(alto/2),-profundidad,profundidad);glutDisplayFunc( dibujarEscala );glutSpecialFunc(TecladoEspecial);glutMouseFunc( ratonEscala );glutMotionFunc( moverEscala );glutMainLoop();return 0;

}

//Rotacion de un Objeto 3D con ratón#include<GL/glut.h>#include<GL/glu.h>#include<GL/gl.h>#include<stdio.h>#include<stdlib.h>#define checkImageWidth 64#define checkImageHeight 64static GLubyte checkImage [checkImageHeight][checkImageWidth][4];static GLuint texName;float angulo=0;int angulox=0,anguloy=0,x,y,xold,yold;char presionado;void mouse(int bouton,int estado,int x,int y);void texto(int x, int y, char *palabra);void makeCheckImage(void){int i,j,c;glColor3f(1.0,1.0,1.0);for(i=0;i<checkImageHeight;i++){for(j=0;j<checkImageWidth;j++){c=((((i&0x8)==0)^((j&0x8))==0))*255;

checkImage[i][j][0]=(GLubyte)c;checkImage[i][j][1]=(GLubyte)c;checkImage[i][j][2]=(GLubyte)c;checkImage[i][j][3]=(GLubyte)255;}}}void InicioOpenGL(void){glClearColor(1.0,0.0,0.0,1.0);glShadeModel(GL_FLAT);glEnable(GL_DEPTH_TEST);glColor3f(1.0,0.0,1.0);makeCheckImage();glPixelStorei(GL_UNPACK_ALIGNMENT,1);glGenTextures(1,&texName);glBindTexture(GL_TEXTURE_2D,texName);glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_REPEAT);glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_REPEAT);glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST);glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST);glTexImage2D(GL_TEXTURE_2D,0,GL_RGBA,checkImageWidth,checkImageHeight,0,GL_RGBA,GL_UNSIGNED_BYTE,checkImage);}void Display(void){

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glRotatef(anguloy,1.0,0.0,0.0);glRotatef(angulox,0.0,1.0,0.0);glRotatef(angulo, 1, 0, 0);glRotatef(angulo, 0, 1, 0);glRotatef(angulo, 0, 0, 1);glEnable(GL_TEXTURE_2D);glTexEnvf(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,GL_DECAL);glBindTexture(GL_TEXTURE_2D,texName);glutSolidTeapot(1);glFlush();glutSwapBuffers();glDisable(GL_TEXTURE_2D);}void reshape(int w, int h){glColor3f(1.0,1.0,0.0);glViewport(0,0,(GLsizei)w,(GLsizei)h);glMatrixMode(GL_PROJECTION);glLoadIdentity();gluPerspective(60.0,(GLfloat)w/(GLfloat)h,1.0,30.0);glMatrixMode(GL_MODELVIEW);

glLoadIdentity();glTranslatef(0.0,0.0,-3.6);}void mouse(int button, int estado,int x,int y) {if (button == GLUT_LEFT_BUTTON && estado == GLUT_DOWN) {glutSetCursor(GLUT_CURSOR_CROSSHAIR);presionado = 1;xold = x;yold=y;}if (button == GLUT_LEFT_BUTTON && estado == GLUT_UP) {glutSetCursor(GLUT_CURSOR_INHERIT);presionado=0;}}void movimiento_mouse(int x,int y) {if (presionado) {angulox=angulox+(x-xold);anguloy=anguloy+(y-yold);glutPostRedisplay();}xold=x;yold=y;}void texto(int x, int y,char *palabra){int i;

glColor3f(0,0,1);

for(i=0;palabra[i];i++)glutBitmapCharacter(GLUT_BITMAP_HELVETICA_12, palabra[i]);}int main(int argc, char **argv){glutInit(&argc,argv);glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);glutInitWindowSize(250,250);glutInitWindowPosition(100,100);glutCreateWindow(argv[0]);InicioOpenGL();glutDisplayFunc(Display);glutReshapeFunc(reshape);glutMouseFunc(mouse);glutMotionFunc(movimiento_mouse);glutMainLoop();return 0;

}