java - Opengl multiple textures with mixed colors -


why colors of textures mixed? how can fix that?

this how draw , initialize:

private void initgl_3d() {     int width = display.getwidth();     int height = display.getheight();     glloadidentity(); // reset projection matrix     glu.gluperspective(45.0f, ((float) width / (float) height), 0.1f, 100.0f); // calculate aspect ratio of window     glmatrixmode(gl_modelview); // select modelview matrix      glloadidentity(); // reset modelview matrix      glshademodel(gl_smooth); // enables smooth shading       glcleardepth(1.0f); // depth buffer setup     glenable(gl_depth_test); // enables depth testing     gldepthfunc(gl_lequal); // type of depth test     glhint(gl_perspective_correction_hint, gl_nicest); // nice perspective calculations }  private void initgl_2d() {     int width = display.getwidth();     int height = display.getheight();      glmatrixmode(gl_projection);     glloadidentity();     glortho(0.0f, width, height, 0.0f, 1, -1);      glmatrixmode(gl_modelview);     glloadidentity();      gldisable(gl_depth_test);      // enable transparency     glenable(gl_blend);     glblendfunc(gl_src_alpha, gl_one_minus_src_alpha); }  private void rendergl() {     glclear(gl_color_buffer_bit | gl_depth_buffer_bit);     int width = display.getwidth();     int height = display.getheight();      glenable(gl_texture_2d);      glviewport(0, 0, width, height); // reset current viewport\     glmatrixmode(gl_projection); // select projection matrix     if (game){         glclearcolor(0.0f, 0.0f, 0.0f, 0.0f); // black background         initgl_3d();         rendergl_3d();     }     initgl_2d();     rendergl_2d(); }  private void rendergl_3d() {     glloadidentity();      gltranslatef(0, 0f, -3f);      glrotatef(rotationx, 1.0f,0.0f,0.0f);      glrotatef(rotationy/4, 0.0f,1.0f,0.0f);      glactivetexture(gl_texture0);     glbindtexture(gl_texture_2d, texture1.gettextureid());      glactivetexture(gl_texture1);     glbindtexture(gl_texture_2d, texture2.gettextureid());      glbegin(gl_triangles);     glmultitexcoord2f(gl_texture0, 0, 0);     glvertex3f(0, 0, 0);      glmultitexcoord2f(gl_texture0, 1, 0);     glvertex3f(1, 0, 0);      glmultitexcoord2f(gl_texture0, 0, 1);     glvertex3f(0, 1, 0);      glmultitexcoord2f(gl_texture1, 1, 1);     glvertex3f(1, 1, 0);      glmultitexcoord2f(gl_texture1, 1, 0);     glvertex3f(1, 0, 0);      glmultitexcoord2f(gl_texture1, 0, 1);     glvertex3f(0, 1, 0);     glend(); }  private void rendergl_2d() {     if (mainmenu) {     gui.setbackground(0);     glloadidentity();     (button button : gui.buttons) {         float posx;         float posy;          if (button.posx == "center") {             posx = display.getwidth()/2-button.width/2;         } else {             posx = integer.parseint(button.posx);         }          if (button.posy == "center") {             posy = display.getheight()/2-button.height/2;         } else {             posy = integer.parseint(button.posy);         }          if(guimousex > posx && guimousey > posy && guimousex < posx + button.width && guimousey < posy + button.height){             button.textureover.bind();             button.mouseover.run();             if (mouse.isbuttondown(0)) {                 button.mousedown.run();             }         } else {             button.texture.bind();         }          float imagewidth = button.texture.getimagewidth();         float texturewidth = button.width/imagewidth;         float imageheight = button.texture.getimageheight();         float textureheight = button.height/imageheight;          glbegin(gl_quads);         gltexcoord2f(0, 0);         glvertex2f(posx, posy);         gltexcoord2f(texturewidth, 0);         glvertex2f(posx + button.width, posy);         gltexcoord2f(texturewidth, textureheight);         glvertex2f(posx + button.width, posy + button.height);         gltexcoord2f(0, textureheight);         glvertex2f(posx, posy + button.height);         glend();     }     } } 

everything draws fine, problem texture colors mixed!

this how textures when run app enter image description here

and these textures theyr normal colors

enter image description here

and

enter image description here

because you're calling "rendergl_3d" inside "rendergl". function draws 2 triangles, 1 texture0 , other texture1.

the 0th stage texcoords triangle in lower left zeros. makes sample top left of green texture, green well. since have blending on, second whitish texture blended on top, making green.

to test this, try making top left pixel of white texture magenta or other distinguishable color. lower-left triangle should dirty magenta (mixed in green due filtering).

as solution, need disable multi-texturing blending (not gl_blend - blends final color provided rasterizer framebuffer). can

    glactivetexture(gl_texture0);     gltexenvi(gl_texture_2d, gl_texture_env_mode, gl_replace); 

and same texture unit 1

    glactivetexture(gl_texture1);     gltexenvi(gl_texture_2d, gl_texture_env_mode, gl_replace); 

this should work.


Comments

Popular posts from this blog

c# - SVN Error : "svnadmin: E205000: Too many arguments" -

c# - Copy ObservableCollection to another ObservableCollection -

All overlapping substrings matching a java regex -