Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 3 of 3

Thread: Rotating specific .obj with keyboard key

  1. #1
    Junior Member Newbie
    Join Date
    Apr 2012
    Posts
    5

    Post Rotating specific .obj with keyboard key

    Hi everyone!

    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.

    First, I declare some static pointes to the .objs that I import:

    Code :
    static Mesh *book, *can, *table;

    Then I render the objects:

    Code :
    void Render(void){
    	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    	glLoadIdentity();
     
     
     
     
    	if (book)
    	{
    		glPushMatrix();
                    glTranslatef(0, -2, -8);
                    book->Render();
    		glPopMatrix();
    	}
     
            ....

    That all works fine. Then my KeyboardCallback:

    Code :
    void KeyboardCallback(unsigned char key, int x, int y){
    	switch (key)
    	{
    	case 27:
    		exit(0);
    		break;
     
    	case 'c':
    	case 'C':
    		RotateBook(book);
    		break;

    And finally, my RotateBook function.

    Code :
    void RotateBook(Mesh *){
    	glPushMatrix();
    	glRotatef(45, 0, 1, 0);
    	book->Render();
    	glPopMatrix();
    }

    Can someone please indicate what I'm doing wrong?

    Thanks in advance.

  2. #2
    Junior Member Newbie
    Join Date
    Jun 2012
    Posts
    12
    First of all You draw the book twice - once in 'Render', second in 'RotateBook'. I think it isn't what You want to be done
    What more - 'RotateBook' rotates the mesh always by 45 deg. only.

    I think that the rotation callback should only change the angel of rotation. The transformation itself should be applied where the drawing code in used. In other words - add some global angel variable (visible from within both 'Render' and 'RotateBook'). It's value will change in the callback, while apply the rotation only 'Render' (in proper order of course ).

    Regrads,
    MK

  3. #3
    Junior Member Newbie
    Join Date
    Apr 2012
    Posts
    5
    Thank you cmaster.matso for your reply!

    I fixed it. Basically my main problem was that I wasn't applying glRotate when the object was first called and rendered, so it obviously would never work (and this way I don't need the
    RotateBook function at all).

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •