【Cherno的OpenGL视频】Abstracting OpenGL into classes

1、OpenGL needs some kind of unique numeric ID that keeps track of every type of object we create.(vertex buffer, index buffer, vertex array, texture…)

2、代码实现:
IndexBuffer类:
在这里插入图片描述
VertexBuffer类:
在这里插入图片描述
Renderer类:
在这里插入图片描述
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"

struct ShaderProgramSource
{
	std::string VertexSource;
	std::string FragmentSource;
};

static ShaderProgramSource ParseShader(const std::string& filepath)
{
	enum class ShaderType
	{
		NONE = -1, VERTEX = 0, FRAGMENT = 1
	};

	std::ifstream stream(filepath);
	std::string line;
	std::stringstream ss[2];
	ShaderType type = ShaderType::NONE;

	// go through the file line-by-line.
	while (getline(stream, line))
	{
		if (line.find("#shader") != std::string::npos)
		{
			if (line.find("vertex") != std::string::npos)
			{
				// set mode to vertex.
				type = ShaderType::VERTEX;
			}
			else if (line.find("fragment") != std::string::npos)
			{
				// set mode to fragment.
				type = ShaderType::FRAGMENT;
			}
		}
		else
		{
			ss[(int)type] << line << '\n';
		}
	}
	return { ss[0].str(), ss[1].str() };
}

static unsigned int CompileShader(unsigned int type, const std::string& source)
{
	unsigned int id = glCreateShader(type);
	const char* src = source.c_str();
	glShaderSource(id, 1, &src, nullptr);
	glCompileShader(id);

	// error handling.
	int result;
	glGetShaderiv(id, GL_COMPILE_STATUS, &result);
	if (result == GL_FALSE)
	{
		int length;
		glGetShaderiv(id, GL_INFO_LOG_LENGTH, &length);
		char* message = (char*)alloca(length * sizeof(char));
		glGetShaderInfoLog(id, length, &length, message);
		std::cout << "CompileShader() Failed, " << (type == GL_VERTEX_SHADER ? "vertex" : "fragment") << " shader!" << std::endl;
		std::cout << message << std::endl;
		glDeleteShader(id);
		return 0;
	}

	return id;
}

// Creat a shader.
static unsigned int CreateShader(const std::string& vertexShader, const std::string& fragmentShader)
{
	unsigned int program = glCreateProgram();
	// create our true shader objects.
	unsigned int vs = CompileShader(GL_VERTEX_SHADER, vertexShader);
	unsigned int fs = CompileShader(GL_FRAGMENT_SHADER, fragmentShader);
	// link vs and fs.
	glAttachShader(program, vs);
	glAttachShader(program, fs);
	// link the program.
	glLinkProgram(program);
	glValidateProgram(program);
	// delete it after linked.
	glDeleteShader(vs);
	glDeleteShader(fs);

	return program;
}

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(640, 640, "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.

	{
		// on the CPU.
		float positions[] = {
			-0.5f, -0.5f, //index0
			 0.5f, -0.5f, //index1
			 0.5f,  0.5f, //index2
			-0.5f,  0.5f, //index3
		};

		// on the CPU.
		unsigned int indices[] = {
			0, 1, 2,//index0 1 2
			2, 3, 0 //index2 3 0
		};

		// Create a vertex array object.
		unsigned int vao;
		GLCall(glGenVertexArrays(1, &vao));
		GLCall(glBindVertexArray(vao));

		// Define a vertex buffer: send positions to GPU.
		VertexBuffer VB(positions, 4 * 2 * sizeof(float));

		// enable or disable a generic vertex attribute array and tell OpenGL what layout of our buffer.
		GLCall(glEnableVertexAttribArray(0));
		GLCall(glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(float) * 2, 0));// this links buffer and vao above.

		// Define a index buffer: send indices to GPU.
		IndexBuffer IB(indices, 6);

		ShaderProgramSource source = ParseShader("res/shaders/Basic.shader");
		// Use our shader.
		unsigned int shader = CreateShader(source.VertexSource, source.FragmentSource);
		GLCall(glUseProgram(shader));

		// 在我们的shader里,每个uniform都会被分配一个ID。
		GLCall(int location = glGetUniformLocation(shader, "u_Color"));
		ASSERT(location != -1);// -1 说明找不到u_Color。
		// after our shader is binded, set u_Color for fragment shader.
		GLCall(glUniform4f(location, 0.8f, 0.3f, 0.8f, 1.0f));

		// Unbinding everything.
		GLCall(glBindVertexArray(0));
		GLCall(glUseProgram(0));
		GLCall(glBindBuffer(GL_ARRAY_BUFFER, 0));
		GLCall(glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0));

		// sth for fun.
		float r = 0.0f;
		float increment = 0.05f;
		// sth for fun.

		/* Loop until the user closes the window */
		while (!glfwWindowShouldClose(window))
		{
			/* Render here */
			GLCall(glClear(GL_COLOR_BUFFER_BIT));

			GLCall(glUseProgram(shader));
			GLCall(glUniform4f(location, r, 0.3f, 0.8f, 1.0f));

			GLCall(glBindVertexArray(vao));
			IB.Bind();

			GLCall(glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, nullptr));

			if (r > 1.0f)
			{
				increment = -0.05f;
			}
			else if (r < 0.0f)
			{
				increment = 0.05f;
			}

			r += increment;

			/* Swap front and back buffers */
			glfwSwapBuffers(window);

			/* Poll for and process events */
			glfwPollEvents();
		}

		// Clean up shader once you're done.
		glDeleteProgram(shader);//should not be glDeleteShader(shader);
	}

	glfwTerminate();
	return 0;
}

3、运行效果:

Something Fun

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值