Dark Bit Factory & Gravity

PROGRAMMING => General coding questions => Topic started by: b0ib0t on January 18, 2008

Title: Software rotations!
Post by: b0ib0t on January 18, 2008
For your pleasure today I have software rendered rotations! Although the quad is rendered by OpenGL and is in fact Hardware, the rotation math is all handled by the cpu.

This is a feature I am working on adding to sen3d.

Any ways, here is the code!

Code: [Select]
dim rad as double               
dim i : i = 0 
dim i2 : i2 = 0
dim pi as double : pi = 3.14
dim new_x as double
dim new_y as double
dim new_z as double     
dim x_ang as double
dim move_speed as double : move_speed = 0.1     
dim model(20) as double   
    model(0)= -1.0
    model(1)= 1.0
    model(2)= 0.0
    model(3)= 1.0
    model(4)= 1.0
    model(5)= 0.0
    model(6)= 1.0
    model(7)= -1.0
    model(8)= 0.0
    model(9)= -1.0
    model(10)= -1.0
    model(11)= 0.0
dim x3d, y3d, z3d
dim tempModel(20) as double

                               
    while true   

        if ( x_ang < 2 * pi ) then   
           
            'model(i2) = 1 * cos(x_ang) 
            'model(i2 + 2) = 1 * sin(x_ang)
               

            glClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT)   
            glLoadIdentity()                           
            glTranslatef(0,0.0,-6.0)                                           
            glColor3f(0.5,0.5,1.0)                     
            glBegin(GL_QUADS)                           
                while(i < 11)
                    x3d = model(i)
                    y3d = model(i + 1)
                    z3d = model(i + 2)

                    tempModel(i) = (y3d * cos(x_ang)) - (z3d * sin(x_ang))
                    tempModel(i+2) = (y3d * sin(x_ang)) + (z3d * cos(x_ang))
                    tempModel(i) = (x3d * cos(x_ang)) - (tempModel(i+2) * sin(x_ang)) 
                    tempModel(i+2) = (x3d * sin(x_ang)) + (tempModel(i+2) * cos(x_ang))

                    glVertex3f(tempModel(i), model(i + 1), tempModel(i + 2))
                    i = i + 3     
                   
                wend
            glEnd()                                                       
            SwapBuffers ()   
        else
            x_ang = 0
        endif
       
        if (i > 11) then
            i = 0
        endif     
       
        x_ang = x_ang + 0.01 
    wend

It is still a bit bugged as I am not 100% sure of what I'm doing. But it is a large step in the right direction. The problem I am faced with in implementing these types of rotations in sen3d is that objects are up to 7000 vertex, and each object in the scene might need to rotate at a slightly different angle due to camera perspective. If anybody has any info on this, please let me know!
Title: Re: Software rotations!
Post by: Shockwave on January 18, 2008
Your rotations when there are lots of objects may getting corrupted by "gimbal lock", it's a problem that most people go through when they are writing thier first 3D engine.

Fortunately we've covered this loads of times here and if you do a search you will find a lot of topics with solutions to this, here is one topic that is very relevant to your question though, Tetra came up with a nice solution;

http://dbfinteractive.com/index.php?topic=1843.0 (http://dbfinteractive.com/index.php?topic=1843.0)

Title: Re: Software rotations!
Post by: Jim on January 18, 2008
I'm a bit baffled about what kind of rotations this code is meant to do.  Looks mostly like a 2d rotation for spinning things round on screen...but it's not really the right maths for doing 3d spinny things.
Where are you getting your facts from b0ib0t?  And what is it you really want to achieve?

Jim
Title: Re: Software rotations!
Post by: b0ib0t on January 18, 2008
I'm not really sure about what kind of rotations it is either to be honest.  I got my idea for the math from a few different examples of rotation. One of the places I was looking around was here...

http://pixwiki.bafsoft.com/mags/5/articles/circle/sincos.htm

The code here is an experiment to see if I could just get something to at least rotate. Ultimately I want to create a 3D camera system for my engine, sen3d.
Title: Re: Software rotations!
Post by: Jim on January 18, 2008
Cool, that looks like a pretty good tutorial :)

Have you ever worked with matrixes/matrices before?
One way of looking at a 2d rotation is in a matrix
If you build a 2x2 array like this
Code: [Select]
dim rotate2d(2,2)
rotate2d(0,0)=cos(angle)
rotate2d(1,0)=-sin(angle)
rotate2d(0,1)=sin(angle)
rotate2d(1,1)=cos(angle)
That is a 2d rotation matrix.
Then to do a 2d rotation on a (x,y) position, you can do this
Code: [Select]
dim point2d(2)
point2d(0)=x
point2d(1)=y
dim output2d(2)

output2d(0) = point2d(0) * rotate2d(0,0) + point2d(1) * rotated2d(0,1)
output2d(1) = point2d(0) * rotate2d(1,0) + point2d(1) * rotated2d(1,1)

Effectively that multiplies each row in the matrix with input vector to give you a rotated vector.

Thinking about it this way makes it more easy to look at how to extend it in to 3d.  For instance you can combine 2 matrices together into a single matrix in such a way that the two rotations they represent are added together, one after the other.  A 3d rotation matrix is just a 2d rotation around the x axis followed by a 2d rotation round the y axis followed by a 2d rotation about the z axis.

Jim
Title: Re: Software rotations!
Post by: relsoft on January 19, 2008
Pimpin' my 3d tutes...

Covers rotations, matrices and basic lookat transforms. :*)

Scroll all the way down .:*)

http://rel.betterwebber.com/index.php?action=contents&item=Tutorials
Title: Re: Software rotations!
Post by: Shockwave on January 19, 2008
Your tutorials are fantastic, I'd like to add your site to the front page if that's ok :)
Title: Re: Software rotations!
Post by: relsoft on January 21, 2008
I'd be honored!
 :buddies: