Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Cannot Generate 3D spherical Model.
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
copthesaint




PostPosted: Sat Mar 24, 2012 6:18 pm   Post subject: Cannot Generate 3D spherical Model.

I have been spending the last week trying to figure out how to generate 3D Spheres for my remake of my asteroids game Ive been working on. However I am having trouble with the faces. What this class does is first get the amount of vertices for the sphere then it sets the values of x, y, and z for the vertices, then with that information it creates faces "triangles" for the points. The generator has to be dynamic so that when The Quality increases or decreases, then the amount of faces change for the sphere.
I've spent alot of time trying to get just one side of the sphere to appear however I have been getting poor results every way Ive been trying to do this. This is my third time remaking this class. In the .exe Ive added, Ive changed a few of the values from below. The quality is set to 8 just so more points are created so I can show more faces, and set (quality - 2) *for creating points* to 5 just so you can see how it shows the triangles further down.

I really need some help, I defiantly need another opinion.

Turing:
class AsteroidGenerator
    import Model3D
    export InitializeAsteroid, ExportAsteroid
    var boundSet : flexible array 0 .. -1 of int
    var points : flexible array 0 .. -1 of array 0 .. 2 of real
    var faces : flexible array 0 .. -1 of array 0 .. 2 of int
    var facesBound, pointsBound, boundSize : int := -1
    var quality : int := 4

    function GetPoint (i : int, j : int) : int
        var tempNum : int := 0
        for y : 0 .. i - 1
            tempNum := tempNum + (boundSet (y))
        end for
        tempNum := tempNum + j
        result tempNum
    end GetPoint

    procedure AddFace (x, y, z : int)
        facesBound := facesBound + 1
        if facesBound > upper (faces) then
            new faces, facesBound
        end if
        faces (facesBound) (0) := x
        faces (facesBound) (1) := y
        faces (facesBound) (2) := z
    end AddFace

    procedure AddFaces (p : array 0 .. 4 of int)
        for i : 1 .. 3
            AddFace (p (0), p (i), p (i + 1))
        end for
    end AddFaces


    procedure InitializeAsteroid (q : int, width, height, depth : real)
        boundSize := (quality * 2) - 1
        if boundSize > upper (boundSet) then
            new boundSet, boundSize
        end if
        var pointsSet : int := 1
        boundSet (0) := 1
        for i : 1 .. quality
            pointsSet := boundSet (0)
            for j : 1 .. i - 1
                pointsSet := pointsSet + boundSet (j)
            end for

            boundSet (i) := pointsSet + 2
        end for

        for i : 0 .. (quality - 1)
            boundSet (boundSize - i) := boundSet (i)
        end for

        for i : 0 .. boundSize
            var DepthPoints : real := cosd (180 / boundSize) * i
            var AngleInc : real := (360 / boundSet (i))
            put boundSet (i)
            for j : 0 .. boundSet (i) - 1
                pointsBound := pointsBound + 1
                if pointsBound > upper (points) then
                    new points, pointsBound
                end if
                points (pointsBound) (0) := (width * DepthPoints) * cosd (AngleInc * j) * 0.01
                points (pointsBound) (1) := (height * DepthPoints) * sind (AngleInc * j) * 0.01
                points (pointsBound) (2) := (depth * DepthPoints) * 0.01

            end for
        end for

        for i : 0 .. quality - 2
            for j : 0 .. boundSet (i) - 1
                var p : array 0 .. 4 of int
                p (0) := GetPoint (i, j)
                p (1) := GetPoint (i + 1, j * 2 - 1)
                p (2) := GetPoint (i + 1, j * 2 + 0)
                p (3) := GetPoint (i + 1, j * 2 + 1)
                p (4) := GetPoint (i, j + 1)
                if j = 0 then
                    p (1) := GetPoint (i + 1, boundSet (i + 1)) - 1
                end if
                if j = boundSet (i) then
                    p (4) := GetPoint (i, 0)
                end if
                AddFaces (p)
            end for
        end for

       

        for i : 0 .. facesBound
            put "Face #", i, " {", faces (i) (0), ",", faces (i) (1), ",", faces (i) (2), "} " ..
            View.Update
            %delay (500)
        end for
        put ""
        put "NumFaces: ", facesBound, " NumPoints: ", pointsBound
        View.Update
        delay (1000)
    end InitializeAsteroid

    function ExportAsteroid : pointer to Model3D
        var Asteroid3D : ^Model3D
        new Model3D, Asteroid3D

        Asteroid3D -> SetConstants (pointsBound + 1, facesBound + 1)
        var exportPoints : array 1 .. (pointsBound + 1) of array 1 .. 3 of real
        for i : 1 .. (pointsBound + 1)
            for j : 1 .. 3
                exportPoints (i) (j) := points (i - 1) (j - 1)
            end for
        end for
        Asteroid3D -> AddPoints (exportPoints)
        var exportFaces : array 1 .. (facesBound + 1) of array 1 .. 3 of int
        for i : 1 .. (facesBound + 1)
            for j : 1 .. 3
                exportFaces (i) (j) := faces (i - 1) (j - 1)
            end for
        end for
        Asteroid3D -> AddFaces (exportFaces)


        result Asteroid3D
    end ExportAsteroid


end AsteroidGenerator



Asteroids 3D 0.5.0.exe.zip
 Description:

Download
 Filename:  Asteroids 3D 0.5.0.exe.zip
 Filesize:  354.04 KB
 Downloaded:  66 Time(s)

Sponsor
Sponsor
Sponsor
sponsor
Zren




PostPosted: Sat Mar 24, 2012 7:58 pm   Post subject: RE:Cannot Generate 3D spherical Model.

The keyword you're looking for is 'mesh'.

And you can't really say "only use this many vertices". You might run into situations where there aren't enough, or too many.

The smallest 3D shape is a tetrahedron (4 sided dice). Sphere's seem to start with a 8 sided diamond made of triangles for better symmetry though. That's probably since all the starting points are on the axes.

Imagine breaking up each of it's triangles into 3 (1 new vertice in the middle) or 4 (triforce) smaller triangles. For a sphere, each point would be pushed sphereRadius away from the origin of the sphere.

http://sol.gfxile.net/sphere/index.html

Posted Image, might have been reduced in size. Click Image to view fullscreen.


From there, you could just randomize everything like:

* radius
* skip a triangles to break up (make sure you don't skip many at the higher depths)
* shift the angle to the point from sphere origin a bit.
copthesaint




PostPosted: Sat Mar 24, 2012 9:43 pm   Post subject: RE:Cannot Generate 3D spherical Model.

Ahh, I understand Thank you zren hadnt thought of it that way. I will try thanks.
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 3 Posts ]
Jump to:   


Style:  
Search: