OpenGL——检测OpenGL和GLSL错误

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;
}
相关推荐
aworkholic6 分钟前
opencv sdk for java中提示无stiching模块接口的问题
java·c++·opencv·jni·opencv4android·stiching
字节程序员10 分钟前
四种自动化测试模型实例及优缺点详解
开发语言·javascript·ecmascript·集成测试·压力测试
程序员老冯头11 分钟前
第十六章 C++ 字符串
开发语言·c++
thesky12345613 分钟前
活着就好20241225
学习·算法
Xenia22321 分钟前
复习篇~第二章程序设计基础
c++·算法
灵槐梦27 分钟前
【速成51单片机】2.点亮LED
c语言·开发语言·经验分享·笔记·单片机·51单片机
想睡觉 . 我也想睡觉 .28 分钟前
【C++算法】1.【模板】前缀和
开发语言·c++·算法
mit6.82431 分钟前
[数据结构] LRU Cache | List&Map 实现
算法
---wzy---37 分钟前
我的JAVA-Web基础(2)
java·开发语言
伟大无须多言1 小时前
企业资源规划系统(ERP)服务器上线项目实施指南
开发语言·php