PDA

View Full Version : how to create this camera ...



orache
06-17-2013, 10:20 AM
hello
i want to create this camera but i can not it please give me (or write me ) a source code because i am Beginner
i want to create camera that when i rotate the camera the camera Coordinate Current coordinates can be transformed into (I do not think Understand My Purpose :sorrow: :D )
give me one very small source code of only camera that rotate and move with keyboard . please help me i need your help

carsten neumann
06-17-2013, 12:22 PM
Why are you not following up on the replies you got in your previous thread (http://www.opengl.org/discussion_boards/showthread.php/181976-one-questions-%28Important%29)? Nobody will be able to write the camera code you want, because you are not clearly saying how that camera is supposed to behave. Without that information it will just be blind guessing, which usually isn't very productive.

orache
06-18-2013, 12:02 AM
i have one code but i can not Understand it :sorrow:
i can use it but i want to create big game and big game engine for example fps game, tps game ,...
it is the code

#include <freeglut.h>
#include <stdlib.h>
#include <math.h>
//angle of rotation
float xpos = 0, ypos = 0, zpos = 0, xrot = 0, yrot = 0, angle=0.0;

//positions of the cubes
float positionz[10];
float positionx[10];

void cubepositions (void) { //set the positions of the cubes

for (int i=0;i<10;i++)
{
positionz[i] = rand()%5 + 5;
positionx[i] = rand()%5 + 5;
}
}

//draw the cube
void cube (void) {
for (int i=0;i<10;i++)
{
glPushMatrix();
glTranslated(-positionx[i + 1] * 10, 0, -positionz[i + 1] *
10); //translate the cube
glutSolidCube(2); //draw the cube
glPopMatrix();
}
}

void init (void) {
cubepositions();
}

void enable (void) {
glEnable (GL_DEPTH_TEST); //enable the depth testing
glEnable (GL_LIGHTING); //enable the lighting
glEnable (GL_LIGHT0); //enable LIGHT0, our Diffuse Light
glShadeModel (GL_SMOOTH); //set the shader to smooth shader

}

void camera (void) {
glRotatef(xrot,1.0,0.0,0.0); //rotate our camera on teh x-axis (left and right)
glRotatef(yrot,0.0,1.0,0.0); //rotate our camera on the y-axis (up and down)
glTranslated(-xpos,-ypos,-zpos); //translate the screen to the position of our camera
}

void display (void) {
glClearColor (0.0,0.0,0.0,1.0); //clear the screen to black
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); //clear the color buffer and the depth buffer
glLoadIdentity();
camera();
enable();
cube(); //call the cube drawing function
glutSwapBuffers(); //swap the buffers
angle++; //increase the angle
}

void reshape (int w, int h) {
glViewport (0, 0, (GLsizei)w, (GLsizei)h); //set the viewport to the current window specifications
glMatrixMode (GL_PROJECTION); //set the matrix to projection

glLoadIdentity ();
gluPerspective (60, (GLfloat)w / (GLfloat)h, 1.0, 1000.0
); //set the perspective (angle of sight, width, height, , depth)
glMatrixMode (GL_MODELVIEW); //set the matrix back to model

}

void keyboard (unsigned char key, int x, int y) {
if (key=='q')
{
xrot += 1;
if (xrot >360) xrot -= 360;
}

if (key=='z')
{
xrot -= 1;
if (xrot < -360) xrot += 360;
}

if (key=='w')
{
float xrotrad, yrotrad;
yrotrad = (yrot / 180 * 3.141592654f);
xrotrad = (xrot / 180 * 3.141592654f);
xpos += float(sin(yrotrad)) ;
zpos -= float(cos(yrotrad)) ;
ypos -= float(sin(xrotrad)) ;
}

if (key=='s')
{
float xrotrad, yrotrad;
yrotrad = (yrot / 180 * 3.141592654f);
xrotrad = (xrot / 180 * 3.141592654f);
xpos -= float(sin(yrotrad));
zpos += float(cos(yrotrad)) ;
ypos += float(sin(xrotrad));
}

if (key=='a')
{
yrot += 1;
if (yrot >360) yrot -= 360;
}

if (key=='a')
{
yrot -= 1;
if (yrot < -360)yrot += 360;
}
if (key==27)
{
exit(0);
}
}

int main (int argc, char **argv) {
glutInit (&argc, argv);
glutInitDisplayMode (GLUT_DOUBLE | GLUT_DEPTH); //set the display to Double buffer, with depth
glutInitWindowSize (500, 500); //set the window size
glutInitWindowPosition (100, 100); //set the position of the window
glutCreateWindow ("A basic OpenGL Window"); //the captionof the window
init (); //call the init function
glutDisplayFunc (display); //use the display function to draw everything
glutIdleFunc (display); //update any variables in display,display can be changed to anyhing, as long as you move the variables to be updated, in this case, angle++;
glutReshapeFunc (reshape); //reshape the window accordingly

glutKeyboardFunc (keyboard); //check the keyboard
glutMainLoop (); //call the main loop
return 0;
}

carsten neumann
06-18-2013, 08:23 AM
This may sound harsh, but until you can understand programs of the size you posted I highly doubt you will have success making a big game or engine. You'll need an understanding of the basics of computer graphics and OpenGL first. There are many tutorials out there that can teach you about these things, take a look at the wiki (http://www.opengl.org/wiki/Getting_Started) (see near the bottom of the linked page). Graphics is a large topic, it takes some time to get up to speed on it; take your time and don't despair.

BTW: For help with you specific program, please ask specific questions. Posting a bunch of code and asking "what does this do?" is not specific and will rarely produce helpful answers.