Reputation: 193
I have this code in c++ that basically creates a series of cubes using GlutSolidCube(1)
.
I do a rescale to have cubes that have 8 in width and 5 in height constant whatever elements. The ZZ value serves to draw different kinds of things.
The problem is that the distances between the Cubes don't make sense: if I draw a cube with 2 units in ZZ it makes sense that the next cube starts in 2 ZZ, right? Why is the result different?
Here is the Code :
#include "stdafx.h"
#include "glut.h"
void reshape(int w, int h) {
float xmin = -10., xmax = 10., ymin = -2., ymax = 10.;
float ratio = (xmax - xmin) / (ymax - ymin);
float aspect = (float)w / h;
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glOrtho(xmin, xmax, ymin, ymax,-2,30);
if (ratio < aspect)
glScalef(ratio / aspect, 1., 1.);
else
glScalef(1., aspect / ratio, 1.);
}
void myDisplay(void)
{
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0f, 1.0f, 1.0f);
glMatrixMode(GL_MODELVIEW);
//Margem 1
glPushMatrix();
glColor3f(0.0f, 0.4f, 0.0f);
glRotatef(90.0, 1.0, 0.0, 0.0);
glScalef(8.0, 5.0, 2.0);
glTranslatef(0.0, 0.0, 0.0);
glutSolidCube(1);
glPopMatrix();
//Estrada
glPushMatrix();
glColor3f(1.0f, 1.0f, 1.0f);
glRotatef(90.0, 1.0, 0.0, 0.0);
glScalef(8.0, 5.0, 3.0);
glTranslatef(0.0, 0.0, -1.);
glutSolidCube(1);
glPopMatrix();
glFlush();
}
void main (int argc,char** argv) {
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(400, 400);
glutInitWindowPosition(-1, -1);
glutCreateWindow("Teste");
glutDisplayFunc(myDisplay);
glutReshapeFunc(reshape);
glutMainLoop();
}
Resulting image:
Upvotes: 2
Views: 136
Reputation: 171117
The image is OK, given your GL calls. By default, the OpenGL camera is looking down the negative Z axis (i.e. the Z axis points towards it). You draw the green box by first rotating around the X axis by 90deg., which makes Z point downwards and Y towards the camera. Then, you scale Z (the up-down axis now) by 2.0, and finally draw the cube. The Z size of the cube is 1 (from glutSolidCube()
) multiplied by 2.0 (the scale) for 2 units total.
For the white cube, you start by the same rotation, scale Z by 3.0, and the translate by 1.0 along the negative scaled Z axis. This is equivalent to 3 unscaled units upwards, and the cube will also have a Z dimension of 3 (1 from glutSolidCube()
times 3.0 scale). So it's bigger and above the green one.
It is unclear to me what you want to achieve, so I cannot suggest the appropriate operations.
As a side note, you should probably use glOrtho
to set up the projection matrix, not the modelview one.
Upvotes: 1