Jump to content
  • Advertisement
Sign in to follow this  
lv_madness

OpenGL How to Use class wcPt3D

This topic is 3977 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

hey again ... i just have a practical exam ... the instructor said that i must know how to use the function rotate3d that i have declared by entering the point using the class wcPt3D .. but i cant have the right output ..below is the code : can anyone help me outwith this .. i dont need advices cuz i have no experience in this .. i need somewone to correct the code plz #include <stdlib.h> #include <math.h> #include <GL/glut.h> /* Set initial display-window size. */ GLsizei winWidth = 600, winHeight = 600; /* Set range for world coordinates. */ GLfloat xwcMin = 0.0, xwcMax = 225.0; GLfloat ywcMin = 0.0, ywcMax = 225.0; class wcPt3D { public: GLfloat x, y, z; }; typedef GLfloat Matrix4x4 [4][4]; Matrix4x4 matComposite; //Global variable, the transformation const GLdouble pi = 4*atan(1.0); wcPt3D p1, p2; void init (void) { /* Set color of display window to white. */ glClearColor (1.0, 1.0, 1.0, 0.0); } /* Construct the 4 by 4 identity matrix. */ void matrix4x4SetIdentity (Matrix4x4 matIdent4x4) { GLint row, col; for (row = 0; row < 4; row++) for (col = 0; col < 4 ; col++) matIdent4x4 [row][col] = (row == col); } /* Premultiply matrix m1 times matrix m2, store result in m2. */ void matrix4x4PreMultiply (Matrix4x4 m1, Matrix4x4 m2) { GLint row, col; Matrix4x4 matTemp; for (row = 0; row < 4; row++) for (col = 0; col < 4 ; col++) matTemp [row][col] = m1 [row][0] * m2 [0][col] + m1 [row][1] * m2 [1][col] + m1 [row][2] * m2 [2][col] + m1 [row][3] * m2 [3][col]; for (row = 0; row < 4; row++) for (col = 0; col < 4; col++) m2 [row][col] = matTemp [row][col]; } /* Procedure for generating 3D translation matrix. */ void translate3D (GLfloat tx, GLfloat ty, GLfloat tz) { Matrix4x4 matTransl3D; /* Initialize translation matrix to identity. */ matrix4x4SetIdentity (matTransl3D); matTransl3D [0][3] = tx; matTransl3D [1][3] = ty; matTransl3D [2][3] = tz; /* Concatenate matTransl3D with composite matrix. */ matrix4x4PreMultiply (matTransl3D, matComposite); } /* Procedure for generating a quaternion rotation matrix. */ // p1 and p2 gives the axis of rotation, void rotate3D (wcPt3D p1, wcPt3D p2, GLfloat radianAngle) { Matrix4x4 matQuatRot; // unit vector calculation for axis of rotation float axisVectLength = sqrt ((p2.x - p1.x) * (p2.x - p1.x) + (p2.y - p1.y) * (p2.y - p1.y) + (p2.z - p1.z) * (p2.z - p1.z)); float ux = (p2.x - p1.x) / axisVectLength; float uy = (p2.y - p1.y) / axisVectLength; float uz = (p2.z - p1.z) / axisVectLength; float cosA = cosf(radianAngle); float oneC = 1 - cosA; float sinA = sinf (radianAngle); /* Set up translation matrix for moving p1 to origin, * and concatenate translation matrix with matComposite. */ translate3D (-p1.x, -p1.y, -p1.z); /* Initialize matQuatRot to identity matrix. */ matrix4x4SetIdentity (matQuatRot); matQuatRot [0][0] = ux*ux*oneC + cosA; matQuatRot [0][1] = ux*uy*oneC - uz*sinA; matQuatRot [0][2] = ux*uz*oneC + uy*sinA; matQuatRot [1][0] = uy*ux*oneC + uz*sinA; matQuatRot [1][1] = uy*uy*oneC + cosA; matQuatRot [1][2] = uy*uz*oneC - ux*sinA; matQuatRot [2][0] = uz*ux*oneC - uy*sinA; matQuatRot [2][1] = uz*uy*oneC + ux*sinA; matQuatRot [2][2] = uz*uz*oneC + cosA; /* Concatenate matQuatRot with composite matrix. */ matrix4x4PreMultiply (matQuatRot, matComposite); /* Construct inverse translation matrix for p1 and * concatenate with composite matrix. */ translate3D (p1.x, p1.y, p1.z); } /* Procedure for generating a 3D scaling matrix. */ void scale3D (GLfloat sx, GLfloat sy, GLfloat sz, wcPt3D fixedPt) { Matrix4x4 matScale3D; /* Initialize scaling matrix to identity. */ matrix4x4SetIdentity (matScale3D); matScale3D [0][0] = sx; matScale3D [0][3] = (1 - sx) * fixedPt.x; matScale3D [1][1] = sy; matScale3D [1][3] = (1 - sy) * fixedPt.y; matScale3D [2][2] = sz; matScale3D [2][3] = (1 - sz) * fixedPt.z; /* Concatenate matScale3D with composite matrix. */ matrix4x4PreMultiply (matScale3D, matComposite); } // defines box vertices int a[3]={10,10,10}, b[3]={10,-10,10}, c[3]={-10,-10,10}, d[3]={-10,10,10}, e[3]={10,10,-10}, f[3]={10,-10,-10}, g[3]={-10,-10,-10}, h[3]={-10,10,-10}; void drawcube(void) { glClear(GL_COLOR_BUFFER_BIT); glColor3f(1.0, 1.0, 1.0); glMatrixMode(GL_MODELVIEW); glBegin(GL_LINE_LOOP); glVertex3iv(a); glVertex3iv(b); glVertex3iv(c); glVertex3iv(d); glEnd(); glBegin(GL_LINE_LOOP); glVertex3iv(a); glVertex3iv(e); glVertex3iv(f); glVertex3iv(b); glEnd(); glBegin(GL_LINE_LOOP); glVertex3iv(d); glVertex3iv(h); glVertex3iv(g); glVertex3iv(c); glEnd(); glBegin(GL_LINE_LOOP); glVertex3iv(e); glVertex3iv(f); glVertex3iv(g); glVertex3iv(h); glEnd(); glFlush(); glutSwapBuffers(); p1.x = p1.y = p1.z = 2.0; p2.x = p2.y = p2.z = 7.0; } int main(int argc, char **argv) { glutInit(&argc, argv); glutInitWindowSize(500, 500); glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE); glutCreateWindow("MY CUBE "); glutDisplayFunc(drawcube); glMatrixMode(GL_PROJECTION); glLoadIdentity(); //OpenGL setting for the current matrix glOrtho(-30.0, 30.0, -30.0, 30.0, -30.0, 30.0); //The glOrtho function multiplies the current matrix by an orthographic matrix // translate3D(2,2,3); // our translation funciton rotate3D(p1,p2,pi/8); // our rotatoin funciotn // scale3D(....); // our scaling funciton // implement the non-OpenGL geometric transformations in your // program. This will be the topic of the practical exam next lecture glMatrixMode(GL_MODELVIEW); glClearColor(1.0, 0.0, 0.0, 0.0); //blck background is used now glutMainLoop(); return(0); }

Share this post


Link to post
Share on other sites
Advertisement
Sounds like homework to me, plus just dumpping code and saying 'make it work' is frowned upon as well; if you still want help then repost with a specific question and proof that you have tried to work this problem out on your own.

Share this post


Link to post
Share on other sites

This topic is 3977 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Guest
This topic is now closed to further replies.
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!