简体   繁体   English

OpenGL阵列移交不起作用

[英]OpenGL array handover do not work

I am trying to program some game with OpenGL and read a whole bunch of tutorials. 我正在尝试使用OpenGL编写一些游戏,并阅读大量教程。 Unfortunately I got a small problem who just interrupts my progress. 不幸的是,我遇到了一个小问题,他打断了我的进度。

I created a "Mesh" class where I handover an array of GLfloats . 我创建了一个“ Mesh”类,在其中移交了GLfloats数组。 These floats are included by an VAO and VBO. 这些浮子包含在VAO和VBO中。 As long as I create the array inside the constructor (with the whole initialization-functions) it works fine. 只要我在构造函数中创建数组(带有整个初始化函数),它就可以正常工作。 But if I want to handover the array as an argument OpenGL just won't draw. 但是,如果我想将数组作为参数传递,OpenGL不会绘制。 Did I forget something? 我忘记了什么吗?

Here is my main code: 这是我的主要代码:

Mesh.cpp 网格

Mesh::Mesh(GLfloat* vertices)
{
    glGenVertexArrays(1, &m_vertexArray);
    glBindVertexArray(m_vertexArray);

        glGenBuffers(1, &m_vertexBuffer);
        glBindBuffer(GL_ARRAY_BUFFER, m_vertexBuffer);
            glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);

            glEnableVertexAttribArray(0);
            glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, (void*)0);

    glBindVertexArray(0);
}

Mesh::~Mesh()
{
    glDeleteVertexArrays(1, &m_vertexArray);
}

void Mesh::draw()
{
    glBindVertexArray(m_vertexArray);
    glDrawArrays(GL_TRIANGLES, 0, 6);
    glBindVertexArray(0);
}

main.cpp main.cpp

[...]    

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

Mesh mesh(vertices);

while (!mainWindow->isClosed())
{
    glClearColor(0.0f, 0.0f, 1.0f, 1.0f);
    glClear(GL_COLOR_BUFFER_BIT);

    shaders->bind();

    // here main functions:

    mesh.draw();

    mainWindow->update();
}

I handover an array of GLfloats 我移交了GLfloats数组

No, you do not "handover" an array of anything. 不,您不“移交”任何东西。 You passed a pointer . 您传递了一个指针 Pointers and arrays are different things in C++. 指针和数组在C ++中是不同的东西。 Most importantly, arrays can "decay" into pointers (that's what allows you to pass them to functions that take pointers), but when they do, all sizing information is lost . 最重要的是,数组可以“分解”为指针(这是使您可以将它们传递给带有指针的函数的原因),但是当它们这样做时,所有大小调整信息都会丢失

sizeof(vertices) is the size of a GLfloat* . sizeof(vertices)GLfloat*的大小。 In all likelihood, that's either 4 or 8. It is most assuredly not the size of the array you had in the caller of the function. 很有可能是4或8。最确定的是,函数调用方中没有数组的大小。

The preferred method for handling this would be to pass the pointer and a size to that function. 处理此问题的首选方法是将指针和大小传递给该函数。 However, sizeof(vertices) will be the number of bytes in the array, not the number of array elements. 但是, sizeof(vertices)将是数组中的字节数,而不是数组元素的数目。

One way to handle this would be to get a std::vector rather than an array, through C++11: 解决此问题的一种方法是通过C ++ 11获得std::vector而不是数组:

std::vector<GLfloat> vertices = {
-0.5f, -0.5f, 0.0f,
 0.5f, -0.5f, 0.0f,
 0.0f,  0.5f, 0.0f,
 -1.0f, -1.0f, 0.0f,
 0.0f, -1.0f, 0.0f,
 -0.5f,  0.0f, 0.0f
};

Mesh mesh(vertices.data(), vertices.size());

Alternately, you can calculate the size with some clever macros: 或者,您可以使用一些聪明的宏来计算大小:

#define ARRAY_COUNT(arr) ( sizeof(arr) / sizeof(arr[0]))

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

Mesh mesh(vertices, ARRAY_COUNT(vertices));

Or, if you want to use cleverer C++11 features: 或者,如果您想使用更聪明的C ++ 11功能:

template<typename T, size_t N>
constexpr size_t array_count(T (&arr)[N]) {return N;}

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

Mesh mesh(vertices, array_count(vertices));

Or you can just skip the middle-man and employ the C++ Core Guidelines support library class span : 或者,您可以跳过中间人并采用C ++ Core Guidelines 支持库类span

Mesh::Mesh(gsl::span<GLfloat> vertices)
{
    glGenVertexArrays(1, &m_vertexArray);
    glBindVertexArray(m_vertexArray);

    glGenBuffers(1, &m_vertexBuffer);
    glBindBuffer(GL_ARRAY_BUFFER, m_vertexBuffer);
    glBufferData(GL_ARRAY_BUFFER, vertices.size_bytes(), vertices.data(), GL_STATIC_DRAW);

    glEnableVertexAttribArray(0);
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, (void*)0);

    glBindVertexArray(0);
}

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

Mesh mesh(vertices);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM