OpenGL之——多纹理,混合纹理设置渲染

版权声明:转载请注明出处 https://blog.csdn.net/qq_35294564/article/details/86546797

 实现下面三种纹理的组合和混合:

 一、首先,获取纹理顶点坐标

#version 330 core
layout (location = 0) in vec3 aPos;
layout (location = 1) in vec2 aTexCoods;
out vec2 TexCoods;
void main(){
	gl_Position = vec4(aPos, 1.0f);
	TexCoods = aTexCoods;
};

二、其次,在片段着色器定义三个采样器,用于对三种纹理的采样:

#version 330 core
in vec2 TexCoods;

uniform sampler2D ourTexture0;
uniform sampler2D ourTexture1;
uniform sampler2D ourTexture2;
uniform int flag;
out vec4 FragColor;

void main()
{
	if(flag == 1){
		FragColor = mix(texture(ourTexture1, TexCoods),texture(ourTexture2, TexCoods),0.2);
	}else{
		FragColor = texture(ourTexture0, TexCoods);
	}
	
}

三、 再次,加载三种纹理

//纹理
	GLuint texture[3];
	glGenTextures(3, texture);
	glBindTexture(GL_TEXTURE_2D, texture[0]);
	//为当前绑定的对象设置环绕,过滤方式
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
	//加载并生成纹理一
	int width, height, nrChannels;
	stbi_set_flip_vertically_on_load(true);//在图像加载时帮助我们翻转y轴,只需要在加载任何图像前加入以下语句即可
	unsigned char *image1 = stbi_load("box2.png", &width, &height, &nrChannels, 0);
	cout << "Channels = " << nrChannels << endl;
	glBindTexture(GL_TEXTURE_2D, texture[0]);
	if (image1) {
		glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, image1);
		glGenerateMipmap(GL_TEXTURE_2D);
	}
	else {
		std::cout << "Failed to load texture1" << std::endl;
	}
	stbi_image_free(image1);
	glBindTexture(GL_TEXTURE_2D, 0);
	//加载并生成纹理二
	unsigned char *image2 = stbi_load("box.jpg", &width, &height, &nrChannels, 0);
	cout << "Channels = " << nrChannels << endl;
	glBindTexture(GL_TEXTURE_2D, texture[1]);
	if (image2) {
		glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, image2);
		glGenerateMipmap(GL_TEXTURE_2D);
	}
	else {
		std::cout << "Failed to load texture2" << std::endl;
	}
	stbi_image_free(image2);
	glBindTexture(GL_TEXTURE_2D, 0);
	//加载并生成纹理三
	unsigned char *image3 = stbi_load("face.png", &width, &height, &nrChannels, 0);
	cout << "Channels = " << nrChannels << endl;
	glBindTexture(GL_TEXTURE_2D, texture[2]);
	if (image3) {
		glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, image3);
		glGenerateMipmap(GL_TEXTURE_2D);
	}
	else {
		std::cout << "Failed to load texture3" << std::endl;
	}
	stbi_image_free(image3);
	glBindTexture(GL_TEXTURE_2D, 0);

 四、设置采样器所在的纹理单元、激活纹理、绑定纹理

    ShaderColor.use();
	ShaderColor.setInt("ourTexture0", 0);//设置采样器ourTexture0属于0号纹理单元
	ShaderColor.setInt("ourTexture1", 1);//设置采样器ourTexture1属于1号纹理单元
	ShaderColor.setInt("ourTexture2", 2);//设置采样器ourTexture2属于2号纹理单元

	while (!glfwWindowShouldClose(window)) {
		processInput(window);

		glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
		glClear(GL_COLOR_BUFFER_BIT);

		ShaderColor.use();

		ShaderColor.setInt("flag", 1);
		glActiveTexture(GL_TEXTURE1);//激活0号纹理
		glBindTexture(GL_TEXTURE_2D, texture[1]);//绑定1号纹理为texture[1]
		glActiveTexture(GL_TEXTURE2);//激活1号纹理
		glBindTexture(GL_TEXTURE_2D, texture[2]);//绑定2号纹理为texture[2]
		glBindVertexArray(VAO);//绑定顶点数组
		glDrawArrays(GL_TRIANGLES, 0, 3);//右上三角形
		
		
		ShaderColor.setInt("flag", 2);
		glActiveTexture(GL_TEXTURE0);//激活0号纹理
		glBindTexture(GL_TEXTURE_2D, texture[0]);//绑定0号纹理为texture[0]
		glDrawArrays(GL_TRIANGLES, 3, 3);//左下三角形
		glBindVertexArray(0);//解绑顶点数组
		glfwSwapBuffers(window);
		glfwPollEvents();
	}

 混合纹理:

完整代码: 

main.cpp


#include <iostream>
#include <glad/glad.h>
#include <GLFW/glfw3.h>
#define STB_IMAGE_IMPLEMENTATION
#include <stb_image.h>
#include "Shader.h"

#pragma comment(lib,"glfw3.lib")

using namespace std;

void framebuffer_size_callback(GLFWwindow* window, int width, int height);
void processInput(GLFWwindow* window);


int main() {
	glfwInit();
	glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
	glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
	glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
	
	GLFWwindow* window = glfwCreateWindow(800, 800, "LearnOpenGL", NULL, NULL);

	if (window == NULL) {
		cout << "Failed to create GLFW window" << endl;
		glfwTerminate();
		return -1;
	}
	glfwMakeContextCurrent(window);

	if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)){
		cout << "Failed to initialize GLAD" << endl;
		return -1;
	}
	glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);

	
	//创建顶点缓存对象用来管理分配到的顶点缓存空间
	//创建一个着色器对象
	
	Shader ShaderColor("shader.vs","shader.frag");
	


	GLfloat vertices[] = {
		// 第一个三角形     texture
		0.5f, 0.5f, 0.0f,  1.0f, 1.0f,  // 右上角
		0.5f, -0.5f, 0.0f, 1.0f, 0.0f,  // 右下角
		-0.5f, 0.5f, 0.0f, 0.0f, 1.0f,  // 左上角
		// 第二个三角形
		0.5f, -0.5f, 0.0f,  1.0f, 0.0f,  // 右下角
		-0.5f, -0.5f, 0.0f, 0.0f, 0.0f, // 左下角
		-0.5f, 0.5f, 0.0f,  0.0f, 1.0f  // 左上角
	}; 
	
	GLuint VAO, VBO;
	glGenVertexArrays(1, &VAO);//创建顶点数组对象
	glGenBuffers(1, &VBO);//创建顶点缓冲对象

	glBindBuffer(GL_ARRAY_BUFFER, VBO);//将顶点缓冲绑定到顶点缓冲对象
	glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);//缓冲区三角形顶点数据



	glBindVertexArray(VAO);//将顶点数组绑定到顶点数组对象
	//设置顶点属性指针
	glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), (GLvoid*)0);
	glEnableVertexAttribArray(0);
	glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), (GLvoid*)(3 * sizeof(GLfloat)));
	glEnableVertexAttribArray(1);
	glBindBuffer(GL_ARRAY_BUFFER, 0);
	glBindVertexArray(0);

	//纹理
	GLuint texture[3];
	glGenTextures(3, texture);
	glBindTexture(GL_TEXTURE_2D, texture[0]);
	//为当前绑定的对象设置环绕,过滤方式
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
	//加载并生成纹理一
	int width, height, nrChannels;
	stbi_set_flip_vertically_on_load(true);//在图像加载时帮助我们翻转y轴,只需要在加载任何图像前加入以下语句即可
	unsigned char *image1 = stbi_load("box2.png", &width, &height, &nrChannels, 0);
	cout << "Channels = " << nrChannels << endl;
	glBindTexture(GL_TEXTURE_2D, texture[0]);
	if (image1) {
		glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, image1);
		glGenerateMipmap(GL_TEXTURE_2D);
	}
	else {
		std::cout << "Failed to load texture1" << std::endl;
	}
	stbi_image_free(image1);
	glBindTexture(GL_TEXTURE_2D, 0);
	//加载并生成纹理二
	unsigned char *image2 = stbi_load("box.jpg", &width, &height, &nrChannels, 0);
	cout << "Channels = " << nrChannels << endl;
	glBindTexture(GL_TEXTURE_2D, texture[1]);
	if (image2) {
		glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, image2);
		glGenerateMipmap(GL_TEXTURE_2D);
	}
	else {
		std::cout << "Failed to load texture2" << std::endl;
	}
	stbi_image_free(image2);
	glBindTexture(GL_TEXTURE_2D, 0);
	//加载并生成纹理三
	unsigned char *image3 = stbi_load("face.png", &width, &height, &nrChannels, 0);
	cout << "Channels = " << nrChannels << endl;
	glBindTexture(GL_TEXTURE_2D, texture[2]);
	if (image3) {
		glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, image3);
		glGenerateMipmap(GL_TEXTURE_2D);
	}
	else {
		std::cout << "Failed to load texture3" << std::endl;
	}
	stbi_image_free(image3);
	glBindTexture(GL_TEXTURE_2D, 0);

	//int flagLoc = glGetUniformLocation(ShaderColor.ID, "flag");//此处不再需要获取地址了
	//glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);

	ShaderColor.use();
	ShaderColor.setInt("ourTexture0", 0);//设置采样器ourTexture0属于0号纹理单元
	ShaderColor.setInt("ourTexture1", 1);//设置采样器ourTexture1属于1号纹理单元
	ShaderColor.setInt("ourTexture2", 2);//设置采样器ourTexture2属于2号纹理单元

	while (!glfwWindowShouldClose(window)) {
		processInput(window);

		glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
		glClear(GL_COLOR_BUFFER_BIT);

		ShaderColor.use();

		ShaderColor.setInt("flag", 1);
		glActiveTexture(GL_TEXTURE1);//激活0号纹理
		glBindTexture(GL_TEXTURE_2D, texture[1]);//绑定1号纹理为texture[1]
		glActiveTexture(GL_TEXTURE2);//激活1号纹理
		glBindTexture(GL_TEXTURE_2D, texture[2]);//绑定2号纹理为texture[2]
		glBindVertexArray(VAO);//绑定顶点数组
		glDrawArrays(GL_TRIANGLES, 0, 3);//右上三角形
		
		
		ShaderColor.setInt("flag", 2);
		glActiveTexture(GL_TEXTURE0);//激活0号纹理
		glBindTexture(GL_TEXTURE_2D, texture[0]);//绑定0号纹理为texture[0]
		glDrawArrays(GL_TRIANGLES, 3, 3);//左下三角形
		glBindVertexArray(0);//解绑顶点数组
		glfwSwapBuffers(window);
		glfwPollEvents();
	}
	glDeleteBuffers(1, &VBO);
	glDeleteVertexArrays(1, &VAO);
	glfwTerminate();
	return 0;
}

void framebuffer_size_callback(GLFWwindow* window, int width, int height) {
	glViewport(0, 0, width, height);
}

void processInput(GLFWwindow* window) {
	if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS) {
		glfwSetWindowShouldClose(window, true);
	}
}

Shader.h

#pragma once

#include <glad/glad.h>
#include <string>
#include <fstream>
#include <sstream>
#include <iostream>

class Shader {
public:
	GLuint ID;//程序ID

	//构造器读取并构建着色器
	Shader(const GLchar* vertexPath, const GLchar* fragmentPath);
	//使用、激活程序
	void use();
	//uniform工具函数
	void setBool(const std::string &name, bool value) const;
	void setInt(const std::string &name, int value) const;
	void setFloat(const std::string &name, float value) const;
};



Shader::Shader(const GLchar* vertexPath, const GLchar* fragmentPath) {
	//从文件路径中获取顶点、片段着色器
	std::string vertexCode;//用来存储顶点着色器的字符串形式
	std::string fragmentCode;//用来存储片段着色器的字符串形式
	std::ifstream vShaderFile;//以输入方式打开
	std::ifstream fShaderFile;//以输入方式打开
	//保证ifstream对象可以抛出异常
	vShaderFile.exceptions(std::ifstream::failbit | std::ifstream::badbit);
	fShaderFile.exceptions(std::ifstream::failbit | std::ifstream::badbit);
	try {
		//打开文件
		vShaderFile.open(vertexPath);//以输入方式打开
		fShaderFile.open(fragmentPath);
		std::stringstream vShaderStream, fShaderStream;
		//读取文件的缓冲内容到数据流中
		vShaderStream << vShaderFile.rdbuf();
		fShaderStream << fShaderFile.rdbuf();
		//关闭文件处理器
		vShaderFile.close();
		fShaderFile.close();
		//转换数据流到string
		vertexCode = vShaderStream.str();//字符串流转换成字符串
		fragmentCode = fShaderStream.str();
	}
	catch (std::ifstream::failure e) {
		std::cout << "ERROR::SHADER::FILE_NOT_SUCCESFULLY_READ" << std::endl;
	}
	const GLchar* vShaderCode = vertexCode.c_str();
	const GLchar* fShaderCode = fragmentCode.c_str();

	//编译着色器
	GLuint vertex, fragment;
	GLint success;
	GLchar infoLog[512];
	//VertexShader
	vertex = glCreateShader(GL_VERTEX_SHADER);
	glShaderSource(vertex, 1, &vShaderCode, NULL);
	glCompileShader(vertex);

	glGetShaderiv(vertex, GL_COMPILE_STATUS, &success);
	if (!success) {
		glGetShaderInfoLog(vertex, 512, NULL, infoLog);
		std::cout << "ERROR::SHADER::VERTEX::COMPILATION_FAILED\n" << infoLog << std::endl;
	}

	//fragmentShader
	fragment = glCreateShader(GL_FRAGMENT_SHADER);
	glShaderSource(fragment, 1, &fShaderCode, NULL);
	glCompileShader(fragment);

	glGetShaderiv(fragment, GL_COMPILE_STATUS, &success);
	if (!success)
	{
		glGetShaderInfoLog(fragment, 512, NULL, infoLog);
		std::cout << "ERROR::SHADER::FRAGMENT::COMPILATION_FAILED\n" << infoLog << std::endl;
	}

	//ShaderProgram
	this->ID = glCreateProgram();//产生着色器程序ID
	glAttachShader(this->ID, vertex);
	glAttachShader(this->ID, fragment);
	glLinkProgram(this->ID);

	glGetProgramiv(this->ID, GL_LINK_STATUS, &success);
	if (!success) {
		glGetProgramInfoLog(this->ID, 512, NULL, infoLog);
		std::cout << "ERROR::SHADER::PROGRAM::LINKING_FAILED...\n" << infoLog << std::endl;
	}
	glDeleteShader(vertex);
	glDeleteShader(fragment);
}

void Shader::use() {
	glUseProgram(this->ID);
}
void Shader::setBool(const std::string &name, bool value) const
{
	glUniform1i(glGetUniformLocation(this->ID, name.c_str()), (int)value);
}
void Shader::setInt(const std::string &name, int value) const
{
	glUniform1i(glGetUniformLocation(this->ID, name.c_str()), value);
}
void Shader::setFloat(const std::string &name, float value) const
{
	glUniform1f(glGetUniformLocation(this->ID, name.c_str()), value);
}

shader.vs

#version 330 core
layout (location = 0) in vec3 aPos;
layout (location = 1) in vec2 aTexCoods;
out vec2 TexCoods;
void main(){
	gl_Position = vec4(aPos, 1.0f);
	TexCoods = aTexCoods;
};

shader.frag

#version 330 core
in vec2 TexCoods;

uniform sampler2D ourTexture0;
uniform sampler2D ourTexture1;
uniform sampler2D ourTexture2;
uniform int flag;
out vec4 FragColor;

void main()
{
	if(flag == 1){
		FragColor = mix(texture(ourTexture1, TexCoods),texture(ourTexture2, TexCoods),0.2);
	}else{
		FragColor = texture(ourTexture0, TexCoods);
	}
	
}

猜你喜欢

转载自blog.csdn.net/qq_35294564/article/details/86546797