Translating one object and not another? Seems simple?

Started by
5 comments, last by SimonForsman 12 years ago
I'm trying to make it so that the objects rendered in the render method are translated by mouse dragging, while another is not. Where should I place the render method call for the other object? I've tried all over, and it doesn't seem to work anywhere?

while (!Display.isCloseRequested())
{
glClear(GL_COLOR_BUFFER_BIT);
if (Mouse.isButtonDown(0))
glPushMatrix();
glTranslatef(xTrans,yTrans,0); //move according to mouse movement

if (Mouse.isButtonDown(0) && Mouse.getX()>0 && Mouse.getX()<639)
xTrans+=Mouse.getDX();
else
xTrans=0;
if (Mouse.isButtonDown(0) && Mouse.getY()>0 && Mouse.getY()<479)
yTrans-=Mouse.getDY();
else
yTrans=0;
render();

if (Mouse.isButtonDown(0))
glPopMatrix();

update();

Display.update();
Display.sync(framesPerSecond);
}
Advertisement
You probably need to play with matrix transformation functions in your render() function. You just need to set a different modelview matrix for each object, not globally.
How would I go about doing that? I'm setting this before entering the main loop:


glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0,640,480,0,1,-1);
glMatrixMode(GL_MODELVIEW);

Is there some way that I can discard the transformations after the render() method is called without affecting already drawn things?
glPushMatrix and glPopMatrix can localize transformations to selected parts of your code only.
[source]
glPushMatrix();
glTranslate(xTrans,yTrans,0);
DrawAnObject();
glPopMatrix();
glPushMatrix();
glTranslate(...); // whatever transformation you want for this other object...
DrawAnotherObject();
glPopMatrix();
[/source]

glPushMatrix and glPopMatrix can localize transformations to selected parts of your code only.
[source]
glPushMatrix();
glTranslate(xTrans,yTrans,0);
DrawAnObject();
glPopMatrix();
glPushMatrix();
glTranslate(...); // whatever transformation you want for this other object...
DrawAnotherObject();
glPopMatrix();
[/source]


That's what I tried, but when I do it the translate is discarded and the object pops back to where it was before. I want one object to stay in the same spot on the screen (it's a GUI) while the other moves around behind it.
That's what my code does; it isolates the translate calls so that only one object is affected by the translate but not the other. If that doesn't work, then you have other things messing up the translations.
Edit: nevermind, i misread the OPs code.
[size="1"]I don't suffer from insanity, I'm enjoying every minute of it.
The voices in my head may not be real, but they have some good ideas!

This topic is closed to new replies.

Advertisement