# artifact in 3D cube - where does it come from?

This topic is 4089 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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 <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;

void initScene()
{
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 */

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);

// 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);
}

void render()
{
//cout << x << " " << y << " " << z << endl;
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);

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);
/*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);
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);
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();
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

##### Share on other sites
hi all i just moved my code over to my main computer here at home and i dont see any artifacts at any range from the camera. it has an NV 6800.

the laptop computer where i was noticing this problem as an ATI Mobility Radeon XPRESS 200.

is there anything in my code that the GPU doesnt like? this is really weird. anyway i thought i would bump up to see if anyone had any ideas. thanks!

##### Share on other sites
Might not be much help, but my ATI 1950 has a few problems with OpenGL. Apparently ATI's driver support for OGL is fairly dodgy (no Non-power-of-2 textures!?), so it may just be the GPU's problem rather than your code's

##### Share on other sites
It sounds like you are hitting your far/near clipping planes and the lines you are seeing are a result of your rendered quad intersecting with those planes and being cut.

 If any portion of your cube crosses the z= 0.1 or z=5000.0 then the above is most likely the issue.

##### Share on other sites
Quote:
 Original post by ju2wheelsIt sounds like you are hitting your far/near clipping planes and the lines you are seeing are a result of your rendered quad intersecting with those planes and being cut. If any portion of your cube crosses the z= 0.1 or z=5000.0 then the above is most likely the issue.

why would this not be GPU independent? as i said with the NV card i get no problems. seems like clipping planes would be un-biased to any particular hardware

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 14
• 14
• 45
• 22
• 27
• ### Forum Statistics

• Total Topics
634044
• Total Posts
3015211
×