[Solved] Simple GLSL Texture Troubleshoot

Started by
3 comments, last by notyourbuddy 11 years, 6 months ago
Just starting out with OpenGL so this is a very simple problem,

I'm trying to load a 64x64 24-bit BMP (all pixel values are set to 235 [a light grey tone]) and access it in a fragment shader, however, all I seem to get are black pixels and not particularly sure why. Will keep investigating, but figured I'd ask the experienced if they know where I've gone wrong.

My basic program flow is as follows:
[source lang="cpp"]GLuint t;
glGenTextures(1, &t);
glBindTexture(GL_TEXTURE_2D, t);

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 64, 64, 0, GL_RGB, GL_UNSIGNED_BYTE, data);

glLinkProgram(obj);

glUseProgram(obj);

glActiveTexture(GL_TEXTURE0);

glBindTexture(GL_TEXTURE_2D, t);

GLint loc = glGetUniformLocation(obj, "_heightfield");
glUniform1i(loc, 0);

Render()
[/source]
My fragment shader:

[source lang="cpp"]#version 400

uniform sampler2D _heightfield;

layout(location = 0) out vec4 _colorOut;

void main(void)
{
vec4 h = texture(_heightfield, vec2(0.5, 0.5)); // trying to grab the center texel

_colorOut = vec4(h[0]/255.0, h[1]/255.0, h[2]/255.0, 1.0);

//_colorOut = vec4(235.0/255.0, 235.0/255.0, 235.0/255.0, 1.0);
}[/source]
The image data in the "data" array is being loaded correctly. The shader program has linked successfully and the fragment shader produces the correct results if I hardcode in the color values, however, when I try to pull texels out of the texture as I mentioned the result is just black pixels as opposed to the 235 grayscale tone from my texture.

Did I forget a step in initializing my texture? Am I incorrectly accessing texels in my fragment shader?
Advertisement
You can set _colorOut directly to the results of the texture call. An rgb texture made of unsigned bytes is a normalized format and will return 0.0-1.0 in the shader.

New C/C++ Build Tool 'Stir' (doesn't just generate Makefiles, it does the build): https://github.com/space222/stir

Did I forget a step in initializing my texture? Am I incorrectly accessing texels in my fragment shader?[/quote]http://www.opengl.org/wiki/Common_Mistakes#Creating_a_complete_texture

[size="1"]And a Unix user said rm -rf *.* and all was null and void...|There's no place like 127.0.0.1|The Application "Programmer" has unexpectedly quit. An error of type A.M. has occurred.
[size="2"]


You can set _colorOut directly to the results of the texture call. An rgb texture made of unsigned bytes is a normalized format and will return 0.0-1.0 in the shader.


Ah cool. Did not know that. Good to know.

Did I forget a step in initializing my texture? Am I incorrectly accessing texels in my fragment shader?
http://www.opengl.or...omplete_texture
[/quote]

Aye that did it. <facepalm>

laugh.png

This topic is closed to new replies.

Advertisement