OpenGL fixed coordinate transformations -
was trying rendering opengl , happened think that: how run transformations in fixed coordinate system. in other words, if rotate object around 1 axis see others going rotate too? therefore, following rotations done on newly construct axis.
i read on documentation (see part 9.070 ) https://www.opengl.org/archives/resources/faq/technical/transformations.htm, have no idea: 1. if works 2. how implement cause don't understand supposed do.
i think won't 1 want , it's guess interesting question.
edit: here implementation doesn't work , fix
evoid display() { glclear(gl_color_buffer_bit | gl_depth_buffer_bit); // clear color , depth buffers glmatrixmode(gl_modelview); // operate on model-view matrix // render color-cube consisting of 6 quads different colors glloadidentity(); // reset model-view matrix gltranslatef(1.5f, 0.0f, -7.0f); // move right , screen //glrotatef(anglecube, 1.0f, 1.0f, 1.0f); // rotate (1,1,1)-axis [new] //matrix transformation matrix obtained translation, scale , rotation composition // in other piece of code wanted try // matrix float[16] , values correct , updated every , make cube rotate glmultmatrixf(matrix); glbegin(gl_quads); // begin drawing color cube 6 quads // top face (y = 1.0f) // define vertices in counter-clockwise (ccw) order normal pointing out glcolor3f(0.0f, 1.0f, 0.0f); // green glvertex3f( 1.0f, 1.0f, -1.0f); glvertex3f(-1.0f, 1.0f, -1.0f); glvertex3f(-1.0f, 1.0f, 1.0f); glvertex3f( 1.0f, 1.0f, 1.0f); // bottom face (y = -1.0f) glcolor3f(1.0f, 0.5f, 0.0f); // orange glvertex3f( 1.0f, -1.0f, 1.0f); glvertex3f(-1.0f, -1.0f, 1.0f); glvertex3f(-1.0f, -1.0f, -1.0f); glvertex3f( 1.0f, -1.0f, -1.0f); // front face (z = 1.0f) glcolor3f(1.0f, 0.0f, 0.0f); // red glvertex3f( 1.0f, 1.0f, 1.0f); glvertex3f(-1.0f, 1.0f, 1.0f); glvertex3f(-1.0f, -1.0f, 1.0f); glvertex3f( 1.0f, -1.0f, 1.0f); // face (z = -1.0f) glcolor3f(1.0f, 1.0f, 0.0f); // yellow glvertex3f( 1.0f, -1.0f, -1.0f); glvertex3f(-1.0f, -1.0f, -1.0f); glvertex3f(-1.0f, 1.0f, -1.0f); glvertex3f( 1.0f, 1.0f, -1.0f); // left face (x = -1.0f) glcolor3f(0.0f, 0.0f, 1.0f); // blue glvertex3f(-1.0f, 1.0f, 1.0f); glvertex3f(-1.0f, 1.0f, -1.0f); glvertex3f(-1.0f, -1.0f, -1.0f); glvertex3f(-1.0f, -1.0f, 1.0f); // right face (x = 1.0f) glcolor3f(1.0f, 0.0f, 1.0f); // magenta glvertex3f(1.0f, 1.0f, -1.0f); glvertex3f(1.0f, 1.0f, 1.0f); glvertex3f(1.0f, -1.0f, 1.0f); glvertex3f(1.0f, -1.0f, -1.0f); glend(); // end of drawing color-cube
the opengl modelview matrix works stack. every transformation performed post-multiplying transformation current mvm matrix. in other words, looks this:
new_mvm = old_mvm * transformation
this causes transformations occur in local coordinate system. article states, need transformation work in fixed-coordinates system ("global coordinates"), need pre-multiply this:
new_mvm = transformation * old_mvm
note instead of retrieving mvm opengl article specifies, can store transformation matrix object data member. each time want transform object, pre-multiply new transformation old one.
once have computed final transformation matrix object pre-multiplying, can pass opengl calling glmultmatrix.
edit: since need calculate matrix yourself, you're going want math library. if you're using glm, can like:
when rotating object:
glm::vec3 axis(x, y, z); glm::mat4 rotation = glm::rotate(glm::mat4(1.0f), angle_in_degrees, axis); totaltransform = rotation * totaltransform;
then in 'display' method:
float *matptr = glm::value_ptr(totaltransform); glmultmatrixf(matptr);
disclaimer: code untested.
Comments
Post a Comment