Thread: Is it sphere

  1. #1
    Registered User planet_abhi's Avatar
    Join Date
    Oct 2002
    Posts
    92

    Is it sphere

    I make the following code for sphere in borland c++ ver 3.0
    is it right sphere?

    Code:
    # include<graphics.h>
    # include<conio.h>
    int main()
    {
    int i,cx=220,cy=300;
    int gd=0,gm=0;
    initgraph(gd,&gm,"");
    cleardevice();
    circle(cx,cy,80);
    for(i=10;i<=80;i++)
    {
    ellipse(cx,cy,90,270,i,80);
    ellipse(cx,cy,270,90,i,80);
    ellipse(cx,cy,90,270,80,i);
    ellipse(cx,cy,270,90,80,i);
    }
    getch();
    closegraph();
    restorecrtmode();
    }
    does it represent a 3rd dimension ?
    if not then which is the right way to do that in above code.
    AbHHinaay

  2. #2
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    No.

    I tried the same way as you at first before I did it the mathematical way.


    newx=centerx+(sin(alpha)*cos(beta)*radius);
    newy=centery+(sin(alpha)*sin(beta)*radius);
    newz=centerz+(sin(beta)*radius);

    where:


    -90<alpha<90
    0<beta<360

  3. #3
    Registered User planet_abhi's Avatar
    Join Date
    Oct 2002
    Posts
    92
    But what's that newx,newy,newz
    i can't visualize the z point
    a little idea i have that

    Code:
    void CreateUnitSphere(int dtheta,int dphi)
    {
       int n;
       int theta,phi;
       XYZ p[4];
    
       for (theta=-90;theta<=90-dtheta;theta+=dtheta) {
          for (phi=0;phi<=360-dphi;phi+=dphi) {
             n = 0;
             p[n].x = cos(theta*DTOR) * cos(phi*DTOR);
             p[n].y = cos(theta*DTOR) * sin(phi*DTOR);
             p[n].z = sin(theta*DTOR);
             n++;
             p[n].x = cos((theta+dtheta)*DTOR) * cos(phi*DTOR);
             p[n].y = cos((theta+dtheta)*DTOR) * sin(phi*DTOR);
             p[n].z = sin((theta+dtheta)*DTOR);
             n++;
             p[n].x = cos((theta+dtheta)*DTOR) * cos((phi+dphi)*DTOR);
             p[n].y = cos((theta+dtheta)*DTOR) * sin((phi+dphi)*DTOR);
             p[n].z = sin((theta+dtheta)*DTOR);
             n++;
             if (theta > -90 && theta < 90) {
                p[n].x = cos(theta*DTOR) * cos((phi+dphi)*DTOR);
                p[n].y = cos(theta*DTOR) * sin((phi+dphi)*DTOR);
                p[n].z = sin(theta*DTOR);
                n++;
             }
    
             /* Do something with the n vertex facet p */
    
          }
       }
    }
    
    
    
    
    
    0<alpha<360
    -90<beta<90
    
    newx=centerx+((cos(alpha)*cos(beta))*radius)
    newy=centery+((cos(alpha)*sin(beta))*radius)
    newz=centerz+((sin(alpha))*radius


    but how can i draw the actual sphere its just the base logic
    AbHHinaay

  4. #4
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    No no no. I'm not sure why you are multiplying three times. You only need the one.

    The new variables are the new coordinates. If you want to create a sphere around something other than 0,0,0 then you must displace the points by a set amount to create a sphere around that point. The radius is simple - it displaces the point from centerx,centery,centerz.

    This is for a 3D sphere. Now in order to create an actual sphere and draw it...u must think a bit. Let's say we want 16 facets in our sphere. To achieve this in 360 degrees of arc we do 360/16 to get the amount of angle we must increment on each iteration through alpha. As well you will want to increment beta by this too to get perfect quads in your sphere. But how do we draw lines when all we have is set amount of vertexes out in memory?

    Simple. We know we have 16 facets right? After 16 facets we move to the next 'level' of the sphere.

    So if we are the start we do this:

    Draw a line between vertex 0 and vertex 1
    Draw a line between vertex 0 and vertex 16
    Draw a line between vertex 1 and vertex 17
    Draw a line between vertex 16 and vertex 17

    0........1
    . .
    . .
    . .
    16......17

    Thus you have your quad. Send these vertexes to a polygon function or to DirectX - along with a texture or portion of a texture and you can texture your sphere - either a diff texture on each quad or like a planet with a large texture across the whole thing - each quad being only a section of the larger texture.

    If you have implemented this formula you know that alpha is the rotation around the sphere on the x axis (lattitude) and beta is the rotation on the y axis (longitude). If you only go from -90 to 0 you will have half a sphere - like a dome. If you only go from 0 to 180 you will have half a sphere (like a dome turned on its side - half of an egg or something)

  5. #5
    Registered User planet_abhi's Avatar
    Join Date
    Oct 2002
    Posts
    92
    Could you please send me the code if not written in OpenGl
    plain c
    i'will be thankful to you
    AbHHinaay

  6. #6
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Yeah....now I just gotta remember where I put it.



Popular pages Recent additions subscribe to a feed

Similar Threads

  1. What is wrong with my code?
    By dakarn in forum C Programming
    Replies: 6
    Last Post: 10-14-2008, 05:16 AM
  2. Lighting a Direct3D 9 mesh sphere
    By bennyandthejets in forum Game Programming
    Replies: 12
    Last Post: 02-14-2005, 01:19 AM
  3. Sphere code not working
    By VirtualAce in forum Game Programming
    Replies: 2
    Last Post: 10-03-2004, 07:29 AM
  4. Sphere backdrop problems
    By VirtualAce in forum Game Programming
    Replies: 26
    Last Post: 10-01-2004, 05:10 PM
  5. What am I doing wrong? Help please...
    By SprinterSteve in forum C Programming
    Replies: 9
    Last Post: 04-17-2003, 09:35 PM