FreeOrion

Forums for the FreeOrion project
It is currently Thu Jun 20, 2013 6:03 am

All times are UTC




Post new topic Reply to topic  [ 50 posts ]  Go to page 1, 2, 3, 4  Next
Author Message
 Post subject: Texture rotation
PostPosted: Sat Mar 15, 2008 2:14 pm 
Offline
Space Squid
User avatar

Joined: Tue Jan 29, 2008 2:06 pm
Posts: 64
Location: Oxford (UK)
Hi
I was thinking on how to render the effect of the galaxy rotating (i.e. making the three BG rotate at diferent speeds) as suggested on sourceforge. The GG texture has only an orthoblit method, and I couldn't find anything related to "texture rotation".

...probably it doesn't make really much sense, a texture is something applied to a 3d object, so it should be the object to rotate... but in our case the tex is simply drawn on the bg. Is there anything already done in GG to rotate images/textures, or the whole bg drawing stuff should be rewritten to work with two texturized planes?
And in the latter case, don't you think that it could be a bit "processor intensive"?

_________________
The only difference between a suicide and a martyrdom is press coverage.
- Chuck Palahniuk (Survivor)

Ubuntu 7.10/Windows Vista/Windows XP (VMWare)/Windows NT 4 (VMWare) on Intel Centrino Duo T5500@1.66GHz with NVidia GeForce GO 7600


Top
 Profile  
 
 Post subject: Re: Texture rotation
PostPosted: Sat Mar 15, 2008 3:02 pm 
Offline
Programming, Design, and De Facto Lead
User avatar

Joined: Wed Oct 08, 2003 1:33 am
Posts: 8062
Location: Vancouver, BC
There is no rotation built into GG::Texture, but you can probably extract the OpenGL texture and use GL commands to do arbitrary rotations and scaling (or 3D texture mapping).

In an appropriate Render() function, the code would probably look something like:
Code:
glBindTexture(GL_TEXTURE_2D, texture->OpenGLId());
glBegin(GL_TRIANGLE_STRIP);
glTexCoord2f(0.0f, 0.0f); glVertex2i(pt1.x, pt1.y);
glTexCoord2f(1.0f, 0.0f); glVertex2i(pt2.x, pt1.y);
glTexCoord2f(1.0f, 1.0f); glVertex2i(pt3.x, pt3.y);
glTexCoord2f(0.0f, 1.0f); glVertex2i(pt4.x, pt4.y);
glEnd();

where either the 2D vertices pt1 through pt4 are screen coordinates to put the corners of the texture. If you appropriately place these vertices, I suspect a rotated (or stretched or skewed or scaled) texture will result. You might also need to set some OpenGL scaling or filtering stuff as well, either to get this to work at all, or to get it looking semi-decent.

Similar code should function for nebula rotation, incidentally.

Note that I know very little about OpenGL and haven't tested any of this.


Top
 Profile  
 
 Post subject: Re: Texture rotation
PostPosted: Sat Mar 15, 2008 3:55 pm 
Offline
Space Squid
User avatar

Joined: Tue Jan 29, 2008 2:06 pm
Posts: 64
Location: Oxford (UK)
Geoff the Medio wrote:
Note that I know very little about OpenGL and haven't tested any of this.


Note that I don't know a thing about OpenGL :D
Ok, I see what I can do ;)

_________________
The only difference between a suicide and a martyrdom is press coverage.
- Chuck Palahniuk (Survivor)

Ubuntu 7.10/Windows Vista/Windows XP (VMWare)/Windows NT 4 (VMWare) on Intel Centrino Duo T5500@1.66GHz with NVidia GeForce GO 7600


Top
 Profile  
 
 Post subject: Re: Texture rotation
PostPosted: Sat Mar 15, 2008 3:57 pm 
Offline
Compilation Expert
User avatar

Joined: Thu Jul 06, 2006 10:30 pm
Posts: 219
Location: Russia/Moscow
You can rotate textures using texture matrix:
Code:
glMatrixMode(GL_TEXTURE);
glTranslatef( ... );
glRotatef( ... );

_________________
In Soviet Russia, forum posts YOU!!


Top
 Profile  
 
 Post subject: Re: Texture rotation
PostPosted: Sat Mar 15, 2008 4:20 pm 
Offline
Space Squid
User avatar

Joined: Tue Jan 29, 2008 2:06 pm
Posts: 64
Location: Oxford (UK)
Ok that is whai I did (the for is on line 1256 of MapWnd.cpp - method RenderBackgrounds()):

Code:
for (int i = 0; i < NUM_BACKGROUNDS; ++i) {
       
       // TODO: Added by Flatline - Galaxy rotation
       glBindTexture(GL_TEXTURE_2D, m_backgrounds[i]->OpenGLId());
       glMatrixMode(GL_TEXTURE);
       glBegin(GL_TRIANGLE_STRIP);
       switch (i%3){
       case 0: // don't rotate
          break;
       case 1: // rotate CW
          glRotatef(-0.1,0,0,1);
          break;
       case 2:
          glRotatef(-0.1,0,0,1);
          break;
       }
       glEnd();
        //////////////////////////

       ....drawing code.....


It looks like it doesn't do anything. Is there anything wrong in that code? Meanwhile I'm going to check what GG's OrthoBlit does ;)

_________________
The only difference between a suicide and a martyrdom is press coverage.
- Chuck Palahniuk (Survivor)

Ubuntu 7.10/Windows Vista/Windows XP (VMWare)/Windows NT 4 (VMWare) on Intel Centrino Duo T5500@1.66GHz with NVidia GeForce GO 7600


Top
 Profile  
 
 Post subject: Re: Texture rotation
PostPosted: Sat Mar 15, 2008 4:29 pm 
Offline
Compilation Expert
User avatar

Joined: Thu Jul 06, 2006 10:30 pm
Posts: 219
Location: Russia/Moscow
glRotate has no effect within glBegin() - glEnd().

_________________
In Soviet Russia, forum posts YOU!!


Top
 Profile  
 
 Post subject: Re: Texture rotation
PostPosted: Sat Mar 15, 2008 4:46 pm 
Offline
Space Squid
User avatar

Joined: Tue Jan 29, 2008 2:06 pm
Posts: 64
Location: Oxford (UK)
loonycyborg wrote:
glRotate has no effect within glBegin() - glEnd().


Ok, I commented the glbegin/end stuff, and it works... the only problem is that EVERYTHING rotates (resource symbols, text,nebulas...) how can I limit the rotation to only three precise textures?

_________________
The only difference between a suicide and a martyrdom is press coverage.
- Chuck Palahniuk (Survivor)

Ubuntu 7.10/Windows Vista/Windows XP (VMWare)/Windows NT 4 (VMWare) on Intel Centrino Duo T5500@1.66GHz with NVidia GeForce GO 7600


Top
 Profile  
 
 Post subject: Re: Texture rotation
PostPosted: Sat Mar 15, 2008 4:56 pm 
Offline
Space Floater

Joined: Wed Mar 12, 2008 10:54 am
Posts: 33
Location: Aachen, Germany
Based on my understanding of OpenGl, things like glRotate or glTranslate apply their translation to the current matrix (which defines how all things thereafter are viewed, and so on). As far as i know you will have to rotate, do your texture stuff and then rotate back to your original position before doing anything else, but I could be wrong.


Top
 Profile  
 
 Post subject: Re: Texture rotation
PostPosted: Sat Mar 15, 2008 5:04 pm 
Offline
Compilation Expert
User avatar

Joined: Thu Jul 06, 2006 10:30 pm
Posts: 219
Location: Russia/Moscow
Just call glLoadIdentity() after you've done with rotated textures.

_________________
In Soviet Russia, forum posts YOU!!


Top
 Profile  
 
 Post subject: Re: Texture rotation
PostPosted: Sat Mar 15, 2008 5:27 pm 
Offline
Space Squid
User avatar

Joined: Tue Jan 29, 2008 2:06 pm
Posts: 64
Location: Oxford (UK)
loonycyborg wrote:
Just call glLoadIdentity() after you've done with rotated textures.


This command just replaces the current rotation matrix with the identity matrix... I need something like "ok now I have rotated stuff, draw it on screen", so that after this I can reset the matrix and restore the normal flow of the program.

_________________
The only difference between a suicide and a martyrdom is press coverage.
- Chuck Palahniuk (Survivor)

Ubuntu 7.10/Windows Vista/Windows XP (VMWare)/Windows NT 4 (VMWare) on Intel Centrino Duo T5500@1.66GHz with NVidia GeForce GO 7600


Top
 Profile  
 
 Post subject: Re: Texture rotation
PostPosted: Sat Mar 15, 2008 5:57 pm 
Offline
Compilation Expert
User avatar

Joined: Thu Jul 06, 2006 10:30 pm
Posts: 219
Location: Russia/Moscow
Assuming that OrthoBlit does actual rendering, if you rotate texture matrix before calling it and reset the matrix after calling it, then only these calls will be affected by rotation.

If you want to restore previous texture matrix you can use glPushMatrix - glPopMatrix:
Code:
glMatrixMode(GL_TEXTURE);
glPushMatrix();
glRotate(...);

//draw

glPopMatrix();

But previous texture matrix most likely will be identity...

_________________
In Soviet Russia, forum posts YOU!!


Top
 Profile  
 
 Post subject: Re: Texture rotation
PostPosted: Sat Mar 15, 2008 6:22 pm 
Offline
Space Squid
User avatar

Joined: Tue Jan 29, 2008 2:06 pm
Posts: 64
Location: Oxford (UK)
Ok, it works. It was my fault (obliviously :D), I tought OpenGL worked with relative rotations, and instead it uses absolute one, so just a "static" had to be added ;)

If you wanna try that's the diff:
Code:
Index: /home/riccardo/code/workspace/freeorion/FreeOrion/UI/MapWnd.cpp
===================================================================
--- /home/riccardo/code/workspace/freeorion/FreeOrion/UI/MapWnd.cpp   (revision 2396)
+++ /home/riccardo/code/workspace/freeorion/FreeOrion/UI/MapWnd.cpp   (working copy)
@@ -1251,9 +1251,27 @@

void MapWnd::RenderBackgrounds()
{
-    double x, y;
+   double x, y;
     glColor3d(1.0, 1.0, 1.0);
+    static float rotate_factor=0;
     for (int i = 0; i < NUM_BACKGROUNDS; ++i) {
+       
+       // TODO: Added by Flatline - Galaxy rotation
+       rotate_factor+=0.01;
+       glMatrixMode(GL_TEXTURE);
+       switch (i%3){
+       case 0: // don't rotate
+          break;
+       case 1: // rotate CW
+          glRotatef(rotate_factor,0,0,1);
+          break;
+       case 2:
+          glRotatef(-rotate_factor,0,0,1);
+          break;
+       }
+       /////////////////////////////////////
+       
+       
         int bg_width = m_backgrounds[i]->Width();
         int bg_height = m_backgrounds[i]->Height();
         int app_width = GG::GUI::GetGUI()->AppWidth();
@@ -1267,6 +1285,8 @@
             }
             x += m_backgrounds[i]->Width();
         }
+        // TODO: Added by Flatline - Galaxy rotation
+        glLoadIdentity();
     }

     for (unsigned int i = 0; i < m_nebulae.size(); ++i) {


(You need just to add the static definition of rotate_factor ouside the loop, the main block of code before the:
Code:
int bg_width = m_backgrounds[i]->Width();
int bg_height = m_backgrounds[i]->Height();
...

block, and the glLoadIdentity() before the Nebulae's loop)

Try it and let me know ;)

PS: perhaps the rotation is a little bit too fast? In case just edit "rotate_factor+=0.01;" with a smaller value...

_________________
The only difference between a suicide and a martyrdom is press coverage.
- Chuck Palahniuk (Survivor)

Ubuntu 7.10/Windows Vista/Windows XP (VMWare)/Windows NT 4 (VMWare) on Intel Centrino Duo T5500@1.66GHz with NVidia GeForce GO 7600


Top
 Profile  
 
 Post subject: Re: Texture rotation
PostPosted: Sat Mar 15, 2008 7:28 pm 
Offline
Graphics Lead Emeritus
User avatar

Joined: Mon Mar 08, 2004 6:17 pm
Posts: 1933
Location: 52°16'N 10°31'E
I'm not sure why we would want to have the background rotating. Is there any chance I could see this somehow?


Top
 Profile  
 
 Post subject: Re: Texture rotation
PostPosted: Sat Mar 15, 2008 7:34 pm 
Offline
Space Squid
User avatar

Joined: Tue Jan 29, 2008 2:06 pm
Posts: 64
Location: Oxford (UK)
pd wrote:
I'm not sure why we would want to have the background rotating. Is there any chance I could see this somehow?


Ok, I explain why I did it.

I didn't read with enough attention the request on SF :oops:

It said that we wanted "randomly rotated nebulae" to break graphic repetition...
When I saw that I thought I was going to drop the code, but hey, they're just a few lines, and I think the effect is cool :D
(I', right now working on doing the *right* thing, i.e. randomly rotating nebulae :D)

If you want to see it in action just add the lines in MapWnd.cpp and re-scons it :)

_________________
The only difference between a suicide and a martyrdom is press coverage.
- Chuck Palahniuk (Survivor)

Ubuntu 7.10/Windows Vista/Windows XP (VMWare)/Windows NT 4 (VMWare) on Intel Centrino Duo T5500@1.66GHz with NVidia GeForce GO 7600


Top
 Profile  
 
 Post subject: Re: Texture rotation
PostPosted: Sat Mar 15, 2008 7:46 pm 
Offline
Graphics Lead Emeritus
User avatar

Joined: Mon Mar 08, 2004 6:17 pm
Posts: 1933
Location: 52°16'N 10°31'E
Flatline wrote:
If you want to see it in action just add the lines in MapWnd.cpp and re-scons it :)


This might be a problem for me, working on windows and having no coding and compiling experience at all. If it isn't too much trouble for you, could you perhaps record a short video showing the effect?

I have some other suggestions regarding the background stars, that I'm gonna post soon, if you are interested in working on those.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 50 posts ]  Go to page 1, 2, 3, 4  Next

All times are UTC


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group