在写opengl函数过程中,容易出现编译通过了,但是出来的窗口确实黑的,或者跟预期的不一样,那么就说明应该是某个函数出错了,其中默认类型转换就是最容易出现的错误,比如 GL_UNSIGNED_INT 被 错误的写成了 CL_INT,这样就会导致整个函数失效了。
可以通过宏定义来解决这个问题,就是通过定义一个宏函数,然后将每个gl函数都用这个宏函数调用,这样可以实现对每个函数进行检测是否出现了错误,并且在控制台打印出错的具体位置。另外,如果在调试模式下(F5),会直接在出错的地方break,能够直接定位到错误的函数。
代码
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#define ASSERT(x) if(!(x)) __debugbreak();
#define GLCall(x) GLClearError();\
x;\
ASSERT(GLLogCall(#x, __FILE__, __LINE__)) //对每个gl函数x,GLCall(x),可以在出错的地方break并且打印出错具体位置。注意:在 \ 后不能出现空格字符
static void GLClearError()
{
while (glGetError() != GL_NO_ERROR); //用于清空之前的error
}
static bool GLLogCall(const char* function, const char* file, int line)
{
while (GLenum error = glGetError()) //改成 if 也可以吧,有什么区别吗
{
std::cout << "[OpenGL Error] (" << error << "): " << function <<
" " << file << ":" << line << std::endl;
return false;
}
return true;
}
struct ShaderProgramSource
{
std::string VertexSource;
std::string FragmentSouce;
};
static ShaderProgramSource ParseShader(const std::string &filepath)
{
std::ifstream 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 CompiledShader(unsigned int type, const std::string &source)
{
unsigned int id = glCreateShader(type); //创建一个着色器
const char* src = source.c_str();
glShaderSource(id, 1, &src, nullptr);
glCompileShader(id); //编译着色器里的程序代码
/*这是一些错误处理*/
int result;
glGetShaderiv(id, GL_COMPILE_STATUS, &result);
if (result == GL_FALSE)
{
int length;
glGetShaderiv(id, GL_INFO_LOG_LENGTH, &length);
char *message = (char*)alloca(length * sizeof(char));
glGetShaderInfoLog(id, length, &length, message);
std::cout << "Failed to compile " << (type == GL_VERTEX_SHADER ? "vertex" : "fragment") << " shader" << std::endl;
std::cout << message << std::endl;
glDeleteShader(id);
return 0;
}
return id;
}
static unsigned int CreateShader(const std::string &vertexShader, const std::string& fragmentShader)
{
unsigned int program = glCreateProgram();
unsigned int vs = CompiledShader(GL_VERTEX_SHADER, vertexShader);
unsigned int fs = CompiledShader(GL_FRAGMENT_SHADER, fragmentShader);
//把两个编译好的着色器连接到同一个程序里面
glAttachShader(program, vs);
glAttachShader(program, fs);
//链接程序(把程序放到显卡上?)
glLinkProgram(program);
//验证程序(非必要?)
//glValidateProgram(program);
glDeleteShader(vs);
glDeleteShader(fs);
return program;
}
int main(void)
{
glewInit();
/* Initialize the library */
if (!glfwInit())
return -1;
GLFWwindow* window;
/* Create a windowed mode window and its OpenGL context */
window = glfwCreateWindow(640, 480, "Hello World", NULL, NULL);
/*if (!window)
{
glfwTerminate();
return -1;
}*/
/* Make the window's context current */
glfwMakeContextCurrent(window);
if (glewInit() != GLEW_OK)
std::cout << "ERROR!" << std::endl;
std::cout << glGetString(GL_VERSION) << std::endl;
float positions[] = {
-0.5f, -0.5f, // 0
0.5f, -0.5f, // 1
0.5f, 0.5f, // 2
-0.5f, 0.5f, // 3
};
unsigned int indices[] = {
0, 1, 2,
2, 3, 0
};
unsigned int buffer;
glGenBuffers(1, &buffer); //创建一个缓存
glBindBuffer(GL_ARRAY_BUFFER, buffer); //绑定缓存
glBufferData(GL_ARRAY_BUFFER, 8 * sizeof(float), positions, GL_STATIC_DRAW); //为缓存填充数据
/*接下来启用顶点着色器*/
glEnableVertexAttribArray(0); //设置顶点着色器属性为坐标位置
//参数分别为坐标属性的索引,每个顶点有2个元素,元素的类型,不需要normalized,步长(每个顶点元素的内存大小),指针的偏移量
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(float) * 2, 0);
unsigned int ibo;
glGenBuffers(1, &ibo); //创建一个缓存
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo); //绑定缓存
glBufferData(GL_ELEMENT_ARRAY_BUFFER, 6 * sizeof(unsigned int), indices, GL_STATIC_DRAW); //为缓存填充数据
ShaderProgramSource source = ParseShader("basic.shader");
/*std::cout << "VERTEX" << std::endl;
std::cout << source.VertexSource << std::endl;
std::cout << "FRAGMENT" << std::endl;
std::cout << source.FragmentSouce << std::endl;*/
unsigned int shader = CreateShader(source.VertexSource, source.FragmentSouce);
glUseProgram(shader);
/* Loop until the user closes the window */
while (!glfwWindowShouldClose(window))
{
/* Render here */
glClear(GL_COLOR_BUFFER_BIT);
GLCall(glDrawElements(GL_TRIANGLES, 6, GL_INT, nullptr)); // !!!这里故意把GL_UNSIGNED_INT写成了GL_INT用来测试宏函数 GLCall(x)
/* Swap front and back buffers */
glfwSwapBuffers(window);
/* Poll for and process events */
glfwPollEvents();
}
glDeleteProgram(shader);
glfwTerminate();
return 0;
}
结果