true isometric projection with opengl - c++

Is there a simple way to have isometric projection?
I mean the true isometric projection, not the general orthogonal projection.
(Isometric projection happens only when projections of unit X, Y and Z vectors are equally long and angles between them are exactly 120 degrees.)

Try using gluLookAt
glClearColor(0.0, 0.0, 0.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
/* use this length so that camera is 1 unit away from origin */
double dist = sqrt(1 / 3.0);
gluLookAt(dist, dist, dist, /* position of camera */
0.0, 0.0, 0.0, /* where camera is pointing at */
0.0, 1.0, 0.0); /* which direction is up */
glMatrixMode(GL_MODELVIEW);
glBegin(GL_LINES);
glColor3d(1.0, 0.0, 0.0);
glVertex3d(0.0, 0.0, 0.0);
glVertex3d(1.0, 0.0, 0.0);
glColor3d(0.0, 1.0, 0.0);
glVertex3d(0.0, 0.0, 0.0);
glVertex3d(0.0, 1.0, 0.0);
glColor3d(0.0, 0.0, 1.0);
glVertex3d(0.0, 0.0, 0.0);
glVertex3d(0.0, 0.0, 1.0);
glEnd();
glFlush();
Results in
We can draw a cube to check that parallel lines are indeed parallel
glPushMatrix();
glTranslated(0.5, 0.5, 0.5);
glColor3d(0.5, 0.5, 0.5);
glutWireCube(1);
glPopMatrix();

An isometric projection is just a matter of using an orthographic projection with a specific rotation angle.
You should be able to choose any of the 8 potential orientations, with a orthographic projection, and get a perfect isometric view of your model. Just follow the math in your referenced Wiki article for setting up the view matrix, and do an orthographic projection for your projection matrix, and you're all set.

Maybe I'm not quite grokking the math correctly, but couldn't you just position your camera as it explains in that Wikipedia link and use a standard orthogonal projection?
Even if it's not the same, the projection stack is entirely up to you.
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
// your isometric matrix here (see math on Wikipedia)
glMatrixMode(GL_MODELVIEW);

If you do not want to use GLU, here is bare bones using glOrtho
void gl_enter_2_5d_mode (void)
{
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
double scale = 50;
glOrtho(-scale,
scale,
-scale * 0.7,
scale * 0.7,
-scale,
scale);
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();
glRotatef(35.264f, 1.0f, 0.0f, 0.0f);
glRotatef(-45.0f, 0.0f, 1.0f, 0.0f);
}
void gl_leave_2_5d_mode (void)
{
glMatrixMode(GL_MODELVIEW);
glPopMatrix();
glMatrixMode(GL_PROJECTION);
glPopMatrix();
}
then draw a cube in it
void cube (double size)
{
glBegin(GL_QUADS);
glVertex3f(size,size,size);
glVertex3f(-size,size,size);
glVertex3f(-size,-size,size);
glVertex3f(size,-size,size);
glVertex3f(size,size,-size);
glVertex3f(-size,size,-size);
glVertex3f(-size,-size,-size);
glVertex3f(size,-size,-size);
glVertex3f(size,size,size);
glVertex3f(size,-size,size);
glVertex3f(size,-size,-size);
glVertex3f(size,size,-size);
glVertex3f(-size,size,size);
glVertex3f(-size,-size,size);
glVertex3f(-size,-size,-size);
glVertex3f(-size,size,-size);
glVertex3f(size,size,size);
glVertex3f(-size,size,size);
glVertex3f(-size,size,-size);
glVertex3f(size,size,-size);
glVertex3f(size,-size,size);
glVertex3f(-size,-size,size);
glVertex3f(-size,-size,-size);
glVertex3f(size,-size,-size);
glEnd();
}
void test (void)
{
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
glBindTexture(GL_TEXTURE_2D, 0);
cube(1.0);
glPolygonMode(GL_FRONT_AND_BACK,GL_FILL);
}
and call something like this
gl_enter_2_5d_mode()
test()
gl_leave_2_5d_mode()
should you want to toggle between 2d and 2.5d (so you can draw your UI) then I have similar functions to enter and leave 2d mode e.g.
void gl_init_2d_mode (void)
{
/*
* Enable Texture Mapping
*/
glEnable(GL_TEXTURE_2D);
/*
* Enable alpha blending for sprites
*/
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
/*
* Setup our viewport
*/
glViewport(0, 0, game.video_pix_width,
game.video_pix_height);
/*
* Make sure we're changing the model view and not the projection
*/
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
/*
* Reset the view
*/
glLoadIdentity();
gl_init_fbo();
}
void gl_enter_2d_mode (void)
{
/*
* Change to the projection matrix and set our viewing volume.
*/
glMatrixMode(GL_PROJECTION);
glPushMatrix();
/*
* Reset the view
*/
glLoadIdentity();
/*
* 2D projection
*/
glOrtho(0,
game.video_gl_width, game.video_gl_height,
0, -1200.0, 1200.0);
/*
* Make sure we're changing the model view and not the projection
*/
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
/*
* Reset the view
*/
glLoadIdentity();
}

Related

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.

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

How do I correctly draw 3D axis in OpenGL?

I am trying to draw the 3D axis. I have also a cone which is rotating around its vertex and I would want the axis starting from there.
Here I have my function to draw the cone and I wrote the functions to draw the axis after gluLookAt:
GLfloat xRotated, yRotated, zRotated;
// Cone
GLdouble base=0.5;
GLdouble height=1.3;
GLint slices =20;
GLint stacks =20;
std::vector<std::array<GLfloat, 3>> data;
void displayCone(void)
{
// set matrix mode
glMatrixMode(GL_MODELVIEW);
// clear model view matrix
glLoadIdentity();
// multiply view matrix to current matrix
gluLookAt(0,2.,0.,0.,0.,-4.5,0,1,0); // <----------------------- add
// ******
glPushMatrix();
glLoadIdentity();
glTranslatef(0.0, 0.0, -4.5);
glBegin(GL_LINES);
glColor3f (1.0, 1.0, 0.0);
glVertex3f(0.0, 0.0, 0.0);
glVertex3f(2.0, 0.0, 0.0);
glColor3f (1.0, 1.0, 0.0);
glVertex3f(0.0, 0.0, 0.0);
glVertex3f(0.0, 2.0, 0.0);
glColor3f (1.0, 1.0, 0.0);
glVertex3f(0.0, 0.0, 0.0);
glVertex3f(0.0, 0.0, 2.0);
glEnd();
glPopMatrix();
// ******
// clear the drawing buffer.
glClear(GL_COLOR_BUFFER_BIT);
// traslate the draw by z = -4.0
// Note this when you decrease z like -8.0 the drawing will looks far , or smaller.
glTranslatef(0.0,0.0,-4.5);
// Red color used to draw.
glColor3f(0.8, 0.2, 0.1);
// changing in transformation matrix.
// rotation about X axis
glRotatef(xRotated,1.0,0.0,0.0);
// rotation about Y axis
glRotatef(yRotated,0.0,1.0,0.0);
// rotation about Z axis
glRotatef(zRotated,0.0,0.0,1.0);
// scaling transfomation
glScalef(1.0,1.0,1.0);
// built-in (glut library) function , draw you a Cone.
// move the peak of the cone to the origin
glTranslatef(0.0, 0.0, -height);
glutSolidCone(base,height,slices,stacks);
// Flush buffers to screen
// gluLookAt(3,3,3,0,0,-4.5,0,1,0); <----------------------- delete
glFlush();
// sawp buffers called because we are using double buffering
// glutSwapBuffers();
}
I thought to draw the 3D axis in this way but I am making some mistakes.
What am I missing?
The lies are cleared immediately after they've been drawn by glClear. Do glClear(GL_COLOR_BUFFER_BIT); at the begin of displayCone:
If you want to draw the lines in view space, then the view matrix has to be set. Remove glLoadIdentity before drawing the lines.
e.g.
void displayCone(void)
{
// clear the drawing buffer.
glClear(GL_COLOR_BUFFER_BIT); // <---- add
// set matrix mode
glMatrixMode(GL_MODELVIEW);
// clear model view matrix
glLoadIdentity();
// multiply view matrix to current matrix
gluLookAt(3.0, 3.0, 3.0-4.5, 0.0, 0.0,-4.5,0,1,0);
// ******
glPushMatrix();
// glLoadIdentity(); <---- delete
glTranslatef(0.0, 0.0, -4.5);
glBegin(GL_LINES);
glColor3f (1.0, 1.0, 0.0);
glVertex3f(0.0, 0.0, 0.0);
glVertex3f(2.0, 0.0, 0.0);
glColor3f (1.0, 1.0, 0.0);
glVertex3f(0.0, 0.0, 0.0);
glVertex3f(0.0, 2.0, 0.0);
glColor3f (1.0, 1.0, 0.0);
glVertex3f(0.0, 0.0, 0.0);
glVertex3f(0.0, 0.0, 2.0);
glEnd();
glPopMatrix();
// clear the drawing buffer.
// glClear(GL_COLOR_BUFFER_BIT); // <---- delete
// traslate the draw by z = -4.0
// Note this when you decrease z like -8.0 the drawing will looks far , or smaller.
glTranslatef(0.0,0.0,-4.5);
// Red color used to draw.
glColor3f(0.8, 0.2, 0.1);
// changing in transformation matrix.
// rotation about X axis
glRotatef(xRotated,1.0,0.0,0.0);
// rotation about Y axis
glRotatef(yRotated,0.0,1.0,0.0);
// rotation about Z axis
glRotatef(zRotated,0.0,0.0,1.0);
// scaling transfomation
glScalef(1.0,1.0,1.0);
// built-in (glut library) function , draw you a Cone.
// move the peak of the cone to the origin
glTranslatef(0.0, 0.0, -height);
glutSolidCone(base,height,slices,stacks);
// Flush buffers to screen
// gluLookAt(3,3,3,0,0,-4.5,0,1,0); <----------------------- delete
glFlush();
// sawp buffers called because we are using double buffering
// glutSwapBuffers();
}

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.