Page 1 of 1

UGGGGH OpenGLES Viewport/Rotating Viewport Help?

Posted: Mon May 24, 2010 8:19 pm
by ibly31
Hi everyone. Whenever I try to start a new iPhone game, I always end up getting discouraged by the stupidest thing: setting up the damn thing to render what I want. I'm working with my iPod Touch. What I want to do is render it in landscape mode. Which never ends up working for me. Anyways, heres what I have to do that:

Code: Select all

 
glViewport(0, 0, 320, 480);
    
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrthof(0.0f, 320, 0.0f, 480, -1.0f, 1.0f);
    glMatrixMode(GL_MODELVIEW);
    
    glClearColor(0.2f, 0.2f, 0.2f, 1.0f);
    glClear(GL_COLOR_BUFFER_BIT);
	glLoadIdentity();
	glPushMatrix();
	glTranslatef(240, 160, 0);
	glRotatef(-90.0, 0, 0, 1);
	glTranslatef(-320, -240, 0);
        glPopMatrix();
I'm not sure what the values do for the last three functions... I just kinda played with them until the square had its bottom left in the bottom left of the screen when in landscape. So this all works(i think...)BUUUTT BUUT BUT BUT... when I try to rotate the square around its center, it doesn't want to work.

Whenever you rotate things in OpenGL, it rotates around the origin. Which is easily overcomeable by translating to the origin, rotating, then translating back. But I usually do that inside push and pop matrix calls, which I already have...

This is all way to confusing. Does anyone have the code to be able to draw things in Landscape(home button on the right), with the bottom left at 0,0 and the top right at 480,320? And also the code to rotate things around their center no matter where they are? Thanks a bajillion if you do.

Re: UGGGGH OpenGLES Viewport/Rotating Viewport Help?

Posted: Mon May 24, 2010 10:09 pm
by davidthefat
ibly31 wrote:Hi everyone. Whenever I try to start a new iPhone game, I always end up getting discouraged by the stupidest thing: setting up the damn thing to render what I want. I'm working with my iPod Touch. What I want to do is render it in landscape mode. Which never ends up working for me. Anyways, heres what I have to do that:

Code: Select all

 
glViewport(0, 0, 320, 480);
    
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrthof(0.0f, 320, 0.0f, 480, -1.0f, 1.0f);
    glMatrixMode(GL_MODELVIEW);
    
    glClearColor(0.2f, 0.2f, 0.2f, 1.0f);
    glClear(GL_COLOR_BUFFER_BIT);
	glLoadIdentity();
	glPushMatrix();
	glTranslatef(240, 160, 0);
	glRotatef(-90.0, 0, 0, 1);
	glTranslatef(-320, -240, 0);
        glPopMatrix();
I'm not sure what the values do for the last three functions... I just kinda played with them until the square had its bottom left in the bottom left of the screen when in landscape. So this all works(i think...)BUUUTT BUUT BUT BUT... when I try to rotate the square around its center, it doesn't want to work.

Whenever you rotate things in OpenGL, it rotates around the origin. Which is easily overcomeable by translating to the origin, rotating, then translating back. But I usually do that inside push and pop matrix calls, which I already have...

This is all way to confusing. Does anyone have the code to be able to draw things in Landscape(home button on the right), with the bottom left at 0,0 and the top right at 480,320? And also the code to rotate things around their center no matter where they are? Thanks a bajillion if you do.
From what I am aware of, the glRotatef takes in 1 int and 3 floats, the degree of rotation is the int and the floats are x,y, z respectively. It rotates the whole viewport on the axis you specify by that degree, not just one object

Re: UGGGGH OpenGLES Viewport/Rotating Viewport Help?

Posted: Mon May 24, 2010 10:39 pm
by ibly31
Yeah, I'm trying to rotate that single object... Hoe do I do that? I've done it before with glRotatef, its just not working now. Surely I'm not the only one with this problem... what do people do, just not use rotation or something?

Re: UGGGGH OpenGLES Viewport/Rotating Viewport Help?

Posted: Mon May 24, 2010 11:39 pm
by avansc
you have to push and pop the matrix

Re: UGGGGH OpenGLES Viewport/Rotating Viewport Help?

Posted: Mon May 24, 2010 11:51 pm
by ibly31

Code: Select all

- (void)drawView {
    
    // Replace the implementation of this method to do your own custom drawing
    
    const GLfloat squareVertices[] = {
		-25,-25,
         25,-25,
		-25, 25,
         25, 25,
    };
    const GLubyte squareColors[] = {
        0, 0, 0, 0,
        255, 0, 0, 0,
        0, 255, 0, 0,
        0, 0, 255, 0,
    };
    
    [EAGLContext setCurrentContext:context];
    
    glBindFramebufferOES(GL_FRAMEBUFFER_OES, viewFramebuffer);
    glViewport(0, 0, backingWidth, backingHeight);
    
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
	glRotatef(-90.0, 0.0, 0.0, 1.0);
    glOrthof(0.0f, 480.0f, 0.0f, 320.0f, -1.0f, 1.0f);
    glMatrixMode(GL_MODELVIEW);
    
    glClearColor(0.2f, 0.2f, 0.2f, 1.0f);
    glClear(GL_COLOR_BUFFER_BIT);
	
	
	glPushMatrix();
	glRotatef(rota, 0, 0, 1);
	glTranslatef(goodloc.x, goodloc.y, 0);
	
	glVertexPointer(2, GL_FLOAT, 0, squareVertices);
    glEnableClientState(GL_VERTEX_ARRAY);
    glColorPointer(4, GL_UNSIGNED_BYTE, 0, squareColors);
    glEnableClientState(GL_COLOR_ARRAY);
    glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
	glPopMatrix();

	
	rota+=0.1;
	if(rota > 360)
		rota = 0;
	    
    glBindRenderbufferOES(GL_RENDERBUFFER_OES, viewRenderbuffer);
    [context presentRenderbuffer:GL_RENDERBUFFER_OES];
}
That makes my square go in a big circle with a radius that is equal to the distance from the square to the bottom left corner of the screen.

I assume when I start to rotate it right after the pushmatrix call that it is at the origin with the center at 0,0,0 because of how I defined its vertices. So i dont need to translate it to origin, right? then i rotate, then i translate to where i want it... shouldnt it be rotated around its own center and just spinning in place??

Re: UGGGGH OpenGLES Viewport/Rotating Viewport Help?

Posted: Mon May 31, 2010 11:25 am
by RyanPridgeon
It's because you're rotating and THEN translating, you want to be doing it the other way round here.

@davidthefat, all 4 arguments are floats.

Re: UGGGGH OpenGLES Viewport/Rotating Viewport Help?

Posted: Mon Jun 07, 2010 8:26 pm
by ibly31
Yeah, I got help from someone on the cocos2d IRC. Any idea why that makes sense?

Shouldn't it work like: Move the thing to the center so that when you rotate it, it rotates around the center, then move it back so it keeps its rotation?

Re: UGGGGH OpenGLES Viewport/Rotating Viewport Help?

Posted: Tue Jun 08, 2010 8:10 am
by Falco Girgis
ibly31 wrote:Yeah, I got help from someone on the cocos2d IRC. Any idea why that makes sense?

Shouldn't it work like: Move the thing to the center so that when you rotate it, it rotates around the center, then move it back so it keeps its rotation?
No, you're right. Your logic was right. You're just accidentally reversing the order with relation to the matrix.

When you do:

Code: Select all

glRotatef(blah);
glTranslatef(blah);
The matrix operation becomes:

ROTATION MATRIX * TRANSLATION MATRIX * VERTICES

The operation you wanted to perform was:

TRANSLATION MATRIX * ROTATION MATRIX * VERTICES

Code: Select all

glTranslatef(blah);
glRotatef(blah);

Re: UGGGGH OpenGLES Viewport/Rotating Viewport Help?

Posted: Tue Jun 08, 2010 10:39 pm
by ibly31
Just wondering, do those functions work on any vertices loaded into OpenGL? Because couldn't you rotate the vertices of a UV Map and have a rotating texture on a static object? Or a rotating object with a texture rotating the opposite directions? Whooa, that would be trippy!

Re: UGGGGH OpenGLES Viewport/Rotating Viewport Help?

Posted: Wed Jun 09, 2010 5:39 am
by RyanPridgeon
Yes, but that would be for the GL_TEXTURE matrix. You see, OpenGL has 4 texture modes. The one you're probably using for transforming your vertices is GL_MODELVIEW. You also have GL_PROJECTION which is applied to all vertices after the modelview matrix, and GL_TEXTURE which transforms the texture coordinates. I forgot what the 4th is.

To switch you use glMatrixMode, which you've probably already been using without really understanding it (sorry if I'm wrong, but I know that's what I used to do ;D ). To switch to the texture matrix, use glMatrixMode(GL_TEXTURE), then use glMatrixMode(GL_MODELVIEW) to go back. The texture matrix doesn't affect the vertex positions, and the modelview matrix doesn't affect the texture coordinates.

Re: UGGGGH OpenGLES Viewport/Rotating Viewport Help?

Posted: Sat Jun 12, 2010 4:14 pm
by ibly31
Yeah, I sure as hell have no idea what I'm doing hahaha. But that makes sense, thanks!