纹理渲染

#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include<iostream>
#include"shader.h"
#include"stb_image.h"
#include<string>
using namespace std;

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);
}

const char *vertexShaderSource = "#version 450 core\n"
"layout (location = 0) in vec3 aPos;\n"
"layout (location = 1) in vec3 aColor;\n"
"out vec3 ourColor;\n"
"void main()\n"
"{\n"
"   gl_Position = vec4(aPos, 1.0);\n"
"   ourColor = aColor;\n"
"}\0";

const char *fragmentShaderSource = "#version 450 core\n"
"out vec4 FragColor;\n"
"in vec3 ourColor;\n"
"void main()\n"
"{\n"
"   FragColor = vec4(ourColor, 1.0f);\n"
"}\n\0";

//片段着色器

const unsigned int SCR_WIDTH = 800;
const unsigned int SCR_HEIGHT = 600;

int main(){
	glfwInit();
	glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
	glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
	glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
	//glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
#ifdef __APPLE__
	glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); // uncomment this statement to fix compilation on OS X
#endif

	GLFWwindow* window = glfwCreateWindow(SCR_WIDTH, SCR_HEIGHT, "LearnOpenGL", NULL, NULL);
	//宽 高 标题
	if (window == NULL)
	{
		cout << "Failed to create GLFW window" <<endl;
		glfwTerminate();
		return -1;
	}
	glfwMakeContextCurrent(window);

	if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))//初始化GLAD
	{
		cout << "Failed to initialize GLAD" <<endl;
		return -1;
	}

	glViewport(0, 0, 800, 600);

	Shader ourShader("4.5.1.vs", "4.5.1.fs");

	float vertices[] = {
		//     ---- 位置 ----       ---- 颜色 ----     - 纹理座标 -
			 0.5f,  0.5f, 0.0f,   1.0f, 0.0f, 0.0f,   1.0f, 1.0f,   // 右上
			 0.5f, -0.5f, 0.0f,   0.0f, 1.0f, 0.0f,   1.0f, 0.0f,   // 右下
			-0.5f, -0.5f, 0.0f,   0.0f, 0.0f, 1.0f,   0.0f, 0.0f,   // 左下
			-0.5f,  0.5f, 0.0f,   1.0f, 1.0f, 0.0f,   0.0f, 1.0f    // 左上
	};
	unsigned int indices[] = {
	   0, 1, 3, // first triangle
	   1, 2, 3  // second triangle
	};


	unsigned int VBO, VAO, EBO;
	//VBO顶点缓冲对象,VAO顶点数组对象,EBO索引缓冲对象
	glGenVertexArrays(1, &VAO);
	glGenBuffers(1, &VBO);
	glGenBuffers(1, &EBO);
	//用glGenBuffers函数和一个缓冲ID生成一个VBO对象和一个EBO

	glBindVertexArray(VAO);
	//用glBindVertexArray绑定VAO

	glBindBuffer(GL_ARRAY_BUFFER, VBO);

	glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
	//用glBindBuffer函数把新创建的缓冲对象绑定到GL_ARRAY_BUFFER目标上

	glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
	//用glBindBuffer函数把新创建的EBO绑定到GL_ELEMENT_ARRAY_BUFFER目标上

	glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
	//用glBufferData函数,它会把之前定义的顶点数据复制到缓冲的内存中
	/*
		GL_STATIC_DRAW :数据不会或几乎不会改变。
		GL_DYNAMIC_DRAW:数据会被改变很多。
		GL_STREAM_DRAW :数据每次绘制时都会改变。
	*/


	glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)0);
	glEnableVertexAttribArray(0);
	glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(3 * sizeof(float)));
	glEnableVertexAttribArray(1);
	glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(6 * sizeof(float)));
	glEnableVertexAttribArray(2);
	/*
	第一个参数指定要配置的顶点属性
	第二个参数指定顶点属性的大小。顶点属性是一个vec3,它由3个值组成,所以大小是3。
	第三个参数指定数据的类型,这里是GL_FLOAT
	第四个参数定义我们是否希望数据被标准化(Normalize)。如果我们设置为GL_TRUE,
	所有数据都会被映射到0(对于有符号型signed数据是-1)到1之间。我们把它设置为GL_FALSE。
	第五个参数叫做步长(Stride),它告诉我们在连续的顶点属性组之间的间隔
	从这个属性第二次出现的地方到整个数组0位置之间有多少字节
	最后一个参数的类型是void*,所以需要我们进行这个奇怪的强制类型转换。
	它表示位置数据在缓冲中起始位置的偏移量(Offset)。
	*/

	glBindVertexArray(0);

	//glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);启用线框模式
	//glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);恢复默认模式


	//纹理处理
	unsigned int texture1, texture2;
	glGenTextures(1, &texture1);
	glBindTexture(GL_TEXTURE_2D, texture1); // all upcoming GL_TEXTURE_2D operations now have effect on this texture object
	// set the texture wrapping parameters
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);	
	// set texture wrapping to GL_REPEAT (default wrapping method)
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
	// set texture filtering parameters
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
	// load image, create texture and generate mipmaps
	stbi_set_flip_vertically_on_load(true);
	int width, height, nrChannels;
	//string path = "C:/Users/Administrator/Desktop/OPENGL/image/container.jpg";
	unsigned char *data = stbi_load("C:/Users/Administrator/Desktop/OPENGL/image/container.jpg", &width, &height, &nrChannels, 0);
	if (data)
	{
		//glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
		//glGenerateMipmap(GL_TEXTURE_2D);
		if (nrChannels == 3)//rgb 适用于jpg图像
			glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);//后面一个是RGBA
		else if (nrChannels == 4)//rgba 适用于png图像
			glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);//注意,两个都是RGBA
		//std::cout<<nrChannels<<std::endl;
		glGenerateMipmap(GL_TEXTURE_2D);

		/*
		第一个参数指定了纹理目标(Target)。设置为GL_TEXTURE_2D意味着会生成与
		当前绑定的纹理对象在同一个目标上的纹理(任何绑定到GL_TEXTURE_1D
		和GL_TEXTURE_3D的纹理不会受到影响)。
		第二个参数为纹理指定多级渐远纹理的级别,如果你希望单独手动设置每个多级渐远纹理的级别的话。
		这里我们填0,也就是基本级别。
		第三个参数告诉OpenGL我们希望把纹理储存为何种格式。我们的图像只有RGB值,
		因此我们也把纹理储存为RGB值。
		第四个和第五个参数设置最终的纹理的宽度和高度。我们之前加载图像的时候储存了它们,
		所以我们使用对应的变量。
		下个参数应该总是被设为0(历史遗留的问题)。
		第七第八个参数定义了源图的格式和数据类型。我们使用RGB值加载这个图像,
		并把它们储存为char(byte)数组,我们将会传入对应值。
		最后一个参数是真正的图像数据。
		*/
	}
	else
	{
		cout << "Failed to load texture" << endl;
	}
	stbi_image_free(data);

	glGenTextures(1, &texture2);
	glBindTexture(GL_TEXTURE_2D, texture2); // all upcoming GL_TEXTURE_2D operations now have effect on this texture object
	// set the texture wrapping parameters
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
	// set texture wrapping to GL_REPEAT (default wrapping method)
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
	// set texture filtering parameters
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
	// load image, create texture and generate mipmaps
	//path = "C:/Users/Administrator/Desktop/OPENGL/image/awesomeface.png";
	data = stbi_load("C:/Users/Administrator/Desktop/OPENGL/image/awesomeface.png", &width, &height, &nrChannels, 0);
	if (data)
	{
		cout << "132\n";
		glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
		glGenerateMipmap(GL_TEXTURE_2D);
	}
	else
	{
		cout << "Failed to load texture" << endl;
	}
	stbi_image_free(data);

	ourShader.use(); // don't forget to activate/use the shader before setting uniforms!
	// either set it manually like so:
	glUniform1i(glGetUniformLocation(ourShader.ID, "texture1"), 0);
	// or set it via the texture class
	ourShader.setInt("texture2", 1);


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

		// 渲染
		// 清除颜色缓冲
		glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
		glClear(GL_COLOR_BUFFER_BIT);
		
		glActiveTexture(GL_TEXTURE0);
		glBindTexture(GL_TEXTURE_2D, texture1);
		glActiveTexture(GL_TEXTURE1);
		glBindTexture(GL_TEXTURE_2D, texture2);

		ourShader.use();
		glBindVertexArray(VAO); 
		glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
		//第一个参数,渲染基元类型
		//第二个参数,顶点数量
		//第三个,参数值类型
		//第四个,偏移量

		glfwSwapBuffers(window);
		//函数会交换颜色缓冲
		glfwPollEvents();
		//检查有没有触发什么事件(比如键盘输入、鼠标移动等)、更新窗口状态,
		//并调用对应的回调函数(可以通过回调方法手动设置)。
	}
	glDeleteVertexArrays(1, &VAO);
	glDeleteBuffers(1, &VBO);
	glDeleteBuffers(1, &EBO);
	//解除绑定
	glfwTerminate();

	return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章