c# - how to make a flag object in a fixed position while there is a movement in lighting -
hi making opengl project in c# , project flag , use different scroll bars move , , i'm trying make flag in fixed position while i'm moving lighting position using scroll bar , please advice ? , or idea on how implement , below cogl.cs i'm using :
using system; using system.collections.generic; using system.windows.forms; using system.drawing; using glfloat = system.single; using tao.freeglut; using tao.opengl; using opengl; using opentk; using opentk.graphics; using opentk.graphics.opengl; using opentk.input; namespace opengl { class cogl { control p; int width; int height; gluquadric obj; public cogl(control pb) { p = pb; width = p.width; height = p.height; initializegl(); obj = glu.glunewquadric(); //!!! } ~cogl() { glu.gludeletequadric(obj); //!!! wgl.wgldeletecontext(m_uint_rc); } uint m_uint_hwnd = 0; public uint hwnd { { return m_uint_hwnd; } } uint m_uint_dc = 0; public uint dc { { return m_uint_dc; } } uint m_uint_rc = 0; public uint rc { { return m_uint_rc; } } void drawoldaxes() { //for time //lights positioning here!!! float[] pos = new float[4]; pos[0] = 100; pos[1] = 10; pos[2] = 10; pos[3] = 1; gl.gllightfv(gl.gl_light0, gl.gl_position, pos); gl.gldisable(gl.gl_lighting); //initial axes gl.glenable(gl.gl_line_stipple); gl.gllinestipple(1, 0xff00); // dotted gl.glbegin(gl.gl_lines); //x red gl.glcolor3f(1.0f, 0.0f, 0.0f); gl.glvertex3f(-3.0f, 0.0f, 0.0f); gl.glvertex3f(3.0f, 0.0f, 0.0f); //y green gl.glcolor3f(0.0f, 1.0f, 0.0f); gl.glvertex3f(0.0f, -3.0f, 0.0f); gl.glvertex3f(0.0f, 3.0f, 0.0f); //z blue gl.glcolor3f(0.0f, 0.0f, 1.0f); gl.glvertex3f(0.0f, 0.0f, -3.0f); gl.glvertex3f(0.0f, 0.0f, 3.0f); gl.glend(); gl.gldisable(gl.gl_line_stipple); } void drawaxes() { gl.glbegin(gl.gl_lines); //x red gl.glcolor3f(1.0f, 0.0f, 0.0f); gl.glvertex3f(-3.0f, 0.0f, 0.0f); gl.glvertex3f(3.0f, 0.0f, 0.0f); //y green gl.glcolor3f(0.0f, 1.0f, 0.0f); gl.glvertex3f(0.0f, -3.0f, 0.0f); gl.glvertex3f(0.0f, 3.0f, 0.0f); //z blue gl.glcolor3f(0.0f, 0.0f, 1.0f); gl.glvertex3f(0.0f, 0.0f, -3.0f); gl.glvertex3f(0.0f, 0.0f, 3.0f); gl.glend(); } float[,,] polygons = new float[46, 46, 3]; public void initflagdata() { (int x = 0; x < 46; x++) (int y = 0; y < 46; y++) { polygons[x, y, 0] = (float)((x / 5.0f) - 4.5f); polygons[x, y, 1] = (float)((y / 5.0f) - 4.5f); polygons[x, y, 2] = (float)(system.math.sin((((x / 5.0f) * 40.0f) / 360.0f) * 3.14159265358979323846 * 2.0f)); } } public bool iswave; public float float_x; public float float_xb; public float float_y; public float float_yb; public void drawflag() { gl.glpushmatrix(); gl.glenable(gl.gl_texture_2d); gl.glcolor3f(1.0f, 1.0f, 1.0f); // try other combinations gl.glbindtexture(gl.gl_texture_2d, textures[0]); //gl.gldisable(gl.gl_lighting); gl.glbegin(gl.gl_quads); (int x = 0; x < 45; x++) { (int y = 0; y < 45; y++) { float_x = ((float)(x)) / 45.0f; // create floating point x value float_y = ((float)(y)) / 45.0f; // create floating point y value float_xb = ((float)(x + 1)) / 45.0f; // create floating point y value+0.0227f float_yb = ((float)(y + 1)) / 45.0f; // create floating point y value+0.0227f // gl.gltexcoord3f(polygons[x, y, 0], polygons[x, y, 1], polygons[x, y, 2]); gl.gltexcoord2f(float_x, float_y); gl.glvertex3f(polygons[x, y, 0], polygons[x, y, 1], polygons[x, y, 2]); gl.gltexcoord2f(float_x, float_yb); //gl.gltexcoord3f(polygons[x, y + 1, 0], polygons[x, y + 1, 1], polygons[x, y + 1, 2]); gl.glvertex3f(polygons[x, y + 1, 0], polygons[x, y + 1, 1], polygons[x, y + 1, 2]); gl.gltexcoord2f(float_xb, float_yb); // gl.gltexcoord3f(polygons[x + 1, y + 1, 0], polygons[x + 1, y + 1, 1], polygons[x + 1, y, 2]); gl.glvertex3f(polygons[x + 1, y + 1, 0], polygons[x + 1, y + 1, 1], polygons[x + 1, y, 2]); gl.gltexcoord2f(float_xb, float_y); //gl.gltexcoord3f(polygons[x + 1, y, 0], polygons[x + 1, y, 1], polygons[x + 1, y, 2]); gl.glvertex3f(polygons[x + 1, y, 0], polygons[x + 1, y, 1], polygons[x + 1, y, 2]); } } if (iswave) waveflag(0); gl.glend(); gl.glpopmatrix(); } glfloat hold; int wiggler = 0; public void waveflag(int value) { if (wiggler == 3) { (int x = 0; x < 45; x++) { hold = polygons[0, x, 2]; (int y = 0; y < 45; y++) { polygons[x, y, 2] = polygons[x + 1, x, 2]; polygons[x, 45, 2] = polygons[x + 1, y, 2]; } polygons[45, x, 2] = hold; } wiggler = 0; } wiggler++; } public void windflag() { if (wiggler == 3) { (int x = 0; x < 45; x++) { hold = polygons[0, x, 2]; (int y = 0; y < 45; y++) { polygons[x, y, 2] = polygons[x + 1, x, 2]; polygons[x, 45, 2] = polygons[x + 1, y, 2]; } polygons[45, x, 2] = hold; } wiggler = 0; } wiggler++; } public void drawfloor() { gl.glenable(gl.gl_lighting); gl.glbegin(gl.gl_quads); //!!! blended reflection gl.glcolor4d(0, 0, 1, 0.5); // gl.glnormal3d(-5, -5, 0); gl.glvertex3d(-5, -5, 0); // gl.glnormal3d(-5, 5, 0); gl.glvertex3d(-5, 5, 0); // gl.glnormal3d(5, 5, 0); gl.glvertex3d(5, 5, 0); //gl.glnormal3d(5, -5, 0); gl.glvertex3d(5, -5, 0); gl.glend(); } public bool isanimate = false; void drawfigures() { gl.glpushmatrix(); // must in scene reflected gl.gllightfv(gl.gl_light0, gl.gl_position, pos); //draw light source gl.gldisable(gl.gl_lighting); gl.gltranslatef(pos[0], pos[1], pos[2]); //yellow light source gl.glcolor3f(1, 1, 0); gl.gltranslatef(-pos[0], -pos[1], -pos[2]); //projection line source plane gl.glbegin(gl.gl_lines); gl.glcolor3d(0.5, 0.5, 0); gl.glvertex3d(pos[0], pos[1],0); gl.glvertex3d(pos[0], pos[1], pos[2]); gl.glend(); if (isanimate) { gl.glrotated(intoptionb, 0, 0, 1); gl.glenable(gl.gl_lighting); } else gl.glrotated(0, 0, 0, 0); gl.glcolor3f(1, 0, 0); gl.gltranslated(0, -0.5, 3.5); gl.glscalef(0.5f, 0.5f, 0.5f); gl.glrotated(70, 1, 1, 1); drawflag(); gl.glpopmatrix(); } public float[] pos = new float[4]; public int intoptionb = 1; public float[] scrollvalue = new float[14]; public float zshift = 0.0f; public float yshift = 0.0f; public float xshift = 0.0f; public float zangle = 0.0f; public float yangle = 0.0f; public float xangle = 0.0f; public int intoptionc = 0; double[] accumulatedrotationstraslations = new double[16]; public bool q = true; public void draw() { //shadows pos[0] =scrollvalue[10]; pos[1] = scrollvalue[11]; pos[2] = scrollvalue[12]; pos[3] = 1.0f; //shadows if (m_uint_dc == 0 || m_uint_rc == 0) return; //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! // see initializegl //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! gl.glclear(gl.gl_color_buffer_bit | gl.gl_depth_buffer_bit | gl.gl_stencil_buffer_bit); gl.glloadidentity(); // not trivial double[] modelvievmatrixbeforespecifictransforms = new double[16]; double[] currentrotationtraslation = new double[16]; q = false; glu.glulookat(scrollvalue[0], scrollvalue[1], scrollvalue[2], scrollvalue[3], scrollvalue[4], scrollvalue[5], scrollvalue[6], scrollvalue[7], scrollvalue[8]); gl.gltranslatef(0.0f, 0.0f, -1.0f); //save current modelview matrix values //in modelvievmatrixbeforespecifictransforms array //modelview matrix ========>>>>>> modelvievmatrixbeforespecifictransforms gl.glgetdoublev(gl.gl_modelview_matrix, modelvievmatrixbeforespecifictransforms); //modelview matrix saved, gl.glloadidentity(); // make identity matrix //make transformation in accordance keycode float delta; if (intoptionc != 0) { delta = 5.0f * math.abs(intoptionc) / intoptionc; // signed 5 switch (math.abs(intoptionc)) { case 1: gl.glrotatef(delta, 1, 0, 0); break; case 2: gl.glrotatef(delta, 0, 1, 0); break; case 3: gl.glrotatef(delta, 0, 0, 1); break; case 4: gl.gltranslatef(delta / 20, 0, 0); break; case 5: gl.gltranslatef(0, delta / 20, 0); break; case 6: gl.gltranslatef(0, 0, delta / 20); break; } } //as result - modelview matrix pure representation //of keycode transform , !!! //save current modelview matrix values //in currentrotationtraslation array //modelview matrix =======>>>>>>> currentrotationtraslation gl.glgetdoublev(gl.gl_modelview_matrix, currentrotationtraslation); //the gl.glloadmatrix function replaces current matrix //the 1 specified in argument. //the current matrix //projection matrix, modelview matrix, or texture matrix, //determined current matrix mode (now modelview mode) gl.glloadmatrixd(accumulatedrotationstraslations); //global matrix //the gl.glmultmatrix function multiplies current matrix //the 1 specified in argument. //that is, if m current matrix , t matrix passed //gl.glmultmatrix, m replaced m • t gl.glmultmatrixd(currentrotationtraslation); //save matrix product in accumulatedrotationstraslations gl.glgetdoublev(gl.gl_modelview_matrix, accumulatedrotationstraslations); //replace modelviev matrix stored modelvievmatrixbeforespecifictransforms gl.glloadmatrixd(modelvievmatrixbeforespecifictransforms); //multiply keycode defined accumulatedrotationstraslations matrix gl.glmultmatrixd(accumulatedrotationstraslations); //reflection //reflection b intoptionb += 10; //for rotation intoptionc += 2; //for rotation // without reflection drawall(); // gl.glenable(gl.gl_blend); gl.glblendfunc(gl.gl_src_alpha, gl.gl_one_minus_src_alpha); //only floor, draw stencil buffer gl.glenable(gl.gl_stencil_test); gl.glstencilop(gl.gl_replace, gl.gl_replace, gl.gl_replace); gl.glstencilfunc(gl.gl_always, 1, 0xffffffff); // draw floor gl.glcolormask((byte)gl.gl_false, (byte)gl.gl_false, (byte)gl.gl_false, (byte)gl.gl_false); gl.gldisable(gl.gl_depth_test); drawfloor(); // restore regular settings gl.glcolormask((byte)gl.gl_true, (byte)gl.gl_true, (byte)gl.gl_true, (byte)gl.gl_true); gl.glenable(gl.gl_depth_test); // reflection drawn stencil buffer value equal 1 gl.glstencilfunc(gl.gl_equal, 1, 0xffffffff); gl.glstencilop(gl.gl_keep, gl.gl_keep, gl.gl_keep); gl.glenable(gl.gl_stencil_test); // draw reflected scene gl.glpushmatrix(); gl.glscalef(1, 1, -1); //swap on z axis gl.glenable(gl.gl_cull_face); gl.glcullface(gl.gl_back); drawfigures(); gl.glcullface(gl.gl_front); drawfigures(); gl.gldisable(gl.gl_cull_face); gl.glpopmatrix(); // draw floor //( half-transparent ( see color's alpha byte))) // in order see reflected objects gl.gldepthmask((byte)gl.gl_false); drawfloor(); gl.gldepthmask((byte)gl.gl_true); // disable gl.gl_stencil_test show all, else cut on gl.gl_stencil gl.gldisable(gl.gl_stencil_test); drawfigures(); //reflection e /* gl.glpushmatrix(); makeshadowmatrix(); gl.glmultmatrixf(cubexform); drawfloor(); gl.glpopmatrix();*/ gl.glflush(); wgl.wglswapbuffers(m_uint_dc); } public virtual void initializegl() { m_uint_hwnd = (uint)p.handle.toint32(); m_uint_dc = wgl.getdc(m_uint_hwnd); // not doing following wgl.wglswapbuffers() on dc // result in failure subsequently create rc. wgl.wglswapbuffers(m_uint_dc); wgl.pixelformatdescriptor pfd = new wgl.pixelformatdescriptor(); wgl.zeropixeldescriptor(ref pfd); pfd.nversion = 1; pfd.dwflags = (wgl.pfd_draw_to_window | wgl.pfd_support_opengl | wgl.pfd_doublebuffer); pfd.ipixeltype = (byte)(wgl.pfd_type_rgba); pfd.ccolorbits = 32; pfd.cdepthbits = 32; pfd.ilayertype = (byte)(wgl.pfd_main_plane); //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! //for stencil support pfd.cstencilbits = 32; //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! int pixelformatindex = 0; pixelformatindex = wgl.choosepixelformat(m_uint_dc, ref pfd); if (pixelformatindex == 0) { messagebox.show("unable retrieve pixel format"); return; } if (wgl.setpixelformat(m_uint_dc, pixelformatindex, ref pfd) == 0) { messagebox.show("unable set pixel format"); return; } //create rendering context m_uint_rc = wgl.wglcreatecontext(m_uint_dc); if (m_uint_rc == 0) { messagebox.show("unable rendering context"); return; } if (wgl.wglmakecurrent(m_uint_dc, m_uint_rc) == 0) { messagebox.show("unable make rendering context current"); return; } initrenderinggl(); } public void onresize() { width = p.width; height = p.height; gl.glviewport(0, 0, width, height); draw(); } protected virtual void initrenderinggl() { if (m_uint_dc == 0 || m_uint_rc == 0) return; if (this.width == 0 || this.height == 0) return; initflagdata(); gl.glshademodel(gl.gl_smooth); gl.glclearcolor(0.5f, 0.5f, 0.5f, 0.5f); gl.glcleardepth(1.0f); gl.glenable(gl.gl_light0); gl.glenable(gl.gl_color_material); gl.glcolormaterial(gl.gl_front_and_back, gl.gl_ambient_and_diffuse); gl.glenable(gl.gl_depth_test); gl.gldepthfunc(gl.gl_lequal); gl.glhint(gl.gl_perspective_correction_hint, gl.gl_nicest); gl.glviewport(0, 0, this.width, this.height); gl.glmatrixmode(gl.gl_projection); gl.glloadidentity(); //nice 3d glu.gluperspective(45.0, 1.0, 0.4, 100.0); gl.glmatrixmode(gl.gl_modelview); gl.glloadidentity(); //save current modelview matrix (now identity) gl.glgetdoublev(gl.gl_modelview_matrix, accumulatedrotationstraslations); //! texture 1a generatetextures(); } public uint[] textures = new uint[1]; void generatetextures() { gl.glblendfunc(gl.gl_src_alpha, gl.gl_one_minus_src_alpha); gl.glgentextures(1, textures); string imagesname = "c:\\users\\monay\\desktop\\finalprojectgl\\myopengl-תקין\\flag_of_israel.bmp"; bitmap image = new bitmap(imagesname); image.rotateflip(rotatefliptype.rotatenoneflipy); //y axis in windows directed downwards, while in opengl-upwards system.drawing.imaging.bitmapdata bitmapdata; rectangle rect = new rectangle(0, 0, image.width, image.height); bitmapdata = image.lockbits(rect, system.drawing.imaging.imagelockmode.readonly, system.drawing.imaging.pixelformat.format24bpprgb); gl.glbindtexture(gl.gl_texture_2d, textures[0]); //2d xyz gl.glteximage2d(gl.gl_texture_2d, 0, (int)gl.gl_rgb8, image.width, image.height, 0, gl.gl_bgr_ext, gl.gl_unsigned_byte, bitmapdata.scan0); gl.gltexparameteri(gl.gl_texture_2d, gl.gl_texture_min_filter, (int)gl.gl_nearest); gl.gltexparameteri(gl.gl_texture_2d, gl.gl_texture_mag_filter, (int)gl.gl_nearest); image.unlockbits(bitmapdata); //image.dispose(); } float[] cubexform = new float[16]; void makeshadowmatrix(float[,] points) { float[] planecoeff = new float[4]; float dot; // find plane equation coefficients // find first 3 coefficients same way // find normal. calcnormal(points, planecoeff); // find last coefficient substitutions planecoeff[3] = -( (planecoeff[0] * points[2, 0]) + (planecoeff[1] * points[2, 1]) + (planecoeff[2] * points[2, 2])); // dot product of plane , light position dot = planecoeff[0] * pos[0] + planecoeff[1] * pos[1] + planecoeff[2] * pos[2] + planecoeff[3]; // projection // first column cubexform[0] = dot - pos[0] * planecoeff[0]; cubexform[4] = 0.0f - pos[0] * planecoeff[1]; cubexform[8] = 0.0f - pos[0] * planecoeff[2]; cubexform[12] = 0.0f - pos[0] * planecoeff[3]; // second column cubexform[1] = 0.0f - pos[1] * planecoeff[0]; cubexform[5] = dot - pos[1] * planecoeff[1]; cubexform[9] = 0.0f - pos[1] * planecoeff[2]; cubexform[13] = 0.0f - pos[1] * planecoeff[3]; // third column cubexform[2] = 0.0f - pos[2] * planecoeff[0]; cubexform[6] = 0.0f - pos[2] * planecoeff[1]; cubexform[10] = dot - pos[2] * planecoeff[2]; cubexform[14] = 0.0f - pos[2] * planecoeff[3]; // fourth column cubexform[3] = 0.0f - pos[3] * planecoeff[0]; cubexform[7] = 0.0f - pos[3] * planecoeff[1]; cubexform[11] = 0.0f - pos[3] * planecoeff[2]; cubexform[15] = dot - pos[3] * planecoeff[3]; } void reducetounit(float[] vector) { float length; // calculate length of vector length = (float)math.sqrt((vector[0] * vector[0]) + (vector[1] * vector[1]) + (vector[2] * vector[2])); // keep program blowing providing exceptable // value vectors may calculated close zero. if (length == 0.0f) length = 1.0f; // dividing each element length result in // unit normal vector. vector[0] /= length; vector[1] /= length; vector[2] /= length; } const int x1 = 0; const int y1 = 1; const int z1 = 2; // points p1, p2, & p3 specified in counter clock-wise order void calcnormal(float[,] v, float[] outp) { float[] v1 = new float[3]; float[] v2 = new float[3]; // calculate 2 vectors 3 points v1[x1] = v[0, x1] - v[1, x1]; v1[y1] = v[0, y1] - v[1, y1]; v1[z1] = v[0, z1] - v[1, z1]; v2[x1] = v[1, x1] - v[2, x1]; v2[y1] = v[1, y1] - v[2, y1]; v2[z1] = v[1, z1] - v[2, z1]; // take cross product of 2 vectors // normal vector stored in out outp[x1] = v1[y1] * v2[z1] - v1[z1] * v2[y1]; outp[y1] = v1[z1] * v2[x1] - v1[x1] * v2[z1]; outp[z1] = v1[x1] * v2[y1] - v1[y1] * v2[x1]; // normalize vector (shorten length one) reducetounit(outp); } } }
Comments
Post a Comment