开发者

opengl multitexturing is wiping out my scene!

I'm trying to implement alpha masking using opengl and multitexturing. I've an alpha image (it's RGB with no data in the alpha channel) and another image (also RGB no alpha) I'd like to mask with the alpha image. I'm using quads in the scene a ground and background quad and then a quad which represents the entity I'm trying to mask.

When I "turn on" the multitexturing code, the ground and background quads are wiped off the screen. I do see the foreground quad with image image and it's masked correctly. When I turn off the multitexturing code, the scene renders as expected, but that removes the foreground object and image (along with multitexturing).

I modified my alpha mask and found out, the multitexturing code is drawing a huge plane (inspite of my uv coordinate calculations and 开发者_如何学JAVAglVertex3f calls) with the original image and the alpha map. I kind of get the impression OpenGL is ignoring my glVertex3f call.

I've had to change GL_SOURCE0_RGB to GL_SOURCE1_RGB b/c if I don't do that I get really strange behavior from the video card (foreground object renders but the mask isn't applied and gets turned into a grey quad on the right side of the screen with a funky projection applied to it). I've tested this on 2 systems and it's the same behavior in both cases.

i'm attempting to use a fixed pipeline multitexturing technique.

GLuint alphaGLTexture; 
SDL_SurfaceToGLTexture(aTexture, &alphaGLTexture);

GLuint fgGLTexture;
SDL_SurfaceToGLTexture(fgTexture, &fgGLTexture);

glActiveTexture(GL_TEXTURE0);
glEnable(GL_TEXTURE_2D);
glBindTexture (GL_TEXTURE_2D, alphaGLTexture);

glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE);
glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE1_RGB, GL_TEXTURE);
glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB, GL_REPLACE);

glActiveTexture(GL_TEXTURE1);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, fgGLTexture); 

glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE);
glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE1_RGB, GL_PREVIOUS);
glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE2_RGB, GL_TEXTURE);
glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND1_RGB, GL_ALPHA);
glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB, GL_MODULATE);

glTranslatef(0.0f, 0.0f, 0.0f);

double u_coord = 0.0f, v_coord = 0.0f;

glBegin(GL_QUADS);

glMultiTexCoord2f(GL_TEXTURE0, u_coord, v_coord);
glMultiTexCoord2f(GL_TEXTURE1, u_coord, v_coord);
glVertex3f(posax, posay, posaz);

u_coord = 1.0f; v_coord = 0.0f;
glMultiTexCoord2f(GL_TEXTURE0, u_coord, v_coord);
glMultiTexCoord2f(GL_TEXTURE1, u_coord, v_coord);
glVertex3f(posbx, posby, posbz);

u_coord = 1.0f; v_coord = 1.0f;
glMultiTexCoord2f(GL_TEXTURE0, u_coord, v_coord);
glMultiTexCoord2f(GL_TEXTURE1, u_coord, v_coord);
glVertex3f(poscx, poscy, poscz);

u_coord = 0.0f; v_coord = 1.0f;
glMultiTexCoord2f(GL_TEXTURE0, u_coord, v_coord);
glMultiTexCoord2f(GL_TEXTURE1, u_coord, v_coord);
glVertex3f(posdx, posdy, posdz);

glEnd();

glLoadIdentity();

The function, SDL_SurfaceToGLTexture, works consistently for the ground and background quads.


You bind the alpha texture to GL_TEXTURE0 and the foreground texture to GL_TEXTURE1. But when drawing you set texture coordinates for GL_TEXTURE1 and GL_TEXTURE2. Is this intended or could it be your problem?

EDIT: Are you sure you have disabled texturing for all units except the ones used when rendering the background quads. And also keep in mind, that the texture environment is per-unit state and not per-texture state, so you also have to restore it when using texturing on the background quads.

Chris's solution is related to this. Just keep in mind that OpenGL is a state machine and when not resetting the active texture unit, all following texture operations apply to GL_TEXTURE1 and when not disabling texturing for the unused units, all following objects (even in the next frame, of course) will be drawn multitextured.


After setting up the texture environment for the second level, try calling "glActiveTexture(GL_TEXTURE0);" again. I've seen much weirdness when I've forgotten to reset the texture level back to GL_TEXTURE0.

Also, scatter some calls to glGetError() after every few OpenGL calls to see if something innocent looking has an error.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜