Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/164.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/opengl/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何正确地将OpenGL代码添加到外部函数中_C++_Opengl - Fatal编程技术网

C++ 如何正确地将OpenGL代码添加到外部函数中

C++ 如何正确地将OpenGL代码添加到外部函数中,c++,opengl,C++,Opengl,我正在尝试使用OpenGL代码从顶点数组中绘制对象,并将其添加到类文件中。但是,代码当前仅在main.cpp文件中有它时运行。在进入绘图循环之前,我从main()函数调用init() init(){ GLuint containerVAO, VBO; glGenVertexArrays(1, &containerVAO); glGenBuffers(1, &VBO); glBindBuffer(GL_ARRAY_BUFFER, VBO);

我正在尝试使用OpenGL代码从顶点数组中绘制对象,并将其添加到类文件中。但是,代码当前仅在main.cpp文件中有它时运行。在进入绘图循环之前,我从main()函数调用init()

init(){
    GLuint containerVAO, VBO;
    glGenVertexArrays(1, &containerVAO);
    glGenBuffers(1, &VBO);

    glBindBuffer(GL_ARRAY_BUFFER, VBO);
    glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);

    glBindVertexArray(containerVAO);
    // Position attribute
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(GLfloat), (GLvoid*)0);
    glEnableVertexAttribArray(0);
    // Normal attribute
    glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(GLfloat),(GLvoid*)(3 * sizeof(GLfloat)));
    glEnableVertexAttribArray(1);
    glBindVertexArray(0);
}
我的绘制循环中的相关代码:

glUseProgram(noTextureShaderID);
glBindVertexArray(containerVAO);
///many different uniforms added here
glDrawArrays(GL_TRIANGLES, 0, 36);
这会创建一个立方体,没问题。 现在,当我替换init()函数中的代码(初始化所有对象,而不仅仅是这个对象)时,我将其更改为:

init(){
    square.init(noTextureShaderID, vertices[], NULL, 36);
    //Square is a global variable within my main.cpp file
}
然后我使用这个函数:

void Mesh::init(const GLuint& shaderid, GLfloat vertices[], const char* tex_file, int num_vertices)
{
GLuint VBO;
vao = NULL;    //This is a variable within the Mesh class
g_point_count = num_vertices;
glGenVertexArrays(1, &vao);
glGenBuffers(1, &VBO);

glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);

glBindVertexArray(vao);
// Position attribute
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(GLfloat), (GLvoid*)0);
glEnableVertexAttribArray(0);
// Normal attribute
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(GLfloat), (GLvoid*)(3 * sizeof(GLfloat)));
glEnableVertexAttribArray(1);
glBindVertexArray(0);
}
然后,在我的draw函数中,我将其称为:

glUseProgram(noTextureShaderID);
glBindVertexArray(square.vao);
///many different uniforms added here
glDrawArrays(GL_TRIANGLES, 0, g_point_count);

但是,即使两个程序似乎有相同的代码,只有第一个版本生成了一个多维数据集。在这方面我缺少了什么?

您的代码在两种情况下都不相同,这与OpenGL无关:

void Mesh::init(const GLuint& shaderid, GLfloat vertices[], const char* tex_file, int num_vertices)
{
     // ...
     glBufferData(..., sizeof(vertices), ...);
}
顶点
在这里通过引用传递,内部函数将永远看不到数组,
sizeof(顶点)
将与
sizeof(GLfloat*)
相同,在今天的机器上通常是4或8。因此,缓冲区只包含前一个或两个浮点


您必须明确地提供数组大小作为附加参数,或者使用一些(引用)更高级别的对象,如
std:vector
,它在内部完全管理数组并允许您查询大小。

您的代码在这两种情况下都不相同,这与OpenGL无关:

void Mesh::init(const GLuint& shaderid, GLfloat vertices[], const char* tex_file, int num_vertices)
{
     // ...
     glBufferData(..., sizeof(vertices), ...);
}
顶点
在这里通过引用传递,内部函数将永远看不到数组,
sizeof(顶点)
将与
sizeof(GLfloat*)
相同,在今天的机器上通常是4或8。因此,缓冲区只包含前一个或两个浮点


您必须明确地提供数组大小作为附加参数,或者使用一些(引用)更高级别的对象,如
std:vector
,它在内部完全管理数组并允许您查询大小。

好的,所以当我将该参数更改为“sizeof(GLfloat)*6*num\u顶点”时,它是有效的。这很好,但我仍然不明白为什么这些数字是有效的。你能给我解释一下吗,或者给我指一个可能解释它的资源吗?等等,我现在明白了。数组中的每个顶点有6个不同的值。我只需要去看看这些值在t中的实际含义他是我程序的上下文。谢谢你的帮助!好的,当我把参数改为“sizeof(GLfloat)*6*num_顶点”,它是有效的。这很好,但我仍然不明白为什么这些数字是有效的。你能给我解释一下吗,或者给我指一个可能解释它的资源吗?等等,我现在明白了。数组中的每个顶点有6个不同的值。我只需要去看看这些值在t中的实际含义他是我节目的背景。谢谢你的帮助!