关于glMapBufferRange

时间:2013-01-21 17:25:10

标签: opengl opengl-3

    GLfloat vertices4[]={-0.9,0.6, -0.9,0.2, -0.6,0.2, -0.6,0.6,
                         -0.4,0.6, -0.4,0.2,  0.1,0.2,  0.1,0.6,
                          0.3,0.6,  0.3,0.2,  0.6,0.2,  0.6,0.6  };

     GLushort indices[]={0,1,2,3, 120, 4,5,6,7, 120, 8,9,10,11};
     GLenum *value=GL_STREAM_DRAW;  
     GLfloat temp_err;

     glEnable(GL_PRIMITIVE_RESTART);
     nResult =GL_ERROR_CHECK((GL_NO_ERROR,"glEnable(GL_PRIMITIVE_RESTART)"));

     glPrimitiveRestartIndex(120);
     nResult = GL_ERROR_CHECK((GL_NO_ERROR,"glPrimitiveRestartIndex(120);"));

     glGenBuffers(1,&bufferid1);
     nResult = GL_ERROR_CHECK((GL_NO_ERROR,"glGenBuffers(1,&bufferid1)"));

     glGenBuffers(1,bufferid);

     glBindBuffer(GL_TEXTURE_BUFFER,bufferid);      
     glBufferData(GL_TEXTURE_BUFFER,sizeof(vertices4),vertices4,*value);

     temp_err=(GLfloat*)glMapBufferRange(GL_TEXTURE_BUFFER,0,8*sizeof(GLfloat),GL_MAP_READ_BIT);


     glBindBuffer(GL_ARRAY_BUFFER,bufferid1);
     nResult = GL_ERROR_CHECK((GL_NO_ERROR,"glBindBuffer(GL_ARRAY_BUFFER,bufferid1);")); 

     glBufferData(GL_ARRAY_BUFFER,8*sizeof(GLfloat),temp_err,*value);


     *attr_vertex = glGetAttribLocation(shader_data.psId, "a_position"); 
     nResult = GL_ERROR_CHECK((GL_NO_ERROR,"glGetAttribLocation(shader_data.psId, \"a_position\");"));

     glVertexAttribPointer(*attr_vertex, 2 , GL_FLOAT, GL_FALSE ,0, 0);
     nResult = GL_ERROR_CHECK((GL_NO_ERROR,"glVertexAttribPointer(attr_vertex, 2 , GL_FLOAT, GL_FALSE ,0, 0);"));

     glEnableVertexAttribArray(*attr_vertex);
     nResult = GL_ERROR_CHECK((GL_NO_ERROR,"glEnableVertexAttribArray(attr_vertex);"));

     glUniform1f(glGetUniformLocation(shader_data.psId,"offset_x"),x);                      
     glUniform1f(glGetUniformLocation(shader_data.psId,"offset_y"),y);                      
     glDrawElements(GL_TRIANGLE_FAN, 14,GL_UNSIGNED_SHORT,indices);

我期待它渲染1个四边形,因为我在“bufferid1”中传递第一个四边形的顶点数据,但它渲染了顶点4中的所有3个四边形。请指出我在这里出错的地方。

1 个答案:

答案 0 :(得分:2)

  

这里我希望它会渲染1个四分之一

你怎么能期待呢?您的渲染调用具体表示渲染14个顶点:

glDrawElements(GL_TRIANGLE_FAN, 14,GL_UNSIGNED_SHORT,indices);

没有理由让OpenGL神奇地知道你这次只意味着四次。