【Cherno的OpenGL视频】Setting up a Test framework for OpenGL

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改变屏幕的背景颜色。
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值