# Camera work

<<Help beginner so God give you some where a special power>>

some how i got working camera but it is not working properly
i want to use it as a person’s eye control by
w,a,s,d and mouse x,y(rotation)
in the following code:
main.cpp

#include <stdlib.h>
#include <math.h>
#include <GL/glut.h>

#define WinWidth 640
#define WinHeight 480
/*This Time

A plane with objects lied on it
i try to move around the world of plane(glRectf is right choice for it instead of those of 4 vertex’s)
*/

//where
GLfloat x = 0.0f,y = 0.0f,z = 0.0f;
//what
GLfloat Cx,Cy,Cz;
GLfloat alpha = 0.0f,beta = 0.0f,gama = 90.0f;
GLfloat col[4] = {0.70f,0.70f,0.70f,0.70f};
// not much but for measureable distance
void fog(GLfloat Density,GLfloat *col)
{
glFogf( GL_FOG_DENSITY, Density );
glFogfv( GL_FOG_COLOR, col );
glFogf( GL_FOG_START, -10.0f );
glFogf( GL_FOG_END, 100.0f );
glFogf( GL_FOG_MODE, GL_EXP );
}

void CameraMan()
{
///gluLookAt(x,y,z,Cx,Cy,Cz,0.0f,1.0f,0.0f);Cx <->x , Cy <-> y Cz <->z
//for circle there must radius let it 0.5f
Cx = (x+1.5f)*sin(alpha);
Cy = (y+1.5f)*cos(beta);
Cz = (z+1.5f)*sin(gama);
if(alpha > 180.0f)alpha = 180.0f;
if(beta > 180.0f)beta = 180.0f;
if(gama > 180.0f)gama = 180.0f;

if(alpha < -180.0f)alpha = -180.0f;
if(beta < -180.0f)beta = -180.0f;
if(gama < -180.0f)gama = -180.0f;

gluLookAt(x,y,z,Cx,Cy,Cz,0.0f,1.0f,0.0f);

}
void ReSce()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glClearColor(0.0f,0.0f,0.0f,0.0f);
glEnable(GL_DEPTH_TEST);
fog(1.0f ,col);
glEnable(GL_FOG);

CameraMan();

glMatrixMode(GL_PROJECTION);
glTranslatef(0.0f,0.0f,-1.0f);

glPushMatrix();
glColor3f(0.0f,1.0f,0.0f);
glutSolidCube(1.0);
glRotatef(90.0f,0.0f,0.0f,0.0f);
glTranslatef(0.0f,0.0f,0.0f);
glColor3f(1.0f,0.0f,0.0f);
glRectf(2.0f,2.0f,-2.0f,-2.0f);
glPopMatrix();

glMatrixMode(GL_MODELVIEW);
glutSwapBuffers();
}

void WinSize(int w,int h)
{
GLfloat As = (GLfloat)w/(GLfloat)h;
glMatrixMode(GL_PROJECTION);
gluPerspective(100.0f,As,-500.0f,500.0f);
glMatrixMode(GL_MODELVIEW);

glViewport(0,0,w,h);
}
void Idle()
glutPostRedisplay();
}
void keyb(unsigned char k , int x, int y)
{
switch(k)
{
case ‘q’:
exit(0);
break;
case ‘w’:
z+=0.4f;
break;
case ‘a’:
::x-=0.4f;
break;

case ‘s’:
z-=0.4f;
break;
case ‘d’:
::x+=0.4f;
break;

}
}
void MoMo(int x,int y)
{GLfloat As = WinWidth/WinHeight;
(void)x;
(void)y;
alpha=x*As;
beta=y/As;
}
int main(int argc,char *argv[])
{
glutInit(&argc,argv);
glutInitWindowSize(WinWidth,WinHeight);
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow(“Win”);
glEnable(GL_FOG);
glutDisplayFunc(ReSce);
glutReshapeFunc(WinSize);
glutKeyboardFunc(keyb);
glutPassiveMotionFunc(MoMo);
glutIdleFunc(Idle);
glutMainLoop();

}

and

ifdef LINUX_USER

makefile:

LIB = -I/usr/lib
INC = -I/usr/include
SRC = main.cpp
COMP = gcc

1st:
\$(COMP) -o Cam2 \$(SRC) \$(INC) \$(LIB) \$(LINK)

#endif

here is big problem:

Q1. as MouseFunc (x,y) not as increment or decrement it direct assign(for eg. alpha b/w 90,-90 but x || y are at 230,340 ) how to resolve it as i want no increment/decrement
after 180/-180

Q2.how to redefine old Axis with new Axis(for eg. by luck(if god with you) if you run code intially dont touch mouse use w,a,s,d now move mouse on GL screen and again use w,a,s,d)
so then you understand how this camera move but in real
life we are not walk usually like that so i want to make it
as in the real life

perhape’s understand but by practical might someone understand
<<Help beginner so God give you some where a special power>>
<<which is in the game>>

Q1.
if (alpha > 180) {
alpha = 180
} else if (alpha < -180) {
alpha = -180
}

thanks for Q1 it works but
i think there must be some implemention of triple buffer for this dirty program (As am not good programmer )
if you had run it and also run translating object with single buffer
Q on moving mouse there at a point a ghost/duplicate/problematic thing happen(how slow down the baby mouse)

Shouldn’t zNear value be >= 1 ?

thanks but if you compare
GLAPI void GLAPIENTRY gluPerspective (GLdouble fovy, GLdouble aspect, GLdouble zNear, GLdouble zFar);
with
gluPerspective(100.0f,As,-500.0f,500.0f);
zNear = -500.0f
do you mean value <1.0f
or
your mean is i need to put value > 1.0f
<<sorry for bad English and weak understanding grammer cause i am Indian(Bhartiya<-in hindi)>>

and after putting glLoadIdentity before CameraMen
i am near to that point <<need some correction in it >>
if please some can help help me
<<Sorry if i use language that hurt or any abuse word>>

well being Indian is not reason for bad English

zNear should be positive… And I also think that it should be 1.0 or higher… But I’m not positively sure about this…

i made change which slow that mouse(x,y) but steal having problem