PDA

View Full Version : TExture access problem



apapaxionga
11-02-2011, 07:06 AM
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) ).r*255;
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) ).r*255;
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) ).r*255;
pos.y = tex2D( tex , float2(0.0,1.0) ).g*255;
}

opos=mul(WorldViewProj,pos);

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

Alfonse Reinheart
11-02-2011, 11:02 AM
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.

Ilian Dinev
11-02-2011, 11:54 AM
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

apapaxionga
11-02-2011, 06:39 PM
Thank you very much ! It's the problem of the type of the edge[2][2][4],it is should be GLubyte

apapaxionga
11-02-2011, 06:42 PM
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

mobeen
11-02-2011, 08:21 PM
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.