help pls - rotating a 3d object

Apprentice
Posts: 17
Joined: 2009.03
Post: #1
I'm trying to figure out how to rotate a 3d object along the y-axis. I've managed to create a 3d disc with some "thickness" along the z-axis, but when I try to rotate, the result is always an empty screen?

Rotation only works if I rotate along the z-axis, but why does it not work along the y?

I'm current using the following code:

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glRotatef(1.0f, 0.0f, 1.0f, 0.0f); // even a 1-degree rotation along the y-axis, and my screen becomes blank?

If I change the glRotatef to rotate along the z-axis, I can see the effects. Is there something I'm missing in order to be able to rotate along y?
Quote this message in a reply
Apprentice
Posts: 17
Joined: 2009.03
Post: #2
anybody help? or at least suggest some guide which can help me make sense of things? projection view, model view, ... I've tried them all...
Quote this message in a reply
Moderator
Posts: 3,579
Joined: 2003.06
Post: #3
More code would help greatly.
Quote this message in a reply
Member
Posts: 749
Joined: 2003.01
Post: #4
Try rotating by even less.

©h€ck øut µy stuƒƒ åt ragdollsoft.com
New game in development Rubber Ninjas - Mac Games Downloads
Quote this message in a reply
Member
Posts: 34
Joined: 2009.01
Post: #5
Is it possible that even the slight rotation you're applying is causing the geometry to be transformed out of the clipping volume?
Quote this message in a reply
Apprentice
Posts: 17
Joined: 2009.03
Post: #6
kalimba Wrote:Is it possible that even the slight rotation you're applying is causing the geometry to be transformed out of the clipping volume?

thanks, but i'm not so sure. How would i check this? I've already tried rotating by 1 degree. I find it hard to believe that this would rotate the object out of view... (I'm assuming that clipping only clips out the stuff that gets rotated out of view? and that it's set by default to mean this?).

For my code, I am basically modifying "GLFun" from the code from "Beginning iPhone Development" (the code is available for download at http://www.iphonedevbook.com/BeginningiP...121808.zip)

This is the relevant bit (GLFunView.m, -(void) draw function)

Code:
            GLfloat thickness[2000]; // thickness is used to give some "depth" to the "slice"
            
            GLfloat xradius = (firstTouch.x > lastTouch.x) ? (firstTouch.x - lastTouch.x)/2 : (lastTouch.x - firstTouch.x)/2;
            GLfloat yradius = (self.frame.size.height - firstTouch.y > self.frame.size.height - lastTouch.y) ? ((self.frame.size.height - firstTouch.y) - (self.frame.size.height - lastTouch.y))/2 : ((self.frame.size.height - lastTouch.y) - (self.frame.size.height - firstTouch.y))/2;
            
            
            GLfloat xOffset = (firstTouch.x > lastTouch.x) ? lastTouch.x + xradius : firstTouch.x + xradius;
            GLfloat yOffset = (self.frame.size.height - firstTouch.y > self.frame.size.height - lastTouch.y) ? self.frame.size.height - lastTouch.y + yradius : self.frame.size.height - firstTouch.y + yradius;

            GLfloat z1 = -1.0f;
            GLfloat z2 = -11.0f;

            glMatrixMode(GL_MODELVIEW);
            glLoadIdentity();
            glRotatef(1.0f, 0.0f, 1.0f, 0.0f);        // rotate along y axis!
            
            vertices[0] = xOffset;
            vertices[1] = yOffset;
            for (int i = 1; i <= 90; i+=1)
            {
                vertices[i*3] = (cos(degreesToRadian(i))*yradius) + xOffset;
                vertices[i*3+1] = (sin(degreesToRadian(i))*yradius) + yOffset;
                vertices[i*3+2] = z1;
                
                thickness[i*6] = thickness[i*6+3] = vertices[i*3];
                thickness[i*6+1] = thickness[i*6+4] = vertices[i*3+1];
                thickness[i*6+2] = z1;
                thickness[i*6+5] = z2;
            }
            glVertexPointer (3, GL_FLOAT, 0, vertices);
            
            glDrawArrays (GL_TRIANGLE_FAN, 0, 91);

            glVertexPointer (3, GL_FLOAT, 0, thickness);
            glDrawArrays (GL_TRIANGLE_STRIP, 0, 180);

If i change the rotate to rotate along the z axis, it works...
Quote this message in a reply
Apprentice
Posts: 17
Joined: 2009.03
Post: #7
as an update, I've tried rotating by even 0.0000001 degrees already along the y axis. No luck!
Quote this message in a reply
Member
Posts: 59
Joined: 2007.12
Post: #8
without looking at the code...

-do you translate to the center of the rotation, rotate, then translate back?
-did you enable back culling?
-what about znear/zfar?
-when rotating, do you apply any other functions that could affect the object being drawn?
-do you draw/flush the drawing after rotating?
Quote this message in a reply
Member
Posts: 749
Joined: 2003.01
Post: #9
jfhtgui Wrote:as an update, I've tried rotating by even 0.0000001 degrees already along the y axis. No luck!

Try 0 then, or -1.

©h€ck øut µy stuƒƒ åt ragdollsoft.com
New game in development Rubber Ninjas - Mac Games Downloads
Quote this message in a reply
Apprentice
Posts: 17
Joined: 2009.03
Post: #10
Najdorf Wrote:Try 0 then, or -1.

yeah, thanks. I've tried 0 already. 0 definitely works. -1, as i've just discovered, seems to produce some clipping from slightly left of center of screen, to the right side of the screen. Dunno why, though. Dont really understand how this whole thing works.
Quote this message in a reply
Apprentice
Posts: 17
Joined: 2009.03
Post: #11
_ibd_ Wrote:without looking at the code...

-do you translate to the center of the rotation, rotate, then translate back?
-did you enable back culling?
-what about znear/zfar?
-when rotating, do you apply any other functions that could affect the object being drawn?
-do you draw/flush the drawing after rotating?

sorry, but I really know nuts. I've taught myself how to "get" and draw triangle fans and strips - but that's about it. Translating to center, rotate, and then translate back does produce better results, thanks. Although there's still clipping (I don't understand how this could happen).

Dunno what culling is! I'm lost, really. Never thought that there could be so much more to rotation than just "rotation"... My simplistic viewpoint of things is, I just create something in 3d-space, specify how to draw and rotate it.. and it should just "work".

When rotating, I don't apply any other functions that could affect the object. Besides the mere drawing of it! (calling glDrawArrays). Anyway, the code is above - I've posted it.
Quote this message in a reply
Member
Posts: 36
Joined: 2009.02
Post: #12
Remember that in OpenGL you must specify transformations in a reverse order. In your case, if you want the rotation along the Y axis to be centered on your object, you must first translate (apply your [xoffset, zoffset]) and then rotate it, instead of doing it the way around.

Try this and see if it works (just added the translation before glRotatef and removed the offsets in the vertices):

Code:
            GLfloat thickness[2000]; // thickness is used to give some "depth" to the "slice"
            
            GLfloat xradius = (firstTouch.x > lastTouch.x) ? (firstTouch.x - lastTouch.x)/2 : (lastTouch.x - firstTouch.x)/2;
            GLfloat yradius = (self.frame.size.height - firstTouch.y > self.frame.size.height - lastTouch.y) ? ((self.frame.size.height - firstTouch.y) - (self.frame.size.height - lastTouch.y))/2 : ((self.frame.size.height - lastTouch.y) - (self.frame.size.height - firstTouch.y))/2;
            
            
            GLfloat xOffset = (firstTouch.x > lastTouch.x) ? lastTouch.x + xradius : firstTouch.x + xradius;
            GLfloat yOffset = (self.frame.size.height - firstTouch.y > self.frame.size.height - lastTouch.y) ? self.frame.size.height - lastTouch.y + yradius : self.frame.size.height - firstTouch.y + yradius;

            GLfloat z1 = -1.0f;
            GLfloat z2 = -11.0f;

            glMatrixMode(GL_MODELVIEW);
            glLoadIdentity();
                        glTranslatef(xOffset, yOffset, 0.0f);
            glRotatef(1.0f, 0.0f, 1.0f, 0.0f);        // rotate along y axis!
            
            vertices[0] = xOffset;
            vertices[1] = yOffset;
            for (int i = 1; i <= 90; i+=1)
            {
                vertices[i*3] = (cos(degreesToRadian(i))*yradius);
                vertices[i*3+1] = (sin(degreesToRadian(i))*yradius);
                vertices[i*3+2] = z1;
                
                thickness[i*6] = thickness[i*6+3] = vertices[i*3];
                thickness[i*6+1] = thickness[i*6+4] = vertices[i*3+1];
                thickness[i*6+2] = z1;
                thickness[i*6+5] = z2;
            }
            glVertexPointer (3, GL_FLOAT, 0, vertices);
            
            glDrawArrays (GL_TRIANGLE_FAN, 0, 91);

            glVertexPointer (3, GL_FLOAT, 0, thickness);
            glDrawArrays (GL_TRIANGLE_STRIP, 0, 180);
Quote this message in a reply
Moderator
Posts: 1,562
Joined: 2003.10
Post: #13
miq01 Wrote:Remember that in OpenGL you must specify transformations in a reverse order.

This really isn't the right way to think of it. When you apply a transformation, you're transforming the world's coordinate system, not your object coordinates. When you send vertices to GL, they're relative to the current world transformation.
Quote this message in a reply
Member
Posts: 749
Joined: 2003.01
Post: #14
I see a number of possible problems:

Did you set your GL_PROJECTION matrix? if it's identity (i.e. you didn't set it) it only draws values with z between -1 and 1 (and also doesn't look 3d) .

You need to set up a 3d projection matrix, with normal opengl you use gluPerspective, glu is not in opengl ES but glu is open source now

Code:
/*
* SGI FREE SOFTWARE LICENSE B (Version 2.0, Sept. 18, 2008)
* Copyright (C) 1991-2000 Silicon Graphics, Inc. All Rights Reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice including the dates of first publication and
* either this permission notice or a reference to
* http://oss.sgi.com/projects/FreeB/
* shall be included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* SILICON GRAPHICS, INC. BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
* OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* Except as contained in this notice, the name of Silicon Graphics, Inc.
* shall not be used in advertising or otherwise to promote the sale, use or
* other dealings in this Software without prior written authorization from
* Silicon Graphics, Inc.
*/

the implementation of gluPerspective

Code:
static void __gluMakeIdentityd(GLdouble m[16])
{
    m[0+4*0] = 1; m[0+4*1] = 0; m[0+4*2] = 0; m[0+4*3] = 0;
    m[1+4*0] = 0; m[1+4*1] = 1; m[1+4*2] = 0; m[1+4*3] = 0;
    m[2+4*0] = 0; m[2+4*1] = 0; m[2+4*2] = 1; m[2+4*3] = 0;
    m[3+4*0] = 0; m[3+4*1] = 0; m[3+4*2] = 0; m[3+4*3] = 1;
}
#define __glPi 3.14159265358979323846

gluPerspective(GLdouble fovy, GLdouble aspect, GLdouble zNear, GLdouble zFar)
{
    GLdouble m[4][4];
    double sine, cotangent, deltaZ;
    double radians = fovy / 2 * __glPi / 180;

    deltaZ = zFar - zNear;
    sine = sin(radians);
    if ((deltaZ == 0) || (sine == 0) || (aspect == 0)) {
    return;
    }
    cotangent = COS(radians) / sine;

    __gluMakeIdentityd(&m[0][0]);
    m[0][0] = cotangent / aspect;
    m[1][1] = cotangent;
    m[2][2] = -(zFar + zNear) / deltaZ;
    m[2][3] = -1;
    m[3][2] = -2 * zNear * zFar / deltaZ;
    m[3][3] = 0;
    glMultMatrixd(&m[0][0]);
}

( you can find the full glu implementation by sgi included with the mesa library download http://www.mesa3d.org/ )

so before calling glMatrixMode(GL_MODELVIEW); you should call glMatrixMode(GL_PROJECTION); gluPerspective( 45.0 <camera angle>, 4/3.0 <screen ratio>, 0.1 <closest thing you draw>, 20.0<furthest thing you draw> );

remeber at this point it draws only stuff with positive z, between 0.1 and 20.

Also with your code you are rotating also the offset of your object, not the object around its axis, as miq01 sais.

©h€ck øut µy stuƒƒ åt ragdollsoft.com
New game in development Rubber Ninjas - Mac Games Downloads
Quote this message in a reply
Apprentice
Posts: 17
Joined: 2009.03
Post: #15
miq01 Wrote:Remember that in OpenGL you must specify transformations in a reverse order. In your case, if you want the rotation along the Y axis to be centered on your object, you must first translate (apply your [xoffset, zoffset]) and then rotate it, instead of doing it the way around.

thanks. Yeah, I did try the translation before. But guess it was the removal of the offset to the vertices that really saw some progress (thanks!)... Now my object becomes "more" visible - although there's still clipping. Am I missing something?
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Rotating a clipped Texture tfifriday 1 1,853 Feb 3, 2009 06:00 AM
Last Post: godexsoft