VBO not working
VBO not working
当我尝试使用vbo时,我只得到一个黑屏。我将GLFW与GLEW一起使用。它对纹理也有同样的作用,但我没有用纹理来判断它是否有效,但出于某种原因,它不起作用。我让它工作,但我对代码做了一些更改,所以我想我可能已经做了一些事情。PS:如果代码中有错误,请告诉我,因为我删除了不影响渲染的代码,我可能已经删除了事故上的重要代码
这是main.cpp,去掉了一些不影响OpenGL的东西:
//Include all OpenGL stuff, such as gl.h, glew.h, and glfw3.h
#include "gl.h"
//Include a header which adds some functions for loading shaders easier, there is nothing wrong with this code though
#include "shader.h"
float data[3][3] = {
{0.0, 1.0, 0.0},
{-1.0, -1.0, 0.0},
{1.0, -1.0, 0.0}
};
int main()
{
if (!glfwInit())
return 1;
GLFWwindow* window;
window = glfwCreateWindow(800, 600, "VBO Test", NULL, NULL);
if (!window)
{
glfwTerminate();
return 1;
}
glfwMakeContextCurrent(window);
if (GLEW_OK != glewInit())
{
return 1;
glfwTerminate();
}
//There are normal error handling stuff I do to ensure everything is loaded properly, so the shaders not loading isn't a concern as it'll clearly tell me :)
GLuint vertexShader = loadShader("shader.vert", GL_VERTEX_SHADER);
GLuint fragmentShader = loadShader("shader.frag", GL_VERTEX_SHADER);
GLuint program = createProgram(vertexShader, fragmentShader);
//Also, the shader files should make everything I draw yellow, and they are not defective
GLuint vbo;
glGenBuffers(1, &vbo);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(data), data, GL_STATIC_DRAW);
glVertexPointer(3, GL_FLOAT, 0, 0);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(data), data, GL_STATIC_DRAW);
glColorPointer(3, GL_FLOAT, 0, 0);
while (!glfwWindowShouldClose(window))
{
glClear(GL_COLOR_BUFFER_BIT);
glUsePorgram(program);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glDrawArrays(GL_TRIANGLES, 0, 3);
glfwSwapBuffers(window);
glfwPollEvents();
}
glfwTerminate();
return 0;
}
着色器.vert
void main(void)
{
gl_Position = gl_Vertex;
}
着色器.frag
void main()
{
//Set fragment
gl_FragColor = vec4( 1.0, 1.0, 0.0, 1.0 );
}
首先想到的是没有为顶点和颜色指针启用客户端状态。
由于颜色和位置数据重叠,颜色看起来会有点奇怪。我只从glVertexPointer和glEnableClientState(GL_VERTEX_ARRAY)开始,使用单个glColor3f调用。然后添加一个颜色数组。
此外,您将同一数据缓冲到同一VBO两次。VBO只需要在调用glBufferData和gl*Pointer函数之前进行绑定。
就我个人而言,我不喜欢2D数组,尽管它可能在静态声明时工作。OpenGL将线性地从内存中取出数据。使用structs数组可能非常不错。
请参阅以下小示例:http://goanna.cs.rmit.edu.au/~pknowles/SimpleVBO.c
相关文章:
- GDB - strcmp not working: __strcmp_sse2_unaligned
- QT qScriptConnect is not working
- QDesktopServices::openUrl() not working Qt
- "Configure: Error: Could Not Find a Working Compiler, See config.log For Details"
- C++ std::set ordering not working with customer class*
- C++ SetWindowText not working
- visual studio 2012 c++ hello world - iostream not working
- getGravity not working?
- Opencv findcontours CV_RETR_EXTERNAL not working
- c++ srand() not working
- std::queue::empty() not working?
- BCB swprintf not working
- 将类型作为指向另一个类的对象的指针的泛型类-NOT WORKING
- LoadLibrary is not working
- gluUnProject not working
- C++ catch bad_alloc not working
- updatelayeredwindow not working
- OpenMP set_num_threads() is not working
- glOrtho not working
- Cuda atomicInc not working