问题描述
我尝试启动下一个代码,但是在glDrawElements(GL_TRIANGLES,6,GL_UNSIGNED_INT,nullptr)行( Application.cpp )上,它给出了错误1285。
如果我将前一行代码替换为glDrawArrays(GL_TRIANGLES,0,3),它将绘制一个三角形,因此着色器,顶点缓冲区,顶点数组绑定可以正常工作。问题应该出在IndexBuffer中,但是我找不到它。
Renderer.h
class CorrectAnswer(Base):
""" Correct answer to quiz questions """
__tablename__ = "correct_answer"
id = Column(Integer,primary_key=True)
question_id = Column(Integer,ForeignKey("question.id"))
answer_id = Column(Integer,ForeignKey("answer.id"))
question = relationship("Question",back_populates="correct_answer")
__table_args__ = (UniqueConstraint("question_id","answer_id"),)
Renderer.cpp
#pragma once
#include <GL/glew.h>
#define ASSERT(x) if (!(x)) __debugbreak();
#define GLCall(x) GLClearError();\
x;\
ASSERT(GLLogCall(#x,__FILE__,__LINE__))
void GLClearError();
bool GLLogCall(const char* function,const char* file,int line);
VertexBuffer.h
#include "Renderer.h"
#include <iostream>
void GLClearError()
{
while (glGetError() != GL_NO_ERROR);
}
bool GLLogCall(const char* function,int line)
{
while (GLenum error = glGetError())
{
std::cout << "[OpenGL Error] (" << error << "): " << function <<
" " << file << ": " << line << std::endl;
return false;
}
return true;
}
VertexBuffer.cpp
#pragma once
class VertexBuffer
{
private:
unsigned int m_RendererID;
public:
VertexBuffer(const void* data,unsigned int size);
~VertexBuffer();
void Bind() const;
void Unbind() const;
};
IndexBuffer.h
#include "VertexBuffer.h"
#include "Renderer.h"
VertexBuffer::VertexBuffer(const void* data,unsigned int size)
{
GLCall(glGenBuffers(1,&m_RendererID));
GLCall(glBindBuffer(GL_ARRAY_BUFFER,m_RendererID));
GLCall(glBufferData(GL_ARRAY_BUFFER,size,data,GL_STATIC_DRAW));
}
VertexBuffer::~VertexBuffer()
{
GLCall(glDeleteBuffers(1,&m_RendererID));
}
void VertexBuffer::Bind() const
{
GLCall(glBindBuffer(GL_ARRAY_BUFFER,m_RendererID));
}
void VertexBuffer::Unbind() const
{
GLCall(glBindBuffer(GL_ARRAY_BUFFER,0));
}
IndexBuffer.cpp
#pragma once
class IndexBuffer
{
private:
unsigned int m_RendererID;
unsigned int m_Count;
public:
IndexBuffer(const unsigned int* data,unsigned int count);
~IndexBuffer();
void Bind() const;
void Unbind() const;
inline unsigned int GetCount() const { return m_Count; }
};
Application.cpp
#include "IndexBuffer.h"
#include "Renderer.h"
IndexBuffer::IndexBuffer(const unsigned int* data,unsigned int count)
: m_Count(count)
{
ASSERT(sizeof(unsigned int) == sizeof(gluint));
GLCall(glGenBuffers(1,&m_RendererID));
GLCall(glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,m_RendererID));
GLCall(GL_ELEMENT_ARRAY_BUFFER,count * sizeof(unsigned int),GL_STATIC_DRAW);
}
IndexBuffer::~IndexBuffer()
{
GLCall(glDeleteBuffers(1,&m_RendererID));
}
void IndexBuffer::Bind() const
{
GLCall(glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,m_RendererID));
}
void IndexBuffer::Unbind() const
{
GLCall(glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,0));
}
Basic.shader
#pragma once
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include "Renderer.h"
#include "VertexBuffer.h"
#include "IndexBuffer.h"
struct ShaderProgramSource
{
std::string VertexSource;
std::string FragmentSource;
};
static ShaderProgramSource ParseShader(const std::string& filepath)
{
std::fstream stream(filepath);
enum class ShaderType
{
NONE = -1,VERTEX = 0,FRAGMENT = 1
};
std::string line;
std::stringstream ss[2];
ShaderType type = ShaderType::NONE;
while (getline(stream,line))
{
if (line.find("#shader") != std::string::npos)
{
if (line.find("vertex") != std::string::npos)
{
type = ShaderType::VERTEX;
}
else if (line.find("fragment") != std::string::npos)
{
type = ShaderType::FRAGMENT;
}
}
else
{
ss[(int)type] << line << '\n';
}
}
return { ss[0].str(),ss[1].str() };
}
static unsigned int CompileShader(unsigned int type,const std::string& source)
{
GLCall(unsigned int id = glCreateShader(type));
const char* src = source.c_str();
GLCall(glShaderSource(id,1,&src,nullptr));
GLCall(glCompileShader(id));
int result;
GLCall(glGetShaderiv(id,GL_COMPILE_STATUS,&result));
if (result == GL_FALSE)
{
int length;
GLCall(glGetShaderiv(id,GL_INFO_LOG_LENGTH,&length));
char* message = (char*)alloca(length * sizeof(char));
GLCall(glGetShaderInfoLog(id,length,&length,message));
std::cout << "Failed to compile " << (type == GL_VERTEX_SHADER ? "vertex" : "fragment") << " shader!" << std::endl;
std::cout << message << std::endl;
GLCall(glDeleteShader(id));
return 0;
}
return id;
}
static unsigned int CreateProgram(const std::string& vertexShader,const std::string& fragmentShader)
{
GLCall(unsigned int program = glCreateProgram());
unsigned int vs = CompileShader(GL_VERTEX_SHADER,vertexShader);
unsigned int fs = CompileShader(GL_FRAGMENT_SHADER,fragmentShader);
GLCall(glAttachShader(program,vs));
GLCall(glAttachShader(program,fs));
GLCall(glLinkProgram(program));
GLCall(glValidateProgram(program));
GLCall(glDeleteShader(vs));
GLCall(glDeleteShader(fs));
return program;
}
int main()
{
GLFWwindow* window;
if (!glfwInit())
{
return -1;
}
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR,3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MInor,3);
glfwWindowHint(GLFW_OPENGL_PROFILE,GLFW_OPENGL_CORE_PROFILE);
window = glfwCreateWindow(640,400,"Hello world",NULL,NULL);
if (!window)
{
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
glfwSwapInterval(1);
if (glewInit() != GLEW_OK)
{
std::cout << "Error while GLEW init!" << std::endl;
}
std::cout << glGetString(GL_VERSION) << std::endl;
{
float positions[] = {
-0.5f,-0.5f,// 0
0.5f,// 1
0.5f,0.5f,// 2
-0.5f,0.5f // 3
};
unsigned int indices[] = {
0,2,3,0
};
unsigned int vao;
GLCall(glGenVertexArrays(1,&vao));
GLCall(glBindVertexArray(vao));
VertexBuffer vb(positions,4 * 2 * sizeof(float));
GLCall(glEnabLevertexAttribArray(0));
GLCall(glVertexAttribPointer(0,GL_FLOAT,GL_FALSE,sizeof(float) * 2,0));
IndexBuffer ib(indices,6);
ShaderProgramSource source = ParseShader("res/shaders/Basic.shader");
unsigned int program = CreateProgram(source.VertexSource,source.FragmentSource);
GLCall(gluseProgram(program));
GLCall(int location = glGetUniformlocation(program,"u_Color"));
ASSERT(location != -1);
GLCall(gluniform4f(location,0.8f,0.3f,1.0f));
GLCall(glBindVertexArray(0));
GLCall(gluseProgram(0));
vb.Unbind();
ib.Unbind();
float r = 0;
float increment = 0.05f;
while (!glfwWindowShouldClose(window))
{
GLCall(glClear(GL_COLOR_BUFFER_BIT));
glClearColor(1,1);
GLCall(gluseProgram(program));
GLCall(gluniform4f(location,r,1.0f));
GLCall(glBindVertexArray(vao));
ib.Bind();
// glDrawArrays(GL_TRIANGLES,3);
GLCall(glDrawElements(GL_TRIANGLES,6,GL_UNSIGNED_INT,nullptr));
if (r > 1.0f)
{
increment = -0.05f;
}
else if (r < 0.0f)
{
increment = 0.05f;
}
r += increment;
glfwSwapBuffers(window);
glfwPollEvents();
}
GLCall(glDeleteProgram(program));
}
glfwTerminate();
return 0;
}
解决方法
这看起来不正确。缺少glBufferData吗?
GLCall(GL_ELEMENT_ARRAY_BUFFER,count * sizeof(unsigned int),data,GL_STATIC_DRAW);