PDA

View Full Version : How do i use array's/vectors with glTranslate?



07-25-2001, 05:58 PM
Okay here are portions of my code:




static GLfloat body[][2]=
{
{0.0, 0.0}, {1.0, 1.0},
{-1.0, 0.0}, {0.0, 1.0},
{-2.0, 0.0}, {-1.0, 1.0},
{-3.0, 0.0}, {-2.0, 1.0}
};

static GLfloat vector[][1]=
{
{0.001}, {0.0},
{0.001}, {0.0},
{0.001}, {0.0},
{0.001}, {0.0}
};


and then i use the arrays in the following code portion:




glTranslatefv(vector[i], vector[f]);
glRectfv(body[i], body[f]);


The glRectfv works but i can't get the glTranslate call to work with vectors or arrays.
I know tha glTranslate requires 3 float values, so my glTranslate function looks like this:

GLfloat empty=0;
glTranslatef( vector[i], vector[f], empty);

I'm doing 2d based stuff and don't need to modify the z values hence the "empty".

Well i hope i made sense. I'm just trying out different ways to work with opengl codes and i thought it would be simpler to work with moving objects in a 3d space by using glTranslate in parallel, and then working on the glTranslate values, as apposed to working on the values for the object's vertices/body.

Thanks for the help.

Cheers

zed
07-25-2001, 10:24 PM
use 0.0 for the 3rd coodinate

07-26-2001, 06:08 PM
zed, i tried the 0.0 for the z and it gets set as a double float. i forgot to set it as 0.0f which will then make it make it a regular float.

Well i guess i'll try posting this thread in the advanced forum, maybe i'll get some answers there. http://www.opengl.org/discussion_boards/ubb/smile.gif

cheers

07-26-2001, 07:23 PM
never mind i figured out another way to do this. i'm going to use structures and manipulate the vertices instead of using glTranslate. Should make coding easier.

for example i would do this:





x_cord[loop] * x_trans[loop]; //x vertex * x vector value
y_cord[loop] * y_trans[loop]; // y vertex * y vector value



then my glDisplay function would just be a simple loop like so:




void display(void)
{

glClear(GL_COLOR_BUFFER_BIT);
glPushMatrix();
for (loop=0;loop<length_of_snake;loop++)
{
float x_cord=snake[loop].x; //grab x cordinate for glRect
float y_cord=snake[loop].y; //grab y cordinate for glRect

glColor3f(1.0, 1.0, 1.0);
glRectf(x_cord, y_cord);
}
glutSwapBuffers();
}


much simpler i think. I just posted this up for anyone who was looking for different ways of moving objects around.

Cheers

Deiussum
07-27-2001, 05:01 AM
I don't quite understand why you are having problems with glTranslate. Updating your vertices might seem like an easy solution now, but what about when you get models with lots of vertices? A better solution is generally to just store a separate position for the model and use glTranslate for that.

BTW, this code isn't valid:



glTranslatefv(vector[i], vector[f]);
glRectfv(body[i], body[f]);


There is no array form of glTranslate and if there were it would take only one vector, not two. Also, OpenGL has no glRectfv function. Unless I'm misunderstanding and these are your own functions. If so, it probably would be a good idea to not name them with the gl prefix, which can cause confusion with people thinking they are pure OpenGL calls.

07-27-2001, 06:37 PM
Also, OpenGL has no glRectfv function.

actually glRectfv is an opengl function call.

You can do glRect*( x1,y1, x2,y2);
where x1,y1 is the top left corner co-ordinates, and x2,y2 is the bottom right co-ordinates (IIRC).

glRectfv(body[i], body[f]);
where body[i]=(x1,y1) and body[f]=(x2,y2)

I know it works cause i've compiled code that uses glRectfv.
However i think you're right that glTranslate can't use vectors. Nothing i've tried works with it. However using the constructs works fine.
yeah using glTranslate is easier, but i can't seem to understand how to use projection matrix which is important for glTranslate calls. I'll have to keep reading over the projection matrix stuff in the red book till i get it.

Deiussum
07-30-2001, 04:53 AM
Ahh... you're right. glRect is a function.

Anyway, you shouldn't be doing glTranslate in the projection matrix, you should be doing it with the model-view matrix. The ONLY calls you should generally use with the projection matrix are glFrustum, glOrtho, gluPerspective, and gluOrtho2D.

The projection matrix is used to define the shape of the viewing volume. The model-view matrix is used to move the "camera" and models. Any camera-like transformations have to come first in the model-view matrix.

Try doing this with your code.

for each object
{
glPushMatrix();
glTranslate();
Draw();
glPopMatrix();
}

That should draw each object in it's correct position and not affect any "camera" transformations you did in the model-view matrix.

08-01-2001, 06:35 PM
Deiussum, thanks for the info.

I've been re-reading the glPopMatrix and glPushMatrix concepts (page 135 in the OpenGL Redbook, version 1.2 third edition), and things are starting to make more sense.

I've been using my own translation function which has been a pain in the ass to code. So i think i'll stick to the glTranslate function. http://www.opengl.org/discussion_boards/ubb/smile.gif

Cheers