PDA

View Full Version : Translation of a texture



Erin C.
11-05-2008, 01:55 PM
Hi there,
I hope someone can help with a problem I'm having. First of all, in my assignment I have to create a simple 2D game. So far, I've been able to draw a line using Bresenham's algorith, and I have it rotating around the x-axis to make it like a radar beam.

Now, I have added a texture from a bitmap. See elow.


void drawUFO()
{

glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, Texture[0]);
glBegin(GL_QUADS);
glTexCoord2f(0,0); glVertex3f(-300,0,0);
glTexCoord2f(1,0); glVertex3f(-250,0,0);
glTexCoord2f(1,-1); glVertex3f(-250,-50,0);
glTexCoord2f(0,-1); glVertex3f(-300,-50,0);
glEnd();
glFlush();
glDisable(GL_TEXTURE_2D);
glutPostRedisplay();
}


Now, I need to make this texture move across the screen. I assume I need to use something like glTranslatef. I've tried variations of things like below, but either the screen just flashes, or nothing is displayed.



void drawUFO()
{
int xCoor=-150;

glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, Texture[0]);
glTranslatef(xCoor, 0,0);
glBegin(GL_QUADS);
glTexCoord2f(0,0); glVertex3f(-300,0,0);
glTexCoord2f(1,0); glVertex3f(-250,0,0);
glTexCoord2f(1,-1); glVertex3f(-250,-50,0);
glTexCoord2f(0,-1); glVertex3f(-300,-50,0);
glEnd();
glFlush();
glDisable(GL_TEXTURE_2D);
xCoor++;
glutSwapBuffers();
glutPostRedisplay();
}


It's quite possible that the problems lies in other areas of my code....

Erin C.
11-05-2008, 01:58 PM
FYI -

main.cpp (http://webpages.uncc.edu/~eahenric/main.cpp)
game.cpp (http://webpages.uncc.edu/~eahenric/game.cpp)

IneQuation.pl
11-05-2008, 02:10 PM
Hmm. I have only worked with GLUT for a little while before realizing it's crap (SDL's GL frontend is way superior, although it requires a bit more knowledge to handle), but I *think* you don't need to use glFlush, glutSwapBuffers and glutPostRedisplay all at the same time. glutSwapBuffers alone should do the trick, try commenting out the other 2 functions.

Also, I'd check those texture coords. Either keep them in the 0..1 range (mind the sign) or make sure GL_WRAP_S and GL_WRAP_T texture params are set to GL_REPEAT, otherwise your sprite might end up looking weird.

Erin C.
11-05-2008, 02:27 PM
Thanks, I fixed the texture coords. I tried commenting out the other stuff, but I'm still receiving a black screen, instead of it drawing anything.

I think it has to do with the fact that I'm trying to "animate" two different functions, maybe? Ahhh, I'm so confused. This is my first time using OpenGL & GLUT - I'm sure that's obvious.

IneQuation.pl
11-05-2008, 02:55 PM
1 thing worth doing - set display() as the glutIdleFunc as well (glutIdleFunc(display) in your main()), because AFAIK the display callback (glutDisplayFunc) is only called when window manager events are processed, and these don't happen all the time.

I've dug up my first OpenGL test program (first and only to use GLUT ;) ). The routine I used there that works is as follows:

void renderMe(void) {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // clear screen
draw(); // actual drawing commands
glutSwapBuffers(); // dump results to screen
}
So, what I would do is strip all your draw* functions of calls which are not primitive drawing commands (i.e. stuff which is not between a glBegin/glEnd pair) and set up your display() like this:

void display(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // clear screen and depth buffer
// whichever of the primitive drawing functions
drawRadar();
drawBox();
drawUFO();
// dump drawing results to screen
glutSwapBuffers();
}
I'm not sure if that glClear call isn't redundant. Google up GLUT documentation, it's possible that glutSwapBuffers also clears the buffer after dumping it to screen.

In general, you should only swap buffers once a frame. This is the function that actually causes the output of your primitive drawing commands to be displayed on the screen. Having it called multiple times per frame (as you now do) is bound to cause flickering.

Hope this helps.

Erin C.
11-05-2008, 06:21 PM
I finally got something to translate with this, but instead of moving the texture, it moves ALL the graphics. Does it have to do with me translating the texture?



glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, Texture[0]);
glColor3f(1,1,1);
glTranslatef(100,0,0);
glPopMatrix();
glBegin(GL_QUADS);
glTexCoord2f(0,1); glVertex3f(-300,0,0);
glTexCoord2f(1,1); glVertex3f(-250,0,0);
glTexCoord2f(1,0); glVertex3f(-250,-50,0);
glTexCoord2f(0,0); glVertex3f(-300,-50,0);
glEnd();
glPushMatrix();
glDisable(GL_TEXTURE_2D);
drawText();

trinitrotoluene
11-05-2008, 06:38 PM
I finally got something to translate with this, but instead of moving the texture, it moves ALL the graphics. Does it have to do with me translating the texture?

If I understand you well, you want to only translate the texture image on your square. To apply a transformation in "texture space" you can use glMatrixMode(GL_TEXTURE);



glMatrixMode(GL_TEXTURE);
glLoadIdentity();
//Texture animation transform
glRotatef(...);
glTranslatef(...);
...
glMatrixMode(GL_MODELVIEW);

satan
11-05-2008, 07:04 PM
So far, I've been able to draw a line using Bresenham's algorith, and I have it rotating around the x-axis to make it like a radar beam.

Is it part of your assignment to do it this way? Otherwise the GL_LINES primitive and glRotatef are the more efficient way of doing this.

Erin C.
11-05-2008, 07:19 PM
So far, I've been able to draw a line using Bresenham's algorith, and I have it rotating around the x-axis to make it like a radar beam.

Is it part of your assignment to do it this way? Otherwise the GL_LINES primitive and glRotatef are the more efficient way of doing this.

Yes, unfortunately. Using the GL_LINES is so much easier. My graphics professor is teaching everything extremely low level, instead of teaching the practical way. :)

IneQuation.pl
11-06-2008, 06:57 AM
I finally got something to translate with this, but instead of moving the texture, it moves ALL the graphics. Does it have to do with me translating the texture?
This is because of how the matrix stack in OpenGL works. This tutorial (http://gpwiki.org/index.php/OpenGL:Tutorials:Theory) should help you understand it (it did help me ;) ).

To put it short, set your matrix mode to model view and then, for each object you want to draw:
push the matrix stack, load identity matrix (glLoadIdentity()), do your transformations (mind the order - matrix multiplication is not commutative), call your primitive drawing functions (glBegin/glEnd pair + the stuff in between), pop the matrix.
This should do.

Erin C.
11-06-2008, 07:49 AM
Yay! :) It finally works!

IneQuation.pl
11-06-2008, 10:22 AM
Glad I helped. :)

dmdrummond
11-06-2008, 04:51 PM
You aren't studying at Swansea Met Uni by any chance are you?