c++ - How to put the "camera" inside a cube in OpenGL -
i figure out how put "camera" inside cube i've created can move in fps-like style. tried use glulookat , gluperspective i'm missing steps. should before glulookat?
here's code written far:
int rotate_y; //used rotate cube y-axis int rotate_x; //used rotate cube x-axis //the display function draws scene , redraws void display(){ //clear screen , z-buffer glclear(gl_color_buffer_bit | gl_depth_buffer_bit); glloadidentity(); //resets transformations glrotatef(rotate_x, 1.0, 0.0, 0.0); glrotatef(rotate_y, 0.0, 1.0, 0.0); //front face of cube - vertex definition glbegin(gl_polygon); glcolor3f(0.0, 1.0, 0.0); glvertex3f(-0.5f, -0.5f, -0.5f); glvertex3f(-0.5f, 0.5f, -0.5f); glvertex3f(0.5f, 0.5f, -0.5f); glvertex3f(0.5f, -0.5f, -0.5f); glend(); //back face of cube - vertex definition glbegin(gl_polygon); glcolor3f(1.0, 0.0, 0.0); glvertex3f(-0.5f, -0.5f, 0.5f); glvertex3f(-0.5f, 0.5f, 0.5f); glvertex3f(0.5f, 0.5f, 0.5f); glvertex3f(0.5f, -0.5f, 0.5f); glend(); //right face of cube - vertex definition glbegin(gl_polygon); glcolor3f(1.0, 0.0, 1.0); glvertex3f(0.5f, -0.5f, 0.5f); glvertex3f(0.5f, 0.5f, 0.5f); glvertex3f(0.5f, 0.5f, -0.5f); glvertex3f(0.5f, -0.5f, -0.5f); glend(); //left face of cube - vertex definition glbegin(gl_polygon); glcolor3f(0.7, 0.7, 0.0); glvertex3f(-0.5f, -0.5f, -0.5f); glvertex3f(-0.5f, 0.5f, -0.5f); glvertex3f(-0.5f, 0.5f, 0.5f); glvertex3f(-0.5f, -0.5f, 0.5f); glend(); //upper face of cube - vertex definition glbegin(gl_polygon); glcolor3f(0.7, 0.7, 0.3); glvertex3f(-0.5f, 0.5f, 0.5f); glvertex3f(-0.5f, 0.5f, -0.5f); glvertex3f(0.5f, 0.5f, -0.5f); glvertex3f(0.5f, 0.5f, 0.5f); glend(); //bottom face of cube - vertex definition glbegin(gl_polygon); glcolor3f(0.2, 0.2, 0.8); glvertex3f(-0.5f, -0.5f, -0.5f); glvertex3f(-0.5f, -0.5f, 0.5f); glvertex3f(0.5f, -0.5f, 0.5f); glvertex3f(0.5f, -0.5f, -0.5f); glend(); glflush(); glutswapbuffers(); //send image screen } //the special keys function allows interaction via keys (also special ones) void specialkeys(int key, int x, int y){ switch (key){ case glut_key_f1: exit(0); break; case glut_key_left: rotate_y -= 5; break; case glut_key_up: rotate_x -= 5; break; case glut_key_right: rotate_y += 5; break; case glut_key_down: rotate_x += 5; break; } glutpostredisplay(); //request screen refresh see changes } int main(int argc, char*argv[]){ //initialize glut glutinit(&argc, argv); //request double buffering, rgb colors window , z-buffer glutinitdisplaymode(glut_double | glut_rgb | glut_depth); //create window glutinitwindowsize(600, 600); glutinitwindowposition(100, 100); glutcreatewindow("space"); //enable depth glenable(gl_depth_test); //callback functions glutdisplayfunc(display); //display - redraws scene glutspecialfunc(specialkeys); //special - allows interaction specialkeys //pass control glut events glutmainloop(); return 0; //this line never reached }
you'll want use gltranslatef(float x,float y,float z) move camera.
note because of how opengl works, transformations apply rest of world , not camera. above function move drawn afterwards (and not camera) amount specified.
to camera move position, you'll want negate components of position before passing them function. move world in opposite direction, putting camera want it.
Comments
Post a Comment