c++ - GLSL(1.2) + VBOs + Textures -


again applying considerable problem. code:

  float ctex[] = {0.0,0.0,                   0.0,1.0,                   1.0,1.0,                   1.0,0.0};    float data[] = {1.0, 1.0,-5.0,                  -1.0,-1.0,-5.0,                   1.0,-1.0,-5.0,                  -1.0, 1.0,-5.0};   gluint ind[] = {0,1,2,0,3,1};             loadtexture();          glgenbuffers(1,&trianglevbo);       glbindbuffer(gl_array_buffer,trianglevbo);       glbufferdata(gl_array_buffer,sizeof(data),data,gl_static_draw);        glgenbuffers(1,&triangleind);       glbindbuffer(gl_element_array_buffer,triangleind);       glbufferdata(gl_element_array_buffer,sizeof(ind),ind,gl_static_draw);       glvertexattribpointer(0,3,gl_float,gl_false,0,0);        glgenbuffers(1,&trianglet[0]);       glbindbuffer(gl_array_buffer,trianglet[0]);       glbufferdata(gl_array_buffer,sizeof(ctex),ctex,gl_static_draw);       glvertexattribpointer(1,2,gl_float,gl_false,0,0);        gluint v,f,p;       v = glcreateshader(gl_vertex_shader);       f = glcreateshader(gl_fragment_shader);       p = glcreateprogram();        char *vsfuente = leeshader("shaders/shader.vert");       char *fsfuente = leeshader("shaders/shader.frag");        const char *vs = vsfuente;       const char *fs = fsfuente;        glshadersource(v,1,&vs,null);       glshadersource(f,1,&fs,null);       free(vsfuente);free(fsfuente);        glcompileshader(v);       glcompileshader(f);        glattachshader(p,v);       glattachshader(p,f);        gllinkprogram(p);        //main loop       while(1){         ...etc.                gluseprogram(p);                 glenablevertexattribarray(0);                   glbindbuffer(gl_array_buffer,trianglevbo);                   gldrawelements(gl_triangles,6,gl_unsigned_int,0);                 gldisablevertexattribarray(0);                 glenablevertexattribarray(1);                   glbindbuffer(gl_array_buffer,triangletex);                   glactivetexture(gl_texture0);                   glbindtexture(gl_texture_2d,idtextere);                   glenable(gl_texture_2d);                 gldisablevertexattribarray(1);               gluseprogram(0);          ...etc.       } 

this vertex shader:

void main(){   gl_texcoord[0] = gl_multitexcoord0;   gl_position = ftransform(); }  

and fragment shader:

uniform sampler2d tex;

void main(){   vec4 color = texture2d(tex,gl_texcoord[0].st);   gl_fragcolor = color; } 

the problem texture not appear or else. what's problem?

thank in advance.

you using old fixed-function attributes in shader (like gl_multitexcoord0 or gl_vertex used ftransform). in application code try load them generic attribute interface (like glvertexattribpointer , glenablevertexattribarray). won't work (it might work attribute 0, alias gl_vertex, though, that's counter-intuitive anyway).

there 2 way fix this. either don't use generic attribute api, old fixed-function attributes, replace glvertexattribpointer(0, ...) glvertexpointer , glvertexattribpointer(1, ...) gltexcoordpointer , likewise glenablevertexattribarray glenableclientstate(gl_vertex_array) , glenableclientstate(gl_texture_coord_array).

or, more modern , future-proof approach, drop usage of old fixed-function stuff inside shaders , put in attributes generic attributes:

attribute vec4 position; attribute vec2 texcoord;  void main() {     gl_texcoord[0] = texcoord;     gl_position = gl_modelviewprojectionmatrix * position; } 

and don't forget call glbindattriblocation(p, 0, "position") , glbindattriblocation(p, 1, "texcoord") before linking program in order assign attribute indices correct attributes.

but second approach, if preferred, might bit heavy change right now, since should accompanied dropping use of old fixed-function stuff inside shaders, modelview projection matrix, should rather custom uniform, or gl_texcoord[0] varying, should rather custom varying.


Comments

Popular posts from this blog

javascript - Count length of each class -

What design pattern is this code in Javascript? -

hadoop - Restrict secondarynamenode to be installed and run on any other node in the cluster -