【Cherno的OpenGL视频】Batching rendering-Textures

  • Post author:
  • Post category:其他




代码实现:


1、在

SandboxLayer.cpp

里增加函数

LoadTexture()



在这里插入图片描述

用来加载这两个logo

在这里插入图片描述


LoadTexture()

代码:

static GLuint LoadTexture(const std::string& path)
{
	int w, h, bits;
	stbi_set_flip_vertically_on_load(1);
	auto* pixels = stbi_load(path.c_str(), &w, &h, &bits, STBI_rgb);
	GLuint textureID;
	glCreateTextures(GL_TEXTURE_2D, 1, &textureID);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB8, w, h, 0, GL_RGB, GL_UNSIGNED_BYTE, pixels);
	stbi_image_free(pixels);
	return textureID;
}


2、在

SandboxLayer.h

里增加2个私有成员


在这里插入图片描述


3、在

OnAttach()

里使用

LoadTexture()

,以及在

vertices

里增加texture coordinates,再增加它的属性设置。


在这里插入图片描述


SandboxLayer.cpp



OnAttach()

函数代码:

void SandboxLayer::OnAttach()
{
	EnableGLDebugging();

	// Init here
	m_Shader = std::unique_ptr<Shader>(Shader::FromGLSLTextFiles(
		"assets/vert.glsl",
		"assets/frag.glsl"
	));

	glClearColor(0.1f, 0.1f, 0.1f, 1.0f);

	float vertices[] = {
		// position.        // RGBA color.            // texture. 
		-1.5f, -0.5f, 0.0f, 0.18f, 0.6f, 0.96f, 1.0f, 0.0f, 0.0f, 
		-0.5f, -0.5f, 0.0f, 0.18f, 0.6f, 0.96f, 1.0f, 1.0f, 0.0f, 
		-0.5f,  0.5f, 0.0f,	0.18f, 0.6f, 0.96f, 1.0f, 1.0f, 1.0f, 
		-1.5f,  0.5f, 0.0f,	0.18f, 0.6f, 0.96f, 1.0f, 0.0f, 1.0f, 

		 0.5f, -0.5f, 0.0f, 1.0f, 0.93f, 0.24f, 1.0f, 0.0f, 0.0f, 
		 1.5f, -0.5f, 0.0f,	1.0f, 0.93f, 0.24f, 1.0f, 1.0f, 0.0f, 
		 1.5f,  0.5f, 0.0f,	1.0f, 0.93f, 0.24f, 1.0f, 1.0f, 1.0f, 
		 0.5f,  0.5f, 0.0f,	1.0f, 0.93f, 0.24f, 1.0f, 0.0f, 1.0f 
	};

	glCreateVertexArrays(1, &m_QuadVA);
	glBindVertexArray(m_QuadVA);

	glCreateBuffers(1, &m_QuadVB);
	glBindBuffer(GL_ARRAY_BUFFER, m_QuadVB);
	glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);

	// Treating the first attribute 0 as the position coordinates.
	glEnableVertexArrayAttrib(m_QuadVB, 0);
	glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 9* sizeof(float), 0);

	// Add another attribute 1 as the color.
	glEnableVertexArrayAttrib(m_QuadVB, 1);
	glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, 9* sizeof(float), (const void*)12);

	// Add another attribute 2 as the texture coordinates.
	glEnableVertexArrayAttrib(m_QuadVB, 2);
	glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 9* sizeof(float), (const void*)28);

	uint32_t indices[] = {
		0, 1, 2, 2, 3, 0,
		4, 5, 6, 6, 7, 4
	};

	glCreateBuffers(1, &m_QuadIB);
	glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_QuadIB);
	glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);

	m_ChernoTex = LoadTexture("assets/Cherno.png");
	m_HazelTex = LoadTexture("assets/Hazel.png");
}


4、设置我们的shader



vert.glsl

代码:

#version 450 core

layout (location = 0) in vec3 a_Position;
layout (location = 1) in vec4 a_Color;
layout (location = 2) in vec2 a_TexCoord;

uniform mat4 u_ViewProj;
uniform mat4 u_Transform;

out vec4 v_Color;
out vec2 v_TexCoord;

void main()
{
	v_Color = a_Color;
	v_TexCoord = a_TexCoord;
	gl_Position = u_ViewProj * u_Transform * vec4(a_Position, 1.0);
}


frag.glsl

代码:

#version 450 core

layout (location = 0) out vec4 o_Color;

in vec4 v_Color;
in vec2 v_TexCoord;

void main()
{
	o_Color = vec4(v_TexCoord, 0.0, 1.0);
}


5、运行效果


彩色的两个方块,可以通过滚轮放大缩小。

在这里插入图片描述


6、在

vertices

里增加texture index,再增加它的属性设置。


在这里插入图片描述


7、设置我们的shader



vert.glsl

代码:

#version 450 core

layout (location = 0) in vec3 a_Position;
layout (location = 1) in vec4 a_Color;
layout (location = 2) in vec2 a_TexCoord;
layout (location = 3) in float a_TexIndex;

uniform mat4 u_ViewProj;
uniform mat4 u_Transform;

out vec4 v_Color;
out vec2 v_TexCoord;
out float v_TexIndex;

void main()
{
	v_Color = a_Color;
	v_TexCoord = a_TexCoord;
	v_TexIndex = a_TexIndex;
	gl_Position = u_ViewProj * u_Transform * vec4(a_Position, 1.0);
}


frag.glsl

代码:

#version 450 core

layout (location = 0) out vec4 o_Color;

in vec4 v_Color;
in vec2 v_TexCoord;
in float v_TexIndex;

void main()
{
	o_Color = vec4(v_TexIndex, v_TexIndex, v_TexIndex, 1.0);
}


8、运行效果


在这里插入图片描述

左边的texture index为0,右边的texture index为1。


9、最后一步。


Bind these textures to the correct texture slots, and then set up some code inside our fragment shader to actually sample from these textures.



SandboxLayer.cpp

里的

OnUpdate()

函数里增加下面这两行代码

在这里插入图片描述



SandboxLayer.cpp

里的

OnAttach()

函数里增加下面这4行代码

在这里插入图片描述


frag.glsl

代码:

#version 450 core

layout (location = 0) out vec4 o_Color;

in vec4 v_Color;
in vec2 v_TexCoord;
in float v_TexIndex;

uniform sampler2D u_Textures[2];

void main()
{
	int index = int(v_TexIndex);
	o_Color = texture(u_Textures[index], v_TexCoord);
}

10、运行效果

在这里插入图片描述



版权声明:本文为AlexiaDong原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。