UGGGGH OpenGLES Viewport/Rotating Viewport Help?

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
User avatar
ibly31
Chaos Rift Junior
Chaos Rift Junior
Posts: 312
Joined: Thu Feb 19, 2009 8:47 pm
Current Project: Like... seven different ones
Favorite Gaming Platforms: Xbox 360, Gamecube
Programming Language of Choice: C++, ObjC
Location: New Jersey.

UGGGGH OpenGLES Viewport/Rotating Viewport Help?

Post 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.
Image
Twitter
Website/Tumblr
My Projects

The best thing about UDP jokes is that I don’t care if you get them or not.
User avatar
davidthefat
Chaos Rift Maniac
Chaos Rift Maniac
Posts: 529
Joined: Mon Nov 10, 2008 3:51 pm
Current Project: Fully Autonomous Robot
Favorite Gaming Platforms: PS3
Programming Language of Choice: C++
Location: California
Contact:

Re: UGGGGH OpenGLES Viewport/Rotating Viewport Help?

Post 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
User avatar
ibly31
Chaos Rift Junior
Chaos Rift Junior
Posts: 312
Joined: Thu Feb 19, 2009 8:47 pm
Current Project: Like... seven different ones
Favorite Gaming Platforms: Xbox 360, Gamecube
Programming Language of Choice: C++, ObjC
Location: New Jersey.

Re: UGGGGH OpenGLES Viewport/Rotating Viewport Help?

Post 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?
Image
Twitter
Website/Tumblr
My Projects

The best thing about UDP jokes is that I don’t care if you get them or not.
User avatar
avansc
Respected Programmer
Respected Programmer
Posts: 1708
Joined: Sun Nov 02, 2008 6:29 pm

Re: UGGGGH OpenGLES Viewport/Rotating Viewport Help?

Post by avansc »

you have to push and pop the matrix
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
User avatar
ibly31
Chaos Rift Junior
Chaos Rift Junior
Posts: 312
Joined: Thu Feb 19, 2009 8:47 pm
Current Project: Like... seven different ones
Favorite Gaming Platforms: Xbox 360, Gamecube
Programming Language of Choice: C++, ObjC
Location: New Jersey.

Re: UGGGGH OpenGLES Viewport/Rotating Viewport Help?

Post 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??
Image
Twitter
Website/Tumblr
My Projects

The best thing about UDP jokes is that I don’t care if you get them or not.
User avatar
RyanPridgeon
Chaos Rift Maniac
Chaos Rift Maniac
Posts: 447
Joined: Sun Sep 21, 2008 1:34 pm
Current Project: "Triangle"
Favorite Gaming Platforms: PC
Programming Language of Choice: C/C++
Location: UK
Contact:

Re: UGGGGH OpenGLES Viewport/Rotating Viewport Help?

Post 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.
Ryan Pridgeon
C, C++, C#, Java, ActionScript 3, HaXe, PHP, VB.Net, Pascal
Music | Blog
User avatar
ibly31
Chaos Rift Junior
Chaos Rift Junior
Posts: 312
Joined: Thu Feb 19, 2009 8:47 pm
Current Project: Like... seven different ones
Favorite Gaming Platforms: Xbox 360, Gamecube
Programming Language of Choice: C++, ObjC
Location: New Jersey.

Re: UGGGGH OpenGLES Viewport/Rotating Viewport Help?

Post 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?
Image
Twitter
Website/Tumblr
My Projects

The best thing about UDP jokes is that I don’t care if you get them or not.
User avatar
Falco Girgis
Elysian Shadows Team
Elysian Shadows Team
Posts: 10294
Joined: Thu May 20, 2004 2:04 pm
Current Project: Elysian Shadows
Favorite Gaming Platforms: Dreamcast, SNES, NES
Programming Language of Choice: C/++
Location: Studio Vorbis, AL
Contact:

Re: UGGGGH OpenGLES Viewport/Rotating Viewport Help?

Post 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);
User avatar
ibly31
Chaos Rift Junior
Chaos Rift Junior
Posts: 312
Joined: Thu Feb 19, 2009 8:47 pm
Current Project: Like... seven different ones
Favorite Gaming Platforms: Xbox 360, Gamecube
Programming Language of Choice: C++, ObjC
Location: New Jersey.

Re: UGGGGH OpenGLES Viewport/Rotating Viewport Help?

Post 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!
Image
Twitter
Website/Tumblr
My Projects

The best thing about UDP jokes is that I don’t care if you get them or not.
User avatar
RyanPridgeon
Chaos Rift Maniac
Chaos Rift Maniac
Posts: 447
Joined: Sun Sep 21, 2008 1:34 pm
Current Project: "Triangle"
Favorite Gaming Platforms: PC
Programming Language of Choice: C/C++
Location: UK
Contact:

Re: UGGGGH OpenGLES Viewport/Rotating Viewport Help?

Post 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.
Ryan Pridgeon
C, C++, C#, Java, ActionScript 3, HaXe, PHP, VB.Net, Pascal
Music | Blog
User avatar
ibly31
Chaos Rift Junior
Chaos Rift Junior
Posts: 312
Joined: Thu Feb 19, 2009 8:47 pm
Current Project: Like... seven different ones
Favorite Gaming Platforms: Xbox 360, Gamecube
Programming Language of Choice: C++, ObjC
Location: New Jersey.

Re: UGGGGH OpenGLES Viewport/Rotating Viewport Help?

Post by ibly31 »

Yeah, I sure as hell have no idea what I'm doing hahaha. But that makes sense, thanks!
Image
Twitter
Website/Tumblr
My Projects

The best thing about UDP jokes is that I don’t care if you get them or not.
Post Reply