• Create Account

Rotating specific .obj with keyboard key

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

4 replies to this topic

#1PeterJobs  Members

Posted 26 June 2012 - 12:35 AM

Good morning guys,

I'm having difficulties in rotating an imported .obj when the key 'C' is pressed. I think my problem is with my KeyboardCallback function, but still, I can't figure out how to do it properly. My program compiles, but nothing happens when the key is pressed.

First, I declare some static pointes to the .objs that I'm importing:

[source lang="cpp"]static Mesh *book, *can, *table;[/source]

Then I render the objects:

[source lang="cpp"]void Render(void){ glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glLoadIdentity(); if (book) { glPushMatrix(); glTranslatef(0, -2, -8); book->Render(); glPopMatrix(); } ....}[/source]

That all works perfectly fine. And here my doubts start. My KeyboardCallback function:

[source lang="cpp"]void KeyboardCallback(unsigned char key, int x, int y){ switch (key) { case 27: exit(0); break; case 'c': case 'C': RotateBook(book); break;}[/source]

And finally, my RotateBook function:

[source lang="cpp"]void RotateBook(Mesh *){ glPushMatrix(); glRotatef(45, 0, 1, 0); book->Render(); glPopMatrix();}[/source]

Can someone please indicate what I'm doing wrong?

#2doeme  Members

Posted 26 June 2012 - 01:16 AM

Is your callback called at all? Also you need some kind of loop because you might want to draw everything again after you move or rotate something. Instead of directly calling the gl-functions in your rotateBook()-function, store the transformation of the book in a set of variables and trigger a new Render() call. Maybe your transformation is executed but you don't see it because it's outside your viewport or frustrum.

Something along this, whith bookTransform a struct of position and rotation-data:

void Render(void){
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

if (book)
{
glPushMatrix();
glRotatef(bookTransform.angle, 0, 1,0);
glTranslatef(bookTransform.x, bookTransform,y, bookTransform.z);
book->Render();
glPopMatrix();
}
....
}


void KeyboardCallback(unsigned char key, int x, int y){
switch (key)
{
case 27:
exit(0);
break;

case 'c':
case 'C':
bookTransform.angle = 45.0f;
Render();
break;
}


#3szecs  Members

Posted 26 June 2012 - 01:21 AM

so, is it glut?
If so, you better call glutPostRedisplay whenever you want to render the whole thing, instead of explicitly calling the render function. You have to call glutDisplayFunc too at initialization with the render function as the argument, of course.

This is just some bugfix, you really should decouple logic from rendering.

Edited by szecs, 26 June 2012 - 01:23 AM.

#4MaxDZ8  Members

Posted 26 June 2012 - 03:10 AM

I'm having quite some difficulty understanding how this would ever work. Let's start from the very basic thing:
[source lang="cpp"]void Render(void){ glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glLoadIdentity(); if (book) { glPushMatrix(); glTranslatef(0, -2, -8); book->Render(); glPopMatrix(); } ....}[/source]
Does this contain a rotate? No. So it will never rotate. We can see it clearly: it starts from identity matrix each time. No rotation here, unless we do that in book->Render().
Now, let's consider the other snippet.
[source lang="cpp"]void RotateBook(Mesh *m){ glPushMatrix(); glRotatef(45, 0, 1, 0); book->Render(); glPopMatrix();}[/source]Makes a render call. I have no idea what will render as the callbacks are - in line of concept - asynchronous. In this specific case it does not matter, the first code snippet clears the buffer anyway so the result of this call is trashed completely.

To work, RotateBook should be called somewhere after LoadIdentity(). This is not how it is supposed to happen.

Instead, you need to have RotateBook update a structure such as a euler triplet or a quaternion. Then, in the Render() function, pull out this value and rotate. That is, something like
[source lang="cpp"]void RotateBook(Mesh *mesh) { mesh->rot += Vec3(.0f, ROT_SPEED * GetTimestep(), .0f); }void Render(void){ glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glLoadIdentity(); if (book) { glPushMatrix(); glRotate3f(book->rot[0], book->rot[1], book->rot[2]); // beware my GL is a bit rusty and I regularly screw matrix op order glTranslatef(0, -2, -8); book->Render(); glPopMatrix(); } ....}[/source]

Previously "Krohm"

#5PeterJobs  Members

Posted 26 June 2012 - 08:42 AM

Guys, thank you all for your answer. All your considerations were basically right, and now it is rotating perfectly. The main problem was that I wasn't applying glRotate when the object was first called and rendered.

You guys rock.. thanks once again!

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.