TExture access problem

i want to create a 2X2 texture . Each pixel keeps the position of a point in 3D space. How can i access the position i kept in the texture in GPU? is the fellowing correct?

void InitTextures()
{
GLuint edge[2][2][4]=
{
1,0,0,1,
0,1,0,1,
0,0,1,1,
0,0,0,1
};

glGenTextures(1,&tex1);
glBindTexture(GL_TEXTURE_2D,tex1);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
//Give it the data
glTexImage2D(GL_TEXTURE_2D,0,GL_RGBA,2,2,0,GL_RGBA,GL_UNSIGNED_BYTE,edge);
}

Draw

glBegin(GL_QUADS);
glVertex3f(1,0,0);
glVertex3f(2,0,0);
glVertex3f(3,0,0);
glVertex3f(4,0,0);
glEnd();

GPU code

uniform float4x4 WorldViewProj : state.matrix.mvp;
void main_v(float4 pos:POSITION,
out float4 opos:POSITION,
out float4 color:COLOR,
uniform sampler2D tex)
{

if(pos.x==1)
{

   pos.z=0;
   pos.x = tex2D( tex , float2(0.0,0.0) ).r*255;
   pos.y = tex2D( tex , float2(0.0,0.0) ).g*255;

}
if(pos.x==2)
{
pos.z=0;
pos.x = tex2D( tex , float2(1.0,0.0) ).r255;
pos.y = tex2D( tex , float2(1.0,0.0) ).g
255;
}
if(pos.x==3)
{
pos.z=0;
pos.x = tex2D( tex , float2(1.0,1.0) ).r255;
pos.y = tex2D( tex , float2(1.0,1.0) ).g
255;
}
if(pos.x==4)
{
pos.z=0;
pos.x = tex2D( tex , float2(0.0,1.0) ).r255;
pos.y = tex2D( tex , float2(0.0,1.0) ).g
255;
}

opos=mul(WorldViewProj,pos);

color=float4(a,0,0,1);
}

GPU code

What shader language is that? Cg? Also, is that supposed to be a vertex shader?

Also, this


pos.x = tex2D( tex , float2(0.0,0.0) ).r*255;
pos.y = tex2D( tex , float2(0.0,0.0) ).g*255;

is highly inefficient. Use swizzling:


pos.xy = tex2D(tex, float2(0.0,0.0)).rg * 255;

This way, you only have to access the texture once.

One more thing:


glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);

Never use GL_CLAMP. What you want is GL_CLAMP_TO_EDGE.

Also, this doesn’t match your edge[] data:

glTexImage2D(GL_TEXTURE_2D,0,GL_RGBA,2,2,0,GL_RGBA,GL_UNSIGNED_BYTE,edge);

Instead:

glTexImage2D(GL_TEXTURE_2D,0,GL_RGBA32UI,2,2,0,GL_RGBA_INTEGER,GL_UNSIGNED_INT,edge);

Though, maybe you should simply change edge[] to be GLubyte

Thank you very much ! It’s the problem of the type of the edge[2][2][4],it is should be GLubyte

The shader language is CG !It’s a vertex shader. It’s the problem of the type of the edge[2][2][4],it is should be GLubyte.
And another ,could you explain why 255 should be mutiply?Just like tex2D( tex , float2(0.0,1.0) ).g*255

could you explain why 255 should be mutiply?

He is scaling the data from 0 to 1 range to 0 to 255 range so that it could be seen in the world.

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.