1、To get the application in order:
To have the ability for us to have different tests or different scenes then we can cycle through, each one of those tests or scenes is meant to demonstrate or just show one concept working, the way we’re gonna set that up is very simple, we’re just gonna have a menu in ImGui that shows all of the available scenes or tests, we’re gonna be able to pick one and when we click it, it opens the scene and we can see all the scene, then we can click “return” button to go back to that menu, it basically just destroy that scene, so every time we go into a new scene it loads everything required for that scene and when we exit the scene, it destroys everything.
2、代码实现:
src目录下新增一个tests文件夹。
新增加的Test.h
文件。
右键OpenGL属性,增加附加包含目录:src。
新增加的TestClearColor.h和TestClearColor.cpp
文件。
The way we set the Test class like this is bc we want all these Test classes to be initialized or created when we enter the test and then destroyed when we exit the test, so the creation and destruction code will be in the constructor and destructor, this way we can freely have any kind of stack variables that are tied to the lifetime of our class.
最后,来到Application.cpp
文件,删除一些代码后的样子。
#include <GL/glew.h>// 放在glfw3.h之前,或者任何OpenGL之前。
#include <GLFW/glfw3.h>
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
#include "Renderer.h"
#include "VertexBuffer.h"
#include "IndexBuffer.h"
#include "VertexArray.h"
#include "Shader.h"
#include "VertexBufferLayout.h"
#include "Texture.h"
#include "glm/glm.hpp"
#include "glm/gtc/matrix_transform.hpp"
#include "imgui/imgui.h"
#include "imgui/imgui_impl_glfw_gl3.h"
int main(void)
{
GLFWwindow* window;
/* Initialize the library */
if (!glfwInit())
return -1;
// 指定用3.3.0版本。
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);//3.0
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);//0.3
// Setting my OpenGL profile to be core!!
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
/* Create a windowed mode window and its OpenGL context */
window = glfwCreateWindow(960, 540, "I am Groot", NULL, NULL);
if (!window)
{
glfwTerminate();
return -1;
}
/* Make the window's context current */
glfwMakeContextCurrent(window);
glfwSwapInterval(1);
// after glfwMakeContextCurrent() -> Testing GLEW.
if (glewInit() != GLEW_OK)
{
std::cout << "glewInit() error" << std::endl;
}
std::cout << glGetString(GL_VERSION) << std::endl;
// Testing GLEW.
{
// For blending.
GLCall(glEnable(GL_BLEND));
GLCall(glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA));
// Renderer.
Renderer renderer;
// Setup ImGui binding.
ImGui::CreateContext();
ImGui_ImplGlfwGL3_Init(window, true);
// Setup ImGui style.
ImGui::StyleColorsDark();
/* Loop until the user closes the window */
while (!glfwWindowShouldClose(window))
{
/* Render here */
renderer.Clear();
// ImGui, can put this anywhere just make sure put it in between ImGui code,
// before you start any ImGui code for this frame you want to have the new frame function call.
ImGui_ImplGlfwGL3_NewFrame();
// ImGui rendering.
ImGui::Render();
ImGui_ImplGlfwGL3_RenderDrawData(ImGui::GetDrawData());
/* Swap front and back buffers */
glfwSwapBuffers(window);
/* Poll for and process events */
glfwPollEvents();
}
}
// ImGui Cleanup.
ImGui_ImplGlfwGL3_Shutdown();
ImGui::DestroyContext();
glfwTerminate();
return 0;
}
在这个代码基础上,包含我们的"tests/TestClearColor.h"
文件,并如图创建和设置我们的test
。
3、运行效果:
可以随意修改屏幕上R G B A的值,也可以通过color picker改变屏幕的背景颜色。