2016-07-05 134 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

相关问题