4

I am trying to make a multi-textured point sprite for an iphone application using OpenGL ES 2.0. I can't find any examples of this on web, and it doesn't seem to be working. Is there some built-in limitation where gl_PointCoord can't be used on multiple textures when using GL_POINTS mode for point sprites?

uniform sampler2D tex;    
uniform sampler2D blur_tex;
vec4 texPixel = texture2D( tex, gl_PointCoord ); 
vec4 blurPixel = texture2D( blur_tex, gl_PointCoord );

I'm sure I am passing in the textures properly, as I can do multi-texturing just fine in TRIANGLE_STRIP mode, but I am hoping to speed things up using point sprites.

If it is possible, a link to an example of working code would super helpful. Thanks!

EDIT:

Here's how I'm passing in the textures to my shader. This lets me do multi-texturing when I am in TRIANGLE or TRIANGLE_STRIP mode.

//pass in position and tex_coord attributes...

//normal tex
glActiveTexture(0);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, tex0);
glUniform1i(SAMPLER_0_UNIFORM, 0);

//blur tex
glActiveTexture(1);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, tex1);
glUniform1i(SAMPLER_1_UNIFORM, 1);
//draw arrays...

However if I am using POINTS mode then I never see the second texture. That is, referring to the shader code above, whether I do

gl_FragColor = texPixel;

OR

gl_FragColor = blurPixel;

I see the same texture. Which seems strange. My guess is that you CAN'T do multi-texturing on a point sprite and somehow having two active textures or two calls to gl_PointCoord causes a problem. But I'm hoping I'm wrong. So if someone has a simple example of multi-texturing working with point sprites in OpenGL ES 2.0 I would be happy to look at that code!

EDIT 2:

vertex shader:

attribute vec4 position;

void main() {
  gl_PointSize = 15.0;   
  gl_Position = position;
}

fragment shader:

precision mediump float; 

uniform sampler2D tex;    
uniform sampler2D blur_tex;

void main() {
  vec4 texPixel = texture2D( tex, gl_PointCoord ); 
  vec4 blurPixel = texture2D( blur_tex, gl_PointCoord );

  //these both do the same thing even though I am passing in two different textures?!?!?!?

  //gl_FragColor = texPixel;
  gl_FragColor = blurPixel;
}
Angus Forbes
  • 982
  • 1
  • 9
  • 21

3 Answers3

3

There is a typo in your main program.

The right parameter to pass to glActiveTexture is GL_TEXTURE0, GL_TEXTURE1, ...

Note that GL_TEXTURE0, GL_TEXTURE1 does not have a value of 0,1 etc.

Since you are passing an invalid value to glActiveTexture, the function will fail and so the active texture will always be a default (probably 0) all your changes are going to texture at 0 position.

1

source In my case there is a blending for points

The possible problem was in nonexistent parameters

glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE );
glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE );
Yuriy Vikulov
  • 2,469
  • 5
  • 25
  • 32
  • yes I have... I am getting ONE of the textures at the right size (as defined by the gl_PointSize in the vertex shader), but the other one is not appearing. Again, this is working with triangles (i can pass in and blend two separate textures), just not with point sprites. – Angus Forbes Apr 26 '11 at 07:11
  • Make gl_FragColor=vec4(1,0,0,1) to make sure if it is in right place. If you'll see red quads then there is a problem in blending, if you'll NOT see red quads, then there is a problem in position or params – Yuriy Vikulov Apr 26 '11 at 08:48
  • but yes, The position and tex_coords are correct and I do see a red rectangle when I set the frag color to red. – Angus Forbes Apr 27 '11 at 09:28
  • hi thanks, can you try it with point sprites and confirm that it works? I think the issue might be with calling gl_PointCoord twice, which only makes sense in GL_POINTS mode (not GL_TRIANGLES). – Angus Forbes Apr 27 '11 at 09:54
  • yes, the code works in TRIANGLES mode, but the whole point is that I can't get it to work with POINT SPRITES! – Angus Forbes Apr 27 '11 at 09:56
  • we can avoid twice calling by declaring vec2 temp_tex=gl_PointCoord; – Yuriy Vikulov Apr 27 '11 at 09:56
  • Have you modified your fragment shader? – Yuriy Vikulov Apr 27 '11 at 10:07
  • well, the best idea i have now is to make the test project myself tonight... i'll tell you result if you're interested – Yuriy Vikulov Apr 27 '11 at 10:24
  • Hm i tryed to do this yesterday, but no results. I event can't do multitexturing with quad, that's a pitty. There is an example of multitexturing for triangles, but can you make changes? Or you may wait for tonight, i cant to test it right now: http://code.google.com/p/opengles-book-samples/source/browse/trunk/Android/Ch10_MultiTexture/src/com/openglesbook/multitexture/MultiTextureRenderer.java – Yuriy Vikulov Apr 28 '11 at 02:01
  • the current texture depends of glTextureBind. I've never seen two textures at one time – Yuriy Vikulov Apr 28 '11 at 02:19
  • Hi Yuriy, thanks for taking the time to look into this. I looked at your source code and your code looks exactly the same as mine, except that you are also passing in vertex coords to your vertex and then to your fragment via a varying (and which you aren't actually using in your fragment shader). If you are saying that this works for you on your Android code, then I am going to assume it is an issue with the ES 2.0 implementation on the iphone. I'd appreciate it if anyone is able reproduce or solve this problem for the iphone (specifically I'm using the iOS simulator via XCode4). – Angus Forbes Apr 28 '11 at 15:28
  • I'm an iPhone person; I'll take Yuriy's stuff and have a go at a quick port over the weekend. I have both the simulator and real hardware to test on. Anyway, +1 to Yuriy for the amount of work put in already. – Tommy Apr 28 '11 at 15:56
  • The base source is an example for "opengl-es 2.0 programming guide", i just adapted for this task. You may look at iPhone projects http://code.google.com/p/opengles-book-samples/source/browse/trunk/#trunk%2FiPhone%2FChapter_10%2FMultiTexture – Yuriy Vikulov Apr 29 '11 at 02:04
  • but be aware of texture loading. it is quite complicated and very slow. In android example my texture loading is much faster than in example – Yuriy Vikulov Apr 29 '11 at 02:13
  • you may see the picture of blending http://vikulov.blogspot.com/2011/05/opengl-es-20-android-multitexturing.html – Yuriy Vikulov May 02 '11 at 07:08
  • have you tried an example? http://code.google.com/p/opengles-book-samples/source/browse/trunk/#trunk%2FiPhone%2FChapter_10%2FMultiTexture – Yuriy Vikulov May 05 '11 at 04:47
  • that example for TRIANGLE but it is simple to change shaders. Give it a try – Yuriy Vikulov May 05 '11 at 04:48
  • Hi, it looks like my problems were due to a stupid error I made. The example at http://code.google.com/p/opengles-book-samples/source/browse/trunk/#trunk%2FiPhone%2FChapter_10%2FMultiTexture does indeed work. Thanks again Yuriy for helping me through this. – Angus Forbes May 14 '11 at 00:46
1

I think may be too late to post this though.

There are two problems in your code. One is the one that Satyakam has pointed out. The other problem is that you should NOT use glUniform1f. Right one is glUniform1i. The deference is f or i on the tail which means float or integer.