0

使用 GLSL 1.2 和 glDrawElements 绘图时遇到问题。这是我的代码:

      //At start of the program.
      GLuint triangleVBO;
      GLuint triangleIND;
      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,
                     -1.0, 1.0,-5.0,
                     -1.0,-1.0,-5.0};
      GLuint ind[] = {0,1,2,0,3,1};

      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);

      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);
              glUseProgram(0);

      .... etc.
   }

问题是它只画一个三角形,什么时候应该是两个正方形。

有人告诉我错误是什么吗?

4

1 回答 1

2

查看您的顶点数据,顶点0和顶点3具有相同的位置(1、1、-5)。所以你的第二个三角形(索引03、 1 )是退化的。

于 2013-07-09T08:39:40.943 回答