there is a sample code for glroatae, gl Translate and zoom by using alLootAt
#include <glut.h>
bool rotationenable = false;
bool scaleenable = false;
bool translateenable = false;
static float angle[3] ={0.0,0.0,0.0};
//scale factors
static float sx = 1.0, sy = 1.0 , sz = 1.0;
//coordinates of cube
float Vertices[8][3] = {
{-1.0,-1.0,1.0},{1.0,-1.0,
{-1.0,-1.0,-1.0}, {1.0,-1.0,-1.0}, {1.0,1.0,-1.0}, {-1.0,1.0,-1.0}};
int vector[] = {0,0,0};
void drawFace(int a,int b,int c,int d)
{
glVertex3fv(Vertices[a]);
glVertex3fv(Vertices[b]);
glVertex3fv(Vertices[c]);
glVertex3fv(Vertices[d]);
}
void cube ()
{
glBegin(GL_QUADS);
//front
glColor3f(1.0, 0.0, 0.0); //red
drawFace(0,1,2,3);
//back
glColor3f(0.0, 1.0, 0.0); //green
drawFace(7,6,5,4);
//top
glColor3f(0.0, 0.0, 1.0); //blue
drawFace(3,2,6,7);
//bottom
glColor3f(1.0, 1.0, 0.0); //yellow
drawFace(1,0,4,5);
//left
glColor3f(1.0, 0.0, 1.0); //pink
drawFace(0,3,7,4);
//right
glColor3f(0.0, 1.0, 1.0); //cyan
drawFace(2,1,5,6);
glEnd();
}
static float x=0.0f,y=0.0f,z=50.0f;
static float lx=0.0f,ly=0.0f,lz=-1.0f;
void display(void){
glClear(GL_COLOR_BUFFER_BI
glLoadIdentity();
gluLookAt(x, y, z,
x + lx,y + ly,z + lz,
0.0f,1.0f,0.0f);
if (translateenable == true)
glTranslated(vector[0], vector[1], vector[2]);
if (rotationenable == true){
glRotatef(angle[0], 1.0, 0.0, 0.0);
glRotatef(angle[1], 0.0, 1.0, 0.0);
glRotatef(angle[2], 0.0, 0.0, 1.0);
}
if (scaleenable == true)
glScalef(0.5, 1.5, 0.7);
glScalef(sx, sy, sz);
cube();
glutSwapBuffers();
}
void processMenuEvents(int option) {
switch (option) {
case (1):
translateenable = true;
vector[0] += 1;
vector[1] += 2;
vector[2] += 3;
break;
case (2):
rotationenable = true;
angle[0] += 45.0;
break;
case (3):
rotationenable = true;
angle[1] += 45.0;
break;
case (4):
rotationenable = true;
angle[2] += 45.0;
break;
case (5):
scaleenable = true;
break;
case (6):
translateenable = false;
rotationenable = false;
scaleenable = false;
angle[0] =0.0;
angle[1] =0.0;
angle[2]=0.0;
sx = 1.0;
sy = 1.0;
sz = 1.0;
break;
default:
break;
}
}
//create menu
void createGLUTMenus() {
int menu,submenu;
submenu = glutCreateMenu(processMenu
glutAddMenuEntry("X",2);
glutAddMenuEntry("Y",3);
glutAddMenuEntry("Z",4);
menu = glutCreateMenu(processMenu
glutAddMenuEntry("Translat
glutAddSubMenu("Rotate Menu",submenu);
glutAddMenuEntry("Scale",5
glutAddMenuEntry("Reset",6
glutAttachMenu(GLUT_RIGHT_
}
//function to move the camera
void moveMeFlat(int i) {
x = x + i*(lx)*0.1;
z = z + i*(lz)*0.1;
glLoadIdentity();
gluLookAt(x, y, z,
x + lx,y + ly,z + lz,
0.0f,1.0f,0.0f);
}
void keyboard(unsigned char key,int x, int y){
switch(key){
case 'f':
moveMeFlat(1);
break;
case 'b':
moveMeFlat(-1);
break;
case '+':
sx += 1.0;
sy += 1.0;
sz += 1.0;
break;
case '-':
if(sx >1.0 && sy >1.0 && sz>1.0){
sx -= 1.0;
sy -= 1.0;
sz -= 1.0;
}
break;
default:
break;
}
}
static float ratio;
//function for resize display window
void reshape ( int w, int h )
{
glViewport ( 0, 0, w, h );
glMatrixMode ( GL_PROJECTION );
glLoadIdentity ( );
if ( h==0 )
gluPerspective ( 60, ( float ) w, 1.0, 5000.0 );
else
gluPerspective ( 60, ( float ) w / ( float ) h, 1.0, 5000.0 );
glMatrixMode ( GL_MODELVIEW );
glLoadIdentity ( );
}
void main(int argc, char ** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_D
glutInitWindowPosition(100
glutInitWindowSize(500,500
glutCreateWindow("sample1"
glutKeyboardFunc(keyboard)
createGLUTMenus();
glutDisplayFunc(display);
glutIdleFunc(display);
glutReshapeFunc(reshape);
glEnable(GL_DEPTH_TEST);
glutMainLoop();
}
Main Topics
Browse All Topics





by: BarcaPosted on 2005-04-25 at 22:13:18ID: 13864206
glRotate, glTanslate
/www/Terry /OpenGL/ In troduction .html
for zoom, you can use glLookAt
http://www.eecs.tulane.edu