Texture rotation

Programmers discuss here anything related to FreeOrion programming. Primarily for the developers to discuss.

Moderator: Committer

Message
Author
User avatar
Flatline
Space Squid
Posts: 64
Joined: Tue Jan 29, 2008 2:06 pm
Location: Oxford (UK)

Texture rotation

#1 Post by Flatline »

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 [email protected] with NVidia GeForce GO 7600

User avatar
Geoff the Medio
Programming, Design, Admin
Posts: 12720
Joined: Wed Oct 08, 2003 1:33 am
Location: Munich

Re: Texture rotation

#2 Post by Geoff the Medio »

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: Select all

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.

User avatar
Flatline
Space Squid
Posts: 64
Joined: Tue Jan 29, 2008 2:06 pm
Location: Oxford (UK)

Re: Texture rotation

#3 Post by Flatline »

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 [email protected] with NVidia GeForce GO 7600

User avatar
loonycyborg
Compilation Expert
Posts: 219
Joined: Thu Jul 06, 2006 10:30 pm
Location: Russia/Moscow

Re: Texture rotation

#4 Post by loonycyborg »

You can rotate textures using texture matrix:

Code: Select all

glMatrixMode(GL_TEXTURE);
glTranslatef( ... );
glRotatef( ... );
In Soviet Russia, forum posts YOU!!

User avatar
Flatline
Space Squid
Posts: 64
Joined: Tue Jan 29, 2008 2:06 pm
Location: Oxford (UK)

Re: Texture rotation

#5 Post by Flatline »

Ok that is whai I did (the for is on line 1256 of MapWnd.cpp - method RenderBackgrounds()):

Code: Select all

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 [email protected] with NVidia GeForce GO 7600

User avatar
loonycyborg
Compilation Expert
Posts: 219
Joined: Thu Jul 06, 2006 10:30 pm
Location: Russia/Moscow

Re: Texture rotation

#6 Post by loonycyborg »

glRotate has no effect within glBegin() - glEnd().
In Soviet Russia, forum posts YOU!!

User avatar
Flatline
Space Squid
Posts: 64
Joined: Tue Jan 29, 2008 2:06 pm
Location: Oxford (UK)

Re: Texture rotation

#7 Post by Flatline »

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 [email protected] with NVidia GeForce GO 7600

m_k
Space Floater
Posts: 33
Joined: Wed Mar 12, 2008 10:54 am
Location: Aachen, Germany

Re: Texture rotation

#8 Post by m_k »

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.

User avatar
loonycyborg
Compilation Expert
Posts: 219
Joined: Thu Jul 06, 2006 10:30 pm
Location: Russia/Moscow

Re: Texture rotation

#9 Post by loonycyborg »

Just call glLoadIdentity() after you've done with rotated textures.
In Soviet Russia, forum posts YOU!!

User avatar
Flatline
Space Squid
Posts: 64
Joined: Tue Jan 29, 2008 2:06 pm
Location: Oxford (UK)

Re: Texture rotation

#10 Post by Flatline »

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 [email protected] with NVidia GeForce GO 7600

User avatar
loonycyborg
Compilation Expert
Posts: 219
Joined: Thu Jul 06, 2006 10:30 pm
Location: Russia/Moscow

Re: Texture rotation

#11 Post by loonycyborg »

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: Select all

glMatrixMode(GL_TEXTURE);
glPushMatrix();
glRotate(...);

//draw

glPopMatrix();
But previous texture matrix most likely will be identity...
In Soviet Russia, forum posts YOU!!

User avatar
Flatline
Space Squid
Posts: 64
Joined: Tue Jan 29, 2008 2:06 pm
Location: Oxford (UK)

Re: Texture rotation

#12 Post by Flatline »

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: Select all

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: Select all

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 [email protected] with NVidia GeForce GO 7600

User avatar
pd
Graphics Lead Emeritus
Posts: 1924
Joined: Mon Mar 08, 2004 6:17 pm
Location: 52°16'N 10°31'E

Re: Texture rotation

#13 Post by pd »

I'm not sure why we would want to have the background rotating. Is there any chance I could see this somehow?

User avatar
Flatline
Space Squid
Posts: 64
Joined: Tue Jan 29, 2008 2:06 pm
Location: Oxford (UK)

Re: Texture rotation

#14 Post by Flatline »

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 [email protected] with NVidia GeForce GO 7600

User avatar
pd
Graphics Lead Emeritus
Posts: 1924
Joined: Mon Mar 08, 2004 6:17 pm
Location: 52°16'N 10°31'E

Re: Texture rotation

#15 Post by pd »

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.

Post Reply