C++/OpenGL VAO Problems

社会主义新天地 提交于 2019-12-17 21:06:44

问题


#define GLEW_STATIC
#include <GL\glew.h>
#include <GLFW\glfw3.h>
#include <GL\glew.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);
}

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.


回答1:


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.

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*)

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

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.

Edit: 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.



来源:https://stackoverflow.com/questions/41302685/c-opengl-vao-problems

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!