user1343820
user1343820

Reputation:

Moving Specific Object in OpenGL C Linux

I'm having a problem moving an specific object in OpenGL using C.

Object CODE

glClearColor(0.0, 0.0, 0.0, 0.0);
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1, 0.73, 0.06);
glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0);
glPushMatrix();
glMatrixMode(GL_MODELVIEW);
glRotatef(0,PacX,PacY,0);
glBegin(GL_QUADS);
    glVertex2f(ax, ay);
    glVertex2f(bx, by);
    glVertex2f(cx, cy);
    glVertex2f(dx, dy);
glEnd();
glPopMatrix();
glFlush();

This will draw a square, but in the window i have other objects, so when i try to move only the square with glTranslatef() it moves all the objects, is there a way or variable where i can store a pointer or an ID to the square so i can move only the square ?

Upvotes: 0

Views: 1491

Answers (2)

Edwin Buck
Edwin Buck

Reputation: 70909

Read about OpenGL display lists, and allocate a display list in the graphics card. From the OpenGL Programming Guide

/* Create display list with Torus and initialize state*/
static void init(void)
{
   theTorus = glGenLists (1);
   glNewList(theTorus, GL_COMPILE);
   torus(8, 25);
   glEndList();

   glShadeModel(GL_FLAT);
   glClearColor(0.0, 0.0, 0.0, 0.0);
}

where torus(8, 25) calls a function that draws the elements that are captured into theTorus by the surrounding glNewList(...) and glEndList() functions. Then your drawing looks more like

void display(void)
{
   glClear(GL_COLOR_BUFFER_BIT);
   glColor3f (1.0, 1.0, 1.0);
   glCallList(theTorus);
   glFlush();
}

which means that you can alter the environment prior to drawing the torus by calling various glRotatef(...) and other scaling and transformation calls.

The entire example I've been pulling references from can be accessed here.

With a few additional data structures, you can hold the orientation of the object in a struct, apply the transforms to the environment, and then draw the particular display list. Don't remember to un-apply the transformation of the environment afterwards, and then you will have effectively rotated, moved, or did whatever to the single object represented in the display list.

Upvotes: 0

unwind
unwind

Reputation: 399703

You need to save and restore the transformation matrix for each object, so that each object gets its own matrix.

See the glPushMatrix() function.

Upvotes: 1

Related Questions