hi there,i’ve got some code for you…
it’s the “hard way” for rendering a hemisphere.sorry,but it’s only for borland c…
if you want to do this in openGL,you should check out things like NURBS and meshes…
//
//this code can be compiled only in borland c++
//
#include <graphics.h>
#include <stdlib.h>
#include <iostream.h>
#include <conio.h>
#include <math.h>
#include <dos.h>
#define Pi 3.1415926536
enum Action{move,draw};
struct Point3D
{
int x;
int y;
int z;
Action action;
};
//this function init’s graphics mode in borland c++
void init_gr(void)
{
//detect the hardware
int gdriver = DETECT, gmode, errorcode;
initgraph(&gdriver, &gmode, "");
errorcode = graphresult();
if (errorcode != grOk) //an error occured
{
cout << "error: " << endl << grapherrormsg(errorcode) << endl;
cout << "press a key:";
getch();
exit(1);
}
}
//ends the gr.mode in borland c++
void end_gr(void)
{
closegraph();
}
//moves the origin:
void MoveTo(int x, int y)
{
moveto(x,y);
}
void LineTo(int x, int y)
{
lineto(x,y);
}
void draw3Dobject(Point3D *object, int N, float rho, float theta,
float phi, float dist_to_screen, int xshift, int yshift)
{
int x,y;
float xe,ye,ze,costh,sinph,cosph,sinth,v11,v12,v13,v21,v22,v32,v33,v23,v43;
/* get the coefficients /
costh=cos(theta);
sinth=sin(theta);
cosph=cos(phi);
sinph=sin(phi);
v11=-sinth; v12=-cosphcosth; v13=-sinphcosth;
v21=costh; v22=-cosphsinth; v23=-sinphsinth;
v32=sinph;
v33=-cosph;
v43=rho;
for (int i=0;i<N;i++)
{
//get the camera position
xe=v11(object+i)->x+v21*(object+i)->y;
ye=v12*(object+i)->x+v22*(object+i)->y+v32*(object+i)->z;
ze=v13*(object+i)->x+v23*(object+i)->y+v33*(object+i)->z+v43;
// screen coordinates
x=dist_to_screenxe/ze+xshift;
y=dist_to_screenye/ze+yshift;
//drawing
if((object+i)->action==move)
MoveTo(x,y);
else
LineTo(x,y);
}
}
int main(void)
{
const int n=10; //number of segments in the hemisphere
Point3D hemisphere[8nn+n]; //all points of the hemisphere
float rho=1800,theta=Pi,phi=3Pi/4,dist_to_screen=600; //camera position
int xshift=300, yshift=250;
float delta=Pi/(2n), R=300; /* parameters of the hemisphere */
float alpha,cosa,sina,beta,cosb,sinb; //support variables
// Init
init_gr();
// generate the hemisphere /
for (int i=0;i<4n;i++)
{
alpha=idelta;
cosa=cos(alpha);
sina=sin(alpha);
for (int j=0;j<n;j++)
{
beta=jdelta;
sinb=sin(beta);
cosb=cos(beta);
hemisphere[i*n+j].x=Rcosasinb;
hemisphere[i*n+j].y=Rsinasinb;
hemisphere[i*n+j].z=-Rcosb;
hemisphere[i*n+j].action=(j==0?move:draw);
}
}
int c=4nn;
for (i=0;i<n;i++)
{
beta=idelta;
sinb=sin(beta);
cosb=cos(beta);
for (int j=0;j<4n+1;j++)
{
alpha=jdelta;
cosa=cos(alpha);
sina=sin(alpha);
hemisphere[c+i*(4n+1)+j].x=Rcosasinb;
hemisphere[c+i(4n+1)+j].y=Rsinasinb;
hemisphere[c+i(4n+1)+j].z=-Rcosb;
hemisphere[c+i*(4*n+1)+j].action=(j==0?move:draw);
}
}
//drawing
draw3Dobject(hemisphere,8nn+n,rho,theta,phi,dist_to_screen,xshift,yshift);
//shutdown
getch();
end_gr();
return 0;
}