Advertisement

glRotate has no effect - why?

Started by May 15, 2004 05:39 AM
11 comments, last by karx11erx 20Β years, 6Β months ago
Hi, I have a simple scene consisting of 3 rectangular textures. I can scale the scene using glTranslate, but I cannot rotate it using glRotate (e.g. X rotation with glRotated (10.0, 1.0, 0.0, 0.0). Why is this? I am clueless.
_________karx11erxVisit my Descent site or see my case mod.
It''s probably because you''re doing something wrong.

Do you honestly think we can help you if you just say ''it doesn''t work''?
Advertisement
Oh, it''s because I am doing something wrong. I didn''t figure that, thank you.

I could print my whole program code here, but nobody would read it.

I can render the textures, and using glTranslate() works. glRotate() has no effect, and I don''t know why. It''s at least not because the whole stuff is setup wrong, or I would neither see a texture nor be able to pan and zoom my scene.

My understanding of glRotate() is that you enter the desired rotation angle (in degrees) and the axis the scene should be rotated around (e.g. glRotated (45,1,0,0) for rotating 45Β° around the X axis). Maybe this is wrong and somebody can enlighten me.
_________karx11erxVisit my Descent site or see my case mod.
I think you will have to post all drawing code...
quote: Original post by karx11erx
I could print my whole program code here, but nobody would read it.

If you posted the whole code, chances are noone would bother readig it, that''s correct. Therefore, don''t post the whole code but only the relevant parts of it. Opening a window, loading images from file, sound and keyboard/mouse code, for example, are not relevant parts in this case. As Three Penguin said, we need code.

quote:
My understanding of glRotate() is that you enter the desired rotation angle (in degrees) and the axis the scene should be rotated around (e.g. glRotated (45,1,0,0) for rotating 45Β° around the X axis). Maybe this is wrong and somebody can enlighten me.

You got that part correct, so nothing wrong with the theory at least ;-)
Okeys, here's the code (the rotate function is at the very bottom):
static UINT8   rgbBuf [64*64*4];static GLuint  glHandles [910];static UINT8   *glPalette = NULL;static HGLRC   glRC;static CDC     *glDC;typedef struct {   long x, y, z;   } APOINT;   typedef struct {   float x, y, z;   } vms_vector;   #define MAX_SEGMENTS 900   APOINT Vertices [4 * MAX_SEGMENTS];                           /*--------------------------*/BOOL CMineView::GLInit (GLvoid){glShadeModel (GL_SMOOTH);glClearColor (0.0f, 0.0f, 0.0f, 0.0f);glClearDepth (1.0f);glEnable (GL_DEPTH_TEST);glDepthFunc (GL_LEQUAL);glEnable (GL_ALPHA_TEST);glAlphaFunc (GL_GEQUAL, 0.5);   glHint (GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);memset (glHandles, 0, sizeof (glHandles));      return TRUE;}                        /*--------------------------*/BOOL GLInitPalette (GLvoid){if (!glPalette) {   HINSTANCE hInst = AfxGetInstanceHandle ();   HRSRC hFind = FindResource (hInst, PaletteResource (), "RC_DATA");   if (hFind) {      HGLOBAL hGlobal = LoadResource (hInst, hFind);      if (hGlobal)         glPalette = (UINT8 *) LockResource (hGlobal);      }   }return (glPalette != NULL);}                        /*--------------------------*/BOOL GLResizeScene (GLvoid) {if (!GLCreateWindow ()) //make soure there is a rendering DC and RC   return FALSE;CRect rc;   GetClientRect (rc);if (!(rc.Width () && rc.Height ()))   return FALSE;glViewport (rc.left, rc.top, rc.Width (), rc.Height ());glMatrixMode (GL_PROJECTION);glLoadIdentity ();glOrtho (0.0, 1.0, 0.0, 1.0, -1.0, 1.0);glMatrixMode (GL_MODELVIEW);glLoadIdentity ();glMatrixMode (GL_PROJECTION);glLoadIdentity ();gluPerspective (90.0f, (GLfloat) rc.Width () / (GLfloat) rc.Height (), 0.1f, 1000000.0f);glMatrixMode (GL_MODELVIEW);glLoadIdentity ();glTranslated (0, 0, 0, - 500);   //move out far enough to see somethingreturn TRUE;}                        /*--------------------------*/void GLCreateTexture (INT16 nTexture) //load a texture and create an RGBA texture from it{if (!GLInitPalette ())   return;INT16 iTexture = nTexture & ~0xC000; if (!glHandles [iTexture]) {   DefineTexture (nTexture, 0, bmBuf, 0, 0);   DrawAnimDirArrows (nTexture, (UINT8 *) bmBuf);   // create RGBA bitmap from source bitmap   int h, i, j;   for (h = i = 0; i < 64*64; i++) {      j = bmBuf ;<br>      j *= 3;<br>      rgbBuf [h++] = glPalette [j++] << 2;<br>      rgbBuf [h++] = glPalette [j++] << 2;<br>      rgbBuf [h++] = glPalette [j++] << 2;<br>      rgbBuf [h++] = (bmBuf  >= 254) ? 0 : 255; // transparent if >= 254<br>      }<br>   glGenTextures (1, glHandles + iTexture); <br>   glEnable (GL_TEXTURE_2D);<br>   glBindTexture (GL_TEXTURE_2D, glHandles [iTexture]); <br>   glTexImage2D (GL_TEXTURE_2D, 0, GL_RGBA, 64, 64, 0, GL_RGBA, GL_UNSIGNED_BYTE, rgbBuf);<br>   glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);<br>   glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);<br>   }<br>}<br><br>                        /*————————–*/<br><br>void GLRenderTexture (INT16 segnum, INT16 sidenum, INT16 nTexture)<br>{<br>   CDSegment *seg = Segments (segnum);<br>   CDSide *side = seg->sides + sidenum;<br>   uvl *uvls;<br>   double l;<br>   APOINT *a;<br>   static int rotOffs [4] = {0,3,2,1};<br>   int j = rotOffs [(nTexture & 0xC000) >> 14]; //take care of rotated 2ndary texture<br><br>GLCreateTexture (nTexture);<br>glEnable (GL_TEXTURE_2D);<br>glBindTexture (GL_TEXTURE_2D, glHandles [nTexture & ~0xC000]); <br>glBegin (GL_TRIANGLE_FAN);<br>for (int i = 0; i < 4; i++) {<br>   uvls = side->uvls + j;<br>   l = uvls->l / UV_FACTOR;<br>   glColor3d (l,l,l);<br>   //glTexCoord2f (uvMap [j][0], uvMap [j][1]); <br>   glTexCoord2d (uvls->u / 2048.0, uvls->v / 2048.0); <br>   a = Vertices + seg->verts [side_vert [sidenum]];<br>   glVertex3f ((float) a->x, (float) a->y, (float) a->z);<br>   j = (j + 1) % 4;<br>   }<br>glEnd ();<br>}<br><br>                        /*————————–*/<br><br>void GLRenderFace (INT16 segnum, INT16 sidenum)<br>{<br>   CDSegment *seg = Segments (segnum);<br>   CDSide *side = seg->sides + sidenum;<br>   UINT8 wallnum = seg->sides [sidenum].wall_num;<br><br>if (side->tmap_num < 0)<br>   return;<br>CDWall *pWall = (wallnum == 255) ? NULL : Walls (wallnum); //non-static textured faces (i.e. can disappear or be moved through)<br>if ((seg->children [sidenum] > -1) &&<br>    (!pWall || (pWall->type == WALL_OPEN) || ((pWall->type == WALL_CLOAKED) && !pWall->cloak_value)))<br>   return;<br>APOINT& p0 = Vertices [seg->verts [side_vert [sidenum] [0]]];<br>APOINT& p1 = Vertices [seg->verts [side_vert [sidenum] [1]]];<br>APOINT& p3 = Vertices [seg->verts [side_vert [sidenum] [3]]];<br><br>vms_vector a,b;<br>a.x = p1.x - p0.x;<br>a.y = p1.y - p0.y;<br>b.x = p3.x - p0.x;<br>b.y = p3.y - p0.y;<br>if (a.x*b.y > a.y*b.x) //only render rearward cube faces<br>   return;<br>GLRenderTexture (segnum, sidenum, side->tmap_num);<br>if (side->tmap_num2) //has a secondary (overlaid) texture<br>   GLRenderTexture (segnum, sidenum, side->tmap_num2);<br>}<br><br>                        /*————————–*/<br><br>BOOL GLRenderScene (GLvoid)   <br>{<br>if (!GLResizeScene ()) // make sure the viewport covers the windows client area<br>   return FALSE;<br>glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);<br>glLoadIdentity ();<br>for (INT16 segnum = SegCount (); segnum–; )<br>   for (INT16 sidenum = 0; sidenum < 6; sidenum++)<br>      GLRenderFace (segnum, sidenum);<br>return TRUE;<br>}<br><br>                        /*————————–*/<br><br>GLvoid GLReset (GLvoid)<br>{<br>glDeleteTextures (910, glHandles);<br>memset (glHandles, 0, sizeof (glHandles));<br>glPalette = NULL;<br>}<br><br>                        /*————————–*/<br><br>GLvoid GLKillWindow (GLvoid)<br>{<br>GLReset ();<br>if (glRC) {<br>   if (!wglMakeCurrent (NULL,NULL))<br>      ErrorMsg ("OpenGL: Release of DC and RC failed.");<br>   if (!wglDeleteContext (glRC))<br>      ErrorMsg ("OpenGL: Release of rendering context failed.");<br>      glRC = NULL;<br>   }<br>if (glDC && !glDC->ReleaseDC ())    {<br>   ErrorMsg ("OpenGL: Release of device context failed.");<br>   glDC = NULL;   <br>   }<br>}<br><br>                        /*————————–*/<br><br>BOOL GLCreateWindow (CDC * pDC)<br>{<br>if (glDC)<br>   return TRUE;<br><br>   GLuint PixelFormat;<br><br>static PIXELFORMATDESCRIPTOR pfd = {<br>   sizeof(PIXELFORMATDESCRIPTOR),<br>   1,   <br>   PFD_DRAW_TO_WINDOW |   PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER,<br>   PFD_TYPE_RGBA,<br>   8,<br>   0, 0, 0, 0, 0, 0,<br>   0,   <br>   0,<br>   0,<br>   0, 0, 0, 0,<br>   16,<br>   0,<br>   0,<br>   PFD_MAIN_PLANE,<br>   0,<br>   0, 0, 0<br>   };<br><br>if (!(glDC = GetDC ())) { //grap a DC from the parent window<br>   GLKillWindow ();<br>   ErrorMsg ("OpenGL: Can't create device context.");<br>   return FALSE;<br>   }<br>glDC->m_hDC = glDC->hDC;<br>if (!(PixelFormat = ChoosePixelFormat (glDC->m_hDC, &pfd))) {<br>   GLKillWindow ();<br>   sprintf (message, "OpenGL: Can't find a suitable pixel format. (%d)", GetLastError ());<br>   ErrorMsg (message);<br>   return FALSE;<br>   }<br>if(!SetPixelFormat(glDC->m_hDC, PixelFormat, &pfd)) {<br>      GLKillWindow();<br>      sprintf (message, "OpenGL: Can't set the pixel format (%d).", GetLastError ());<br>      ErrorMsg (message);<br>      return FALSE;<br>   }<br>if (!(glRC = wglCreateContext (glDC->m_hDC))) {<br>   GLKillWindow ();<br>   sprintf (message, "OpenGL: Can't create a rendering context (%d).", GetLastError ());<br>   ErrorMsg (message);<br>   return FALSE;<br>   }<br><br>if(!wglMakeCurrent (glDC->m_hDC, glRC)) {<br>   GLKillWindow ();<br>   sprintf (message, "OpenGL: Can't activate the rendering context (%d).", GetLastError ());<br>   ErrorMsg (message);<br>   return FALSE;<br>   }<br>if (!GLInit())   {<br>   GLKillWindow ();<br>   ErrorMsg ("OpenGL: Initialization failed.");<br>   return FALSE;<br>   }<br>return TRUE;<br>}<br>                        /*————————–*/<br><br>void GLPan (char direction, INT32 incr)<br>{<br>   static float xPan = 0;<br>   static float yPan = 0;<br>   static float yPan = 0;<br><br>if (incr) {<br>   switch(direction) {<br>      default:<br>      case 'X': <br>         m_xPan += incr;<br>         break;<br>      case 'Y': <br>         yPan += incr; <br>         break;<br>      case 'Z': <br>         zPan += incr; <br>         break;<br>      }<br>glTranslated (xPan, yPan, zPan);<br>GLRenderScene ();<br>}<br><br>                        /*————————–*/<br><br>void GLRotate(char direction, double angleIncr)<br>{<br>   static double angle = 0;<br>angleIncr *= 180.0 / PI; //angleIncr comes in radians<br>angle += angleIncr;<br>if (angle <= -360)<br>   angle += 360;<br>else if (a >= 360)<br>   angle -= 360;<br>switch(direction) {<br>   default:<br>   case 'X': <br>      glRotated (angle, 1.0, 0.0, 0.0);<br>      break;<br>   case 'Y': <br>      glRotated (angle, 0.0, 1.0, 0.0);<br>      break;<br>   case 'Z': <br>      glRotated (angle, 0.0, 0.0, 1.0);<br>      break;<br>   }<br>GLRenderScene (); //well … nothing changes here<br>} </pre>        <br><br><SPAN CLASS=editedby>[edited by - karx11erx on May 15, 2004 4:56:49 PM]</SPAN>
_________karx11erxVisit my Descent site or see my case mod.
Advertisement
How can even glTranslate work? You always set the curent matrix to identity in GLRenderScene.
btw. please read the FAQ about how to post code in a readable manner.
I told you to post relevant parts only, and yet more than half the code is texture, window and vector code. Everything that is not absolutely necessary to demonstrate the problem is not relevant.

The code is not that easy to follow due to all the unecessary stuff, but it looks to me the rotation, and any other previous transformation, is immediately cancelled in GLRenderScene.
Thanks for trying to help.

I can''t post what''s relevant if I cannot judge what''s relevant (/me == OpenGL n00b). Imo my code is very readable and not overly long. I''ve got all the code from NeHe tutorials btw, including the placement of the glLoadIdentity() calls.

I''ve found out myself however what went wrong. It had just irritated me that zooming/panning worked, but rotating not.

GLPan and GLRotate will now store angles and displacements in a few global variables, and GLRenderScene() calls glTranslated() and glRotated() with these values.

The next problem is that my scene gets badly distorted (the faces appear stretched or shrunk) when rotating it. Perspective problem?
_________karx11erxVisit my Descent site or see my case mod.
Doesn''t the rotation angle have to be in radians?

This topic is closed to new replies.

Advertisement