C ++ / OpenGL VAO问题 [英] C++/OpenGL VAO Problems
问题描述
#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 TITLEDynamic
GLFWwindow * window;
int vaoID;
浮点顶点[] = {-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<< 无法初始化GLFW! <<的std :: ENDL;
window = glfwCreateWindow(WIDTH,HEIGHT,TITLE,NULL,NULL);
glfwMakeContextCurrent(window);
glfwSwapInterval(1);
if(GLEW_OK!= glewInit())
std :: cout<< GLEW搞砸了! <<的std :: ENDL;
std :: cout<< 你的GL版本:<< 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);
$ b这是我创建和渲染包含VBO的VAO的代码,顶点位置。但不幸的是它不起作用。它画一个三角形而不是四边形。但是,当我在while循环之前将loadToVAO函数的代码放在update函数中时,它的工作原理就是这样。 <$> <$> c $ c> sizeofloadToVAO
的内容复制到更新函数时,sizeof(vertices)
基本上是sizeof(float [18])
,它是数组的总大小。 $ b但是在
loadToVAO
,sizeof(vertices)
将函数参数vertices
作为输入,而不是全局变量。由于在C ++中未定义大小的数组参数被视为相同类型的点,因此我们在这里:
sizeof(vertices)== sizeof(float [])== sizeof(float *)
这是指针的大小4或8)而不是数据的大小。
为了解决这个问题,您可以将数组的大小也传递给函数,即C要走的路。更现代的方法是使用
std :: array
或std :: vector
来将数据存储在第一位。
编辑:在第二期中,我看到您首先使用了
sizeof(vertices)* 8
。我不确定8是从哪里来的,因为你既没有8个元素,也没有8个字节的大小。#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.
解决方案The
sizeof
operator always returns the size of the underlying type. When you copy the content ofloadToVAO
to the update function,sizeof(vertices)
is basicallysizeof(float[18])
, which is the total size of the array.But inside the
loadToVAO
, thesizeof(vertices)
takes the function parametervertices
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
orstd::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.这篇关于C ++ / OpenGL VAO问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!