Texture usage with vertex shader in GLSL

Hi every one,
I had a problem by using textures.
My idea is pretty simple, where I want to use textures as a storage to my 2D arrays the represent a hierarchical system. So my arrays look like:

Ar[res][res] that is of structure:
struct Point
{
float nx,ny,nz // normal coordinates
,h; // height value
};

for simplicity I loaded my array int array with one float which is the height value :

for(int j = 0; j < 1024;j++){
for(int i =0; i<1024;i++){
text_data[i][j] = Arr[i][j].h;
}
}

and then I have stored this array in 2D texture as follows :

//here I set shaders that I will mention in step 2
setShaders();
//setp 1 : textures
//enabling 2D texture
glEnable(GL_TEXTURE_2D);

//allocate texture
glGenTextures( 1, &texID);

// bind texture
glBindTexture( GL_TEXTURE_2D, texID);

//load data into texture
//--------- question 1 :
// does the data array here "text_data[][]" works well
// I am not sure if my problem starts here,i.e. 2D array?
//---------------------
glTexImage2D(GL_TEXTURE_2D,0,1,res,res,0,GL_RED,GL_FLOAT,text_data );//
// activate texture
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D,texID);

//setp 2 : shaders

void setShaders() {

char *vs = NULL,*fs = NULL,*fs2 = NULL;

v = glCreateShader(GL_VERTEX_SHADER);
f = glCreateShader(GL_FRAGMENT_SHADER);

vs = textFileRead("shader.vert");
fs = textFileRead("shader.frag");

const char * vv = vs;
const char * ff = fs;

glShaderSource(v, 1, &vv,NULL);
glShaderSource(f, 1, &ff,NULL);

free(vs);free(fs);

glCompileShader(v);
glCompileShader(f);

p = glCreateProgram();
glAttachShader(p,f);
glAttachShader(p,v);

glLinkProgram(p);
glUseProgram(p);
// get location of uniform for texture
GLint loctex = glGetUniformLocation(p,"texID");
//set uniform variable
glUniform1i(loctex,0);


}

and I will mention only vertex shader since it is the most important for my project:

/* -------------------------

Vertex shader

--------------------------- */
//-----------question 2:
//should I use the same name
//of texture ID that mentioned
//befor "texID"?
//----------------------

uniform sampler2D texID;

void main()
{
vec2 pos_texture;
vec4 pos,texel;
float pos_z;

// load Vertex position into pos
pos = vec4(gl_Vertex);

//take only x,y to get the corresponding height
pos_texture = vec2(pos);

/* get height from given coordinates */
texel = texture2D (texture ,vec2(pos_texture));
//set this height to z direction of vertex as heigt
pos.z =texel.x ;

// pass the new pos as new vertex to be drawn
gl_Position = gl_ModelViewProjectionMatrix * pos ;


}

but the height values don’t seem to be read from texture since I have always flat plane and I have tried something else like just passing the vertex to the
“pos” to figure out where is the problem exactly and it works…that means the problem either with “glTexImage2D” to store data or with “texture2D” to read data…

I really appreciate any suggestion or advice related to that problem.

Is there any way to check weather the data are loaded to the texture before
using it in vertex shader…since I am new in the textures currently.

By the ways I am using GLSL with opengl

Sheree

Need to check that GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS > 0 and use texture2DLod to sample from a VS. As I recall there are some format restrictions too. Check your shader/program logs for errors.

Thanks Brolingstanz, I have checked it, but it wasn’t seem to be the cause of the problem…
Any way I had solve the problem by a help from friends on Gamedev.net forum:
The problem was that I didn’t set the MIN/MAG filters,therefore my textures were blank … :slight_smile:

Now my next question is :
Could I use Mipmapping for the case where I want to get each new texture built from original one ?! …which is not the case if we would like to get the benefits from Mipmapping in Opengl ,So what I have done in my project is building hierarchical system " 2D grids " in preprocessing set and then store them in textures at this point I think it would be more efficient if I get only one and let Mipmapping mechanism do the building and then I can easily access to the desired texture from its level ID since I always want to access to an arbitrary texel in vertex shader.

any suggestions would be absolutely appreciated.

Sheree

You can use any lod you want; you just have to provide for your own filtering (if any) between texels and mip levels - there’s no way to infer how these operations should be defined at the vertex level…

Okay, got it.
Thanks for reply.

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