Jump to content
  • Advertisement
Sign in to follow this  
awg_x

OpenGL My way to compute the rotation of vertices only works for certain angles

This topic is 4844 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

Hi fellows, I need to compute the rotation of some Polygons. For this I thought, that I have found the right way, so that the result exactly looks like when I have used glRotatef() instead of calculating it myself. Unfortunately when I look at the result, it differs from what glRotatef() would do. When I use 180° for the angle, then all looks like it should, but not, when I use 80° for example :( I thought of "double" instead of "float" and also used a very long "pi" for the calculation, but this doesn't seem to be the source for the wrong result. Here is a testprogramm, which should show you my problem: #include <iostream> #include <SDL_ttf.h> #include <GL/gl.h> #include <GL/glu.h> #include <cmath> using namespace std; SDL_Surface *sdlsurface1 = NULL; SDL_Event event1; GLboolean doNotEnd; void initGL(GLvoid) { glShadeModel(GL_SMOOTH); glClearColor(0.0f, 0.0f, 0.0f, 0.0f); glClearDepth(1.0f); glEnable(GL_DEPTH_TEST); glDepthFunc(GL_LEQUAL); glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); } int main(int argc, char *argv[]) { cout << "Starting ..." << endl; if (SDL_Init(SDL_INIT_VIDEO|SDL_INIT_TIMER) == -1 ) { cout << "Couldn't initialize SDL: " << SDL_GetError() << endl; exit(1); } SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); sdlsurface1 = SDL_SetVideoMode(1024, 768, 32, SDL_HWSURFACE|SDL_HWPALETTE|SDL_FULLSCREEN|SDL_OPENGLBLIT|SDL_GL_DOUBLEBUFFER|SDL_HWACCEL); if (sdlsurface1 == NULL) { cout << "Couldn't set 1024x768x32 video mode: " << SDL_GetError() << endl; exit(1); } initGL(); glViewport( 0, 0, (GLsizei)1024, (GLsizei)768); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(45.0f, (GLfloat)1024/(GLfloat)768, 0.1f, 800.0f); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); SDL_ShowCursor(SDL_ENABLE); glClearColor(0.0f, 0.0f, 0.0f, 0.0f); doNotEnd = true; SDL_EnableUNICODE(1); GLfloat DEG2RAD = M_PI/*3.1415926535897932384626433832795*/ / 180, *vertexCoordinates; GLboolean doGLRotate = true; GLubyte actCycle, actCoord, Offset; vertexCoordinates = new GLfloat[12]; while (doNotEnd == true) { vertexCoordinates[0] = -0.03125f; vertexCoordinates[1] = -0.03125f; vertexCoordinates[2] = 0.0f; vertexCoordinates[3] = 0.03125f; vertexCoordinates[4] = -0.03125f; vertexCoordinates[5] = 0.0f; vertexCoordinates[6] = 0.03125f; vertexCoordinates[7] = 0.03125f; vertexCoordinates[8] = 0.0f; vertexCoordinates[9] = -0.03125f; vertexCoordinates[10] = 0.03125f; vertexCoordinates[11] = 0.0f; if (SDL_PollEvent(&event1) == 1) { if (event1.key.keysym.sym == SDLK_ESCAPE) doNotEnd = false; if (event1.key.keysym.sym == SDLK_SPACE) { if (doGLRotate == true) doGLRotate = false; else doGLRotate = true; } } glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); glLoadIdentity(); glColor3ub(255, 255, 255); glTranslatef(0, 0, -0.13); if (doGLRotate == true) glRotatef(80, 0, 1, 0); else { actCoord = 3; for (actCycle = 0; actCycle < 4; actCycle++) { Offset = 0; vertexCoordinates[Offset + (actCoord*actCycle)] = ((vertexCoordinates[Offset + (actCoord*actCycle)] * cos(80 * DEG2RAD)) + (vertexCoordinates[2 + Offset + (actCoord*actCycle)] * sin(80 * DEG2RAD))); Offset += 2; vertexCoordinates[Offset + (actCoord*actCycle)] = ((vertexCoordinates[Offset + (actCoord*actCycle)] * cos(80 * DEG2RAD)) - (vertexCoordinates[Offset + (actCoord*actCycle) - 2] * sin(80 * DEG2RAD))); } } glBegin(GL_QUADS); glVertex3f(vertexCoordinates[0], vertexCoordinates[1], vertexCoordinates[2]); glVertex3f(vertexCoordinates[3], vertexCoordinates[4], vertexCoordinates[5]); glVertex3f(vertexCoordinates[6], vertexCoordinates[7], vertexCoordinates[8]); glVertex3f(vertexCoordinates[9], vertexCoordinates[10], vertexCoordinates[11]); glEnd(); SDL_GL_SwapBuffers(); } SDL_Quit(); cout << "Ending ..." << endl; return 0; } You can quit the programm by pressing the escape key. When the programm starts, it draws the quad rotated with glRotatef(). As long as you press the space key, it will draw the quad rotated with my own calculated vertice coordinates. When you release the space key it is drawn with glRotatef(), again. Is there someone, who can tell me, what to change, so that it exactly looks the same for all angles, when pressing the space key, without using glRotatef()? Greetings Alex

Share this post


Link to post
Share on other sites
Advertisement
Sorry for bothering you all.

Four minutes after posting and reading the source again in the browser I found my error myself =D

I used for the old X-coordinate in the line, where the new Z-coordinate is computed, the new (and changed) X-coordinate and not the old one. When saving this old one first and then using it in the line where the new Z-coordinate is computed, then it looks exactly the same.


Alex :D

Share this post


Link to post
Share on other sites
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!