PDA

View Full Version : opengl viewing



Sanjeev
06-22-2010, 04:02 AM
Hi Friends,

I have made two cones one attached to the other (Hierarchically), on the positive z-axis. I want to look at it as if I am looking at the cones from behind.

I would be very greatful, if someone could suggest how to do that please.

Regards
Roy

codebreaker
06-22-2010, 05:57 AM
I think you should be able to do this with gluPerspective() and gluLookAt() from the utilities (GLU).

Sanjeev
06-25-2010, 02:48 AM
I think you should be able to do this with gluPerspective() and gluLookAt() from the utilities (GLU).

this is my code which makes two cones hierarchically aligned about the positive z-axis. i want to look at it as if the cones are facing away from my, that is as if they were aligned about the negative z-axis. i would be very thankful if you could or someone could help. i really need it working.



GLint angle[6];

GLPoint::GLPoint(GLdouble _x=0,GLdouble _y=0,GLdouble _z=0)
{
x = _x;
y = _y;
z = _z;
}

void GLPoint::operator()(GLdouble _x=0,GLdouble _y=0,GLdouble _z=0)
{
x = _x;
y = _y;
z = _z;
}

void GLPoint::Display()
{
cout<<x<<" "<<y<<" "<<z<<endl;
}

//Initialisation of background clor and drawing color
void myInit()
{
glClearColor(0.75,0.75,0.75,0.0); //set background clour
glColor3d(0.0,0.0,0.0); // set drawing colour
glPointSize(2.0);
glLineWidth(2.0);

}

void setWindow(GLdouble left,GLdouble right,GLdouble bottom,GLdouble top,GLdouble near,GLdouble far)
{
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(left,right,bottom,top,near,far);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(20,60,70,0,0,0,0,1,0);
}

void setViewport(GLint left,GLint right,GLint bottom,GLint top)
{
glViewport(left,bottom,right-left,top-bottom);
}

void Reshape(GLint w, GLint h)
{
setViewport (0,w,0,h);
setWindow(-10,100,-30,100,-100,100);
}

void drawDot(GLPoint &amp;P)
{
glBegin(GL_POINTS);
glVertex3d(P.x,P.y,P.z);
glEnd();
}

void myMouse(int button,int state,int x,int y)
{
if(state==GLUT_DOWN &amp;&amp; button==GLUT_LEFT_BUTTON){
for(int i=3;i<=5;i++){
angle[i]= rand()%120;
cout<<"angle["<<i<<"]="<<angle[i]<<endl;
}
}
else if(button==GLUT_RIGHT_BUTTON){
glClear(GL_COLOR_BUFFER_BIT);
glFlush();
}
glutPostRedisplay();
}

void co_ordinate(void)
{
glClear(GL_COLOR_BUFFER_BIT);

glPushMatrix();
glColor3d(1.0,0.0,0.0);
glBegin(GL_LINES);
glVertex3d(0,0,0);
glVertex3d(10,0,0);
glEnd();
glTranslated((10-2),0,0);
glRotated(90,0,1,0);
glutWireCone(0.4,2,12,9);
glPopMatrix();

glPushMatrix();
glColor3d(0.0,1.0,0.0);
glBegin(GL_LINES);
glVertex3d(0,0,0);
glVertex3d(0,10,0);
glEnd();
glTranslated(0,(10-2),0);
glRotated(-90,1,0,0);
glutWireCone(0.4,2,12,9);
glPopMatrix();

glPushMatrix();
glColor3d(0.0,0.0,1.0);
glBegin(GL_LINES);
glVertex3d(0,0,0);
glVertex3d(0,0,10);
glEnd();
glTranslated(0,0,(10-2));
glutWireCone(0.4,2,12,9);
glPopMatrix();

}


void Robot_Display()
{
GLdouble L1=30; //Upper_Arm Length
GLdouble L2=45; //Lower_Arm Length

//Initial VM position is at origin pointing in direction(0,0,1)
co_ordinate();
GLUquadricObj *qobj1 = gluNewQuadric();
GLUquadricObj *qobj2 = gluNewQuadric();
gluQuadricDrawStyle(qobj1,GLU_FILL);
gluQuadricDrawStyle(qobj2,GLU_FILL);

glPushMatrix();
glTranslated(80,90,20);
glRotated(angle[0],1,0,0);
glRotated(angle[1],0,1,0);
glRotated(angle[2],0,0,1);
glPushMatrix();
glColor3d(0.20,0.20,0.20);
gluCylinder(qobj1,6,4,L1,10,10);
glPopMatrix();
glTranslated(0,0,L1);
glRotated(angle[3],1,0,0);
glRotated(angle[4],0,1,0);
glRotated(angle[5],0,0,1);
glColor3d(0.50,0.50,1.0);
gluCylinder(qobj1,4,2,L2,10,10);
glPopMatrix();

glutSwapBuffers();
}

codebreaker
06-25-2010, 11:42 AM
I'm not sure what you are trying to do, but your
gluLookAt(20,60,70,0,0,0,0,1,0);
is setting the camera between you and the screen, as indicated by the positive setting for the Z eye coordinate (70).
If you are trying to look at the same scene from somewhere behind the screen, and assuming that your target object is centered at 0,0,0 (where you are looking toward from the eye coordinate), I think you could try something as simple as changing the "70" in that call to "-70".