PDA

View Full Version : Invisible points in 3D Mesh



Vishnudev
05-02-2017, 11:54 PM
I have created a 3D Mesh using glut. The code is as below. My problem here is whenever I try changing y-axis translation of any point either it doesn't change or it isnt visible. But when I change y-axis of a set of points as given in the line-comment it works.



#include <GL/glut.h>
#include <stdio.h>
void init(void)
{
glClearColor(0,0,0,0);

}
void idleFunction(void)
{
glutPostRedisplay();
}
void DrawCube(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity();
glTranslatef(0.0,0.0,-6.5);
glPointSize(2.0);
for(float i=-2.0;i<=2.0;i+=0.05)
for(float j=-1.0;j<=4.0;j+=0.1)
{
// if(i>=-0.5 &&i<=-0.4 && j>1.0 &&j<1.26){ *****This works*****
if (i==-1.8){ // ******This doesn't******
glBegin(GL_POINTS);
glColor3f(0.0f,1.0f,0.0f);
glVertex3f(i,-0.2f,j);
glEnd();
}
else
{
glBegin(GL_POINTS);
glColor3f(0.0f,1.0f,0.0f);
glVertex3f( i,-1.0f, j);
glEnd();

}
}
glFlush();
}


void reshape(int x, int y)
{
glViewport(0,0,x,y); //Use the whole window for rendering
if (y == 0 || x == 0) return; //Nothing is visible then, so return
//Set a new projection matrix
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(50,(float)x/(float)y,3.5,1000);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}

int main(int argc, char** argv)
{

glutInit(&argc, argv);
//we initizlilze the glut. functions
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowSize(800,600);
glutInitWindowPosition(100, 100);
glutCreateWindow(argv[0]);
init();
glutDisplayFunc(DrawCube);
glutReshapeFunc(reshape);
glutIdleFunc(idleFunction);

glutMainLoop();
return 0;
}

Cornix
05-03-2017, 02:21 AM
Why do you believe your float i will ever be exactly -1.8?
Floats are not exact. They are not accurate. Never compare a float value with a constant except for perhaps 0.0 or low integers.

Try to check the following: (i >= -1.8 - e && i <= -1.8 + e) with a small e, perhaps e = 0.05.

GClements
05-03-2017, 04:07 PM
Floats are not exact.
Floats are exact. It's the results of calculations involving them which may not be.



Never compare a float value with a constant except for perhaps 0.0 or low integers.

Small integers (below 224 for single-precision or 253 for double precision) can be represented exactly, as can fractions arising from dividing such integers by a power of two.

But aside from whether the constant can be represented exactly, you also need to consider whether the value being compared against it will have had rounding error introduced by the calculations which produced it.

In the OP's code, testing i==1.0 would be equally unreliable. Even though 1.0 can be represented exactly, 0.05 can't be, so adding 0.05 to a number twenty times doesn't produce the same result as adding 1.0 once. In fact, the effect of adding 0.05 to a number twenty times depends upon the number you start with, as the rounding error at each step may depend upon the actual value being rounded.

To avoid accumulating rounding error when generating an arithmetic sequence, use an integer variable for the loop counter and apply a scale and offset at each step. IOW, use


for (int i = 0; i < n; i++) {
float x = a+b*i;
...
}

rather than


for (float x = a; x < a+b*n; x += b) {
...
}

Vishnudev
05-10-2017, 02:44 AM
Thanks for the help guys! Problem was the use of float in for loop.