|
Title: Help rotating objects using OpenGL ES 2.0 and GLKit [SOLVED] Post by: ParticleGames on October 30, 2011, 04:43:31 PM I am starting out in iPhone game development, and I have learned how to draw polygons on to the screen. I have just implemented rotation of the objects. When the objects rotate, instead of rotating at the center of themselves, they rotate around the origin of the screen ((0,0) or the top left of the screen). Is there a way to transfer the point of rotation to the center of the object being rotated?
Code: static const GLfloat squareVertices[] = { 100,100, 200,100, 100,200, 200,200 }; static const GLubyte squareColors[] = { 255, 255, 0, 255, 0, 255, 255, 255, 0, 0, 0, 0, 255, 0, 255, 255, }; -(void)glkViewControllerUpdate:(GLKViewController *)controller{ GLKMatrix4 projectionMatrix = GLKMatrix4MakeOrtho(0.0, 320.0, 480.0, 0.0, 1, -1); self.effect.transform.projectionMatrix = projectionMatrix; float transformY = 100 * (sinf([self timeSinceFirstResume]) + 1); GLKMatrix4 modelView; GLKMatrix4 transformMatrix; GLKMatrix4 rotationMatrix; rotationZ += 90 * self.timeSinceLastUpdate; rotationMatrix = GLKMatrix4MakeRotation(GLKMathDegreesToRadians(rotationZ), 0, 0, 1); transformMatrix = GLKMatrix4MakeTranslation(0, transformY, 0); modelView = GLKMatrix4Multiply(rotationMatrix, transformMatrix); self.effect.transform.modelviewMatrix = modelView; } - (void)glkView:(GLKView *)view drawInRect:(CGRect)rect { glClearColor(0.65f, 0.65f, 0.65f, 1.0f); glClear(GL_COLOR_BUFFER_BIT); // Render the object with GLKit [self.effect prepareToDraw]; glEnableVertexAttribArray(GLKVertexAttribPosition); glEnableVertexAttribArray(GLKVertexAttribColor); glVertexAttribPointer(GLKVertexAttribPosition, 2, GL_FLOAT, GL_FALSE, 0, squareVertices); glVertexAttribPointer(GLKVertexAttribColor, 4, GL_UNSIGNED_BYTE, GL_FALSE, 0, squareColors); glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); glDisableVertexAttribArray(GLKVertexAttribPosition); glDisableVertexAttribArray(GLKVertexAttribColor); } Any help would be greatly appreciated. Thanks :) Title: Re: Help rotating objects using OpenGL ES 2.0 and GLKit Post by: ThemsAllTook on October 30, 2011, 06:31:25 PM I'm not familiar with GLKit, but assuming your analysis is correct, it looks like the problem is with your projection matrix. If GLKMatrix4MakeOrtho(-3, 3, -2, 2, 1, -1) does what I think it does, it'll give you a 3:2 ratio, which doesn't match the iPhone's screen dimensions. You might want to do something like this:
Code: float ratio = [UIScreen mainScreen].bounds.size.width / [UIScreen mainScreen].bounds.size.height; GLKMatrix4 projectionMatrix = GLKMatrix4MakeOrtho(-ratio * 2, ratio * 2, -2, 2, 1, -1); Title: Re: Help rotating objects using OpenGL ES 2.0 and GLKit Post by: CowBoyDan on October 30, 2011, 07:29:55 PM I think most iPhone/ipod touch are 3:2, but the advice not to hard code values is sound, depending on the game design that is, if some stretching is ok it wont matter, but it sounds like it will in your case.
Title: Re: Help rotating objects using OpenGL ES 2.0 and GLKit [NOT SOLVED] Post by: ParticleGames on October 31, 2011, 03:10:15 PM Thank you! I had aligned the screen ratio in the wrong parameters. I adjusted it to go by pixels. Although now I have a new issue. The objects aren't getting morphed, but they aren't rotating correctly still. I updated my first post to explain.
Title: Re: Help rotating objects using OpenGL ES 2.0 and GLKit [NOT SOLVED] Post by: Vino on October 31, 2011, 03:16:23 PM Looks like you're translating before rotating. You need to rotate then translate. Rotate while the model is at the origin (in local model space) then translate it to where you want it to be. If you translate and then rotate, then your model will be out somewhere when you rotate, and it'll revolve around the origin like the earth going around the sun.
I dunno what the functions to do so are, I don't know enough about the GLKMatrix4 class you're using. Look at the rotate function, it takes an input matrix as its first argument. That means it multiplies the input matrix by the rotation and returns the result. You need to find something that does that for translation as well and use it after the rotation function. Maybe some learning about transformation matrices (http://en.wikipedia.org/wiki/Transformation_matrix) is in your future! Title: Re: Help rotating objects using OpenGL ES 2.0 and GLKit [NOT SOLVED] Post by: ParticleGames on October 31, 2011, 03:41:50 PM I changed the code (see above) but unfortunately it is still not working. Even worse, I don't understand why :P The same thing is still happening, and changing the order of the multiplication of the matrices is having no effect.
EDIT: After lots of research and multiple tests, I think it must have something to do with me changing the way the coordinate system works, with the top left being (0,0) and the bottom right being (320,480). EDIT2: My program works if I change the coordinate system to: Code: GLKMatrix4 projectionMatrix = GLKMatrix4MakeOrtho(-4, 4, -6, 6, 1, -1); and my vertices to: Code: static const GLfloat squareVertices[] = { -1,1, 1,1, -1,-1, 1,-1 }; Is there a flaw I am overlooking in my pixel-based coordinate system? I prefer pixel-based because I am a user of SDL so it feels the most smooth. Title: Re: Help rotating objects using OpenGL ES 2.0 and GLKit [NOT SOLVED] Post by: Vino on October 31, 2011, 06:48:30 PM I must have misunderstood your problem then, sorry. Unfortunately I have no insights into your current predicament.
Title: Re: Help rotating objects using OpenGL ES 2.0 and GLKit [NOT SOLVED] Post by: ParticleGames on November 01, 2011, 04:04:23 PM Ok, thank you for your help anyways. I will implement a normal coordinate system then until I can get help on the iOS developer forums. I will sign up soon :) Thanks again
|