PDA

View Full Version : Multitexturing does not works



Lator
07-01-2008, 12:21 AM
Hello.
I use multitexturing as described in tutorials:
At first, I declare function's pointers as globals:

PFNGLACTIVETEXTUREARBPROC glActiveTextureARB;
PFNGLMULTITEXCOORD2FARBPROC glMultiTexCoord2f;
Then, I initialize them:

glActiveTextureARB = (PFNGLACTIVETEXTUREARBPROC)wglGetProcAddress ("glActiveTextureARB");
glMultiTexCoord2f = (PFNGLMULTITEXCOORD2FARBPROC) wglGetProcAddress("glMultiTexCoord2fARB");
Loading texture as GL_TEXTURE0_ARB

glGenTextures(1, &unTexture[0]);
glBindTexture(GL_TEXTURE_2D, unTexture[0]);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, imageWidth, imageHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, ilGetData());

glActiveTextureARB(GL_TEXTURE0_ARB);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, unTexture[0]);
And mapping texture to polygon:

glActiveTextureARB(GL_TEXTURE0_ARB);
glBindTexture(GL_TEXTURE_2D, unTexture[0]);
glEnable(GL_TEXTURE_2D);
glBegin(GL_QUADS);
glMultiTexCoord2f(GL_TEXTURE0_ARB, 1, 1);
glTexCoord2f(1, 1);
glVertex3d(1, 1, 1);
glMultiTexCoord2f(GL_TEXTURE0_ARB, 1, 0);
glTexCoord2f(1, 0);
glVertex3d(1, 0, 1);
glMultiTexCoord2f(GL_TEXTURE0_ARB, 0, 0);
glTexCoord2f(0, 0);
glVertex3d(-1, 0, 1);
glMultiTexCoord2f(GL_TEXTURE0_ARB, 0, 1);
glTexCoord2f(0, 1);
glVertex3d(-1, 1, 1);
glEnd();
glDisable(GL_TEXTURE_2D);
I checked up returned by wglGetProcAddress() - they are not NULL.
But texture mapping does not works at all. But if replace "unTexture[0] by "0" (which is the default texture) it works, but, of course, only for one texture. It looks like my video card does not support multitexturing (but it does).
Tell me, please, what can be wrong?

dletozeun
07-01-2008, 01:17 AM
Maybe I am wrong, but I did think it is possible to use glMultiTexCoord* and glTexCoord* at the same time. If I wanted to map 2 textures with 2 texture units 0 and 1.
Actually, what is the matter glTexCoord* calls in your code?

You should:
- active TU0
-- bind texture
-- enable 2D texturing
- active TU1
-- bind texture
-- enable 2D texturing

- set texture coordinates for TU0 (glMultiTexCoord)
- set texture coordinates for TU1 (glMultiTexCoord)
-- draw polygons

- active TU0
-- disable 2D texturing
- active TU1
-- disable 2D texturing

Lator
07-01-2008, 02:39 AM
I do so:

void LoadTexture(char *imageName)
{
ilLoadImage(imageName);
int imageWidth = ilGetInteger(IL_IMAGE_WIDTH);
int imageHeight = ilGetInteger(IL_IMAGE_HEIGHT);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, imageWidth, imageHeight, 0, GL_BGR, GL_UNSIGNED_BYTE, ilGetData());
ilDeleteImage(0);
}
Loading texture:

LoadTexture("CatEye.bmp");
glActiveTextureARB(GL_TEXTURE0_ARB);
glBindTexture(GL_TEXTURE_2D, 0);
glEnable(GL_TEXTURE_2D);
And mapping texture:

glEnable(GL_TEXTURE_2D);
glBegin(GL_QUADS);
glMultiTexCoord2f(GL_TEXTURE0_ARB, 1, 1);
glVertex3d(1, 1, 1);
glMultiTexCoord2f(GL_TEXTURE0_ARB, 1, 0);
glVertex3d(1, 0, 1);
glMultiTexCoord2f(GL_TEXTURE0_ARB, 0, 0);
glVertex3d(-1, 0, 1);
glMultiTexCoord2f(GL_TEXTURE0_ARB, 0, 1);
glVertex3d(-1, 1, 1);
glEnd();
glDisable(GL_TEXTURE_2D);
It works only for GL_TEXTURE0_ARB. If replace it by some other value in loading and mapping, it will not work. And program always tries to use last loaded texture and it does not make a difference wich GL_TEXTUREX_ARB I use.
What's wrong?
And it would be perfect to look at a working example.

dletozeun
07-01-2008, 03:48 AM
If I assume you want to apply 2 textures on a polygon, what do you do with these textures? Do you blend, add, ... them?
You can configure this with:
glTexEnv (http://www.opengl.org/sdk/docs/man/xhtml/glTexEnv.xml)

Or use a shader.

Lator
07-01-2008, 04:00 AM
At least, I want to map textures to a difference polygons which must be rendered in the scene at the same moment. My scene requires 3 textures and without multitexturing I must to load them every 1/45 seconds - that's what I do now. But my video card supports 8 simultaneous 2048:2048 textures, what's would be more preferably to use multitexturing. But I can't!
What's the problem? (Code is the same as above).

Dark Photon
07-01-2008, 05:18 AM
What's wrong?
Post a small, compilable test program, and I bet someone will have time to take a look at it.


And it would be perfect to look at a working example.
The OpenGL red book (http://www.opengl.org/documentation/red_book/) probably has some. They're all over the net as well:

* The ARB_multitexture extension (http://www.berkelium.com/OpenGL/GDC99/multitexture.html)
* NeHe - Lesson 22 (http://nehe.gamedev.net/data/lessons/lesson.asp?lesson=22)
* Clockwork Coders: Tutorial 8 (http://www.clockworkcoders.com/oglsl/tutorial8.htm)
* Tutorial 22: Multitexturing (http://jerome.jouvie.free.fr/OpenGl/Tutorials/Tutorial22.php)

Just google for OpenGL multitexturing.

Multitexturing isn't that different from single texturing. You set up and bind your textures to texture units in the same way. You enable the right texture units, set how you want them to "combine" with each other, and render.

However, your last post suggests you don't really want multitexturing (multiple textures applied to one polygon), but rather would just like to try and minimize texture binds by binding, say, the next 8, and then just render a bunch of polys, each of which uses only 1 of the 8 textures. Is this what you're trying to do? If so, you need shaders.

Again, a small compilable test program would do wonders for clarity and understanding. Sample glGetError() (http://www.opengl.org/sdk/docs/man/xhtml/glGetError.xml) after each call and make sure you've got GL_NO_ERROR. Then post it if you don't figure it out.

Zengar
07-01-2008, 06:16 AM
glBindTexture(GL_TEXTURE_2D, 0)

So you unbind the texture here? Or is it a typo?

I don't really know much about multitexturing or ARB combiners, as I never learned them (who would, if you have shaders), but AFAIK the multitexture is applied in layers. That is, you combine the results from texture 0 with texture 1, texture 2 etc. If you just enable texture 1 without enabling the 0, it probably won't work (speculation!). Try using two textures.

Lator
07-01-2008, 06:33 AM
What's the shaders in a short words? Can it serve as equivalent of multitexturing?

Zengar
07-01-2008, 06:53 AM
Shaders are small programs that run on GPU and allow you do all sorts of things, for example, combine as much textures as you want in a way you want :)

Lator
07-01-2008, 06:56 AM
Mmm... Ok, anyway, is it preferably to use multitexturing or "Shaders" can substitute it?

Ilian Dinev
07-01-2008, 07:10 AM
shaders all the way.
The driver "emulates" multitexturing by composing a shader :)

Zengar
07-01-2008, 07:12 AM
The classical multitexturing became obsolete years ago already. As Illian says, moders hardware is very flexible (=programmable), so the classic old stuff liek vertex lighting, multitexture, fog usw. is all emulated using driver-own GPU programms (=shaders)

Lator
07-01-2008, 07:20 AM
Does it mean that it is possibly to use "shaders" and forget about so functions as "glActiveTextureARB"?

dletozeun
07-01-2008, 07:34 AM
No, you still need to use texture units to bind more than one texture at once and you still need to specify texture coordinates per unit (if they are different).
But with a shader, you don't have to enable/disable 2D texturing for each unit, you simply bind one texture to the TU you want.

In the shader code, you can very simply do all kind of texture blending or other operations per fragment.

For more information about glsl shaders read this good tutorial:

glsl lighthouse 3D tutorial (http://www.lighthouse3d.com/opengl/glsl/)

Then you will love shaders. :)

kaerimasu
07-02-2008, 07:04 AM
Just draw polygon A with texture A bound. Then draw polygon B with texture B bound. I don't think you need multitexturing or shaders to do as you describe.

- Chris

Lator
07-02-2008, 07:10 AM
But to draw a textured polygon it's need to have the loaded texture. Problem is that loading texture works only if to load that as defalut texture (0). If I'll try to load texture as glBindTexture(, not zero); it will not map :(.

Dark Photon
07-02-2008, 11:12 AM
If I'll try to load texture as glBindTexture(, not zero); it will not map :(.
Again, post a short test program.

And as everyone suggested, unless you have to use multitexturing, consider shaders. They're much more flexible. OTOH, if you just want to multiply two textures in the fragment shader with your interpolated lighting color (or something equally simple and supported by multitexturing), you're gonna get a coded solution faster with multitexturing.

Post your short test program and we'll fix you up.

kaerimasu
07-03-2008, 08:54 AM
You can create multiple texture objects and only use one texture unit. No need to call glActiveTexture(). Using multiple textures is different from multitexturing.



GLuint tex_ids[2];
glGenTextures(2, tex_ids);
// setup textures

glBindTexture(GL_TEXTURE_2D, tex_ids[0]);
// draw first polygon

glBindTexture(GL_TEXTURE_2D, tex_ids[1]);
// draw second polygon


I didn't gather from your problem description that you were trying to combine two textures on one polygon, but just that you had separate textures for separate polygons.

- Chris