Display 3d point - c++

I am trying to display 3d point (50,30,20) using opengl but noting is displayed on the screen. How can I solve it?
init:
void init()
{
glClearColor(0.0, 0.0, 0.0, 0.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(20.0, 70.0, 10.0, 40.0, 10.0, 30.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
Display:
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3f(1.0, 1.0, 1.0);
glPointSize();
glBegin(GL_POINTS);
glVertex3f(50.0, 30.0, 20.0);
glEnd();
glFlush();
glutSwapBuffers();
}

Your point is clipped by the near plane of the orthographic projection:
glOrtho(20.0, 70.0, 10.0, 40.0, 10.0, 30.0);
glVertex3f(50.0, 30.0, 20.0);
The OpenGL coordinate system is a right handed system (see Right-hand rule). In view space the y axis points upwards and the x axis points to the right. Since the z axis is the Cross product of the x and y axis, it points out of the view.
Hence you've to shift the point along the negative z axis in between the near and far plane:
glVertex3f(50.0, 30.0, 20.0);
glVertex3f(50.0, 30.0, -20.0);

Related

Perspective projection for a polygon Using OpenGL

I'm trying to implement a perspective projection using open-GL
But when I apply gluPerspective(0,0.5,0.5,5) method, the polygon is not shown in a perspective view and shown in orthogonal view instead
here is the output
Anyone can help
my code:
#include<GL/glut.h>
float angle = 2;
void myinit(void)
{
glClearColor(1.0, 1.0, 1.0, 0.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
//glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 5);
//glFrustum(-0.5, 2.4, -0.5, 0.5, -0.5, 0.5);
//glFrustum(-5.0, 5.0, -5.0, 5.0, 5, 100);
gluPerspective(0,0.5,0.5,5);
}
void polygon(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(0.0, 0.0, 1.0);
//glLineWidth(2);
//glRotatef(angle, 0.0, 0.0, 1.0);
glBegin(GL_POLYGON);
glVertex3f(0.25, 0.25, 0.0);
glVertex3f(0.75, 0.25, 0.0);
glVertex3f(0.75, 0.75, 0.0);
glVertex3f(0.25, 0.75, 0.0);
//glVertex3f(0, 0.5, 0.0);
glEnd();
glFlush();
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowPosition(50, 100);
glutInitWindowSize(1000, 1000);
glutCreateWindow("Open Gl 2D Geometric Transformation");
myinit();
glutDisplayFunc(polygon);
glutMainLoop();
return 0;
}
the first argument to to gluPerspective is wrong:
gluPerspective(0,0.5,0.5,5);
The first argument to gluPerspective is the vertical field of view angle in degrees. The value has to be greater than 0.0 and less than 180. 0.0 is an invalid argument and causes undefined behavior.
Probably the instruction doesn't set the matrix at all.
Anyway, if you set a correct angle, then the geometry will be clipped. The perspective projection matrix defines a Viewing frustum. All the geometry which is not in between the near and fare plane is clipped. In your case the near plane is 0.5 and the far plane 5.0.
Set a view matrix and translate the geometry in between the near and far plane, by shifting the geometry along the negative z axis. For instance (0, 0, -2.5).
The 2nd argument of gluPerspective is the aspect ration. Since the size of the window is 1000x1000, the aspect ratio has to be 1.0:
void myinit(void)
{
glClearColor(1.0, 1.0, 1.0, 0.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
GLdouble fov = 90.0; // 90 degrees
GLdouble aspect = 1.0;
GLdouble near_dist = 0.5;
GLdouble far_dist = 5.0;
gluPerspective(fov, aspect, near_dist, far_dist);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(0.0f, 0.0f, -2.5f); // near_dist < 2.5 < far_dist
}

Why does my wired sphere turn into ellipsoid when translating and changing camera angle?

I need to translate my wired sphere along z-axis back and forth while also changing camera angle. Whenever my sphere gets translated, it slowly turns into ellipsoid. I really don't understand why. Here you can see pieces of code where I believe is a mistake. Also, shapes shouldn't be changed when resizing the window, only their size.
void init() {
glClearColor(0.0, 0.0, 0.0, 0.0);
glEnable(GL_DEPTH_TEST);
}
void display(void) {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glLoadIdentity();
gluLookAt(ex, ey, ez, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
glTranslatef(0.0,0.0,tra);
glScalef(0.65, 0.65, 0.65);
glColor3f(1.0f, 0.8f, 1.0f);
glutWireSphere(0.65, 10, 15);
glPopMatrix();
glPushMatrix();
glLoadIdentity();
gluLookAt(ex, ey, ez, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
glColor3f(0.1f, 0.8f, 1.0f);
glutWireTorus(0.25, 1.0, 15, 15);
glPopMatrix();
glFlush();
glFlush();
}
void reshape(int w, int h) {
glViewport(0, 0, (GLsizei)w, (GLsizei)h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(70.0, (GLfloat)w / (GLfloat)h, 1.0, 80.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
[...] while also changing camera angle. Whenever my sphere gets translated, it slowly turns into ellipsoid.
That's caused by the Perspective distortion or wide-angle distortion and increases towards the edge of the view. The effect can be decreased by reducing the field of view angle, but the effect will never be canceled completely (except parallel projection).
See als How to fix perspective projection distortion?.
Also, shapes shouldn't be changed when resizing the window, only their size."
At perspective projection the size of the objects is always relative to the size of the viewport rather than the size of your screen.
If you don't want perspective distortion and if you want that the size of the objects has to be relative to the size of the screen (measured in pixel), then you have to use an orthographic projection, relative to the size of the viewport.
For instance:
void reshape(int w, int h) {
glViewport(0, 0, (GLsizei)w, (GLsizei)h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
//gluPerspective(70.0, (GLfloat)w / (GLfloat)h, 1.0, 80.0);
float sx = w / 100.0f;
float sy = h / 100.0f;
glOrtho(-sx/2.0f, sx/2.0f, -sy/2.0f, sy/2.0f, 1.0f, 80.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}

Objects shake when rotating

I'm facing a problem in my opengl code
I'm trying to build a house, and rotate it 360°, for simplicity let's assume the house has the front wall with window and dor, and a back wall.
I'm using DEPTH_BUFFER not to see the back wall when viewing the front wall, and the other way around, but when I rotate the house the door and window start to shake and get distorced.
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3f(0.0, 0.0, 0.0);
glLoadIdentity();
gluLookAt(0.0, 0.0, 40.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
glRotatef(angle, 0.0, 1.0, 0.0);
glEnable(GL_DEPTH_TEST);
glColor3f(0.0, 0.0, 0.0);
glBegin(GL_POLYGON);
glVertex3f(8.0, 3.0, 0.0);
glVertex3f(8.0, -10.0, 0.0);
glVertex3f(1.0, -10.0, 0.0);
glVertex3f(1.0, 3.0, 0.0);
glEnd();
glColor3f(0.0, 0.0, 0.0);
glBegin(GL_POLYGON);
glVertex3f(-9.0, -4.0, 0.0);
glVertex3f(-9.0, 3.0, 0.0);
glVertex3f(-2.0, 3.0, 0.0);
glVertex3f(-2.0, -4.0, 0.0);
glEnd();
glColor3f(1.0, 0.0, 0.0);
glBegin(GL_POLYGON);
glVertex3f(10.0, -10.0, -20.0);
glVertex3f(-10.0, -10.0, -20.0);
glVertex3f(-10.0, 10.0, -20.0);
glVertex3f(10.0, 10.0, -20.0);
glEnd();
glColor3f(1.0, 0.0, 0.0);
glBegin(GL_POLYGON);
glVertex3f(10.0, -10.0, 0.0);
glVertex3f(10.0, 10.0, 0.0);
glVertex3f(-10.0, 10.0, 0.0);
glVertex3f(-10.0, -10.0, 0.0);
glEnd();
glDisable(GL_DEPTH_TEST);
glutSwapBuffers();
The issue is called Z-fighting. This is caused, because depth of the "door", "window" and "wall" are equal. The vertiex coordinates are transformed by the model view matrix and projection matrix and interpolated for each fragment which is covered by the polygon. This results in inaccuracies of the final z coordinate (depth).
Enable the polygon fill offset (glPolygonOffset) by before drawing the walls, to solve the issue:
glEnable(GL_DEPTH_TEST);
glDisable( GL_POLYGON_OFFSET_FILL );
// draw door and window
// ...
glEnable( GL_POLYGON_OFFSET_FILL );
glPolygonOffset( 1, 1 );
// draw walls
// ...
Polygon fill offset manipulates the depth of a fragment by a minimum amount. This causes that the depth of the "walls" is different to the depth of the "window" and "door", even after the transformation by the model view and projection matrix.
Since an offset is added to the depth of the "wall", the "wall" is always behind the window and the door, independent of the point of view.

QGLWidget not displaying object as shown

I am making a GUIon in Qt and I am using QGLWidgt to dispaly motion of an object.
For debugging puposes, I hard coded on matrix for try to display a teapot, but nothing is showing up on the screen. I have tried many things, I just dont know what is wrong.
EDIT
I figured out that it is showing the teapot, but the scaling is way off.. If i change the position was far from where it was. When I changed the position from -2.700, 2.000, 0.000 to -0.0270, 0.0200, 0.000, I could see it. I guess my question now is how to set the size of the screen to show what is being displayed??
The paintGL, resideGL and intializeGL are given below.
void GLWidget::initializeGL()
{
glClearColor(1.0,1.0,1.0,1.0);
GLUquadricObj *qobj = gluNewQuadric();
gluQuadricDrawStyle(qobj,GLU_FILL);
gluQuadricNormals(qobj,GLU_SMOOTH);
glClearDepth( 1.0f );
glEnable(GL_DEPTH_TEST);
glShadeModel(GL_SMOOTH);
double eqn[] = {0.01f,0.0f,0.01f,-1.0f};
// enable clip plane
::glClipPlane(GL_CLIP_PLANE0,eqn);
setupLight();
}
void GLWidget::paintGL() {
double MOpenGLStack[16] = {0.4314, 0.875, 0.2181, 0.000,
0.0567, -0.267, 0.961, 0.000,
0.900, -0.402, -0.165, 0.000,
-2.700, 2.000, 0.000, 1.000};
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(0.0, 0.0, -5.0);
glRotatef(xRot / 16.0, 1.0, 0.0, 0.0);
glRotatef(yRot / 16.0, 0.0, 1.0, 0.0);
glRotatef(zRot / 16.0, 0.0, 0.0, 1.0);
glTranslated(xTrans / 16.0, yTrans/ 16.0, 0.0f);
::glPushMatrix();
::glMultMatrixd(MOpenGLStack);
glutSolidTeapot(0.15);
::glPopMatrix();
}
void GLWidget::resizeGL(int w, int h){
int side = qMin(w, h);
glViewport((w - side) / 2, (h - side) / 2, side, side);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(30.0f, (double)w/(double)h, 0.1f, 1000.0f);
glOrtho(-0.5, +0.5, -0.5, +0.5, 4.0, 15.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity ();
glTranslatef(0.0f, 0.0f, -10.0f);
}
There is a problem here:
gluPerspective(30.0f, (double)w/(double)h, 0.1f, 1000.0f);
glOrtho(-0.5, +0.5, -0.5, +0.5, 4.0, 15.0);
The first line is overriden by the second line - you cannot have both perspective and orthographic view at the same time. If you do not want to view the teapot in parallel view, you can comment the second line.

opengl - having trouble implementing a perspective view

I'm writing an OpenGL application. The program needs to have a simple shape at the center and the user can change the view from orthographic to perspective and vice versa. I don't have any problem in handling the orthographic view but I can't switch from ortho to perspective, even though gluPerspective seems pretty straightforward.
The code I wrote looks something like this:
void myDisplay(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glTranslatef(0, 0, -3);
glRotatef(xRot, 1.0, 0.0, 0.0);
glRotatef(yRot, 0.0, 1.0, 0.0);
glRotatef(zRot, 0.0, 0.0, 1.0);
glScalef(scale, scale, scale);
glPushAttrib(GL_ENABLE_BIT);
if (currentStyle) {
if (currentStyle == STYLE_DOTTED) glLineStipple(currentStyle, 0xAAAA);
else glLineStipple(currentStyle, 0x0F0F * currentStyle);
glEnable(GL_LINE_STIPPLE);
}
setColor();
drawObjectOfType();
glPopAttrib();
glFlush();
}
void myKeyboard(unsigned char key, int x, int y)
{
switch (key)
{
case 'p':
isOrtho = !isOrtho;
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if(!isOrtho) {
gluPerspective(55,1,5,3);
gluLookAt(0,0,-7,0,0,0,0,1,0);
} else {
glOrtho(-2.0, 2.0, -2.0, 2.0, -10.0, 10.0);
}
glMatrixMode(GL_MODELVIEW);
break;
}
glutPostRedisplay();
}
The program also has lighting in it. Here's the init function that handles the lighting.
void init()
{
GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 1.0 };
GLfloat mat_shininess = { 100.0 };
GLfloat light_specular[] = { 1.0, 1.0, 1.0, 1.0 };
GLfloat light_position[] = { 0.0, 0.0, 10.0, 0.0 };
glLightfv(GL_LIGHT0, GL_POSITION, light_position);
glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
glMaterialf(GL_FRONT, GL_SHININESS, mat_shininess);
glShadeModel(GL_SMOOTH);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
createMenus();
glEnable(GL_DEPTH_TEST);
glClearColor(0.0, 0.0, 0.0, 1.0);
}
What happens with this code is the view changes from ortho to perspective, but distance between the camera and the polygons seems to be reverted, meaning the closer the polygon to the camera, the smaller it looks. For example, I have a cube as the shape and the face that is closer to me looks smaller whereas the oppposite should have been the case.
The last two parameters to gluPerspective() are zNear and zFar. You have set the near clipping plane further away than the far clipping plane, which reversed the perspective correction.
gluLookAt defines a viewing transformation, and should therefore be part of your MODELVIEW transformation. Your code multiplies it into the PROJECTION transformation. Try a sequence like this for your perspective setup case:
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(55,1,3,5);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0,0,-7,0,0,0,0,1,0);
Then use glPushMatrix and glPopMatrix instead of glLoadIdentity in your myDisplay function.
Your near and far plane are quite close to each other. We don't see in the code you posted what the range of the coordinates for your object are. You'll want to be careful that it ends up between your near and far plance, so that it won't get clipped away.