// ------------------------------------------------------------------------ // This program is complementary material for the book: // // Frank Nielsen // // Visual Computing: Geometry, Graphics, and Vision // // ISBN: 1-58450-427-7 // // Charles River Media, Inc. // // // All programs are available at http://www.charlesriver.com/visualcomputing/ // // You may use this program for ACADEMIC and PERSONAL purposes ONLY. // // // The use of this program in a commercial product requires EXPLICITLY // written permission from the author. The author is NOT responsible or // liable for damage or loss that may be caused by the use of this program. // // Copyright (c) 2005. Frank Nielsen. All rights reserved. // ------------------------------------------------------------------------ // ------------------------------------------------------------------------ // File: 3dtransformations.cpp // // Description: Common 3D transformations: rotations, shears, translations... // ------------------------------------------------------------------------ #include "stdafx.h" #include #include "bunny.h" int width=512,height=512; GLfloat cx,cy,cz; #define INITIALIZE(M) for(int ii=0;ii<4;ii++) for(int jj=0;jj<4;jj++) M[4*ii+jj]=((ii==jj) ? 1.0 : 0.0); void gravity() { int i; cx=cy=cz=0.0; for(i=0;i360) spin-=360;} glRotatef(spin,0.0,1.0,0.0); glPolygonMode(GL_FRONT_AND_BACK,GL_FILL); glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT_AND_DIFFUSE,color); glDrawElements(GL_TRIANGLES,3*NUM_TRIANGLES,GL_UNSIGNED_INT,triangles); glPushMatrix(); glTranslatef(cx,cy,cz); DrawAxis(0.15); glPopMatrix(); glPopMatrix(); DrawAxis(); glPopMatrix(); // Swap the buffers && redraw glutSwapBuffers(); glutPostRedisplay(); } #include GLfloat angle=3.14159265/4.0; void keyb(unsigned char key, int x, int y) { int i,j; float shear=1.0; switch(key) { case ' ': rotate=!rotate; break; case 'I': case 'i': for(i=0;i<4;i++) for(j=0;j<4;j++) M[4*i+j]=((i==j) ? 1.0 : 0.0); break; case 'T': case 't': for(i=0;i<4;i++) for(j=0;j<4;j++) M[4*i+j]=((i==j) ? 1.0 : 0.0); M[INDEX(0,3)]=cx; M[INDEX(1,3)]=cy; M[INDEX(2,3)]=cz; break; case 'E': case 'e': for(i=0;i<4;i++) for(j=0;j<4;j++) M[4*i+j]=((i==j) ? 1.0 : 0.0); M[INDEX(0,0)]=cos(angle); M[INDEX(0,1)]=-sin(angle); M[INDEX(1,0)]=sin(angle); M[INDEX(1,1)]=cos(angle); break; case 'R': case 'r': for(i=0;i<4;i++) for(j=0;j<4;j++) M[4*i+j]=((i==j) ? 1.0 : 0.0); M[INDEX(1,1)]=cos(angle); M[INDEX(1,2)]=-sin(angle); M[INDEX(2,1)]=sin(angle); M[INDEX(2,2)]=cos(angle); break; case 'W': case 'w': for(i=0;i<4;i++) for(j=0;j<4;j++) M[4*i+j]=((i==j) ? 1.0 : 0.0); M[INDEX(0,0)]=cos(angle); M[INDEX(0,2)]=-sin(angle); M[INDEX(2,0)]=sin(angle); M[INDEX(2,2)]=cos(angle); break; case 'S': case 's': INITIALIZE(M); M[INDEX(0,0)]=1.5;M[INDEX(1,1)]=0.5;M[INDEX(2,2)]=0.25; break; case 'O': case 'o': for(i=0;i<4;i++) for(j=0;j<4;j++) M[4*i+j]=((i==j) ? 1.0 : 0.0); M[INDEX(0,0)]=M[INDEX(1,1)]=-1; //M[INDEX(2,2)]-1; break; case 'Y': case 'y': for(i=0;i<4;i++) for(j=0;j<4;j++) M[4*i+j]=((i==j) ? 1.0 : 0.0); M[INDEX(0,0)]=-1; break; case 'X': case 'x': for(i=0;i<4;i++) for(j=0;j<4;j++) M[4*i+j]=((i==j) ? 1.0 : 0.0); M[INDEX(1,1)]=-1; break; case 'Z':case 'z': for(i=0;i<4;i++) for(j=0;j<4;j++) M[4*i+j]=((i==j) ? 1.0 : 0.0); M[INDEX(2,2)]=-1; case 'H': case 'h': for(i=0;i<4;i++) for(j=0;j<4;j++) M[INDEX(i,j)]=rand()/RAND_MAX; M[INDEX(0,3)]=M[INDEX(1,3)]=M[INDEX(2,3)]=0.0; M[INDEX(3,3)]=1.0; break; // SHEARS // shear zy case 'J':case 'j': for(i=0;i<4;i++) for(j=0;j<4;j++) M[4*i+j]=((i==j) ? 1.0 : 0.0); M[INDEX(2,1)]=shear; break; case 'K':case 'k': for(i=0;i<4;i++) for(j=0;j<4;j++) M[4*i+j]=((i==j) ? 1.0 : 0.0); M[INDEX(0,1)]=shear; break; case 'L':case 'l': for(i=0;i<4;i++) for(j=0;j<4;j++) M[4*i+j]=((i==j) ? 1.0 : 0.0); M[INDEX(1,0)]=shear; break; } printf("Model Matrix:\n"); for(i=0;i<4;i++) {for(j=0;j<4;j++) printf("%f ",M[INDEX(i,j)]); printf("\n");} } void reshape(int x, int y){ glViewport(0,0,x,y); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(50,x/(y*1.0),0.001,1000.0); }