2016-07-05 135 views
-4

「sizeof(points)」是拋出錯誤的部分(標記如下)。我不知道發生了什麼事情。我是OpenGL的新手,我正在試驗我學到的東西,使得繪製多個三角形成爲可能。我也放在代碼在pastebin here「不完整類型不允許」錯誤

VertexObject.h

#pragma once 

#include <stdio.h> 
#include <stdlib.h> 

#include <GL\glew.h> 
#include <GLFW\glfw3.h> 

class VertexObject 
{ 

public: 
    VertexObject (); 

    void SetArray (GLfloat Points []); 

    void SetBuffer (GLuint* VBO); 

    GLfloat Points [ ] = { 
     1.0f , 0.0f , 1.0f, 
     0.0f , 1.0f , 1.0f, 
     -1.0f , 0.0f , 1.0f 
    }; 

private: 



}; 

VertexObject.cpp

#include "VertexObject.h" 

#include <stdio.h> 
#include <stdlib.h> 

#include <GL\glew.h> 
#include <GLFW\glfw3.h> 

void VertexObject::SetArray (GLfloat Points [ ]) 
{ 

    //Generate Vertex Array Object 
    GLuint vaoID1; 
    //Generates an array for the VAO 
    glGenVertexArrays (1 , &vaoID1); 
    //Assigns the array to the Vertex Array Object 
    glBindVertexArray (vaoID1); 

    //Fills in the array 

    for (int i = 0; i < sizeof (Points); i++) //Error occurs here 
    { 

     this->Points [ i ] = Points [ i ]; 

    } 

} 

void VertexObject::SetBuffer (GLuint* VBO) 
{ 

    //Generate Vertex Buffer Object 
    glGenBuffers (1 , VBO); 
    glBindBuffer (GL_ARRAY_BUFFER , *VBO); 
    glBufferData (GL_ARRAY_BUFFER ,sizeof(Points) , Points , GL_STATIC_DRAW); 

} 
+0

這意味着你沒有在任何地方定義的點。 –

+0

它在頭文件 –

+0

中定義並且即使當我使用「this-> points」時,它仍然會拋出錯誤 –

回答

0

正如PCAF說,sizeof(Points)給你指針的大小,而不是元素的數量在數組Points中。

你可以認爲你可以代替sizeof(Points)sizeof(this->Points)但有一個問題:sizeof(this->Points)不給你9(元素的數量),但9 * sizeof(GLfloat)

所以,你應該用sizeof(Points)/sizeof(Points[0])

但我認爲更大的問題是,

GLfloat Points [ ] = { 
    1.0f , 0.0f , 1.0f, 
    0.0f , 1.0f , 1.0f, 
    -1.0f , 0.0f , 1.0f 
}; 

不是C++ 11(C++ 11,而不是C++ 98,因爲在課堂上有效非靜態數據成員的初始化是C++ 11功能),因爲在課堂數組必須明確的大小,因此

GLfloat Points [ 9 ] = { 
    1.0f , 0.0f , 1.0f, 
    0.0f , 1.0f , 1.0f, 
    -1.0f , 0.0f , 1.0f 
}; 

但是如果你必須明確的大小,你可以在靜態記住它成員,像

static const std::size_t pointsSize = 9; 
GLfloat Points [ pointsSize ] = { 
    1.0f , 0.0f , 1.0f, 
    0.0f , 1.0f , 1.0f, 
    -1.0f , 0.0f , 1.0f 
}; 

,並用它在循環,這樣

for (auto i = 0U ; i < pointsSize ; ++i) 
{ this->Points [ i ] = Points [ i ]; } 
+0

我不明白的是它在SetArray中工作,但不在SetBuffer中。 –

+0

@JamesYeoman - 對不起,但我不知道你最後的評論(這是我的錯:我不是英語母語的人):你是說我的建議在'setArray()中正常工作,但不起作用在'setBuffer()'中?無論如何,請指望在'setBuffer()'中使用'sizeof(Points)'是完全不同的事情。 (繼續) – max66

+0

@JamesYeoman - (繼續)第一:在'setBuffer()''Points'沒有被方法參數覆蓋,所以'sizeof(Points)'就像'sizeof(this-> Points)'。第二:在'setBuffer()'中,你需要數組的大小,而不是元素的數量,所以如果你使用調用'getBufferData()'的'pointSize',你只需要一部分(第四個?) '。結論(如果我沒有錯):'setBuffer()'中的'sizeof(Points)'應該保持爲'sizeof(Points)' – max66

相關問題