PDA

View Full Version : Need help with the Camera viewing

chang
04-10-2012, 02:36 AM
I am a beginner and i have recently started learning opengl .

so,please help me with the camera viewing and especially with gluPerspective ,i am having a lot of problems because of that.....

I had written a program that creates the image of the universe .i had created the orbits of the planets in the x-z plane and i wanna see it from upward i.e. from a point about h height from the x-z plane ..
this is my code -

#include <iostream>
#include <GL/glut.h>
#include <GL/glu.h>
#include <GL/gl.h>
#include <cmath>

using namespace std;

void render(){
glClearColor(0.0,0.0,1.0,1.0);
// glEnable(GL_DEPTH_TEST);
}

GLfloat xrot = 360.0f,yrot = 0.0f,zrot = 0.0f;
void draw()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glTranslatef(0.0f,0.0f,-100.0f);
GLfloat x,z,angle,y = 0.0f;
glPushMatrix();
glRotatef(yrot,0.0,1.0,0.0);
glColor3f(1.0,1.0,0.0);
glutSolidSphere(7.0f,20,20);
glBegin(GL_POINTS);
for (angle = 0.0f;angle <= 3.1415f*2.0f*3.0f; angle += 0.08f){
glColor3f(1.0,0.0,0.0);
x = 30.0f*sin(angle);
z = 30.0f*cos(angle);
// y = 30.0f*cos(4 * angle);
// if (x < 5.0f &amp;&amp; x > 5.0f )
glVertex3f(x,y,z);
x = 50.0f*sin(angle);
z = 50.0f*cos(angle);
glColor3f(0.0,0.0,0.0);
glVertex3f(x,y,z);

}
glEnd();
glTranslatef(-30.0f,00.0f,0.0f);
glColor3f(0.0,0.0,0.0);
glutSolidSphere(3.0f,20,20);

glPopMatrix();
glRotatef((yrot),0.0,-1.0,0.0);
glTranslatef(-50.0f,00.0f,0.0f);
glColor3f(1.0,1.0,1.0);
glutSolidSphere(3.0f,20,20);

glutSwapBuffers();

}
void update(int x){
yrot += 0.75f;
glutPostRedisplay();
glutTimerFunc(33,update,0);
}
void reshape(int w,int h){
glViewport(0,0,w,h);
glMatrixMode(GL_PROJECTION);
// glTranslatef(0.0,10.0,-100.0);
// glRotatef(90,1,0,0);

// gluLookAt(0,0,0,0,0,-100, 1,0,0);
gluPerspective(90.0,w/h,1.0,200.0);
glMatrixMode(GL_MODELVIEW);
}
int main(int argc,char **argv){
glutInit(&amp;argc,argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
glutInitWindowSize(800,600);
glutCreateWindow("chang");
render();
glutDisplayFunc(draw);
glutTimerFunc(1,update,0);
glutReshapeFunc(reshape);
glutMainLoop();
}

someone plese help me this ..
and also if u can explain me some about gluPerspective then it would be a lot more helpful...

rgwda
04-10-2012, 03:53 AM
void draw()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glTranslatef(0.0f,0.0f,-100.0f);

glRotatef( 15.0, 1.0, 0.0, 0.0 ); /* ADD THIS CODE */
/* IT WILL TILT THE PLANE OF ROTATION BY 15 DEGREES */

GLfloat x,z,angle,y = 0.0f;
glPushMatrix();
glRotatef(yrot,0.0,1.0,0.0);
glColor3f(1.0,1.0,0.0);
glutSolidSphere(7.0f,20,20);
glBegin(GL_POINTS);
for (angle = 0.0f;angle <= 3.1415f*2.0f*3.0f; angle += 0.08f){
glColor3f(1.0,0.0,0.0);
x = 30.0f*sin(angle);
z = 30.0f*cos(angle);
// y = 30.0f*cos(4 * angle);
// if (x < 5.0f &amp;&amp; x > 5.0f )
glVertex3f(x,y,z);
x = 50.0f*sin(angle);
z = 50.0f*cos(angle);
glColor3f(0.0,0.0,0.0);
glVertex3f(x,y,z);

}

chang
04-12-2012, 01:42 PM
thanks bro that was really helpful...

could you tell me something more about glrotatef and camera pespectives such as gluperspective and alulookat or provide me reference to some site or book that could help me understand these clearly...............