3D Sphere Rendering - Programmers Heaven

#### Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

#### Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

# 3D Sphere Rendering

Posts: 1Member
Hi everybody, I'm into this project to create a 3D sphere as my semester project in Computer Graphics.

I'm using Borland C++ Builder as my environment, if I'm correct, to generate 3D graphics but now I have this problem to render the surfaces of the sphere.

It would be a great help if anybody could look into my problem...!

//---------------------------------------------------------------------------

#include
#pragma hdrstop
#include "math.h"
#include "matrix_and_vector.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
#define pi 3.141592654
#define ALPHA ((pi/180)*63.4)
#define GAMMA ((pi/180)*60)
#define L (1/tan(ALPHA))
#define sNmax 30

TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void matrix_multiply(float vrtx[4],float trnx_matrix[4][4])
{
float ans [4]={0,0,0,0};
int i,j;
for (i=0;i<4;i++)
{
for (j=0;j<4;j++)
{
ans[i]+=trnx_matrix[i][j]*vrtx[j];
}
}
for (i=0;i<4;i++)
{
vrtx[i]=ans[i];
}
}

struct vertex
{
float vrtx[4];
float xp,yp;
public:
void vertex_input(float x1,float y1,float z1)
{
vrtx[0]=x1;vrtx[1]=y1;vrtx[2]=z1;vrtx[3]=1;
}

void operator =(vertex v1)
{
vrtx[0]=v1.vrtx[0];
vrtx[1]=v1.vrtx[1];
vrtx[2]=v1.vrtx[2];
}

void projected_vertex()
{
xp=vrtx[0]+vrtx[2]*L*cos(GAMMA);
yp=vrtx[1]+vrtx[2]*L*sin(GAMMA);
}
};

void translation(vertex v[sNmax+1][sNmax+1],float x,float y,float z)
{
int i,j;
float trns_matrix[4][4]={1,0,0,x,
0,1,0,y,
0,0,1,z,
0,0,0,1};
for (i=0;i<=sNmax;i++)
{
for (j=0;j<=sNmax;j++)
{
matrix_multiply(v[i][j].vrtx,trns_matrix);
}
}
}

class edge
{
private:
vertex v1;
vertex v2;
public:
void edge_input(vertex v11,vertex v22)
{
v1=v11;v2=v22;
}
void plot_edge()
{
v1.projected_vertex();
v2.projected_vertex();
Form1->Canvas->Pen->Color=clRed;
Form1->Canvas->MoveTo(v1.xp,v1.yp);
Form1->Canvas->LineTo(v2.xp,v2.yp);
}
};

class edge_table
{
private:
edge edge_lat[sNmax][sNmax];
edge edge_long[sNmax][sNmax];

public:
void edge_table_lat(vertex v[sNmax+1][sNmax+1])
{
int i,j;
for (i=0;i=-90,a<=sNmax;phi-=(180/sNmax),a++)
{
for (theta=180,b=0;theta>=-180,b<=sNmax;theta-=(360/sNmax),b++)
{
xs=r*cos((pi/180)*phi)*cos((pi/180)*theta);
ys=r*cos((pi/180)*phi)*sin((pi/180)*theta);
zs=r*sin((pi/180)*phi);
v[a][b].vertex_input(xs,ys,zs);
}
}
translation(v,x,y,z);
}

void sphere_edge_table()
{
sphere_edge.edge_table_lat(v);
sphere_edge.edge_table_long(v);
}

};

void __fastcall TForm1::Button1Click(TObject *Sender)
{
sphere s(200,200,200,150);
s.sphere_vertex_table();
s.sphere_edge_table();
}
//---------------------------------------------------------------------------