2012-04-06 54 views
1

我正在使用GLKit繪製一個像素。我可以成功地繪製在(10,10)的像素座標,如果我有:GLKit:[繪製像素]初始化GLfloat陣列

glClearColor(0.65f, 0.65f, 0.65f, 1.0f); 
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 

// Prepare the effect for rendering 
[self.effect prepareToDraw]; 

GLfloat points[] = 
{ 
    10.0f, 10.0f, 
}; 

glClearColor(1.0f, 1.0f, 0.0f, 1.0f); 

GLuint bufferObjectNameArray; 
glGenBuffers(1, &bufferObjectNameArray); 
glBindBuffer(GL_ARRAY_BUFFER, bufferObjectNameArray); 

glBufferData(
      GL_ARRAY_BUFFER, 
      sizeof(points), 
      points, 
      GL_STATIC_DRAW); 

glEnableVertexAttribArray(GLKVertexAttribPosition); 

glVertexAttribPointer(
         GLKVertexAttribPosition, 
         2, 
         GL_FLOAT, 
         GL_FALSE, 
         2*4, 
         NULL); 
glDrawArrays(GL_POINTS, 0, 1); 

但我想在運行時多少決定和正是我想要畫的像素,所以我想這一點,但它是借鑑像素在(10,0),這裏有什麼錯誤:

glClearColor(0.65f, 0.65f, 0.65f, 1.0f); 
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 

// Prepare the effect for rendering 
[self.effect prepareToDraw]; 

GLfloat *points = (GLfloat*)malloc(sizeof(GLfloat) * 2); 
for (int i=0; i<2; i++) { 
    points[i] = 10.0f; 
} 

glClearColor(1.0f, 1.0f, 0.0f, 1.0f); 

GLuint bufferObjectNameArray; 
glGenBuffers(1, &bufferObjectNameArray); 
glBindBuffer(GL_ARRAY_BUFFER, bufferObjectNameArray); 

glBufferData(
      GL_ARRAY_BUFFER, 
      sizeof(points), 
      points, 
      GL_STATIC_DRAW); 

glEnableVertexAttribArray(GLKVertexAttribPosition); 

glVertexAttribPointer(
         GLKVertexAttribPosition, 
         2, 
         GL_FLOAT, 
         GL_FALSE, 
         2*4, 
         NULL); 
glDrawArrays(GL_POINTS, 0, 1); 

請幫助我。

編輯: 問題 其實這個問題是:我無法弄清楚是什麼之間的區別:我的選擇將是這個問題

GLfloat points[] = 
{ 
    10.0f, 10.0f, 
}; 

GLfloat *points = (GLfloat*)malloc(sizeof(GLfloat) * 2); 
for (int i=0; i<2; i++) { 
    points[i] = 10.0f; 
} 

回答

0

glBufferData數據調用中的sizeof(points):在這種情況下,它將返回指針的大小,其中是一個字,即4個字節(或類似的東西)。您應該通過真實陣列的大小,它與您在malloc代碼中計算的大小相同。