简体   繁体   中英

GLSL(1.2) + VBOs + Textures

Again applying their considerable help with a problem. This is my 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 is my Vertex Shader:

void main(){
  gl_TexCoord[0] = gl_MultiTexCoord0;
  gl_Position = ftransform();
} 

And this my Fragment Shader:

uniform sampler2D tex;

void main(){
  vec4 color = texture2D(tex,gl_TexCoord[0].st);
  gl_FragColor = color;
}

The problem is that the texture does not appear or anything else. I could say What's the problem?

Thank you very much in advance.

You are using the old fixed-function attributes in your shader (like gl_MultiTexCoord0 or gl_Vertex as used by ftransform ). But in your application code you try to load them with the generic attribute interface (like glVertexAttribPointer and glEnableVertexAttribArray ). This won't work (it might work for attribute 0, which is an alias for gl_Vertex , though, but that's counter-intuitive anyway).

There are two way to fix this. Either don't use the generic attribute API, but the old fixed-function attributes, so replace glVertexAttribPointer(0, ...) with glVertexPointer and glVertexAttribPointer(1, ...) with glTexCoordPointer and likewise glEnableVertexAttribArray with glEnableClientState(GL_VERTEX_ARRAY) and glEnableClientState(GL_TEXTURE_COORD_ARRAY) .

Or, the more modern and future-proof approach, drop the usage of the old fixed-function stuff inside your shaders and put in your attributes as generic attributes:

attribute vec4 position;
attribute vec2 texCoord;

void main() {
    gl_TexCoord[0] = texCoord;
    gl_Position = gl_ModelViewProjectionMatrix * position;
}

And don't forget to call glBindAttribLocation(p, 0, "position") and glBindAttribLocation(p, 1, "texCoord") before linking the program in order to assign the attribute indices to the correct attributes.

But the second approach, even if preferred, might be a bit too heavy a change for you right now, since it should actually be accompanied by dropping any use of old fixed-function stuff inside your shaders, like the modelview projection matrix, which should rather be a custom uniform, or the gl_TexCoord[0] varying, which should rather be a custom varying.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM