c - Glut object's moving without telling -
i'm working glut , c, purpose screen 3d lego , 2 kaplas dynamic width, height , depth. there's few keyboard listeners move camera, , rotate kaplas.
my problem : when press touch of keyboard or mouse (wich not dealed in code) lego x , z positions increased !
here's code , details :
int main(int argc, char **argv) { // init glut , create window glutinit(&argc, argv); glutinitdisplaymode(glut_depth | glut_double | glut_rgba); glutinitwindowposition(100,100); glutinitwindowsize(800,800); glutcreatewindow("lighthouse3d- glut tutorial"); glclearcolor(0.5f, 0.5f, 0.5f, 0.5f); glutdisplayfunc(renderscene); //my display function glutreshapefunc(resize); //my reshape function glutkeyboardfunc(key); //basic keyboard listener function glutspecialfunc(specialkey); //special keyboard listener function glenable(gl_depth_test); glenable(gl_normalize); glenable(gl_color_material); glenable(gl_lighting); glenable(gl_light0); gllightiv(gl_light0, gl_position, light_position); glutmainloop(); return 1; }
the code below reshape function :
static void resize(int width, int height) { if(height == 0) height = 1; const float ar = (float) width / (float) height; glmatrixmode(gl_projection); glloadidentity(); glviewport(0, 0, width, height); gluperspective(90, ar, 1, 10000); glmatrixmode(gl_modelview); glloadidentity(); }
next functions creating kapla , lego :
void setkapla() { glcolor3f(0.76f, 0.42f, 0.0f); float rx = xrot; //moved keyboard's 'x' key float ry = yrot; //moved keyboard's 'y' key float rz = zrot; //moved keyboard's 'z' key float px = xpos; //not dynamic, equal 0.0f float py = ypos; //not dynamic, equal 0.0f float pz = zpos; //not dynamic, equal -30.0f glpushmatrix(); gltranslatef(px, py, pz); //place origin point static coords glrotatef(anglerot, rx, ry, rz); //rotate object following //dynamic coords glscalef(1.0f, 3.0f, 15.0f); //multiplying following cube //proportions glutsolidcube(2.0f); //base cube glpopmatrix(); }
now lego :
void setlego(int zheight) { gluquadricobj* quadric = glunewquadric(); //creatin quadric object gluquadricdrawstyle(quadric, glu_fill); glcolor3f(0.72f, 0.72f, 0.05f); glpushmatrix(); gltranslatef(poslegox, poslegoy, poslegoz); //setting lego //position //dynamic coords, setted //in function in //next bloc of code glrotatef(-90, 1, 0, 0); //rotation next cylinder glucylinder(quadric, 2.4, 2.4, 1.7, 10, 10); //cylinder in top of //the cube gludisk(quadric, 0.0, 2.4, 10, 10); //closing bottom of //cylinder gltranslatef(0.0f, 0.0f, 1.7f); //translated top of //cylinder gludisk(quadric, 0.0, 2.4, 10, 10); //closing top of cylinder gltranslatef(0.0f, 0.0f, ((-3.2f * zheight) / 2.0f) - 1.7f); //setting lego's basic cube position glscalef(7.8f, 7.8f, 3.2f * zheight); //setting lego's basic cube //size function param glutsolidcube(1.0f); //creating cube glpopmatrix(); gludeletequadric(quadric); }
instead of kapla, lego not immediatly created in display function, before following function called multiply basic lego's make complete lego (following params) :
void addlego(int depth, int height, int width) { int = 0; int j = 0; for(i=0 ; i<depth ; i++) { //multiply basic lego right depth poslegox += 7.8f; //incrementing global variable wich change //basic position of current lego in above //function setlego(height); //creating current basic lego } for(j=0 ; j<width ; j++) { //multiply basic lego right depth poslegoz -= 7.8; //decrementing global variable wich change //basic position of current lego in above //function setlego(height); //creating current basic lego } }
and display function :
void renderscene(void) { glclear(gl_color_buffer_bit | gl_depth_buffer_bit); printf(" xcam : %f | ycam : %f\n", xcam, ycam); glpushmatrix(); glulookat(xcam, ycam, zcam, xcenter, ycenter, zcenter, xvec, yvec, zvec); //move camera keyboard's arrow //touches xpos = -10.0f; //x position of first kapla ypos = -10.0f; //y position of first kapla setkapla(); //set first kapla xpos = 10.0f; //x position of second kapla ypos = 10.0f; //y position of second kapla setkapla(); //set second kapla addlego(2, 5, 3); //creating complete lego 2 basic legos in //depth, 5 legos in height , 3 legos in width glpopmatrix(); glutswapbuffers(); }
my basic , special keyboard functions using keys : q, a, z, x, y, space, +, -, glut_key_left, glut_key_right, glut_key_up, glut_key_down.
when pressed key of mouse or keyboard, poslegox
, poslegoz
each incrementing , decrementing following code in addlego()
function, there's no event in code wich control it.
the result complete lego run away camera when press key, not kapla !!
!important! made lot of tests, , problem located in addlego()
function, tried put in glpushmatrix()
, glpopmatrix()
in different lines, tried put glflush()
, glloadidentity()
nothing worked ...
can have advice stop ?
thanks lot !
have nice day,
ankomm
solution edit :
i found problem 1 of teacher : functions wich listening keyboard , mouse events listening keys , buttons they're not setted in functions, , refresh redisplay (wich call display function [here : renderscene()
]). renderscene()
call function wich increment , decrement global variables wich been setted when programm compile. solution set variables values @ beginning of function :
void addlego(int depth, int height, int width) { poslegox = 0.0f; poslegoz = -50.0f;//set values here when function //called, position of first lego //the same int = 0; int j = 0; for(i=0 ; i<depth ; i++) { //multiply basic lego right depth poslegox += 7.8f; //incrementing global variable wich change //basic position of current lego in above //function setlego(height); //creating current basic lego } for(j=0 ; j<width ; j++) { //multiply basic lego right depth poslegoz -= 7.8; //decrementing global variable wich change //basic position of current lego in above //function setlego(height); //creating current basic lego } }
and works !
Comments
Post a Comment