PDA

View Full Version : How to otbain 2D projections of a 3D cube?



profbalse
09-15-2003, 08:12 AM
Hi Wizards!!!,
I am in a great problem ...as I am not able to obtain the 2D projection[ Square] of a 3D Cube on the same screen ...Is ther a feasible way to do that...Can I get a sample code from any of you guys..Please reply me...I am in need of great help.....Many Tbanks...Cheers

kehziah
09-16-2003, 03:18 AM
What do you mean by 'on the same screen' ?

gluOrtho2D might be what you're looking for.

mikael_aronsson
09-16-2003, 03:24 AM
Yes, have a look at orthographic projection, this is 3D but without any perspective correction so if you look at a box straight down on one of the planes it will look like a square, if that is what you are after...

Mikael

profbalse
09-16-2003, 07:02 AM
Hi Kehziah and Michael...
Actually, I am simualting the X-ray Environment in 3D for my research...So, my objective is to get the projection displayed on the screen alongwith the 3D object[A cube..initially]..I will present the code below which will be of use for you to reply..I guess..
----These are the values which you should enter in the console------------------------
ENTER THE SCALE DATA FOR THE CUBE[X,Y,Z]:2.0 1.0 1.0

ENTER THE ORIENTATION ANGLE[X,Y,Z]0.0 0.0 0.0

ENTER THE PERSPECTIVE VIEW DATA[A,AR,NV,FV]:48 4 1 10

ENTER THE TRANSLATION DATA:[X,Y,Z]0.0 0.0 -4.0
---------------------------------------------
CODE...BELOW
---------------------------------------------
#include <GL\glut.h>
#include<GL\glu.h>
#include<iostream.h>
#include<process.h>
#include<stdio.h>
//#pragma comment( linker, "/subsystem:\"windows\" /entry:\"mainCRTStartup\"" )
typedef struct
{
GLdouble sx,sy,sz,X,Y,Z,V,tx,ty,tz,nv,fv,ar;
}input;
input in;


void Display()
{ // GLdouble mvm[16];
glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity();
glTranslatef(in.tx,in.ty,in.tz); //standard 0.0,0.0,4.0
glRotatef(in.X,1.0,0.0,0.0);
glRotatef(in.Y,0.0,1.0,0.0);
glRotatef(in.Z,0.0,0.0,1.0);
glScalef(in.sx,in.sy,in.sz);
glColor3f(1.0,1.0,0.0); //standard 2.0,1.0,1.0
glutWireCube(1.0);
glTranslatef(0.0,-0.1,0.1);
glutSolidCube(0.09);
glColor3f(0.0,1.0,1.0);
glTranslatef(0.0,-0.01,0.1);
glutSolidCube(0.02);
glColor3f(1.0,0.0,1.0);
glTranslatef(0.0,-0.1,0.1);
glutSolidCube(0.08);
glColor3f(0.0,0.0,1.0);
glTranslatef(0.02,0.1,-0.1);
glutSolidCube(0.082);
glColor3f(1.0,0.0,0.0);
glTranslatef(0.06,0.06,-0.06);
glutSolidCube(0.082);
glColor3f(0.0,0.0,1.0);
glTranslatef(0.0,-0.08,0.1);
glutSolidCube(0.045);
glColor3f(1.0,0.0,1.0);
glTranslatef(0.0,0.08,0.1);
glutSolidCube(0.05);
glFlush();
glutSwapBuffers();
}
void Reshape(int x, int y)
{
if (y == 0 &#0124; &#0124; x == 0)
return;
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(in.V,in.ar,in.nv,in.fv); //standard:48,6,1,10
//glOrtho(0.0,800.0,0.0,600.0);
//glFrustum(-1,1,-1,1,2,20);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glViewport(0,0,x,y);

}


void KeyPress(unsigned char key,int x,int y)
{
switch(key)
{
case 'a':
in.X=in.X+5;
Display();
break;
case 's':
in.X=in.X-5;
Display();
break;
case 'd':
in.Y=in.Y+5;
Display();
break;
case 'f':
in.Y=in.Y-5;
Display();
break;
case 'z':
in.Z=in.Z+5;
Display();
break;
case 'x':
in.Z=in.Z-5;
Display();
break;
case 'q':
in.sx=in.sx+0.5;
in.sy=in.sy+0.5;
in.sz=in.sz+0.5;
Display();
break;
case 'w':
in.sx=in.sx-0.5;
in.sy=in.sy-0.5;
in.sz=in.sz-0.5;
Display();
break;
case 'e':
in.tx=in.tx+0.5;
in.ty=in.ty+0.5;
in.tz=in.tz+0.5;
Display();
break;
case 'r':
in.tx=in.tx-0.5;
in.ty=in.ty-0.5;
in.tz=in.tz-0.5;
Display();
break;
case 'c':
in.ar=in.ar+5;
Display();
break;
case 'v':
in.ar=in.ar-5;
Display();
break;
case 'l':
in.nv=in.nv+1.0;
Display();
break;
case 'k':
in.nv=in.nv-1.0;
Display();
break;
case 'b':
in.fv=in.fv+1.0;
Display();
break;
case 't':
in.fv=in.fv-1.0;
Display();
break;
case 'g':
in.V=in.V+5.0;
Display();
break;
case 'h':
in.V=in.V-5.0;
Display();
break;
case 'p':
cout<<"\n\n\t"<<"\n X="<<in.X<<"\n Y="<<in.Y<<"\n Z="<<in.Z<<"\n SX="<<in.sx<<"\n SY="<<in.sy<<"\n SZ="<<in.sz<<"\n TX="<<in.tx<<"\n TY="<<in.ty<<"\n TZ="<<in.tz<<"\n V="<<in.V<<"\n AR="<<in.ar<<"\n NV="<<in.nv<<"\n FV="<<in.fv;
glTranslatef(in.tx,in.ty,in.tz);
glRotatef(in.X,1.0,0.0,0.0);
glRotatef(in.Y,0.0,1.0,0.0);
glRotatef(in.Z,0.0,0.0,1.0);
glScalef(in.sx,in.sy,in.sz);
glutWireCube(1.0);
Display();
break;


}
}

int main(int argc, char **argv)
{
cout<<"\nENTER THE SCALE DATA FOR THE CUBE[X,Y,Z]:";
cin>>in.sx>>in.sy>>in.sz;
cout<<"\n ENTER THE ORIENTATION ANGLE[X,Y,Z]";
cin>>in.X>>in.Y>>in.Z;
cout<<"\n ENTER THE PERSPECTIVE VIEW DATA[A,AR,NV,FV]:";
cin>>in.V>>in.ar>>in.nv>>in.fv;
cout<<"\n"<<"ENTER THE TRANSLATION DATA:[X,Y,Z]";
cin>>in.tx>>in.ty>>in.tz;
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize(300,300);
glPolygonMode(GL_FRONT_AND_BACK,GL_LINE);
glutCreateWindow("BeeJay");
glClearColor(0.0,0.0,0.0,0.0);
glutDisplayFunc(Display);
glutReshapeFunc(Reshape);
glutKeyboardFunc(KeyPress);
glutMainLoop();
return 0;
}
---------------------------------------------
I think you guys must have spotte th problem by now..If so, Please lemme know.....Thanks

Ciao

BeeJay

kehziah
09-16-2003, 10:57 PM
So you draw some cubes with perspective projection. You would like to have an orthographic view of the same objects in the same window, right?
If so, you should do the following :
- glViewport to specify the area of the window where you want your perspective view
- setup a perspective projection matrix
- draw your objects
- glViewport to specify the other area of the window
- setup an orthographic projection matrix
- draw your objects