PDA

View Full Version : Trying to figure our glOrtho



tr1234586
02-13-2010, 01:28 PM
Hi,
I am trying to design a room thats 19ft long, 8ft high and 11ft deep. I can't seem to be able to set my camera(eye) at the center of the room and be able to look around and see all the walls from the inside.




#include "../shared/gltools.h"
#include "../shared/glframe.h"
#include "../shared/math3d.cpp"
#include <math.h>

GLFrame frameCamera;
//used following coordinates to keep eye at (0,0,0)
GLfloat vertices[8][3]=
{
{-9.5,-4.0,-6.5},//1
{-9.5,-4.0,6.5},
{-9.5,4.0,-6.5},//3
{-9.5,4.0,6.5},
{9.5,-4.0,-6.5},//5
{9.5,-4.0,6.5},
{9.5,4.0,-6.5},//7
{9.5,4.0,6.5}
}, angles[3]={0.0f};

GLfloat color[8][3]=
{
{1,1,1},
{0,0,0},
{0,0,1},
{0,1,0},
{1,0,0},
{0,1,1},
{1,0,1},
{1,1,0}
};
GLint axis;

void display();
void SpecialKeys(int, int, int);
void keyboard(unsigned char key, int x, int y);
void myInit();
void reshape(int w, int h);

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

glutInit(&amp;argc, argv);
glutInitWindowSize(600,600);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGBA|GLUT_DEP TH);
glutCreateWindow("My Room");
myInit();
glutDisplayFunc(display);
glutSpecialFunc(SpecialKeys);
glutKeyboardFunc(keyboard);
glutReshapeFunc(reshape);
glutMainLoop();
return 0;
}

void display()
{
glClear(GL_COLOR_BUFFER_BIT);
frameCamera.ApplyCameraTransform();

glBegin(GL_QUADS);

//front face - white
glColor3fv(color[0]);
glVertex3fv(vertices[4]);
glColor3fv(color[0]);
glVertex3fv(vertices[8]);
glColor3fv(color[0]);
glVertex3fv(vertices[6]);
glColor3fv(color[0]);
glVertex3fv(vertices[2]);


//back face - black
glColor3fv(color[1]);
glVertex3fv(vertices[7]);
glColor3fv(color[1]);
glVertex3fv(vertices[3]);
glColor3fv(color[1]);
glVertex3fv(vertices[1]);
glColor3fv(color[1]);
glVertex3fv(vertices[5]);

//right face - green
glColor3fv(color[3]);
glVertex3fv(vertices[7]);
glColor3fv(color[3]);
glVertex3fv(vertices[8]);
glColor3fv(color[3]);
glVertex3fv(vertices[6]);
glColor3fv(color[3]);
glVertex3fv(vertices[5]);

//left face - blue
glColor3fv(color[2]);
glVertex3fv(vertices[4]);
glColor3fv(color[2]);
glVertex3fv(vertices[2]);
glColor3fv(color[2]);
glVertex3fv(vertices[1]);
glColor3fv(color[2]);
glVertex3fv(vertices[3]);

//top face - red
glColor3fv(color[4]);
glVertex3fv(vertices[4]);
glColor3fv(color[4]);
glVertex3fv(vertices[3]);
glColor3fv(color[4]);
glVertex3fv(vertices[7]);
glColor3fv(color[4]);
glVertex3fv(vertices[8]);

//bottom face - cyan
glColor3fv(color[5]);
glVertex3fv(vertices[5]);
glColor3fv(color[5]);
glVertex3fv(vertices[6]);
glColor3fv(color[5]);
glVertex3fv(vertices[2]);
glColor3fv(color[5]);
glVertex3fv(vertices[1]);

//glTranslatef(0.0f,0.0f,0.0f);
glEnd();

glutSwapBuffers();
}


void keyboard(unsigned char key, int x, int y)
{
switch(key)
{
case 27:
case 'q':
case 'Q':
exit(0);
case 'x':
case 'X':
axis=0; break;
case 'y':
case 'Y':
axis=1;break;
case 'z':
case 'Z':
axis=2; break;
}
}
void myInit()
{
glClearColor(0.7,0.7,0.7,1);
glEnable(GL_DEPTH);
glEnable(GL_CULL_FACE);
}
void reshape(int w, int h)
{
GLfloat fAspect;
GLfloat windowWidth, windowHeight;
if(h==0)
h=1;
if(w <= h)
{
windowHeight = 3.0f*(GLfloat)h/(GLfloat)w;
windowWidth = 3.0f;
}
else
{
windowWidth = 3.0f *(GLfloat)w/(GLfloat)h;
windowHeight = 3.0f;
}
glViewport(0,0,w,h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(??, windowWidth, ??, windowHeight, ??, ??);
glMatrixMode(GL_MODELVIEW);
glutPostRedisplay();
}
void SpecialKeys(int key, int x, int y)
{
if(key == GLUT_KEY_UP)
frameCamera.RotateLocalX(0.001f);

if(key == GLUT_KEY_DOWN)
frameCamera.RotateLocalX(-0.001f);

if(key == GLUT_KEY_LEFT)
frameCamera.RotateLocalY(0.001);

if(key == GLUT_KEY_RIGHT)
frameCamera.RotateLocalY(-0.001);

// Refresh the Window
glutPostRedisplay();
}


This is my first program using OpenGL and I think my problem lies with my glOrtho funtion, but am not sure hence i posted my entire program.

Any help will be much appriciated.
Or a link to a section of this forum with the solution would be great too.

Thanks,
T

marshats
02-14-2010, 06:09 PM
Your question is incomplete -- what is the code for frameCamera.ApplyCameraTransform()?

However, your vertices seem to fall within the range of +-10 so I would have expected


glOrtho(-10.0,10.0,-10.0,10.0,-10.0,10.0);

strattonbrazil
02-14-2010, 10:43 PM
Just so you understand, glOrtho is going to push everything in the range you specify into that [-1,1]x[-1,1]x[-1,1] box before it gets stretched out across the viewport (which is usually the whole screen).

A lot of the time you'll see code that has a width and height that is the same as the viewport, which will smash it to the box and stretch it back in a one-to-one mapping so a pixel at (10,10) will end up at (10,10). It's nice for working in 2D sometimes as the coordinates match pixel for pixel with the screen.

glOrtho(0, viewportWidth, 0, viewportHeight, ??, ??)

As marshats said, if you put that (-10,10,...) it will make sure that range in camera space ends up on your viewport.