ErrorUtil.h
cpp
#pragma once
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <iostream>
struct ErrorUtil {
static void printShaderLog (GLuint shader);
static void printProgramLog (int prog);
static bool checkOpenGLError ();
};
namespace UsageExample {
using namespace std;
constexpr int numVAOs = 1;
GLuint renderingProgram;
GLuint vao[numVAOs];
// 检测OpenGL错误的示例如下:
GLuint
createShaderProgram ()
{
GLint vertCompiled;
GLint fragCompiled;
GLint linked;
const char *vshaderSource = "#version 430 \n"
"void main(void) \n"
"{ gl_Position = vec4(0.0, 0.0, 0.0, 1.0); }";
const char *fshaderSource = "#version 430 \n"
"out vec4 color; \n"
"void main(void) \n"
"{ color = vec4(0.0, 0.0, 1.0, 1.0); }";
GLuint vShader = glCreateShader (GL_VERTEX_SHADER);
GLuint fShader = glCreateShader (GL_FRAGMENT_SHADER);
GLuint vfprogram = glCreateProgram();
glShaderSource (vShader, 1, &vshaderSource, NULL);
glShaderSource (fShader, 1, &fshaderSource, NULL);
//捕获编译着色器时的错误
glCompileShader (vShader);
ErrorUtil::checkOpenGLError();
glGetShaderiv (vShader, GL_COMPILE_STATUS, &vertCompiled);
if (vertCompiled == 1) {
cout << "vertex compilation success" << endl;
} else {
cout << "vertex compilation failed" << endl;
ErrorUtil::printShaderLog (vShader);
}
glCompileShader (fShader);
ErrorUtil::checkOpenGLError();
glGetShaderiv (fShader, GL_COMPILE_STATUS, &fragCompiled);
if (fragCompiled == 1) {
cout << "fragment compilation success" << endl;
} else {
cout << "fragment compilation failed" << endl;
ErrorUtil::printShaderLog (fShader);
}
//捕获链接着色器时的错误
glAttachShader (vfprogram, vShader);
glAttachShader (vfprogram, fShader);
glLinkProgram (vfprogram);
glLinkProgram (vfprogram);
ErrorUtil::checkOpenGLError();
glGetProgramiv (vfprogram, GL_LINK_STATUS, &linked);
if (linked == 1) {
cout << "linking succeeded" << endl;
} else {
cout << "linking failed" << endl;
ErrorUtil::printProgramLog (vfprogram);
}
return vfprogram;
}
void
init (GLFWwindow *window)
{
renderingProgram = createShaderProgram();
glGenVertexArrays (numVAOs, vao);
glBindVertexArray (vao[0]);
}
void
display (GLFWwindow *window, double currentTime)
{
glUseProgram (renderingProgram);
glPointSize (30.0f);
glDrawArrays (GL_POINTS, 0, 1);
}
int
main (void)
{
if (!glfwInit()) {
exit (EXIT_FAILURE);
}
glfwWindowHint (GLFW_CONTEXT_VERSION_MAJOR, 4);
glfwWindowHint (GLFW_CONTEXT_VERSION_MINOR, 3);
GLFWwindow *window = glfwCreateWindow (600, 600, "Chapter 2 - program 3", NULL, NULL);
glfwMakeContextCurrent (window);
if (glewInit() != GLEW_OK) {
exit (EXIT_FAILURE);
}
glfwSwapInterval (1);
init (window);
while (!glfwWindowShouldClose (window)) {
display (window, glfwGetTime());
glfwSwapBuffers (window);
glfwPollEvents();
}
glfwDestroyWindow (window);
glfwTerminate();
exit (EXIT_SUCCESS);
}
} // namespace UsageExample
ErrorUtil.cpp
cpp
#include "ErrorUtil.h"
void
ErrorUtil::printShaderLog (GLuint shader)
{
int len = 0;
int chWrittn = 0;
char *log;
glGetShaderiv (shader, GL_INFO_LOG_LENGTH, &len);
if (len > 0) {
log = (char *)malloc (len);
glGetShaderInfoLog (shader, len, &chWrittn, log);
std::cout << "Shader Info Log: " << log << std::endl;
free (log);
}
}
void
ErrorUtil::printProgramLog (int prog)
{
int len = 0;
int chWrittn = 0;
char *log;
glGetProgramiv (prog, GL_INFO_LOG_LENGTH, &len);
if (len > 0) {
log = (char *)malloc (len);
glGetProgramInfoLog (prog, len, &chWrittn, log);
std::cout << "Program Info Log: " << log << std::endl;
free (log);
}
}
bool
ErrorUtil::checkOpenGLError()
{
bool foundError = false;
int glErr = glGetError();
while (glErr != GL_NO_ERROR) {
std::cout << "glError: " << glErr << std::endl;
foundError = true;
glErr = glGetError();
}
return foundError;
}