openGL二:纹理 texture

#include "glew.h"
#include "glfw3.h"
#include "SOIL2.h"

//如何解析输入的数据流 0,1,2 分三部分
GLchar * vertextSrc = " #version 330 core \n \
						layout (location = 0) in vec3 position; \
						layout (location = 1) in vec3 color; \
						layout (location = 2) in vec2 texcoord;\
						out vec3 outcolor; \
						out vec2 outtexcoord; \
						void main(){gl_Position = vec4(position, 1.0f);outcolor = color;outtexcoord = texcoord;}";


// texture 是纹理接口 返回纹理颜色, texture(参数1=采样器, 参数2=纹理坐标)
// sampler2D 纹理采样器
// mix :纹理混合接口
GLchar * fragmentSrc = "#version 330 core \n \
					   in vec3 outcolor;\
					   in vec2 outtexcoord;\
					   out vec4 color; \
					   uniform sampler2D ourTexture;\
					   uniform sampler2D ourTexture2;\
					   void main(){ \
					   		vec2 tempTexcoord = vec2(outtexcoord.x, 1.0f - outtexcoord.y);\
					   		color = mix( texture(ourTexture, tempTexcoord), texture(ourTexture2,tempTexcoord) ,0.2); \
						}";
//

int main()
{
	glfwInit();
	GLFWwindow* window = glfwCreateWindow(500,600,"Texture", NULL,NULL);

	glfwMakeContextCurrent(window);

	glewInit();



	/*GLfloat arrays[] = {
		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, 0.0f, 0.0f,	   0.0f, 1.0f
	};*/
		GLfloat vertexArray[] = {
		/* 点位置				颜色				纹理坐标*/
		0.5f, 0.5f,  0.0f,	   1.0f, 1.0f, 1.0f,     2.0f, 2.0f,
		0.5f, -0.5f, 0.0f,     0.0f, 1.0f, 0.0f,	 2.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, 0.0f, 0.0f,     0.0f, 2.0f,

	};
	//纹理坐标超出(0,1)范围的部分按照纹理设置的环绕方式进行复制

	GLuint  earrays[] = {
		0,1,3,
		1,2,3
	};


	//创建数组缓冲对象
	GLuint VAO, VBO, EBO;
	glGenVertexArrays(1, &VAO);
	glGenBuffers(1, &VBO);
	glGenBuffers(1, &EBO);


	glBindVertexArray(VAO);
	glBindBuffer(GL_ARRAY_BUFFER,VBO);
	glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
	

	//GPU 开辟开辟缓存存放数据
	glBufferData(GL_ARRAY_BUFFER, sizeof(arrays), arrays, GL_STATIC_DRAW);
	glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(earrays), earrays, GL_STATIC_DRAW);

	//如何解析数据到顶点着色器,  8 个数据作为一个步长
	glVertexAttribPointer(0, 3,GL_FLOAT,false, 8* sizeof(GLfloat),(GLvoid*)0); //第一个输入变量的解析规则 layout(location=0)
	glVertexAttribPointer(1, 3, GL_FLOAT, false, 8 * sizeof(GLfloat), (GLvoid*)(3 * sizeof(GLfloat))); // layout(location = 1)
	glVertexAttribPointer(2, 2, GL_FLOAT, false, 8 * sizeof(GLfloat), (GLvoid*)(6 * sizeof(GLfloat))); // ...


	glEnableVertexAttribArray(0);
	glEnableVertexAttribArray(1);
	glEnableVertexAttribArray(2);

	//glDeleteBuffers(VBO);
	//glDeleteBuffers(EBO);
	glBindVertexArray(0);

	//创建着色器程序
	GLuint Vertexshader, fragmentShader, programShader;
	Vertexshader = glCreateShader(GL_VERTEX_SHADER);
	fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
	programShader = glCreateProgram();

	GLint succeed;
	char info[512];

	glShaderSource(Vertexshader,1,&vertextSrc,(GLint*)0);
	glCompileShader(Vertexshader);
	glGetShaderiv(Vertexshader, GL_COMPILE_STATUS, &succeed);
	if (!succeed)
	{
		glGetShaderInfoLog(Vertexshader, 512, NULL, info);
	}

	glShaderSource(fragmentShader, 1, &fragmentSrc, (GLint*)0);
	glCompileShader(fragmentShader);
	glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &succeed);
	if (!succeed)
	{
		glGetShaderInfoLog(fragmentShader, 512, NULL, info);
	}

	glAttachShader(programShader,Vertexshader);
	glAttachShader(programShader, fragmentShader);
	glLinkProgram(programShader);

	glDeleteShader(Vertexshader);
	glDeleteShader(fragmentShader);

	//创建纹理对象
	GLuint texture, texture2;
	glGenTextures(1, &texture);

	glActiveTexture(GL_TEXTURE0);//激活纹理单元0
	glBindTexture(GL_TEXTURE_2D, texture);//绑定应用该纹理对象
	
	//设置该纹理的纹理环绕方式和纹理过滤方式
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_MIRRORED_REPEAT);//x 轴纹理渲染方式,重复渲染
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_MIRRORED_REPEAT); // y轴纹理渲染方式

	//纹理过滤:纹理像素映射到纹理坐标上的过程中做的纹理像素处理, 定义一个点(点所在的像素颜色区域,返回颜色值的规则)
	// GL_NEAREST: 邻近过滤 ,返回所在点的当前固定颜色值,整体看起来颜色分明,像素点一样
	// GL_LINEAR: 线性过滤, 返回所在点的(单位面积内的综合颜色值),整体看起来比较模糊,线性过度染色
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,GL_NEAREST);//缩小过滤方式
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,GL_LINEAR);


	int width, height;
	unsigned char* image = SOIL_load_image("container.jpg", &width, &height, 0, SOIL_LOAD_RGB);
	//利用图片和纹理对象生成图片纹理
	glTexImage2D(GL_TEXTURE_2D,0, GL_RGB, width, height, 0,GL_RGB, GL_UNSIGNED_BYTE, image);
	//设置渐远纹理
	glGenerateMipmap(GL_TEXTURE_2D);

	//销毁图片资源和取消绑定该纹理对象
	SOIL_free_image_data(image);
	glBindTexture(GL_TEXTURE_2D, 0);

	//纹理2:绑定纹理,激活纹理单元,设置全局的采样值值
	glGenTextures(1, &texture2);
	glActiveTexture(GL_TEXTURE1);//激活纹理单元1
	glBindTexture(GL_TEXTURE_2D, texture2);//绑定纹理对象

/*GL_REPEAT	对纹理的默认行为。重复纹理图像。
		GL_MIRRORED_REPEAT	和GL_REPEAT一样,但每次重复图片是镜像放置的。
		GL_CLAMP_TO_EDGE	纹理坐标会被约束在0到1之间,超出的部分会重复纹理坐标的边缘,产生一种边缘被拉伸的效果。
		GL_CLAMP_TO_BORDER	超出的坐标为用户指定的边缘颜色。*/
		
	//设置纹理2的渲染方式:环绕,渲染
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_MIRRORED_REPEAT);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_MIRRORED_REPEAT);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,GL_NEAREST);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);


	//纹理图片二
	unsigned char* image2 = SOIL_load_image("awesomeface.png", &width, &height, 0, SOIL_LOAD_RGB);//加载图片
	//生成图片纹理
	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, image2);
	//设置渐远纹理
	glGenerateMipmap(GL_TEXTURE_2D);
	//回收资源取消绑定
	SOIL_free_image_data(image2);
	glBindTexture(GL_TEXTURE_2D, 0);


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

		
		glClearColor(1.0f, 0.0f, 1.0f, 1.0f);
		glClear(GL_COLOR_BUFFER_BIT);
		glUseProgram(programShader);

		glActiveTexture(GL_TEXTURE0);
		glBindTexture(GL_TEXTURE_2D, texture);
		//设置全局变量 uniform 的值, 这里设置该纹理采样器的值为0,第一个纹理单元
		glUniform1i(glGetUniformLocation(programShader,"ourTexture"),0);

		glActiveTexture(GL_TEXTURE1);
		glBindTexture(GL_TEXTURE_2D, texture2);
		glUniform1i(glGetUniformLocation(programShader, "ourTexture2"), 1);
		
		glBindVertexArray(VAO);
	
		glDrawElements(GL_TRIANGLES,6,GL_UNSIGNED_INT,0);
		glBindVertexArray(0);

		glfwSwapBuffers(window);

	}

	glfwTerminate();


	return 0;
}

你可能感兴趣的:(opengl,计算机视觉)