opengl - making lighting inside a cube and reflecting the texture -
i'm trying make light shine inside cube , reflect image on bottom of cube,i have been trying make work seems keep failing supposed do,may 1 explain me,what should add code below,normally code reflecting sides of cube on floor created....
this cube code below
void drawcube(int textureid, float angle) { gldisable(gl_texture_2d); glpushmatrix(); glrotatef( rotate_x, 1.0, 0.0, 0.0 ); glrotatef( rotate_y, 0.0, 1.0, 0.0 ); glbegin(gl_quads); //bottom face glcolor3f(1.0f, 0.0f, 1.0f); glnormal3f(0.0, -1.0f, 0.0f); glvertex3f(-box_size / 2, -box_size / 2, -box_size / 2); glvertex3f(box_size / 2, -box_size / 2, -box_size / 2); glvertex3f(box_size / 2, -box_size / 2, box_size / 2); glvertex3f(-box_size / 2, -box_size / 2, box_size / 2); glend(); glenable(gl_texture_2d); glbindtexture(gl_texture_2d, textureid); gltexparameteri(gl_texture_2d, gl_texture_min_filter, gl_linear); gltexparameteri(gl_texture_2d, gl_texture_mag_filter, gl_linear); glcolor3f(1.0f, 1.0f, 1.0f); glbegin(gl_quads); //front face glnormal3f(0.0, 0.0f, 1.0f); gltexcoord2f(0.0f, 0.0f); glvertex3f(-box_size / 2, -box_size / 2, box_size / 2); gltexcoord2f(1.0f, 0.0f); glvertex3f(box_size / 2, -box_size / 2, box_size / 2); gltexcoord2f(1.0f, 1.0f); glvertex3f(box_size / 2, box_size / 2, box_size / 2); gltexcoord2f(0.0f, 1.0f); glvertex3f(-box_size / 2, box_size / 2, box_size / 2); glnormal3f(-1.0, 0.0f, 0.0f); gltexcoord2f(0.0f, 0.0f); glvertex3f(-box_size / 2, -box_size / 2, -box_size / 2); gltexcoord2f(1.0f, 0.0f); glvertex3f(-box_size / 2, -box_size / 2, box_size / 2); gltexcoord2f(1.0f, 0.0f); glvertex3f(-box_size / 2, box_size / 2, box_size / 2); gltexcoord2f(1.0f, 0.0f); glvertex3f(-box_size / 2, box_size / 2, -box_size / 2); //back face glnormal3f(0.0, 0.0f, -1.0f); gltexcoord2f(0.0f, 0.0f); glvertex3f(-box_size / 2, -box_size / 2, -box_size / 2); gltexcoord2f(1.0f, 0.0f); glvertex3f(-box_size / 2, box_size / 2, -box_size / 2); gltexcoord2f(1.0f, 1.0f); glvertex3f(box_size / 2, box_size / 2, -box_size / 2); gltexcoord2f(0.0f, 1.0f); glvertex3f(box_size / 2, -box_size / 2, -box_size / 2); glend(); glpopmatrix(); }
this code light is
void drawscene() { glclear(gl_color_buffer_bit | gl_depth_buffer_bit | gl_stencil_buffer_bit); glmatrixmode(gl_modelview); glloadidentity(); gltranslatef(0.0f, 0.0f, -40.0f); glrotatef(30, 1, 0, 0); glfloat ambientlight[] = {0.3f, 0.3f, 0.3f, 1.0f}; gllightmodelfv(gl_light_model_ambient, ambientlight); glfloat lightcolor[] = {0.7f, 0.7f, 0.7f, 1.0f}; glfloat lightpos[] = {-2 * box_size, box_size, 4 * box_size, 1.0f}; gllightfv(gl_light0, gl_diffuse, lightcolor); gllightfv(gl_light0, gl_position, lightpos); glpushmatrix(); gltranslatef(0, box_height, 0); drawcube(_textureid, _angle); glpopmatrix(); glenable(gl_stencil_test); //enable using stencil buffer glcolormask(0, 0, 0, 0); //disable drawing colors screen gldisable(gl_depth_test); //disable depth testing glstencilfunc(gl_always, 1, 1); //make stencil test pass //make pixels in stencil buffer set 1 when stencil test passes glstencilop(gl_keep, gl_keep, gl_replace); //set of pixels covered floor 1 in stencil buffer drawfloor(_textureid); glcolormask(1, 1, 1, 1); //enable drawing colors screen glenable(gl_depth_test); //enable depth testing //make stencil test pass when pixel 1 in stencil buffer glstencilfunc(gl_equal, 1, 1); glstencilop(gl_keep, gl_keep, gl_keep); //make stencil buffer not change //draw cube, reflected vertically, @ pixels stencil //buffer 1 glpushmatrix(); glscalef(1, -1, 1); gltranslatef(0, box_height, 0); drawcube(_textureid, _angle); glpopmatrix(); gldisable(gl_stencil_test); //disable using stencil buffer //blend floor onto screen glenable(gl_blend); glcolor4f(1, 1, 1, 0.7f); drawfloor(_textureid); gldisable(gl_blend); glutswapbuffers(); }
Comments
Post a Comment