Texture sampling for Shaders

Started by
13 comments, last by Vilem Otte 13 years, 1 month ago
Here we go, the following is trivial:


AABB.render(gl2);

gl2.glUseProgram(programId);

elevationMap.getGLTextureObject().activate(gl2, GL2.GL_TEXTURE0);
elevationMap.getGLTextureObject().bind(gl2);

normalMap.getGLTextureObject().activate(gl2, GL2.GL_TEXTURE1);
normalMap.getGLTextureObject().bind(gl2);

int step = layerArray[level].step;

int location = gl2.glGetUniformLocation(programId, "gridScale");
gl2.glUniform2f(location, step, step);

location = gl2.glGetUniformLocation(programId, "gridOrigin");
gl2.glUniform2f(location, originX, originY);

patchVBO.bind(gl2);
patchIBO.bind(gl2);

patchVBO.glEnableClientState(gl2);
patchIBO.glDrawElements(gl2, GL2.GL_TRIANGLE_STRIP);
patchVBO.glDisableClientState(gl2);

patchVBO.unbind(gl2);
patchIBO.unbind(gl2);

elevationMap.getGLTextureObject().unbind(gl2);
normalMap.getGLTextureObject().unbind(gl2);

gl2.glUseProgram(0);


So that's the rendering code, don't be confused by OOP approach, these are my own shell-classes for Textures, VBO's and IBO's - I thnik it's easy to understand what is happening inside. For instance, unbind - is just a call of glBindTexture(0). I'm saying that so you won't assume that the problem is inside my classes since all this was tested lots of times and worked fine until this shader appeared.


uniform sampler2DRect elevationMap;
uniform sampler2DRect normalMap;

uniform vec2 gridScale;
uniform vec2 gridOrigin;

void main()
{
vec2 gridPosition = gridOrigin + gl_Vertex.xy * gridScale;
float elevation = texture2DRect(elevationMap,gridPosition).b;

gl_Position = gl_ModelViewProjectionMatrix * vec4(gridPosition.x,elevation * 255.0,gridPosition.y,1.0);

gl_Color = vec4(elevation,elevation,elevation,1.0);

gl_FrontColor = gl_Color;
}


Now the problem: first of all - text with FPS and etc has disappeared. And the second - texturing in other parts of application works wrong. Consider the following screenshot:

07446555498996711106.png

This is how it works when rendering function (which is posted above with shader) is commented. And the code is simple:


gl2.glEnable(GL.GL_TEXTURE_2D);

glTextureObject.bind(gl2);

gl2.glBegin(GL.GL_TRIANGLE_FAN);

gl2.glTexCoord2f(0.0f, 0.0f);
gl2.glVertex3f(-20, -20, 0);
gl2.glTexCoord2f(1.0f, 0.0f);
gl2.glVertex3f(+20, -20, 0);
gl2.glTexCoord2f(0.5f, 1.0f);
gl2.glVertex3f(0, 20, 0);

gl2.glEnd();

glTextureObject.unbind(gl2);

gl2.glDisable(GL.GL_TEXTURE_2D);


But when I uncomment the rendering function with shader (and in code rendering function with shader is located before rendering this single triangle with texture) - I see this:

10097654444128787054.png

As you can see texture is gone somehow, as well as text... I'm completely stuck here, could you assist me? :)
Advertisement
gl_Color is an attribute given to you by GLSL
dont change internal variables
try gl_FrontColor = vec4() etc and see if that fixes the problem
Nice catch there, but it didn't help :(
Indeed, gl_Color is read only. It stores the current material color. This shader should fail to compile, do you check for errors/retrieve the info log?

EDIT:
And are you sure your graphics card supports vertex shader texture fetches?
Yep, no errors! :) How can that be? But in any case, I've already removed the snippet with gl_Color - the problem is still the same - no text, no texturing in another parts of application

EDIT: I believe I don't have to check it with Radeon HD 5870... Anyway - this shader creates landscape - and it works - coz everything is fine with landscape - you can notice a part of it on the second screenshot (when rendering function with shader is ON). The problem occures with other parts of application (where shader program is already OFF) - like if some OpenGL resources were not releasesd properly or some states haven't been switched or whatever... That's why I'm asking - I'm completely confused!
Then use shaders to render the text.
Sig: http://glhlib.sourceforge.net
an open source GLU replacement library. Much more modern than GLU.
float matrix[16], inverse_matrix[16];
glhLoadIdentityf2(matrix);
glhTranslatef2(matrix, 0.0, 0.0, 5.0);
glhRotateAboutXf2(matrix, angleInRadians);
glhScalef2(matrix, 1.0, 1.0, -1.0);
glhQuickInvertMatrixf2(matrix, inverse_matrix);
glUniformMatrix4fv(uniformLocation1, 1, FALSE, matrix);
glUniformMatrix4fv(uniformLocation2, 1, FALSE, inverse_matrix);

Then use shaders to render the text.


Ok, forget about the text - it's not worth it. But what am I going to do with texturing in another parts of application, as I've showed on the screenshot texturing stops working after using this shader, how can that be?

[quote name='V-man' timestamp='1299769438' post='4784011']
Then use shaders to render the text.


Ok, forget about the text - it's not worth it. But what am I going to do with texturing in another parts of application, as I've showed on the screenshot texturing stops working after using this shader, how can that be?
[/quote]

I don't know.
Either it is a bug in your code or a bug in the driver.

Are you sure it is not a problem with your code? Have you tested on other machines? Are you called glGetError() to make sure no errors are generated?

Have you considered using shaders to render everything?
Sig: http://glhlib.sourceforge.net
an open source GLU replacement library. Much more modern than GLU.
float matrix[16], inverse_matrix[16];
glhLoadIdentityf2(matrix);
glhTranslatef2(matrix, 0.0, 0.0, 5.0);
glhRotateAboutXf2(matrix, angleInRadians);
glhScalef2(matrix, 1.0, 1.0, -1.0);
glhQuickInvertMatrixf2(matrix, inverse_matrix);
glUniformMatrix4fv(uniformLocation1, 1, FALSE, matrix);
glUniformMatrix4fv(uniformLocation2, 1, FALSE, inverse_matrix);
When rendering the single triangle, are you using the same normal map texture as for your terrain? Because that appears to be a rectangle texture, which means, IIRC, that the texture coordinates do not go from 0.0 to 1.0, but from 0.0 to the size of the texture (so you're currently just rendering one pixel of texture across that tri).

IIRC, that the texture coordinates do not go from 0.0 to 1.0, but from 0.0 to the size of the texture (so you're currently just rendering one pixel of texture across that tri).


That's not correct, texcoords are always from 0 to 1 regardless of the dimensions of the texture. You can use special commands to sample rectangle textures in the shader by pixel index, but for regular sampling and fixed function texcoords are always from 0 to 1.
[size=2]My Projects:
[size=2]Portfolio Map for Android - Free Visual Portfolio Tracker
[size=2]Electron Flux for Android - Free Puzzle/Logic Game

This topic is closed to new replies.

Advertisement