here’s my source code:
#include <iostream.h>
#include <math.h>
#include <GL/glut.h>
class Point{
public:
Point(){
x=y=z=0.0;
}
Point(double X,double Y,double Z){
x=X;y=Y;z=Z;
}
double x,y,z;
bool exists;
};
class Voxel{
public:
Voxel(){}
Voxel(double x,double y, double z, int r){
location.x = x;
location.y = y;
location.z = z;
radius = r;
}
void draw(){}
void move(Point p){
location.x += p.x;
location.y += p.y;
location.z += p.z;
}
Point location;
int radius;
};
class Vector{
public:
Vector(){dx=dy=dz=0;}
Vector(double x, double y, double z){
dx = x; dy = y; dz = z;}
double dx;
double dy;
double dz;
};
ostream& operator<<(ostream &os, Point p){
os<<p.x<<" “<<p.y<<” "<<p.z;
return os;
}
ostream& operator<<(ostream &os, Voxel v){
os<<v.location;
return os;
}
ostream& operator<<(ostream &os, Vector v){
os<<v.dx<<" “<<v.dy<<” "<<v.dz;
return os;
}
bool operator==(Point a, Point b){
return (a.x==b.x && a.y==b.y && a.z==b.z);
}
Vector CrossProd(Vector v1, Vector v2){
Vector v(v1.dyv2.dz-v1.dzv2.dy, v1.dzv2.dx-v1.dxv2.dz, v1.dxv2.dy-v1.dyv2.dx);
return v;
}
void Fix(Vector &v){
if (v.dx == 0) v.dx = .0000001;
if (v.dy == 0) v.dy = .0000001;
if (v.dz == 0) v.dz = .0000001;
}
double Dx(Point view, Vector vv, Voxel voxel){
Fix(vv);
Vector vvv(vv.dx*-1,-1*(vv.dx*-1*vv.dx + vv.dz*-1*vv.dz)/vv.dy,vv.dz*-1);
vv = CrossProd(vvv,vv);
double d = -1*(vv.dx*(view.x+1) + vv.dy*(view.y+1) + vv.dz*(view.z-(vv.dx+vv.dy)/vv.dz));
return
(vv.dx*voxel.location.x + vv.dy*voxel.location.y + vv.dz*voxel.location.z + d)
/ sqrt(vv.dx*vv.dx + vv.dy*vv.dy + vv.dz*vv.dz);
}
double Dy(Point view, Vector vv, Voxel voxel){
Fix(vv);
vv.dy = -1*(vv.dx*-1*vv.dx + vv.dz*-1*vv.dz)/vv.dy;
vv.dx = vv.dx*-1;
vv.dz = vv.dz*-1;
double d = -1*(vv.dx*(view.x+1) + vv.dy*(view.y+1) + vv.dz*(view.z-(vv.dx+vv.dy)/vv.dz));
return
(vv.dx*voxel.location.x + vv.dy*voxel.location.y + vv.dz*voxel.location.z + d)
/ sqrt(vv.dx*vv.dx + vv.dy*vv.dy + vv.dz*vv.dz);
}
double Dz(Point view, Vector vv, Voxel voxel){
Fix(vv);
double d = -1*(vv.dx*(view.x+1) + vv.dy*(view.y+1) + vv.dz*(view.z-(vv.dx+vv.dy)/vv.dz));
return
(vv.dx*voxel.location.x + vv.dy*voxel.location.y + vv.dz*voxel.location.z + d)
/ sqrt(vv.dx*vv.dx + vv.dy*vv.dy + vv.dz*vv.dz);
}
Point p[30000];
Voxel v[30000];
Point view(10,10,10);
Vector vv(4,4,4);
Vector vvv(1/4,1,-1/4);
int winH=640, winW=480;
//-------------------------Miller’s code, modified by me--------------------------------------
// Draw the axis’
void DrawAxis(void)
{
int step = 10;
int hashWidth = 5;
glColor3f(0.5, 0.5, 0.5);
// Hash marks for the y - axis
for (int y = step; y < winH / 2; y += step)
{
glBegin(GL_LINES);
glVertex2f(-hashWidth, y);
glVertex2f(hashWidth, y);
glVertex2f(-hashWidth, -y);
glVertex2f(hashWidth, -y);
glEnd();
}
// Hash marks for the x - axis
for (int x = step; x < winW / 2; x += step)
{
glBegin(GL_LINES);
glVertex2f(x, hashWidth);
glVertex2f(x, -hashWidth);
glVertex2f(-x, hashWidth);
glVertex2f(-x, -hashWidth);
glEnd();
}
glColor3f(1, 1, 1);
glBegin(GL_LINES);
// x - axis
glVertex2f(-winW / 2, 0);
glVertex2f(winW / 2, 0);
// y - axis
glVertex2f(0, winH / 2);
glVertex2f(0, -winH / 2);
glEnd();
}
void DrawPoints(Point view, Vector vv)
{
double dx, dy, dz;
int finalDx, finalDy;
for (int q=0; q<30000; q++)
if (v[q].location.exists)
{
dx = Dx(view, vv, v[q]);
dy = Dy(view, vv, v[q]);
dz = Dz(view, vv, v[q]);
if (dz>0){
if (dx>0) finalDx = dx - (dz/dx/10);
if (dx<0) finalDx = dx + (dz/dx/10);
if (dx==0) finalDx = 0;
if (dy>0) finalDy = dy - (dz/dy/10);
if (dy<0) finalDy = dy + (dz/dy/10);
if (dy==0) finalDy = 0;
p[q].exists = true;
p[q].x = finalDx;
p[q].y = finalDy;
}
}
}
void glutDisplay(void)
{
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
for (int q=0; q<30000; q++)
if (p[q].exists)
{
glColor3f(1, 0, 0.5);
glBegin(GL_POINTS);
glVertex2f(p[q].x, p[q].y);
glEnd();
}
DrawAxis();
glutSwapBuffers();
}
void glutMouse(int button, int state, int x, int y)
{
if (button == GLUT_LEFT_BUTTON)
{
view.x = view.x + vv.dx;
view.y = view.y + vv.dy;
view.z = view.z + vv.dz;
}
else if (button == GLUT_RIGHT_BUTTON){
view.x = view.x - vv.dx;
view.y = view.y - vv.dy;
view.z = view.z - vv.dz;
}
DrawPoints(view,vv);
glutPostRedisplay();
}
void glutResize(int w, int h)
{
if (!h)
return;
winW = w;
winH = h;
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glViewport(0, 0, winW, winH);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
// Creates a 2D projection
// winW / 2 is far right, - is far left
// winH / 2 is top, - is bottom
glOrtho(-winW / 2, winW / 2, -winH / 2, winH / 2, -1, 1);
glutPostRedisplay();
}
void glInit (void)
{
glClearColor(0, 0, 0, 0);
glPointSize(2);
// Makes our points round rather than boxes
glEnable(GL_POINT_SMOOTH);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
}
//---------------------------------------Miller’s code-------------------------------
void main(){
for (int t=0; t<30000; t++){
v[t].location.exists = false;}
double a;
int yy;
int pos = 0;
for ( yy=0; yy<40; yy++){
for (a=0.0; a<6.1; a = a + .3){
v[pos].location.x = cos(a)*40.0;
v[pos].location.y = sin(a)*40.0;
v[pos].location.z = 20 + yy;
v[pos].location.exists = true;
pos++;
}
}
for (t=0; t<300; t+=3){
for (yy=0; yy<300; yy+=3){
v[pos].location.x = t;
v[pos].location.y = yy;
v[pos].location.z = 50;
v[pos].location.exists = true;
pos++;
}
}
for (a=0.0; a<6.1; a = a + .0061){
v[pos].location.x = 20;
v[pos].location.y = sin(a)*200.0;
v[pos].location.z = cos(a)*200.0;
v[pos].location.exists = true;
pos++;
}
cout<<"The number of Voxels displayed is: "<<pos<<endl;
DrawPoints(view, vv);
glutInitDisplayMode (GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
glutInitWindowSize (640,480);
glutCreateWindow ("View");
glutDisplayFunc (glutDisplay);
glutReshapeFunc (glutResize);
glutMouseFunc(glutMouse);
glInit();
glutMainLoop();
}