Jump to content
  • Advertisement
Sign in to follow this  
guin

1D texture in GLSL

This topic is 4562 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm having problems getting a 1D texture to work in GLSL, though it might be the app code at fault because this is the first time I have used 1D textures. Take a look:

[main.cpp]

bool loadTextures ()
{

...

	glBindTexture (GL_TEXTURE_1D, spect);
	glTexImage1D	(GL_TEXTURE_1D, 0, 3, spectData->sizeX, 0, GL_RGB, GL_UNSIGNED_BYTE, spectData);
	glTexParameteri (GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
	glTexParameteri (GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
	glTexParameteri (GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_CLAMP);
	glTexParameteri (GL_TEXTURE_1D, GL_TEXTURE_WRAP_T, GL_CLAMP);

...

};

void shaders ()
{
	spectLoc = glGetUniformLocation (p, "spect");
	glUniform1i (spectLoc, spect);
};

[shader.fs]

uniform sampler1D spect;
uniform int time;

void main ()
{
	float f = (sin ((float) time / 100) / 2.0) + 0.5;
	vec4 color = texture1D (spect, f);
	gl_FragColor = vec4 (color.r, color.g, color.b, (color.r + color.g + color.b) / 3.0);
}

I hope I have shown enough information. The aim is to make a flashing rainbow effect, but the fragment shader is failing on the above code (result: white polygons). The texture file itself is a 256x1 24-bit bitmap. Now it does work correctly if I load the texture as 2D, using the following code (but I want to avoid loading it as 2D here):

[main.cpp]

bool loadTextures ()
{

...

glBindTexture (GL_TEXTURE_2D, spect);
glTexImage2D (GL_TEXTURE_2D, 0, 3, spectData->sizeX, spectData->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, spectData->data);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);

...

};

void shaders ()
{
	spectLoc = glGetUniformLocation (p, "spect");
	glUniform1i (spectLoc, spect);
};

[shader.fs]

uniform sampler2D spect;
uniform int time;

void main ()
{
	float f = (sin ((float) time / 100) / 2.0) + 0.5;
	vec4 color = texture2D (spect, vec2 (f, 0.0));
	gl_FragColor = vec4 (color.r, color.g, color.b, (color.r + color.g + color.b) / 3.0);
}

Share this post


Link to post
Share on other sites
Advertisement
Your code listed below should be this


float f = (sin (float(time) / 100) / 2.0) + 0.5;


you can't do C style casts in GLSL you need to do C++ casts.

Share this post


Link to post
Share on other sites
That wasn't the issue but thanks for the heads up. But I just noticed a typo in the above code that did completely fix it so nvm.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!