Jump to content

  • Log In with Google      Sign In   
  • Create Account


PeterJobs

Member Since 26 Jun 2012
Offline Last Active Jun 27 2012 09:11 AM

Topics I've Started

Keyboard triggers animation

26 June 2012 - 02:29 PM

Hi everyone!

I needed a little help with a similar question this morning, but in this one I'm really stuck and don't know what I'm doing, so any help will be appreciated.

I have to move smoothly an object from one side of my 3d room to another, every time a single key is pressed. I declare a global variable with the initial x position of it, and then I render it:

[source lang="cpp"]glPushMatrix();glTranslatef(positionObject, 1, -6);object->Render();glPopMatrix();[/source]

So I guess that, for the smooth movement, I'll need a timer that shows the object position several times per second, right? I'm doing it like this (not the proper way to do it, I'm aware):

[source lang="cpp"]void Timer (int value) { if (positionObject == -1.5) { while (positionObject <= -2.3) positionObject += -0.1; } else { while (positionObject != -1.5) positionObject -= -0.1; } glutPostRedisplay(); glutTimerFunc (33, Timer, 1);}[/source]

And my final doubt is: what do I call in my keyboard callback function for it to work?

[source lang="cpp"]void KeyboardCallback (unsigned char key, int x, int y){ switch (key) { case 'r': case 'R': //do sth; break; }}[/source]

Rotating specific .obj with keyboard key

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?
Thanks in advance.

PARTNERS