PDA

View Full Version : When I display with glOrtho , I'm getting inverted image how to correct this?



src
02-12-2002, 05:38 PM
Hi,
I'm using the following code to toggle b/n
Ortho and Perspective.
But it doesn't toggle properly (toggles only first time).
Also, the image is inverted when I use
glOrtho
Please help me modify code. The code is given below:
#include "glut.h"
#include <math.h>
#include "gl\gl.h"
#include "gl\glu.h"
#include <iostream.h>
#include <stdlib.h>

static GLdouble ortho_left = -5;
static GLdouble ortho_right = 5;
static GLdouble ortho_bottom = -5;
static GLdouble ortho_top = 5;
static GLdouble ortho_near = 1;
static GLdouble ortho_far = 30;

static float angle=0.0,ratio;
static float x=0.0f,y=1.75f,z=5.0f;
static float lx=0.0f,ly=0.0f,lz=-1.0f;
static GLint snowman_display_list;

static int k=0;

void renderScene(void);

void changeSize(int w, int h)
{

if(k==0)
{
if(h == 0)
h = 1;
ratio = 1.0f * w / h;
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glViewport(0, 0, w, h);
gluPerspective(45,ratio,1,1000);
renderScene();
glutPostRedisplay();
}
else
{
if(h == 0)
h = 1;
ratio = 1.0f * w / h;
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glViewport(0, 0, w, h);
//glOrtho(-200.0,200.0,-200.0,200.0,10.0,250.0);
if (w>h) glOrtho(ortho_left*w/h,ortho_right *w/h,ortho_top,ortho_bottom,ortho_near,ortho_far);
else glOrtho(ortho_left,ortho_right,ortho_top*h/w,ortho_bottom*h/w,ortho_near,ortho_far);
renderScene();
glutPostRedisplay();
}

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(x, y, z,x + lx,y + ly,z + lz,0.0f,1.0f,0.0f);

glutPostRedisplay();

}


void drawSnowMan() {

glColor3f(1.0f, 1.0f, 1.0f);

// Body
glTranslatef(0.0f ,0.75f, 0.0f);
glutSolidSphere(0.75f,20,20);


// Head
glTranslatef(0.0f, 1.0f, 0.0f);
glutSolidSphere(0.25f,20,20);

// Eyes
glPushMatrix();
glColor3f(0.0f,0.0f,0.0f);
glTranslatef(0.05f, 0.10f, 0.18f);
glutSolidSphere(0.05f,10,10);
glTranslatef(-0.1f, 0.0f, 0.0f);
glutSolidSphere(0.05f,10,10);
glPopMatrix();

// Nose
glColor3f(1.0f, 0.5f , 0.5f);
glRotatef(0.0f,1.0f, 0.0f, 0.0f);
glutSolidCone(0.08f,0.5f,10,2);
}

GLuint createDL()
{
GLuint snowManDL;
snowManDL = glGenLists(1);
glNewList(snowManDL,GL_COMPILE);
drawSnowMan();
glEndList();
return(snowManDL);
}

void initScene()
{

glEnable(GL_DEPTH_TEST);
snowman_display_list = createDL();

}
void renderScene(void) {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

// Draw ground

glColor3f(0.6f, 0.6f, 0.6f);
glBegin(GL_QUADS);
glVertex3f(-100.0f, 0.0f, -100.0f);
glVertex3f(-100.0f, 0.0f, 100.0f);
glVertex3f( 100.0f, 0.0f, 100.0f);
glVertex3f( 100.0f, 0.0f, -100.0f);
glEnd();

// Draw 4 SnowMen

for(int i = 0; i < 2; i++)
for(int j=0; j < 2; j++) {
glPushMatrix();
glTranslatef(i*10.0,0,j * 10.0);
glCallList(snowman_display_list);;
glPopMatrix();
}

glutSwapBuffers();
}

void orientMe(float ang) {


lx = sin(ang);
lz = -cos(ang);
glLoadIdentity();
gluLookAt(x, y, z,x + lx,y + ly,z + lz,0.0f,1.0f,0.0f);
}


void moveMeFlat(int i)
{
x = x + i*(lx)*0.1;
z = z + i*(lz)*0.1;
glLoadIdentity();
gluLookAt(x, y, z, x + lx,y + ly,z + lz, 0.0f,1.0f,1.0f);
}
void moveMeFlat1(int i)
{
x = x ;
z = z ;
glLoadIdentity();
gluLookAt(x, y, z,x + lx,y + ly,z + lz,0.0f,0.0f,0.0f);
}
void processNormalKeys(unsigned char key, int x, int y) {

if (key == 27)
exit(0);
switch(key)
{
case 'p': if (k==0) {k=1;}
else if (k==1) {k=0;}

if (k==0)
{
cout<<"persp"<<endl;
gluPerspective(45,1.0f *(640/360),1,1000);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(x, y, z,x + lx,y + ly,z + lz,0.0f,1.0f,0.0f);
glEnable(GL_DEPTH_TEST);
renderScene();
glutPostRedisplay();
}
else
{
glEnable(GL_DEPTH_TEST);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(ortho_left,ortho_right,ortho_top,ortho_bot tom,ortho_near,ortho_far);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(x, y, z,x + lx,y + ly,z + lz,1.0f,1.0f,0.0f);
cout<<"ortho"<<endl;
renderScene();
glutPostRedisplay();
}
glutPostRedisplay();


break;
}
}

void inputKey(int key, int x, int y) {

switch (key) {
case GLUT_KEY_LEFT :
if(k==0)
{
angle -= 0.01f;orientMe(angle);
}
else
{
angle -= 0.01f;orientMe(angle);
}
break;
case GLUT_KEY_RIGHT :
if(k==0)
{
angle += 0.01f;orientMe(angle);
}
else
{
angle += 0.01f;orientMe(angle);
}
break;
break;
case GLUT_KEY_UP :
if (k==0){
moveMeFlat(1);} else {moveMeFlat1(1);}break;
case GLUT_KEY_DOWN :
if (k==0){
moveMeFlat(-1);} else {moveMeFlat1(-1);} break;
}
}
int main(int argc, char **argv)
{

glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
glutInitWindowPosition(100,100);
glutInitWindowSize(640,360);
glutCreateWindow("");
initScene();
glutKeyboardFunc(processNormalKeys);
glutSpecialFunc(inputKey);
glutDisplayFunc(renderScene);
glutIdleFunc(renderScene);
glutReshapeFunc(changeSize);
glutMainLoop();
return(0);
}

Mr_Smith
02-13-2002, 07:17 AM
lots of code http://www.opengl.org/discussion_boards/ubb/smile.gif
glOrtho(XMin,XMax,YMin,YMax
alse
glOrtho(Left,Right,Top,Bottom
i didnt read it all but it looks like your setting the Max less than the min hoping ti will invert it, it doesnt Max always has to be greater than Min it seems, meaning 0 is at the top of the screen.
eg
123456789
2
3
4
5
6
7
8
9

[This message has been edited by Mr_Smith (edited 02-13-2002).]