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