我想創建一個類來處理OpenGL緩衝區,如頂點緩衝區對象或顏色緩衝區。自定義OpenGL緩衝區類不顯示任何東西
這裏是buffer.h:
#pragma once
#include <GL/glew.h>
class glBuffer
{
public:
glBuffer(GLenum target);
void setdata(const void *data, GLenum mode);
void bind(GLuint index, GLint valuePerVertex, GLenum variableType = GL_FLOAT, GLsizei stride = 0, int offset = 0);
void unbind();
GLuint getBufferID() const;
~glBuffer();
private:
bool m_active;
GLuint m_buffer;
GLuint m_index;
GLenum m_target;
};
而且buffer.cpp:
#include "buffer.h"
#include <GL/glew.h>
#include <iostream>
glBuffer::glBuffer(GLenum target)
{
m_target = target;
m_active = false;
glGenBuffers(1, &m_buffer);
}
void glBuffer::setdata(const void *data, GLenum mode)
{
glBindBuffer(m_target, m_buffer);
glBufferData(m_target, sizeof(data), data, mode);
glBindBuffer(m_target, 0);
}
void glBuffer::bind(GLuint index, GLint valuePerVertex, GLenum variableType, GLsizei stride, int offset)
{
m_active = true;
m_index = index;
glEnableVertexAttribArray(m_index);
glBindBuffer(m_target, m_buffer);
glVertexAttribPointer(
m_index,
valuePerVertex,
variableType,
GL_FALSE, //normalized?
stride,
(void*)offset //buffer offset
);
}
void glBuffer::unbind()
{
m_active = false;
glBindBuffer(m_target, 0);
glDisableVertexAttribArray(m_index);
}
GLuint glBuffer::getBufferID() const
{
return m_buffer;
}
glBuffer::~glBuffer()
{
if (!m_active){
unbind();
}
glDeleteBuffers(1, &m_buffer);
}
這是我如何使用它在我的應用程序,在那裏我#include "buffer.h"
:
glBuffer vbo(GL_ARRAY_BUFFER);
vbo.setdata(color_buffer_data, GL_STATIC_DRAW);
vbo.bind(0, 3);
取代:
GLuint vbo;
glGenBuffers(1, &vbo);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertex_buffer_data), vertex_buffer_data, GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glVertexAttribPointer(
0, // attribute 0. No particular reason for 0, but must match the layout in the shader.
3, // size
GL_FLOAT, // type
GL_FALSE, // normalized?
0, // stride
(void*)0 // array buffer offset
);
當我編譯並運行它時,我得到一個沒有任何繪製的黑色窗口。
發生了什麼事? PS:我正在使用vs,glfw3,glew。
在[mcve]中編輯。對於我們所有人來說,您都處於核心環境並忘記了VAO。 – genpfault
另外,'glBuffer :: setdata()'中的'sizeof(data)'在大多數平臺上將是'4'或'8',而不是像你希望的'data'的長度。傳遞一個單獨的'size'參數或使用像'std :: array <>'/'std :: vector <>'這樣的容器。 – genpfault