<i id='sSN9n'><tr id='sSN9n'><dt id='sSN9n'><q id='sSN9n'><span id='sSN9n'><b id='sSN9n'><form id='sSN9n'><ins id='sSN9n'></ins><ul id='sSN9n'></ul><sub id='sSN9n'></sub></form><legend id='sSN9n'></legend><bdo id='sSN9n'><pre id='sSN9n'><center id='sSN9n'></center></pre></bdo></b><th id='sSN9n'></th></span></q></dt></tr></i><div id='sSN9n'><tfoot id='sSN9n'></tfoot><dl id='sSN9n'><fieldset id='sSN9n'></fieldset></dl></div>
    <legend id='sSN9n'><style id='sSN9n'><dir id='sSN9n'><q id='sSN9n'></q></dir></style></legend>
    <tfoot id='sSN9n'></tfoot>
      <bdo id='sSN9n'></bdo><ul id='sSN9n'></ul>
    1. <small id='sSN9n'></small><noframes id='sSN9n'>

      C++/OpenGL VAO 问题

      时间:2023-09-17
        <bdo id='blenU'></bdo><ul id='blenU'></ul>
          <tbody id='blenU'></tbody>
        1. <tfoot id='blenU'></tfoot>

          • <small id='blenU'></small><noframes id='blenU'>

            <i id='blenU'><tr id='blenU'><dt id='blenU'><q id='blenU'><span id='blenU'><b id='blenU'><form id='blenU'><ins id='blenU'></ins><ul id='blenU'></ul><sub id='blenU'></sub></form><legend id='blenU'></legend><bdo id='blenU'><pre id='blenU'><center id='blenU'></center></pre></bdo></b><th id='blenU'></th></span></q></dt></tr></i><div id='blenU'><tfoot id='blenU'></tfoot><dl id='blenU'><fieldset id='blenU'></fieldset></dl></div>

                <legend id='blenU'><style id='blenU'><dir id='blenU'><q id='blenU'></q></dir></style></legend>
              • 本文介绍了C++/OpenGL VAO 问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                #define GLEW_STATIC
                #include <GLglew.h>
                #include <GLFWglfw3.h>
                #include <GLglew.h>
                #include <glm.hpp>
                #include <iostream>
                #include <fstream>
                #include <string>
                
                #define WIDTH 800
                #define HEIGHT 600
                #define TITLE "Dynamic"
                
                GLFWwindow* window;
                int vaoID;
                
                float vertices[] = {-0.5f, 0.5f, 0, -0.5f, -0.5f, 0,    0.5f, 0.5f, 0,  0.5f, 0.5f, 0,  -0.5f, -0.5f,   0,  0.5f, -0.5f, 0};
                
                void loadToVAO(float vertices[]);
                
                void update() {
                    loadToVAO(vertices);
                    while (!glfwWindowShouldClose(window)) {
                        glfwPollEvents();
                        glClear(GL_COLOR_BUFFER_BIT);
                        glClearColor(1, 0, 0, 1);
                        glDrawArrays(GL_TRIANGLES, 0, 6);
                        glfwSwapBuffers(window);
                    }
                }
                
                int main() {
                    if (!glfwInit())
                        std::cout << "Couldn't initialize GLFW!" << std::endl;
                
                    window = glfwCreateWindow(WIDTH, HEIGHT, TITLE, NULL, NULL);
                    glfwMakeContextCurrent(window);
                    glfwSwapInterval(1);
                
                    if (GLEW_OK != glewInit())
                        std::cout << "GLEW fucked up!" << std::endl;
                
                    std::cout << "Your GL version: " << glGetString(GL_VERSION) << std::endl;
                    glEnable(GL_CULL_FACE);
                    glCullFace(GL_BACK);
                    update();
                }
                
                
                
                void loadShaders() {
                
                }
                
                void loadToVAO(float vertices[]) {
                    GLuint vboID;
                    GLuint vaoID;
                    glGenBuffers(1, &vboID);
                    glBindBuffer(GL_ARRAY_BUFFER, vboID);
                    glBufferData(GL_ARRAY_BUFFER, sizeof(vertices) * 8, vertices, GL_STATIC_DRAW);
                    glGenVertexArrays(1, &vaoID);
                    glBindVertexArray(vaoID);
                    std::cout << vaoID << std::endl;
                    glVertexAttribPointer(0, 3, GL_FLOAT, false, 0, 0);
                    glEnableVertexAttribArray(0);
                }
                

                这是我用于创建和渲染包含具有顶点位置的 VBO 的 VAO 的代码.但不幸的是它不起作用.它绘制一个三角形而不是一个四边形.但是当我把loadToVAO函数的代码放在while循环之前的update函数中时,它就起作用了.

                This is my code for creating and rendering a VAO that contains a VBO with the vertex positions. But unfortunately it doesn't work. It draws a triangle instead of a quad. But when I put the code of the loadToVAO function in the update function before the while loop, it works.

                推荐答案

                sizeof 运算符始终返回基础类型的大小.当你把loadToVAO的内容复制到update函数中时,sizeof(vertices)基本上就是sizeof(float[18]),也就是数组的总大小.

                The sizeof operator always returns the size of the underlying type. When you copy the content of loadToVAO to the update function, sizeof(vertices) is basically sizeof(float[18]), which is the total size of the array.

                但是在 loadToVAO 中,sizeof(vertices) 将函数参数 vertices 作为输入而不是全局变量.由于在 C++ 中未定义大小的数组参数被视为我们这里的相同类型的点:

                But inside the loadToVAO, the sizeof(vertices) takes the function parameter vertices as input and not the global variable. Since array parameters of undefined size in C++ are treated as points of the same type we have here:

                sizeof(vertices) == sizeof(float[]) == sizeof(float*)
                

                它是指针的大小(4 或 8),不再是数据的大小.

                which is the size of a pointer (4 or 8) and not the size of the data anymore.

                要解决这个问题,您可以将数组的大小也传递给函数,这是 C 的方法.更现代的方法是首先使用 std::arraystd::vector 来存储数据.

                To solve this, you can either pass the size of the array also to the function, which is the C way to go. The more modern way is to use std::array or std::vector to store the data in the first place.

                第二次查看时,我看到您首先使用了 sizeof(vertices) * 8.我不太确定 8 是从哪里来的,因为你既没有 8 个元素,浮点数也没有 8 个字节的大小.

                On the second look I saw that you used sizeof(vertices) * 8 in the first place. I'm not really sure where the 8 comes from, since you neither have 8 elements nor does a float have a size of 8 bytes.

                这篇关于C++/OpenGL VAO 问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:OpenGL 着色器无法编译 下一篇:OpenGL 忽略四边形并使它们成为三角形

                相关文章

                最新文章

                  <bdo id='N7D1R'></bdo><ul id='N7D1R'></ul>
              • <small id='N7D1R'></small><noframes id='N7D1R'>

                  <tfoot id='N7D1R'></tfoot>

                1. <i id='N7D1R'><tr id='N7D1R'><dt id='N7D1R'><q id='N7D1R'><span id='N7D1R'><b id='N7D1R'><form id='N7D1R'><ins id='N7D1R'></ins><ul id='N7D1R'></ul><sub id='N7D1R'></sub></form><legend id='N7D1R'></legend><bdo id='N7D1R'><pre id='N7D1R'><center id='N7D1R'></center></pre></bdo></b><th id='N7D1R'></th></span></q></dt></tr></i><div id='N7D1R'><tfoot id='N7D1R'></tfoot><dl id='N7D1R'><fieldset id='N7D1R'></fieldset></dl></div>

                    <legend id='N7D1R'><style id='N7D1R'><dir id='N7D1R'><q id='N7D1R'></q></dir></style></legend>