mirror of
https://github.com/JoeyDeVries/LearnOpenGL.git
synced 2026-01-02 04:37:54 +08:00
Added source code for debugging tutorials; new code_viewer directly loads from GitHub.
This commit is contained in:
@@ -58,6 +58,7 @@ set(CHAPTERS
|
||||
3.model_loading
|
||||
4.advanced_opengl
|
||||
5.advanced_lighting
|
||||
6.in_practice
|
||||
)
|
||||
|
||||
set(1.getting_started
|
||||
@@ -110,6 +111,12 @@ set(5.advanced_lighting
|
||||
9.ssao
|
||||
)
|
||||
|
||||
set(6.in_practice
|
||||
1.debugging
|
||||
2.text_rendering
|
||||
)
|
||||
|
||||
|
||||
|
||||
configure_file(configuration/root_directory.h.in configuration/root_directory.h)
|
||||
include_directories(${CMAKE_BINARY_DIR}/configuration)
|
||||
|
||||
246
src/6.in_practice/1.debugging/debugging.cpp
Normal file
246
src/6.in_practice/1.debugging/debugging.cpp
Normal file
@@ -0,0 +1,246 @@
|
||||
#define GLEW_STATIC
|
||||
#include <GL/glew.h>
|
||||
#include <GLFW/glfw3.h>
|
||||
#include <glm/glm.hpp>
|
||||
#include <glm/gtc/matrix_transform.hpp>
|
||||
#include <glm/gtc/type_ptr.hpp>
|
||||
#include <SOIL.h>
|
||||
|
||||
#include <learnopengl/filesystem.h>
|
||||
#include <learnopengl/shader.h>
|
||||
|
||||
// Function prototypes
|
||||
void key_callback(GLFWwindow* window, int key, int scancode, int action, int mode);
|
||||
|
||||
// Properties
|
||||
const GLuint SCR_WIDTH = 800, SCR_HEIGHT = 600;
|
||||
|
||||
|
||||
GLenum glCheckError_(const char *file, int line)
|
||||
{
|
||||
GLenum errorCode;
|
||||
while ((errorCode = glGetError()) != GL_NO_ERROR)
|
||||
{
|
||||
std::string error;
|
||||
switch (errorCode)
|
||||
{
|
||||
case GL_INVALID_ENUM: error = "INVALID_ENUM"; break;
|
||||
case GL_INVALID_VALUE: error = "INVALID_VALUE"; break;
|
||||
case GL_INVALID_OPERATION: error = "INVALID_OPERATION"; break;
|
||||
case GL_STACK_OVERFLOW: error = "STACK_OVERFLOW"; break;
|
||||
case GL_STACK_UNDERFLOW: error = "STACK_UNDERFLOW"; break;
|
||||
case GL_OUT_OF_MEMORY: error = "OUT_OF_MEMORY"; break;
|
||||
case GL_INVALID_FRAMEBUFFER_OPERATION: error = "INVALID_FRAMEBUFFER_OPERATION"; break;
|
||||
}
|
||||
std::cout << error << " | " << file << " (" << line << ")" << std::endl;
|
||||
}
|
||||
return errorCode;
|
||||
}
|
||||
#define glCheckError() glCheckError_(__FILE__, __LINE__)
|
||||
|
||||
void APIENTRY glDebugOutput(GLenum source,
|
||||
GLenum type,
|
||||
GLuint id,
|
||||
GLenum severity,
|
||||
GLsizei length,
|
||||
const GLchar *message,
|
||||
void *userParam)
|
||||
{
|
||||
if(id == 131169 || id == 131185 || id == 131218) return; // ignore these non-significant error codes
|
||||
|
||||
std::cout << "---------------" << std::endl;
|
||||
std::cout << "Debug message (" << id << "): " << message << std::endl;
|
||||
|
||||
switch (source)
|
||||
{
|
||||
case GL_DEBUG_SOURCE_API: std::cout << "Source: API"; break;
|
||||
case GL_DEBUG_SOURCE_WINDOW_SYSTEM: std::cout << "Source: Window System"; break;
|
||||
case GL_DEBUG_SOURCE_SHADER_COMPILER: std::cout << "Source: Shader Compiler"; break;
|
||||
case GL_DEBUG_SOURCE_THIRD_PARTY: std::cout << "Source: Third Party"; break;
|
||||
case GL_DEBUG_SOURCE_APPLICATION: std::cout << "Source: Application"; break;
|
||||
case GL_DEBUG_SOURCE_OTHER: std::cout << "Source: Other"; break;
|
||||
} std::cout << std::endl;
|
||||
|
||||
switch (type)
|
||||
{
|
||||
case GL_DEBUG_TYPE_ERROR: std::cout << "Type: Error"; break;
|
||||
case GL_DEBUG_TYPE_DEPRECATED_BEHAVIOR: std::cout << "Type: Deprecated Behaviour"; break;
|
||||
case GL_DEBUG_TYPE_UNDEFINED_BEHAVIOR: std::cout << "Type: Undefined Behaviour"; break;
|
||||
case GL_DEBUG_TYPE_PORTABILITY: std::cout << "Type: Portability"; break;
|
||||
case GL_DEBUG_TYPE_PERFORMANCE: std::cout << "Type: Performance"; break;
|
||||
case GL_DEBUG_TYPE_MARKER: std::cout << "Type: Marker"; break;
|
||||
case GL_DEBUG_TYPE_PUSH_GROUP: std::cout << "Type: Push Group"; break;
|
||||
case GL_DEBUG_TYPE_POP_GROUP: std::cout << "Type: Pop Group"; break;
|
||||
case GL_DEBUG_TYPE_OTHER: std::cout << "Type: Other"; break;
|
||||
} std::cout << std::endl;
|
||||
|
||||
switch (severity)
|
||||
{
|
||||
case GL_DEBUG_SEVERITY_HIGH: std::cout << "Severity: high"; break;
|
||||
case GL_DEBUG_SEVERITY_MEDIUM: std::cout << "Severity: medium"; break;
|
||||
case GL_DEBUG_SEVERITY_LOW: std::cout << "Severity: low"; break;
|
||||
case GL_DEBUG_SEVERITY_NOTIFICATION: std::cout << "Severity: notification"; break;
|
||||
} std::cout << std::endl;
|
||||
std::cout << std::endl;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
// GLFW initialization
|
||||
glfwInit();
|
||||
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
|
||||
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
|
||||
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
|
||||
glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);
|
||||
glfwWindowHint(GLFW_OPENGL_DEBUG_CONTEXT, GL_TRUE); // comment this line in a release build!
|
||||
GLFWwindow* window = glfwCreateWindow(SCR_WIDTH, SCR_HEIGHT, "LearnOpenGL", nullptr, nullptr); // Windowed
|
||||
glfwMakeContextCurrent(window);
|
||||
|
||||
// GLFW callback setup
|
||||
glfwSetKeyCallback(window, key_callback);
|
||||
|
||||
// GLEW initialization
|
||||
glewExperimental = GL_TRUE;
|
||||
glewInit();
|
||||
// Due to a bug in GLEW the glewInit call always generates an OpenGL error; clear the flag(s) by calling glGetError();
|
||||
glGetError();
|
||||
|
||||
// enable OpenGL debug context if context allows for debug context
|
||||
GLint flags; glGetIntegerv(GL_CONTEXT_FLAGS, &flags);
|
||||
if (flags & GL_CONTEXT_FLAG_DEBUG_BIT)
|
||||
{
|
||||
glEnable(GL_DEBUG_OUTPUT);
|
||||
glEnable(GL_DEBUG_OUTPUT_SYNCHRONOUS); // makes sure errors are displayed synchronously
|
||||
glDebugMessageCallback(glDebugOutput, nullptr);
|
||||
glDebugMessageControl(GL_DONT_CARE, GL_DONT_CARE, GL_DONT_CARE, 0, nullptr, GL_TRUE);
|
||||
}
|
||||
|
||||
// OpenGL initialization
|
||||
int width, height;
|
||||
glfwGetFramebufferSize(window, &width, &height); // ensures we get proper viewport dimensions on high DPI devices
|
||||
glViewport(0, 0, width, height);
|
||||
glEnable(GL_DEPTH_TEST);
|
||||
glEnable(GL_CULL_FACE);
|
||||
|
||||
|
||||
// OpenGL initial state
|
||||
Shader shader("debugging.vs", "debugging.frag");
|
||||
|
||||
// Configure 3D cube
|
||||
GLuint cubeVAO, cubeVBO;
|
||||
GLfloat vertices[] = {
|
||||
// Back face
|
||||
-0.5f, -0.5f, -0.5f, // Bottom-left
|
||||
0.5f, 0.5f, -0.5f, // top-right
|
||||
0.5f, -0.5f, -0.5f, // bottom-right
|
||||
0.5f, 0.5f, -0.5f, // top-right
|
||||
-0.5f, -0.5f, -0.5f, // bottom-left
|
||||
-0.5f, 0.5f, -0.5f, // top-left
|
||||
// Front face
|
||||
-0.5f, -0.5f, 0.5f, // bottom-left
|
||||
0.5f, -0.5f, 0.5f, // bottom-right
|
||||
0.5f, 0.5f, 0.5f, // top-right
|
||||
0.5f, 0.5f, 0.5f, // top-right
|
||||
-0.5f, 0.5f, 0.5f, // top-left
|
||||
-0.5f, -0.5f, 0.5f, // bottom-left
|
||||
// Left face
|
||||
-0.5f, 0.5f, 0.5f, // top-right
|
||||
-0.5f, 0.5f, -0.5f, // top-left
|
||||
-0.5f, -0.5f, -0.5f, // bottom-left
|
||||
-0.5f, -0.5f, -0.5f, // bottom-left
|
||||
-0.5f, -0.5f, 0.5f, // bottom-right
|
||||
-0.5f, 0.5f, 0.5f, // top-right
|
||||
// Right face
|
||||
0.5f, 0.5f, 0.5f, // top-left
|
||||
0.5f, -0.5f, -0.5f, // bottom-right
|
||||
0.5f, 0.5f, -0.5f, // top-right
|
||||
0.5f, -0.5f, -0.5f, // bottom-right
|
||||
0.5f, 0.5f, 0.5f, // top-left
|
||||
0.5f, -0.5f, 0.5f, // bottom-left
|
||||
// Bottom face
|
||||
-0.5f, -0.5f, -0.5f, // top-right
|
||||
0.5f, -0.5f, -0.5f, // top-left
|
||||
0.5f, -0.5f, 0.5f, // bottom-left
|
||||
0.5f, -0.5f, 0.5f, // bottom-left
|
||||
-0.5f, -0.5f, 0.5f, // bottom-right
|
||||
-0.5f, -0.5f, -0.5f, // top-right
|
||||
// Top face
|
||||
-0.5f, 0.5f, -0.5f, // top-left
|
||||
0.5f, 0.5f, 0.5f, // bottom-right
|
||||
0.5f, 0.5f, -0.5f, // top-right
|
||||
0.5f, 0.5f, 0.5f, // bottom-right
|
||||
-0.5f, 0.5f, -0.5f, // top-left
|
||||
-0.5f, 0.5f, 0.5f // bottom-left
|
||||
};
|
||||
glGenVertexArrays(1, &cubeVAO);
|
||||
glGenBuffers(1, &cubeVBO);
|
||||
// Fill buffer
|
||||
glBindBuffer(GL_ARRAY_BUFFER, cubeVBO);
|
||||
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
|
||||
// Link vertex attributes
|
||||
glBindVertexArray(cubeVAO);
|
||||
glEnableVertexAttribArray(0);
|
||||
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), (GLvoid*)0);
|
||||
glEnableVertexAttribArray(1);
|
||||
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), (GLvoid*)(3 * sizeof(GLfloat)));
|
||||
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
||||
glBindVertexArray(0);
|
||||
|
||||
// Load cube texture
|
||||
GLuint textureID;
|
||||
glGenTextures(1, &textureID);
|
||||
int texWidth, texHeight;
|
||||
unsigned char *image = SOIL_load_image(FileSystem::getPath("resources/textures/wood.png").c_str(), &texWidth, &texHeight, 0, SOIL_LOAD_RGB);
|
||||
|
||||
glBindTexture(GL_TEXTURE_2D, textureID);
|
||||
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, texWidth, texHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, image);
|
||||
glGenerateMipmap(GL_TEXTURE_2D);
|
||||
|
||||
glTexParameteri(GL_FRAMEBUFFER, GL_TEXTURE_WRAP_S, GL_REPEAT);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
||||
glBindTexture(GL_TEXTURE_2D, 0);
|
||||
SOIL_free_image_data(image);
|
||||
|
||||
// Set up projection matrix
|
||||
// TODO(Joey): check with new version of GLM and then use glm::radians
|
||||
glm::mat4 projection = glm::perspective(45.0f, (float)SCR_WIDTH / (float)SCR_HEIGHT, 0.1f, 10.0f);
|
||||
shader.Use(); // use before setting uniforms
|
||||
glUniformMatrix4fv(glGetUniformLocation(shader.Program, "projection"), 1, GL_FALSE, glm::value_ptr(projection));
|
||||
|
||||
glCheckError();
|
||||
|
||||
glClearColor(0.1f, 0.1f, 0.1f, 1.0f);
|
||||
while (!glfwWindowShouldClose(window))
|
||||
{
|
||||
// Check and call events
|
||||
glfwPollEvents();
|
||||
|
||||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||
|
||||
GLfloat rotationSpeed = 10.0f;
|
||||
GLfloat angle = (float)glfwGetTime() * rotationSpeed;
|
||||
glm::mat4 model;
|
||||
model = glm::translate(model, glm::vec3(0.0, 0.0f, -2.5));
|
||||
model = glm::rotate(model, angle, glm::vec3(1.0f, 1.0f, 1.0f));
|
||||
glUniformMatrix4fv(glGetUniformLocation(shader.Program, "model"), 1, GL_FALSE, glm::value_ptr(model));
|
||||
|
||||
glBindVertexArray(cubeVAO);
|
||||
glDrawArrays(GL_TRIANGLES, 0, 36);
|
||||
glBindVertexArray(0);
|
||||
|
||||
// Swap the buffers
|
||||
glfwSwapBuffers(window);
|
||||
}
|
||||
|
||||
glfwTerminate();
|
||||
return 0;
|
||||
}
|
||||
|
||||
// is called whenever a key is pressed/released via GLFW
|
||||
void key_callback(GLFWwindow* window, int key, int scancode, int action, int mode)
|
||||
{
|
||||
if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
|
||||
glfwSetWindowShouldClose(window, GL_TRUE);
|
||||
}
|
||||
7
src/6.in_practice/1.debugging/debugging.frag
Normal file
7
src/6.in_practice/1.debugging/debugging.frag
Normal file
@@ -0,0 +1,7 @@
|
||||
#version 330 core
|
||||
out vec4 FragColor;
|
||||
|
||||
void main()
|
||||
{
|
||||
FragColor = vec4(1.0f, 0.0f, 0.0f, 1.0f);
|
||||
}
|
||||
10
src/6.in_practice/1.debugging/debugging.vs
Normal file
10
src/6.in_practice/1.debugging/debugging.vs
Normal file
@@ -0,0 +1,10 @@
|
||||
#version 330 core
|
||||
layout (location = 0) in vec3 position;
|
||||
|
||||
uniform mat4 projection;
|
||||
uniform mat4 model;
|
||||
|
||||
void main()
|
||||
{
|
||||
gl_Position = projection * model * vec4(position, 1.0f);
|
||||
}
|
||||
Reference in New Issue
Block a user