Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Texture mixing using texture_env_combine



So, I've got a terrain engine which uses 3 textures, a repeating surface texture, repeating detail texture, and a 1-to-1 color/shadow map texture. It works nicely and looks good.

I want to add to it a fourth repeating texture which is applied selectively based on some function, say, one minus the z-component of the surface normal or the vertex height. It would be, for example, a rocky texture applied on hillsides but not on flat surfaces. Pretty ordinary stuff.

My plan, as it stands, is to use the alpha channel of the color map as the filter against which to apply the fourth texture. This way I could use four textures only.

Of course, I know that not a lot of cards support 4 texture units and I'll implement a fallback for the GF4MX and so on. But for cards that support it, I'd like to do it.

Anyway, I wrote a simple GLUT app which draws a single textured quad and lets me fool around and try to get this working.

The testbed app fills the quad with a repeating "O" texture across it and applies a repeating "X" texture where the alpha channel of the colormap is non-zero, using GL_INTERPOLATE. This much works GREAT.

It's applying the colormap that's throwing me for a loop. And I think it comes down to me simply not grokking the texture_env_combine functionality, though I've read the SGI doc and several resources on the web.

My colormap/mixmap is as such
RGB: a gaudy swirly set of colors for easy examination
ALPHA: black, fading circularly to white in the center

Here's my display function:

void display(void)
{
	glClearColor (0.5,0.5,0.5,1);
	glClearDepth( 0 );
	glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );

	glDisable( GL_LIGHTING );
	glShadeModel( GL_SMOOTH );
	
	int cx = _width / 2;
	int cy = _height / 2;
	int size = (int)((_width * 3.0) / 4.0);
	int hSize = size / 2;
	float tex0Scale = 12.0f;
	float tex1Scale = 12.0f;
	
	/*
		Bind textures
	*/

	glActiveTextureARB( GL_TEXTURE0_ARB );
	glEnable( GL_TEXTURE_2D );
	glBindTexture( GL_TEXTURE_2D, _texture0 );

	glActiveTextureARB( GL_TEXTURE1_ARB );
	glEnable( GL_TEXTURE_2D );
	glBindTexture( GL_TEXTURE_2D, _texture1 );

	glActiveTextureARB( GL_TEXTURE2_ARB );
	glEnable( GL_TEXTURE_2D );
	glBindTexture( GL_TEXTURE_2D, _mixmap );

	glActiveTextureARB( GL_TEXTURE3_ARB );
	glEnable( GL_TEXTURE_2D );
	glBindTexture( GL_TEXTURE_2D, _mixmap );

	/*
		Set up mixmap blending, use alpha channel of TEXTURE2
		as interpolative factor between TEXTURE0 and TEXTURE1
	*/
	glActiveTextureARB( GL_TEXTURE2_ARB );
	glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE_ARB );

	glTexEnvi( GL_TEXTURE_ENV, GL_SOURCE0_RGB_ARB, GL_TEXTURE0_ARB );
	glTexEnvi( GL_TEXTURE_ENV, GL_OPERAND0_RGB_ARB, GL_SRC_COLOR );
	glTexEnvi( GL_TEXTURE_ENV, GL_SOURCE1_RGB_ARB, GL_TEXTURE1_ARB );
	glTexEnvi( GL_TEXTURE_ENV, GL_OPERAND1_RGB_ARB, GL_SRC_COLOR );
	glTexEnvi( GL_TEXTURE_ENV, GL_SOURCE2_RGB_ARB, GL_TEXTURE2_ARB );
	glTexEnvi( GL_TEXTURE_ENV, GL_OPERAND2_ALPHA_ARB, GL_SRC_ALPHA );

	glTexEnvi( GL_TEXTURE_ENV, GL_COMBINE_RGB_ARB, GL_INTERPOLATE_ARB );

/*
Set up colormap blending
*/
glActiveTextureARB( GL_TEXTURE3_ARB );

glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE_ARB );
glTexEnvi( GL_TEXTURE_ENV, GL_SOURCE0_RGB_ARB, GL_TEXTURE3_ARB );
glTexEnvi( GL_TEXTURE_ENV, GL_OPERAND0_RGB_ARB, GL_SRC_COLOR );
glTexEnvi( GL_TEXTURE_ENV, GL_COMBINE_RGB_ARB, GL_ONE_MINUS_SRC_COLOR );


	//why doesn't this give me a constant alpha?
	glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE_ALPHA_ARB );
	glTexEnvi( GL_TEXTURE_ENV, GL_SOURCE0_ALPHA_ARB, GL_CONSTANT_ARB );
	glTexEnvi( GL_TEXTURE_ENV, GL_OPERAND0_ALPHA_ARB, GL_ONE );	
	glTexEnvi( GL_TEXTURE_ENV, GL_COMBINE_ALPHA_ARB, GL_REPLACE );

	
	glPushMatrix();
	glTranslatef( cx, cy, 0 );
	
	glColor4f( 1, 1, 1, 1 );
	glBegin( GL_QUADS );
	
		glMultiTexCoord2fARB( GL_TEXTURE0_ARB, -tex0Scale, -tex0Scale );
		glMultiTexCoord2fARB( GL_TEXTURE1_ARB, -tex1Scale, -tex1Scale );
		glMultiTexCoord2fARB( GL_TEXTURE2_ARB, 0, 0 );
		glMultiTexCoord2fARB( GL_TEXTURE3_ARB, 0, 0 );
		glVertex2i( -hSize, -hSize );
		
		glMultiTexCoord2fARB( GL_TEXTURE0_ARB, tex0Scale, -tex0Scale );
		glMultiTexCoord2fARB( GL_TEXTURE1_ARB, tex1Scale, -tex1Scale );
		glMultiTexCoord2fARB( GL_TEXTURE2_ARB, 1, 0 );
		glMultiTexCoord2fARB( GL_TEXTURE3_ARB, 1, 0 );
		glVertex2i( hSize, -hSize );
		
		glMultiTexCoord2fARB( GL_TEXTURE0_ARB, tex0Scale, tex0Scale );
		glMultiTexCoord2fARB( GL_TEXTURE1_ARB, tex1Scale, tex1Scale );
		glMultiTexCoord2fARB( GL_TEXTURE2_ARB, 1, 1 );
		glMultiTexCoord2fARB( GL_TEXTURE3_ARB, 1, 1 );
		glVertex2i( hSize, hSize );
		
		glMultiTexCoord2fARB( GL_TEXTURE0_ARB, -tex0Scale, tex0Scale );
		glMultiTexCoord2fARB( GL_TEXTURE1_ARB, -tex1Scale, tex1Scale );
		glMultiTexCoord2fARB( GL_TEXTURE2_ARB, 0, 1 );
		glMultiTexCoord2fARB( GL_TEXTURE3_ARB, 0, 1 );
		glVertex2i( -hSize, hSize );	
	
	glEnd();
	
	glPopMatrix();

	glutSwapBuffers();
}

And here's a link to the output. On the left side is the display without the colormap applied. On the right is the display *with* the colormap applied. For some reason, the colormap works correctly where the colormap's alpha is non-zero, but it fades to black where the colormap's alpha goes to zero.

Here's the image:
http://home.earthlink.net/~zakariya/files/BooHoo2.png

I have to assume I'm doing something boneheaded. But please, don't just point me to the COMBINE documentation. I've read it a dozen times. I just don't *get* it, I guess. Could somebody clear it up for me? I'm so baffled, and heartbroken.


Shamyl Zakariya
"this is, after all, one of those movies where people spend a great
deal of time looking at things and pointing."
From a review of _Fantastic Voyage_


_______________________________________________
Do not post admin requests to the list. They will be ignored.
Mac-opengl mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/mac-opengl/email@hidden

This email sent to email@hidden


Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.