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:

static Mesh *book, *can, *table;

Then I render the objects:

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:

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.

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

Can someone please indicate what I’m doing wrong?

Thanks in advance.

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 :slight_smile:
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

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 [LEFT]RotateBook function at all).[/LEFT]