glRotatef let my triangle grow!

Tutorials concerning the OpenGL® ES cross-platform API for full-function 2D and 3D graphics on the Google-Android platform.

glRotatef let my triangle grow!

Postby WarrenFaith » Mon Aug 17, 2009 2:10 pm

Hey,

i started playing with OpenGL and was wondering about a growing of my triangle when I let it rotate....
To see it better I rotate at an angle and than at the same angle in the other direction. So normally nothing should be visible but you can see my triangle growing!

My onDrawFrame method:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. @Override
  2.  
  3. public void onDrawFrame(GL10 gl) {
  4.  
  5.     // define the color we want to be displayed as the "clipping wall"
  6.  
  7.     gl.glClearColor(_red, _green, _blue, 1.0f);
  8.  
  9.    
  10.  
  11.     // reset the matrix - good to fix the rotation to a static angle
  12.  
  13.     //gl.glLoadIdentity();
  14.  
  15.    
  16.  
  17.     // clear the color buffer and the depth buffer to show the ClearColor
  18.  
  19.     // we called above...
  20.  
  21.     gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
  22.  
  23.    
  24.  
  25.     gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
  26.  
  27.    
  28.  
  29.     initTriangle();
  30.  
  31.     gl.glColor4f(0.5f, 0f, 0f, 0.5f);
  32.  
  33.     gl.glVertexPointer(3, GL10.GL_FLOAT, 0, _vertexBuffer);
  34.  
  35.     gl.glDrawElements(GL10.GL_TRIANGLES, _nrOfVertices, GL10.GL_UNSIGNED_SHORT, _indexBuffer);
  36.  
  37.    
  38.  
  39.     // set rotation
  40.  
  41.     gl.glRotatef(100, 0f, 1f, 0f);
  42.  
  43.     gl.glRotatef(-100, 0f, 1f, 0f);
  44.  
  45.    
  46.  
  47.     initStaticTriangle();
  48.  
  49.     gl.glColor4f(0f, 0.5f, 0f, 0.5f);
  50.  
  51.     gl.glVertexPointer(3, GL10.GL_FLOAT, 0, _vertexBufferStatic);
  52.  
  53.     gl.glDrawElements(GL10.GL_TRIANGLES, _nrOfVertices, GL10.GL_UNSIGNED_SHORT, _indexBufferStatic);
  54.  
  55. }
Parsed in 0.033 seconds, using GeSHi 1.0.8.4

Some ideas?
Attachments
vortex.zip
source code
(3.72 KiB) Downloaded 120 times
big-triangle.png
Grown triangle after 20 seconds
big-triangle.png (11.26 KiB) Viewed 2095 times
small-triangle.png
Normal triangle after application start
small-triangle.png (10.73 KiB) Viewed 2095 times
WarrenFaith
Moderator
Moderator
 
Posts: 227
Joined: Fri Mar 13, 2009 10:59 am
Location: Berlin, Germany

Top

OpenGL rotation is matrix bound

Postby cg0601 » Tue Aug 18, 2009 5:51 pm

I am far from an OpenGL person, so here is an attempt to explain what went "wrong".

When you call an action on a matrix, like glTranslate() and glRotate(), the actions are done using real world coordinates, around the real world axis.
So in the first rotate glRotate(100, 0, 1, 0); you turn your triangle around the Y-axis for 100 degrees. This causes that your triangle will be in a different position and the Y-axis will not always go through the same point in the triangle anymore as before. I think this was your idea. The next rotate(-100) will rotate again around the Y-axis, but the Y-axis is not in the middle of your triangle.
Without having looked at the code it is kind of hard for me to tell where it is, but the result is not surprising.

If your goal is to add different rotations, you could do something like:

gl.glLoadIdentity();
float rot1 = 100.0f;
float rot2 = -100.0f;
gl.glRotatef(rot1 + rot2, 0, 1, 0);

If you want to abtain something different explain it here and maybe I can help you find the correct code.
cg0601
Junior Developer
Junior Developer
 
Posts: 16
Joined: Tue Aug 04, 2009 8:53 am

Top

Return to Android 2D/3D Graphics - OpenGL Tutorials

Who is online

Users browsing this forum: No registered users and 2 guests