glsl 1.2 + DrawElements

时间:2013-07-09 04:58:31

标签: c++ c opengl glsl

使用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.
   }

问题是它只绘制一个三角形,应该是两个正方形。

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

1 个答案:

答案 0 :(得分:2)

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