hey all i have a 3D cube in wireframe that i am rendering and for some reason i am getting a diagonal line only at certain distances from the camera...mainly when the cube is close to the camera. at first i thought i would drawing the cube in the incorrect order so i tried to comment out but just one side to see but i am still getting the artifact. here is my code below. Reader class is just a class that reads a file, goes through the file and calculate the minx/maxx, miny/maxy, minz/maxz and from that i know how big in each axis to draw my bounding box. the file has about 1.3 million points and in the render below i am only rendering about 600,000.
again i dont see the diagonal line when i am further away from the camera but i dont know how my gluLookAt or perspective setup is messing things up. my "eye" is set so big b/c if i don't it seems that moving the camera around does some really strange things like the cube disappearing and moving in weird fashion. anyway here is the code and render below. thanks for your help!
this image is
here
#include "stdafx.h"
//#ifdef WIN32
//#include "stdafx.h" //windows stuff
//#endif
#include "reader.h"
#include <iostream>
#include <fstream>
#include <vector>
#include <glut.h>
#include <math.h>
using namespace std;
float x;
float y;
float z;
float rot1 = 0.0;
float rot2 = 0.0;
float speed = 100.0;
reader::bounding_box box;
vector<reader::point> coord;
void initScene()
{
glShadeModel(GL_SMOOTH); // Enable Smooth Shading
glClearColor(0.0f, 0.0f, 0.0f, 0.5f); // Black Background
glClearDepth(1.0f); // Depth Buffer Setup
//glEnable(GL_LIGHTING); //only enable if created own lights
glEnable(GL_DEPTH_TEST); // Enables Depth Testing
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_LINE_SMOOTH);
glEnable(GL_COLOR_MATERIAL);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); // Really Nice Perspective Calculation */
reader *a = new reader("output.dat");
box = a->get_bounding_box();
cout << "init scene: " << box.minx << " " << box.maxx << " " << box.miny << " " << box.maxy << " " << box.minz << " " << box.maxz << endl;
x = (box.maxx - box.minx) / 2;
y = (box.maxy - box.miny) / 2;
z = box.minz - 200.0;
a->get_data(coord);
}
void keys(unsigned char key, int x, int y)
{
if (key == 27) exit(0);
glutPostRedisplay(); //time to redraw and update
}
void resize(int w, int h)
{
// Prevent a divide by zero, when window is too short
// (you cant make a window of zero width).
if(h == 0)
h = 1;
float ratio = 1.0 * (GLfloat)w / (GLfloat)h;
// Reset the coordinate system before modifying
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
// Set the viewport to be the entire window
glViewport(0, 0, w, h);
// Set the correct perspective.
gluPerspective(45.0, ratio, 0.1, 5000.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void render()
{
//cout << x << " " << y << " " << z << endl;
glLoadIdentity();
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
gluLookAt(x, y, z, 0.0, 0.0, 100000.0, 0.0, 1.0, 0.0); //cam pos, eye, up
glPushMatrix();
glColor3f(1.0, 1.0, 1.0);
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
glRotatef(rot1, 0, 1, 0);
glBegin(GL_QUADS);
//front quad
glVertex3f(box.minx, box.miny, box.minz);
glVertex3f(box.maxx, box.miny, box.minz);
glVertex3f(box.maxx, box.maxy, box.minz);
glVertex3f(box.minx, box.maxy, box.minz);
//right quad
/*glVertex3f(box.maxx, box.miny, box.minz);
glVertex3f(box.maxx, box.miny, box.maxz);
glVertex3f(box.maxx, box.maxy, box.maxz);
glVertex3f(box.maxx, box.maxy, box.minz);
//rear quad
glVertex3f(box.maxx, box.miny, box.maxz);
glVertex3f(box.minx, box.miny, box.maxz);
glVertex3f(box.minx, box.maxy, box.maxz);
glVertex3f(box.maxx, box.maxy, box.maxz);
//left quad
glVertex3f(box.minx, box.miny, box.maxz);
glVertex3f(box.minx, box.miny, box.minz);
glVertex3f(box.minx, box.maxy, box.minz);
glVertex3f(box.minx, box.maxy, box.maxz);*/
glEnd();
glPopMatrix();
glPushMatrix();
//glLoadIdentity();
glColor3f(1.0, 0.0, 0.0);
glPointSize(0.5);
glEnable(GL_POINT_SMOOTH);
//*****change to display list*****//
glBegin(GL_POINTS);
for(int i = 0; i < coord.size(); i+=200)
glVertex3f(coord.x, coord.y, coord.z);
glEnd();
glPopMatrix();
rot1 += 2.0;
glutSwapBuffers();
}
void special(int key, int x1, int y1)
{
switch (key)
{
case GLUT_KEY_LEFT :
x -= speed;
break;
case GLUT_KEY_RIGHT :
x += speed;
break;
case GLUT_KEY_UP :
y += speed;
break;
case GLUT_KEY_DOWN :
y -= speed;
break;
case GLUT_KEY_PAGE_UP :
z += speed;
break;
case GLUT_KEY_PAGE_DOWN :
z -= speed;
break;
}
glutPostRedisplay();
}
int _tmain(int argc, _TCHAR* argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
glutInitWindowPosition(150,100);
glutInitWindowSize(800,600);
glutCreateWindow("Dispersion of Source");
initScene();
glutDisplayFunc(render);
glutIdleFunc(render);
glutReshapeFunc(resize);
glutKeyboardFunc(keys);
glutSpecialFunc(special);
glutMainLoop();
return 1;
}
*****output******
Quote:
set of points: 1325537
init scene: -629.096 590.87 -99.97 693.241 -586.534 593.4