Difficulty with orbiting around any object on OpenGL sceen - c++

After reading a lot of guides about orbiting around camera and asking some additional question on SO I came up with SSCCE what I have so far. Maybe this way it will be easier for other to understand what I need and for me what is the answer.
So far it looks like this:
the code for drawing objects:
glPushMatrix();
glTranslatef(translate[0], translate[1], translate[2]);
glRotatef(rot, 0.0, 1.0, 0.0);
glTranslatef(0,0,0);
glColor3f(1.0,0.0,0.0);
fig.DrawBox(5.0f,5.0f,-5.0f,-5.0f,5.0f,-5.0f);
glPopMatrix();
glPushMatrix();
glTranslatef(translate[0], translate[1], translate[2]);
glTranslatef(2,0,0);
glRotatef(rot, 0.0, 1.0, 0.0);
glTranslatef(0,0,0);
glColor3f(0.0,1.0,1.0);
fig.DrawBox(1.5f,1.5f,-1.5f,-1.5f,1.5f,-1.5f);
glTranslatef(-4, 1.5, 0);
glColor3f(1.0,0.0,1.0);
fig.DrawBox(0.5f,0.5f,-0.5f,-0.5f,0.5f,-0.5f);
glTranslatef(4-1, 1.5-0.5, 0);
glColor3f(0.0,0.0,1.0);
fig.DrawBox(0.5f,0.5f,-0.5f,-0.5f,0.5f,-0.5f);
glPopMatrix();
translate is position updated during navigation and rot is rotation.
The red box is a main container and everything is inside it. 'Camera' can go outside of it, but during rotation it should rotate as well. As for the small boxes I'd like to be able to orbit around each one of them. So far I can orbit around this green one, but when I move to purple then it doesn't work.
In my final app I will have hundreds of objects so I am not sure if current method of using only one glPushMatrix() glPopMatrix() is the best idea (I am concerned about proper placing them), but when I am using push pop for every object then each one rotates by it's own pivot point.
EDIT:
I've been working on my code and I managed to achieve something more then before. Although it's still not exactly what I'm looking for.
My code:
glPushMatrix();
glTranslatef(translate[0], translate[1], translate[2]);
glRotatef(rot, 0.0, 1.0, 0.0);
glTranslatef(0,0,0);
glColor3f(1.0,0.0,0.0);
fig.DrawBox(5.0f,5.0f,-5.0f,-5.0f,5.0f,-5.0f);
glPopMatrix();
glPushMatrix();
glTranslatef(translate[0], translate[1], translate[2]);
glRotatef(-rot, 0.0, 1.0, 0.0);
glTranslatef(4,0,0);
glPushMatrix();
glPushMatrix();
glTranslatef(2,0,0);
glColor3f(0.0,1.0,1.0);
fig.DrawBox(1.5f,1.5f,-1.5f,-1.5f,1.5f,-1.5f);
glPopMatrix();
glPushMatrix();
glTranslatef(-4, 1.5, 0);
glColor3f(1.0,0.0,1.0);
fig.DrawBox(0.5f,0.5f,-0.5f,-0.5f,0.5f,-0.5f);
glPopMatrix();
glPushMatrix();
glTranslatef(-1, -0.5, 0);
glColor3f(0.0,0.0,1.0);
fig.DrawBox(0.5f,0.5f,-0.5f,-0.5f,0.5f,-0.5f);
glPopMatrix();
glPopMatrix();
glPopMatrix();
And how it looks now:
so as you can see, I was able to move rotation point, but at the same time I've moved whole group inside box. And I don't want to do that.

After many changes and ideas I've finally figured out the proper way to achieve what I was looking for. It turned out that I needed to switch order of translations:
glPushMatrix();
glTranslatef(0, translate[1], translate[2]);
glRotatef(rot, 0.0, 1.0, 0.0);
glTranslatef(translate[0], 0,0);
glColor3f(1.0,0.0,0.0);
fig.DrawBox(5.0f,5.0f,-5.0f,-5.0f,5.0f,-5.0f);
glPopMatrix();
glPushMatrix();
glTranslatef(0, translate[1], translate[2]);
glRotatef(rot, 0.0, 1.0, 0.0);
glTranslatef(translate[0],0,0);
glPushMatrix();
glPushMatrix();
glTranslatef(2,0,0);
glColor3f(0.0,1.0,1.0);
fig.DrawBox(1.5f,1.5f,-1.5f,-1.5f,1.5f,-1.5f);
glPopMatrix();
glPushMatrix();
glTranslatef(-4, 1.5, 0);
glColor3f(1.0,0.0,1.0);
fig.DrawBox(0.5f,0.5f,-0.5f,-0.5f,0.5f,-0.5f);
glPopMatrix();
glPushMatrix();
glTranslatef(-1, -0.5, 0);
glColor3f(0.0,0.0,1.0);
fig.DrawBox(0.5f,0.5f,-0.5f,-0.5f,0.5f,-0.5f);
glPopMatrix();
glPopMatrix();
glPopMatrix();
Now the rotation point is exactly in front of me no matter where the camera is.

OpenGL rotating a camera around a point Maybe that answer could help you. I think you have to: translate-rotate-translate.

Related

Why does glTranslatef() resize object?

I have 3 rectangles and I need to place them in shape of podium. At this moment they look like this:
Code of display func:
glPushMatrix();
glRotated(rotate_x, 1.0, 0.0, 0.0);
glRotated(rotate_y, 0.0, 1.0, 0.0);
glScalef(1, 3, 1);
glColor3fv(gold);
glutSolidCube(2);
glPopMatrix();
glPushMatrix();
glTranslated(2, 0, -3);
glRotated(rotate_x, 1.0, 0.0, 0.0);
glRotated(rotate_y, 0.0, 1.0, 0.0);
glScalef(1, 2, 1);
glColor3fv(silver);
glutSolidCube(2);
glPopMatrix();
glPushMatrix();
glTranslatef(-2, 0, 0);
glScalef(1, 1, 1);
glRotated(rotate_x, 1.0, 0.0, 0.0);
glRotated(rotate_y, 0.0, 1.0, 0.0);
glColor3fv(bronze);
glutSolidCube(2);
glPopMatrix();
When I try to move silver rectangle a liitle bit down to make it on same level as yellow one by using glTranslatef(-2, 0, -2); it just becomes smaller:
The first two parameters of glTranslatef works just fine moving object left/right and closer/further, so why does third parameter changes object's size?
You moved it farther away. Objects which are farther away appear smaller; that's just how perspective works. And since you have no lighting, background objects, or any other depth cues, being farther away is visibly identical to scaling it to a smaller size.

How to prevent scaling lower than defined (OpenGL)

I have a simple OpenGL object which has defined certain glScalef size. It needs glutMouseFunc to make it work as I imagined. So, this is what I've imagined:
On GLUT_LEFT_BUTTON object needs to go bigger for 0.1, so:
glScalef(scalesize+0.1,scalesize+0.1,0.0.);
polygon();
On GLUT_RIGHT_BUTTON object needs to go smaller for 0.1, so:
glScalef(scalesize-0.1,scalesize-0.1,0.0);
polygon();
This is my polygon function:
void polygon(void){
glBegin(GL_POLYGON);
glColor3f(1.0,0.0,0.0);
glVertex2f(-0.15, -0.15);
glColor3f(0.0,0.0,0.0);
glVertex2f(-0.15, 0.15);
glColor3f(0.0,0.0,1.0);
glVertex2f(0.15, 0.15);
glColor3f(0.0,1.0,0.0);
glVertex2f(0.15, -0.15);
glEnd();
glEnable (GL_LINE_STIPPLE);
glLineWidth(3.0);
glLineStipple (6, 0x1C47);
glBegin(GL_LINES);
glColor3f(0.0,0.0,0.0);
glVertex2f(-0.25, -0.25);
glVertex2f(-0.25, 0.25);
glVertex2f(0.25, 0.25);
glVertex2f(0.25, -0.25);
glVertex2f(-0.25, 0.25);
glVertex2f(0.25, 0.25);
glVertex2f(-0.25, -0.25);
glVertex2f(0.25, -0.25);
glEnd();
}
This is my scene function:
void scene(void){
glClear(GL_COLOR_BUFFER_BIT);
glPushMatrix();
glScalef(scalesize,scalesize,0.0);
polygon();
glPopMatrix();
glFlush();
}
float scalesize = 1.0;
Now I'm not sure how to prevent scaling lower than 0.15, regarding to my object size. I've tried with if statement to check if scalesize is bigger than 1.0, but didn't worked. Any solutions?
Thanks
Ok, thanks to #radical7 I've figured it out. Already did everything what he said, but the real solution was to let scene function do its job, I didnt have to call glScalef in my mouse function.
Thanks again.

Saving rotation Matrix in OpenGL

I'm writing an OpenGl program about a cone rotating in 3D space and I have a txt with datas of the angles and the axes around whom the rotation takes place.
The problem is that OpenGL requires the total rotation angle while in the file I have the increment in angle for every time step.
here my code :
GLdouble matrice1 [] {1.,0,0,0,0,1.,0,0,0,0,1.,0,0,0,0,1.};
GLdouble * matrice = matrice1;
void displayCone(void){
// clear the drawing buffer.
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); //
// set matrix mode
glMatrixMode(GL_MODELVIEW);
// clear model view matrix
glLoadIdentity();
//gluLookAt(3.0, 3.0, 3.0-4.5, 0.0, 0.0,-4.5,0,0,1);
gluLookAt(3.0, 3.0, 3.0, 0.0, 0.0,0.0,0,0,1);
glBegin(GL_LINES);
glColor3f (1.0, 0.0, 0.0);
glVertex3f(0.0, 0.0, 0.0);
glVertex3f(2.0, 0.0, 0.0);
glColor3f (0.0, 1.0, 0.0);
glVertex3f(0.0, 0.0, 0.0);
glVertex3f(0.0, 2.0, 0.0);
glColor3f (0.0, 0.0, 1.0);
glVertex3f(0.0, 0.0, 0.0);
glVertex3f(0.0, 0.0, 2.0);
glEnd();
//glLoadMatrixd(matrice);
glColor3f(0.8, 0.2, 0.1);
glMultMatrixd(matrice); // <------ ** me trying to implement the previous rotation before //rotating of the increment angle xRotated
glRotated(xRotated,a1,a2,a3); // <----- **here the rotation**
glGetDoublev(GL_MODELVIEW, matrice); // <----- ** me trying to save the total rotation //matrix **
glTranslatef(0.0, 0.0, height);
glutSolidCone(base,height,slices,stacks);
glPushMatrix();
glTranslatef(0.0, 0.0, height-0.1);
glutSolidCylinder(0.07,height_c,slices,stacks);
glPopMatrix();
glPushMatrix();
glScalef(1.0,1.0,-1.0);
//glTranslatef(0.0, 0.0, 2*height);
//glPopMatrix();
//glPushMatrix();
glutSolidCone(base,height,slices,stacks);
// Flush buffers to screen
glPopMatrix();
glFlush();
}
What I'm trying to do here with glGetDoublevand glMultMatrixdis to save the precedent Rotation Angle but this doesn't work. Does anyone know how to save the precedent rotation matrix in order to get the total rotation and not the RotationAngle of the increment angle ?
For one, that code you have there is using the old and busted fixed function, legacy API that's been out of fashion for well over 15 years.
More to the point, you can use a proper 3D graphics math library (GLM, Eigen, linmath.h) to manage a rotation matrix and apply it to the OpenGL transformation matrix stack using glMultMatrix instead of using multiple calls to glRotate. Or manage the whole transformation matrix yourself and just load it with glLoadMatrix.
And when you do your own matrix management, it's only a small step to ditch the fixed function pipeline and use shaders, where you pass the matrix as so called uniform values.

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.

Colors disappear after drawing image

I am trying to draw a sphere and a cube at the same time on my screen. But the colors of my cube(which gets drawn first) dissapear. I don't understand why.
the sphere on the right is fine. But my cube on the left isn't.
I added texture to both:
I can perfectly draw both of them seperately, but when I try to draw both of them on one widget something goes wrong.
I tought the popping and pushing would solve this issue, but it doesn't.
code:
void MyGLWidget::drawCube()
{
glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
glLoadIdentity();
gluLookAt(position,0.5,-0.1,-0.5,-0.5,0,0,0,1);
glTranslatef( 0.5, 0, 0.0);
glRotatef(getCubeAngle(), 1.0f, 0.0f, 0.0f);
glTranslatef(0, 0, 0);
glDisable(GL_TEXTURE_GEN_S);
glDisable(GL_TEXTURE_GEN_T);
glPushMatrix();
glBindTexture(GL_TEXTURE_2D, texture[0]);
glBegin(GL_QUADS);
//back
glTexCoord2f(0.0, 1.0); glVertex3f(-0.1, 0.1,-0.1 );
glTexCoord2f(1.0, 1.0); glVertex3f(0.1, 0.1,-0.1);
glTexCoord2f(1.0, 0.0); glVertex3f(0.1,-0.1,-0.1);
glTexCoord2f(0.0, 0.0); glVertex3f(-0.1,-0.1,-0.1);
/*rest of cube gets drawn*/
glEnd();
glFlush();
glPopMatrix();
}
void MyGLWidget::drawSun()
{
glPushMatrix();
glLoadIdentity();
glBindTexture(GL_TEXTURE_2D, texturePlanet[0]);
glPushMatrix();
glScalef(1,1,1);
glLoadIdentity();
GLUquadricObj *quadric=gluNewQuadric();
gluQuadricTexture(quadric, GLU_TRUE);
gluQuadricNormals(quadric, GLU_SMOOTH);
glEnable(GL_TEXTURE_2D); //
glBindTexture(GL_TEXTURE_2D,texturePlanet[0]);//
gluSphere(quadric, 0.25, 360,360);
glDisable(GL_TEXTURE_2D);//
gluDeleteQuadric(quadric);
glPopMatrix();
}
void MyGLWidget::paintGL()
{
drawCube();
drawSun();
}
It is because In your cube drawing, you didn't enable texturing.
glEnable(GL_TEXTURE_2D); //ADD THIS TO ENABLE TEXTURING
glPushMatrix();
glBindTexture(GL_TEXTURE_2D, texture[0]);
glBegin(GL_QUADS);
//back
glTexCoord2f(0.0, 1.0); glVertex3f(-0.1, 0.1,-0.1 );
glTexCoord2f(1.0, 1.0); glVertex3f(0.1, 0.1,-0.1);
glTexCoord2f(1.0, 0.0); glVertex3f(0.1,-0.1,-0.1);
glTexCoord2f(0.0, 0.0); glVertex3f(-0.1,-0.1,-0.1);
/*rest of cube gets drawn*/
glEnd();
glFlush();
glPopMatrix();
glDisable(GL_TEXTURE_2D); // ADD THIS TO DISABLE TEXTURING