openGL不允许我在类中绘制

vc6uscn9  于 2022-11-23  发布在  其他
关注(0)|答案(1)|浏览(154)

我尝试创建一个文本类,它有自己的顶点数组,顶点缓冲区,索引缓冲区,并在函数调用中绘制它们。它看起来像这样:
第一个
这是创建所有内容的构造函数:

Text::Text(std::string fontFilePath, std::string text, float posX, float posY)
    : m_FontFilePath(fontFilePath), m_Text(text)
{
    VertexArray va2;
    va2.Bind();

    m_TextPos = new float[m_Text.size() * 24];

    std::cout << posX << " " << posY<<"\n";
    
    float unitH = 0.1f, unitW = 0.1f;
    int countNow = 0;
    for (auto letter : m_Text)
    {
        addLetter(m_TextPos, {
            // posX          posY poZ       TextureCoord, TextureIndex
               posX,          posY, 0,          0.0f, 0.0f, 0.0f,
               posX + unitW, posY,  0,          1.0f, 0.0f, 0.0f,
               posX + unitW, posY + unitH, 0,  1.0f, 1.0f, 0.0f,
               posX,          posY + unitH, 0,  0.0f, 1.0f, 0.0f
            }, countNow);

        posX += unitW;
        countNow += 24;
    }
    // this creates a square for every letter of the word    

    VertexBuffer vb2(m_TextPos, sizeof(float) * 24 * m_Text.size());
    VertexBufferLayout vbl2;
    vbl2.Push<float>(3);
    vbl2.Push<float>(2);
    vbl2.Push<float>(1);
    va2.AddBuffer(vb2, vbl2);
    
    m_Index = new unsigned int[m_Text.size() * 6];
    for (int i = 0; i < m_Text.size(); i++) {
        m_Index[i * 6 + 0] = i * 4 + 0;
        m_Index[i * 6 + 1] = i * 4 + 1;
        m_Index[i * 6 + 2] = i * 4 + 2;
        m_Index[i * 6 + 3] = i * 4 + 2;
        m_Index[i * 6 + 4] = i * 4 + 3;
        m_Index[i * 6 + 5] = i * 4 + 0;
    }
    // this creates the m_Index array
    IndexBuffer ibo2(m_Index, m_Text.size() * 6);

    m_Vbo = &vb2;
    m_Ibo = &ibo2;
    m_Va = &va2;
    m_IndexCount = 6 * m_Text.size();

    std::cout << "INDEXES:\n";
    for (int i = 0; i < m_Text.size() * 6; i++) {
        std::cout << m_Index[i] << " ";
        if ((i + 1) % 6 == 0) std::cout << "\n";
    }
    std::cout << "\nINSIDE VBO:\n";
    for (int i = 0; i < 24 * m_Text.size(); i++) {
        std::cout << m_TextPos[i] << " ";
        if ((i + 1) % 6 == 0)std::cout << "\n";
    }

    va2.Unbind();
}

IndexBufferVertexBuffer中有正确的数据(我都检查过了)。如果我把这个类中的所有数据移到main.cpp中并在那里绘制它,它工作得很好,但我觉得很奇怪。主循环看起来像这样:

Text test(filePath, "randomText", 0.1f, 0.1f);
while (!glfwWindowShouldClose(window))
    {
        glClear(GL_COLOR_BUFFER_BIT);
        glClearColor(1.0f, 1.0f, 0.0f, 1.0f);
        
        // va = vertex array I created above this while loop, contains
        va.Bind(); // 2 squares, it draws good
        glDrawElements(GL_TRIANGLES, 12, GL_UNSIGNED_INT, nullptr);

        test.drawText(); // nothing draws on screen

        glfwSwapBuffers(window);

        glfwPollEvents();
    }
w1e3prcc

w1e3prcc1#

您的缓冲区对象是Text::Text范围内的局部变量。属性是dangling pointers。您必须创建动态对象。删除局部变量va2vb2ibo2,但分配动态内存并使用new operator创建对象:

Text::Text(std::string fontFilePath, std::string text, float posX, float posY)
    : m_FontFilePath(fontFilePath), m_Text(text)
{
    m_Va = new VertexArray(); 
    m_Va->Bind();

    // [...]

    m_Vbo = new VertexBuffer(m_TextPos, sizeof(float) * 24 * m_Text.size());
    
    // [...]

    m_Ibo = new IndexBuffer(m_Index, m_Text.size() * 6);
    
    // [...]
}

不要忘记delete类的析构函数中的对象。

相关问题