#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 纹理采样器
GLchar * fragmentSrc = "#version 330 core \n \in vec3 outcolor;\in vec2 outtexcoord;\out vec4 color; \uniform sampler2D ourTexture;\void main(){ \color = texture(ourTexture, outtexcoord) *vec4(outcolor,1.0f) ; \}";
//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};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;glGenTextures(1, &texture);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("E:/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);while (!glfwWindowShouldClose(window)){glfwPollEvents();glClearColor(1.0f, 0.0f, 1.0f, 1.0f);glClear(GL_COLOR_BUFFER_BIT);glBindTexture(GL_TEXTURE_2D, texture);glUseProgram(programShader);glBindVertexArray(VAO);glDrawElements(GL_TRIANGLES,6,GL_UNSIGNED_INT,0);glBindVertexArray(0);glfwSwapBuffers(window);}glfwTerminate();return 0;
}
注意点
1.检验着色器编译有没有通过,vec3转vec4有没有正确
2.创建EBO(元素缓存对象)时的绑定数据和加载数据的枚举为 E_ELEMENT_ARRAY_BUFFER
3.使能顶点数组的指定属性
glEnableVertexAttribArray(0);
使能第一个属性:位置
glEnableVertexAttribArray(1);
使能第二个属性:颜色
glEnableVertexAttribArray(2);
使能第三个属性:纹理坐标