Jump to content
  • Advertisement
Sign in to follow this  
jocasa

Vertex from texture colors

This topic is 2638 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

Can you shortly describe how can one with GLSL assign the three components RGB of the color of each point in a texture to the coordinates XYZ of some vertex? There should be as much vertex as points in the texture.

My guess is to pass the texture to the vertex shader and there perform texture2D (tex, gl_TexCoord[0].st) to read the color values. Then just treat the resulting vector as the position. But I don't really know how to make this, or how to assign the thus obtained position to a vertex when rendering.

Any idea?

Share this post


Link to post
Share on other sites
Advertisement
The vertex shader would look something like this

attribute vec2 inTexCoord;
uniform sampler2D Texture0;
uniform mat4 ProjectionModelviewMatrix;
varying vec2 TexCoord;
void main()
{
vec4 texel, newVertex;
//Read the texture, mipmap = 0
texel = texture2DLod(Texture0, inTexCoord, 0.0);
newVertex = texel.xyz;
newVertex.w = 1.0;
gl_Position = ProjectionModelviewMatrix * newVertex;
TexCoord = inTexCoord;
}


http://www.opengl.or...x_Texture_Fetch

Share this post


Link to post
Share on other sites
Thanks for the info.

It makes some sense in my untrained mind. What I don't get yet is how to render the vertex given the shader above. Do I have to set up vertex arrays? I have only seen this done with prefixed vertex coordinates, but not the other way round.

Would you explain this to me, please?

Share this post


Link to post
Share on other sites
You can use whatever means you want to send your vertex because sending a vertex is not really related to the execution of the shader.

But people tend to use generic vertex attributes these days : glVertexAttribPointer
and they store their index/vertex/normal/texcoord/whatever in VBO.

Share this post


Link to post
Share on other sites
There is no need for attributes actually, for what you want to achieve. You could use built-in attribute gl_VertexID for calculating the position in the texture. The only constraint is: it can be executed only on NVIDIA hardware. I don't possess any AMD card, but according to other peoples claim, it is not supported in AMD drivers (at least it was not being supported; maybe the latest drivers change that).

Share this post


Link to post
Share on other sites
Thanks for the info.

In the end the shaders and the program run OK, as follows:


// Vertex Shader

attribute vec2 in_tex_coord;
uniform sampler2D tex;
varying vec2 tex_coord;

void main()
{
vec4 texel = texture2DLod (tex, in_tex_coord, 0.0);

vec4 new_vertex = vec4 (texel.xyz, 1.0);

gl_Position = gl_ModelViewProjectionMatrix*2.0*(new_vertex-0.5);

tex_coord = in_tex_coord;
}


// Fragment shader

uniform sampler2D tex;
varying vec2 tex_coord;

void main()
{
gl_FragColor = texture2D (tex, tex_coord);
}


And in the rendering function:


glUseProgram (program);

glActiveTexture (GL_TEXTURE0);
glBindTexture (GL_TEXTURE_2D, texture);

glBegin (GL_POINTS);
for (int i=0; i<tex_width; i++)
{
float x = float(i)/float(tex_width-1);
for (int j=0; j<tex_height; j++)
{
float y = float(j)/float(tex_height-1);
glVertexAttrib2f (loc_in_tex_coord, x, y);
}
}
glEnd();

glBindTexture (GL_TEXTURE_2D, 0);


With this, a collection of colored points is rendered that maps somehow the color contents of the texture.

Bye!

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!