Rotating specific .obj with keyboard key

Started by
3 comments, last by PeterJobs 11 years, 10 months ago
[color=#333333][font=Verdana, Arial, Tahoma, Calibri, Geneva, sans-serif]

[background=rgb(250, 250, 250)]Good morning guys,[/background][/font]

[color=#333333][font=Verdana, Arial, Tahoma, Calibri, Geneva, sans-serif]

[background=rgb(250, 250, 250)]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.[/background][/font]

[color=#333333][font=Verdana, Arial, Tahoma, Calibri, Geneva, sans-serif]

[background=rgb(250, 250, 250)]First, I declare some static pointes to the .objs that I'm importing:[/background][/font]

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

[color=#333333][font=Verdana, Arial, Tahoma, Calibri, Geneva, sans-serif]

[background=rgb(250, 250, 250)]Then I render the objects:[/background][/font]

[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]

[color=#333333][font=Verdana, Arial, Tahoma, Calibri, Geneva, sans-serif]

[background=rgb(250, 250, 250)]That all works perfectly fine. And here my doubts start. My KeyboardCallback function:[/background][/font]

[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]

[color=#333333][font=Verdana, Arial, Tahoma, Calibri, Geneva, sans-serif]

[background=rgb(250, 250, 250)]And finally, my RotateBook function:[/background][/font]

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

[color=#333333][font=Verdana, Arial, Tahoma, Calibri, Geneva, sans-serif]

[background=rgb(250, 250, 250)]Can someone please indicate what I'm doing wrong?[/background][/font]
[color=#333333][font=Verdana, Arial, Tahoma, Calibri, Geneva, sans-serif]

[background=rgb(250, 250, 250)]Thanks in advance.[/background][/font]

Advertisement
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);
glLoadIdentity();

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;
}
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.
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 [font=courier new,courier,monospace]book->Render()[/font].
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, [font=courier new,courier,monospace]RotateBook [/font]should be called somewhere after [font=courier new,courier,monospace]LoadIdentity()[/font]. This is not how it is supposed to happen.

Instead, you need to have [font=courier new,courier,monospace]RotateBook [/font]update a structure such as a euler triplet or a quaternion. Then, in the [font=courier new,courier,monospace]Render() [/font]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"

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!

This topic is closed to new replies.

Advertisement