[Gambas-user] gb3: OpenGL, rotation and translation of matrix affecting multiple quads

tommyline at ...2340... tommyline at ...2340...
Tue Aug 9 08:02:55 CEST 2011


Hi Kevin,
You can't do this in one rotatef() function.

You have to rotate every quad separately, using sequence:
gl.Loadidentity - to reset matrix at 0,0,0
for each quad do
gl.pushmatrix() - save the global matrix
gl.translatei() - move to the center of each quad
gl.rotatef(angle,x,y,z) - rotate each quad
draw each quad
gl.popmatrix() - back to the 0,0,0 origin.

Tomek.



----- Original Message -----
From: "Kevin Fishburne" <kevinfishburne at ...1887...>
To: gambas-user at lists.sourceforge.net
Sent: Tuesday, 9 August, 2011 2:44:10 AM
Subject: [Gambas-user] gb3: OpenGL, rotation and translation of matrix affecting multiple quads

Here's looking at you Tomek, although anyone who knows give me a shout out.

I'm using a nested For...Next loop to render a 2D grid of quads. I need 
the grid of quads to be rotated about its "center", but would like to 
make a single matrix transformation instead of doing one for each quad. 
Is this possible, and if so, how? Even if I have to do multiple matrix 
transformations, that is fine, as long as I don't have to do one for 
every single quad.

Right now my code (removing all experiments, just bare code) looks like 
this:

Public Sub Tile_Grid()

   ' Draw tile grid texture array in render window.

   ' General declarations.
   Dim TileGridX As Short  ' Tile in tile grid texture array being rendered.
   Dim TileGridY As Short  ' Tile in tile grid texture array being rendered.
   Dim TileGridX1 As Short ' Starting tile in tile grid texture array to 
be rendered.
   Dim TileGridY1 As Short ' Starting tile in tile grid texture array to 
be rendered.
   Dim TileGridX2 As Short ' Ending tile in tile grid texture array to 
be rendered.
   Dim TileGridY2 As Short ' Ending tile in tile grid texture array to 
be rendered.
   Dim PixelX As Short     ' Pixel coordinates of tile being rendered.
   Dim PixelY As Short     ' Pixel coordinates of tile being rendered.
   Dim OffsetX As Short    ' Number of pixels to offset matrix to 
accommodate camera position and tile grid resolution.
   Dim OffsetY As Short    ' Number of pixels to offset matrix to 
accommodate camera position and tile grid resolution.
   Dim StepX As Short      ' Direction to step through tile grid texture 
array loop.
   Dim StepY As Short      ' Direction to step through tile grid texture 
array loop.

   ' Assign initial values to variables.
   TileGridX1 = TileGrid.CenterX
   TileGridY1 = TileGrid.CenterY
   TileGridX2 = TileGrid.CenterX + TileGrid.Size - 1
   TileGridY2 = TileGrid.CenterY + TileGrid.Size - 1
   If TileGridX1 < TileGridX2 Then
     StepX = 1
   Else
     StepX = -1
   Endif
   If TileGridY1 < TileGridY2 Then
     StepY = 1
   Else
     StepY = -1
   Endif

   ' Rotate and translate matrix.
   Gl.LoadIdentity()
   Gl.Translatef(- OffsetX, - OffsetY, 0)
   Gl.Rotatef(- Client.Orientation, 0, 0, 1)
   Gl.Translatef(OffsetX, OffsetY, 0)

   ' Render specified tiles in cell grid to tile grid.
   For TileGridY = TileGridY1 To TileGridY2 Step StepY
     For TileGridX = TileGridX1 To TileGridX2 Step StepX
       ' Select texture using its ID.
       Gl.BindTexture(Gl.GL_TEXTURE_2D, tTileGrid[Convert.Wrap_Short(0, 
TileGrid.Size - 1, TileGridX), Convert.Wrap_Short(0, TileGrid.Size - 1, 
TileGridY)][0])
       ' Create the quad the texture is drawn on.
       Gl.Begin(Gl.GL_QUADS)
         ' Bottom-left vertex.
         Gl.TexCoord2i(0, 0)
         Gl.Vertex3i(PixelX + OffsetX, PixelY + OffsetY, 0)
         ' Bottom-right vertex.
         Gl.TexCoord2i(1, 0)
         Gl.Vertex3i(PixelX + OffsetX + 128, PixelY + OffsetY, 0)
         ' Top-right vertex.
         Gl.TexCoord2i(1, 1)
         Gl.Vertex3i(PixelX + OffsetX + 128, PixelY + OffsetY + 128, 0)
         ' Top-left vertex.
         Gl.TexCoord2i(0, 1)
         Gl.Vertex3i(PixelX + OffsetX, PixelY + OffsetY + 128, 0)
       Gl.End()
       ' Adjust pixel position.
       PixelX = PixelX + 128
       If PixelX = TileGrid.Size * 128 Then PixelX = 0
     Next
     ' Adjust pixel positions.
     PixelX = 0
     PixelY = PixelY + 128
     If PixelY = TileGrid.Size * 128 Then PixelY = 0
   Next

   ' Reset matrices so subsequent SDL writes won't get botched.
   Gl.LoadIdentity()

End

Notice I didn't assign values to OffsetX or OffsetY. Didn't want to 
include any failed experiments. I also attached the code for better 
legibility.

I can offset the position of the quads easily using Gl.Translatef, but 
the group ALWAYS seems to rotate with the upper-left corner as the 
origin. I need to be able to control the origin. I can do this easily 
with a single quad, but seem to have trouble doing it "globally" with 
the For...Next loops of quads.

-- 
Kevin Fishburne
Eight Virtues
www:http://sales.eightvirtues.com
e-mail:sales at ...1887...
phone: (770) 853-6271


------------------------------------------------------------------------------
uberSVN's rich system and user administration capabilities and model 
configuration take the hassle out of deploying and managing Subversion and 
the tools developers use with it. Learn more about uberSVN and get a free 
download at:  http://p.sf.net/sfu/wandisco-dev2dev

_______________________________________________
Gambas-user mailing list
Gambas-user at lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/gambas-user




More information about the User mailing list