refactor out boring code

This commit is contained in:
Artur Tamborski 2021-02-10 00:36:43 +01:00
parent 008e811958
commit da3f2ec685

View File

@ -13,6 +13,8 @@
#include <iostream> #include <iostream>
GLFWwindow *initialize_glfw_window(void);
void framebuffer_size_callback(GLFWwindow* window, int width, int height); void framebuffer_size_callback(GLFWwindow* window, int width, int height);
void mouse_callback(GLFWwindow* window, double xpos, double ypos); void mouse_callback(GLFWwindow* window, double xpos, double ypos);
void scroll_callback(GLFWwindow* window, double xoffset, double yoffset); void scroll_callback(GLFWwindow* window, double xoffset, double yoffset);
@ -36,57 +38,14 @@ float lastFrame = 0.0f;
int main() int main()
{ {
// glfw: initialize and configure if ((GLFWwindow *window = initialize_glfw_window()) == nullptr)
// ------------------------------
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
#ifdef __APPLE__
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
#endif
// glfw window creation
// --------------------
GLFWwindow* window = glfwCreateWindow(SCR_WIDTH, SCR_HEIGHT, "LearnOpenGL", NULL, NULL);
if (window == NULL)
{
std::cout << "Failed to create GLFW window" << std::endl;
glfwTerminate();
return -1; return -1;
}
glfwMakeContextCurrent(window);
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
glfwSetCursorPosCallback(window, mouse_callback);
glfwSetScrollCallback(window, scroll_callback);
// tell GLFW to capture our mouse // load objects and shaders
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED); Shader kniedeShader("9.2.geometry_shader.vert", "9.2.geometry_shader.frag");
Model kniedeModel("../resources/objects/kniede/kniede.obj");
// glad: load all OpenGL function pointers Shader cubemapShader("6.2.cubemaps.vert", "6.2.cubemaps.frag");
// ---------------------------------------
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
{
std::cout << "Failed to initialize GLAD" << std::endl;
return -1;
}
// configure global opengl state
// -----------------------------
glEnable(GL_DEPTH_TEST);
// build and compile shaders
// -------------------------
Shader nanosuitShader("9.2.geometry_shader.vert", "9.2.geometry_shader.frag");
//Model nanosuitModel("../resources/objects/nanosuit/nanosuit.obj");
Model nanosuitModel("../resources/objects/kniede/kniede.obj");
// build and compile shaders
// -------------------------
Shader shader("6.2.cubemaps.vert", "6.2.cubemaps.frag");
Shader skyboxShader("6.2.skybox.vert", "6.2.skybox.frag"); Shader skyboxShader("6.2.skybox.vert", "6.2.skybox.frag");
// set up vertex data (and buffer(s)) and configure vertex attributes // set up vertex data (and buffer(s)) and configure vertex attributes
@ -191,6 +150,7 @@ int main()
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)0); glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)0);
glEnableVertexAttribArray(1); glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)(3 * sizeof(float))); glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)(3 * sizeof(float)));
// skybox VAO // skybox VAO
unsigned int skyboxVAO, skyboxVBO; unsigned int skyboxVAO, skyboxVBO;
glGenVertexArrays(1, &skyboxVAO); glGenVertexArrays(1, &skyboxVAO);
@ -219,8 +179,8 @@ int main()
// shader configuration // shader configuration
// -------------------- // --------------------
shader.use(); cubemapShader.use();
shader.setInt("skybox", 0); cubemapShader.setInt("skybox", 0);
skyboxShader.use(); skyboxShader.use();
skyboxShader.setInt("skybox", 0); skyboxShader.setInt("skybox", 0);
@ -234,39 +194,34 @@ int main()
float currentFrame = glfwGetTime(); float currentFrame = glfwGetTime();
deltaTime = currentFrame - lastFrame; deltaTime = currentFrame - lastFrame;
lastFrame = currentFrame; lastFrame = currentFrame;
// input
// -----
processInput(window); processInput(window);
// render
// ------
glClearColor(0.1f, 0.1f, 0.1f, 1.0f); glClearColor(0.1f, 0.1f, 0.1f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// draw scene as normal // draw scene as normal
glm::mat4 model = glm::mat4(1.0f);
glm::mat4 view = camera.GetViewMatrix(); glm::mat4 view = camera.GetViewMatrix();
glm::mat4 projection = glm::perspective(glm::radians(camera.Zoom), (float)SCR_WIDTH / (float)SCR_HEIGHT, 0.1f, 100.0f); glm::mat4 projection = glm::perspective(glm::radians(camera.Zoom), (float)SCR_WIDTH / (float)SCR_HEIGHT, 0.1f, 100.0f);
glm::mat4 model = glm::mat4(1.0f);
// model = glm::translate(model, glm::vec3(0.0f, 0.0f, 0.0f)); // translate it down so it's at the center of the scene // model = glm::translate(model, glm::vec3(0.0f, 0.0f, 0.0f)); // translate it down so it's at the center of the scene
// model = glm::scale(model, glm::vec3(1.0f, 1.0f, 1.0f)); // it's a bit too big for our scene, so scale it down // model = glm::scale(model, glm::vec3(1.0f, 1.0f, 1.0f)); // it's a bit too big for our scene, so scale it down
model = glm::rotate(model, glm::radians(-90.0f), glm::vec3(1.0f, 0.0f, 0.0f)); model = glm::rotate(model, glm::radians(-90.0f), glm::vec3(1.0f, 0.0f, 0.0f));
nanosuitShader.use(); kniedeShader.use();
nanosuitShader.setMat4("projection", projection); kniedeShader.setMat4("model", model);
nanosuitShader.setMat4("view", view); kniedeShader.setMat4("view", view);
nanosuitShader.setMat4("model", model); kniedeShader.setMat4("projection", projection);
nanosuitModel.Draw(nanosuitShader); kniedeModel.Draw(kniedeShader);
// draw scene as normal // draw scene as normal
shader.use();
model = glm::mat4(1.0f); model = glm::mat4(1.0f);
view = camera.GetViewMatrix(); cubemapShader.use();
projection = glm::perspective(glm::radians(camera.Zoom), (float)SCR_WIDTH / (float)SCR_HEIGHT, 0.1f, 100.0f); cubemapShader.setMat4("model", model);
shader.setMat4("model", model); cubemapShader.setMat4("view", view);
shader.setMat4("view", view); cubemapShader.setMat4("projection", projection);
shader.setMat4("projection", projection); cubemapShader.setVec3("cameraPos", camera.Position);
shader.setVec3("cameraPos", camera.Position);
// cubes // cubes
glBindVertexArray(cubeVAO); glBindVertexArray(cubeVAO);
glActiveTexture(GL_TEXTURE0); glActiveTexture(GL_TEXTURE0);
@ -280,6 +235,7 @@ int main()
view = glm::mat4(glm::mat3(camera.GetViewMatrix())); // remove translation from the view matrix view = glm::mat4(glm::mat3(camera.GetViewMatrix())); // remove translation from the view matrix
skyboxShader.setMat4("view", view); skyboxShader.setMat4("view", view);
skyboxShader.setMat4("projection", projection); skyboxShader.setMat4("projection", projection);
// skybox cube // skybox cube
glBindVertexArray(skyboxVAO); glBindVertexArray(skyboxVAO);
glActiveTexture(GL_TEXTURE0); glActiveTexture(GL_TEXTURE0);
@ -294,17 +250,58 @@ int main()
glfwPollEvents(); glfwPollEvents();
} }
// optional: de-allocate all resources once they've outlived their purpose:
// ------------------------------------------------------------------------
glDeleteVertexArrays(1, &cubeVAO);
glDeleteVertexArrays(1, &skyboxVAO);
glDeleteBuffers(1, &cubeVBO);
glDeleteBuffers(1, &skyboxVAO);
glfwTerminate(); glfwTerminate();
return 0; return 0;
} }
// initialize GLFW window and OpenGL context with glew
// ---------------------------------------------------------------------------------------------------------
GLFWwindow *initialize_glfw_window(void)
{
// glfw: initialize and configure
// ------------------------------
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
#ifdef __APPLE__
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
#endif
// glfw window creation
// --------------------
GLFWwindow* window = glfwCreateWindow(SCR_WIDTH, SCR_HEIGHT, "LearnOpenGL", NULL, NULL);
if (window == NULL)
{
std::cout << "Failed to create GLFW window" << std::endl;
glfwTerminate();
return nullptr;
}
glfwMakeContextCurrent(window);
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
glfwSetCursorPosCallback(window, mouse_callback);
glfwSetScrollCallback(window, scroll_callback);
// tell GLFW to capture our mouse
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
// glad: load all OpenGL function pointers
// ---------------------------------------
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
{
std::cout << "Failed to initialize GLAD" << std::endl;
return nullptr;
}
// configure global opengl state
// -----------------------------
glEnable(GL_DEPTH_TEST);
return window;
}
// process all input: query GLFW whether relevant keys are pressed/released this frame and react accordingly // process all input: query GLFW whether relevant keys are pressed/released this frame and react accordingly
// --------------------------------------------------------------------------------------------------------- // ---------------------------------------------------------------------------------------------------------
void processInput(GLFWwindow *window) void processInput(GLFWwindow *window)