标签:
VERTEX_SHADER:
#version 150 core in vec2 position; in vec3 color; in vec2 texcoord; out vec3 Color; out vec2 TexCoord; void main(){ Color=color; TexCoord=texcoord; gl_Position=vec4(position,0,1); }
FRAGMENT_SHADER:
#version 150 core in vec3 Color; in vec2 TexCoord; out vec4 outColor; uniform sampler2D texKit; uniform sampler2D texPupy; uniform vec3 triangleColor; void main(){ //outColor=texture(texKit,TexCoord)*vec4(Color,1.0); vec4 colKit=texture(texKit,TexCoord); vec4 colPupy=texture(texPupy,TexCoord); outColor=mix(colKit,colPupy,0.2); }
源码:
#include"glew.h" #include<Windows.h> #include"freeglut.h" #include<gl\GL.h> #include<random> #include"LoadShaders.h" #include"SOIL.h" GLuint vao; GLuint vbo; GLuint ebo; GLuint tbo[2]; void Init(){ GLfloat vertices[] = { -0.5,0.5, 1.0,0.0,0.0, 0.0,0.0, 0.5,0.5, 0.0,1.0,0.0, 1.0,0.0, 0.5,-0.5, 0.0,0.0,1.0, 1.0,1.0, -0.5,-0.5, 1.0,1.0,1.0, 0.0,1.0 }; GLuint elements[] = { 0, 1, 2,2,3,0 }; ShaderInfo shaders[] = { { GL_VERTEX_SHADER, "vertexShader.txt" }, { GL_FRAGMENT_SHADER, "fragementShader.txt" }, { GL_NONE, NULL } }; GLuint shaderProgram = LoadShaders(shaders); //**VAO开始记录 glGenVertexArrays(1, &vao); glBindVertexArray(vao); //*VBO,需要数据配接 glGenBuffers(1, &vbo); glBindBuffer(GL_ARRAY_BUFFER, vbo); glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); //*数据配接 glUseProgram(shaderProgram); GLuint posAttri = glGetAttribLocation(shaderProgram, "position"); glVertexAttribPointer(posAttri, 2, GL_FLOAT, GL_FALSE, 7 * sizeof(GL_FLOAT), 0); glEnableVertexAttribArray(posAttri); GLuint colorAttri = glGetAttribLocation(shaderProgram, "color"); glVertexAttribPointer(colorAttri, 3, GL_FLOAT, GL_FALSE, 7 * sizeof(GL_FLOAT), (void*)(2 * sizeof(GL_FLOAT))); glEnableVertexAttribArray(colorAttri); GLuint texAttri = glGetAttribLocation(shaderProgram, "texcoord"); glVertexAttribPointer(texAttri, 2, GL_FLOAT, GL_FALSE, 7 * sizeof(GL_FLOAT), (void*)(5*sizeof(GL_FLOAT))); glEnableVertexAttribArray(texAttri); //*EBO glGenBuffers(1, &ebo); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo); glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(elements), elements, GL_STATIC_DRAW); //*TBO glGenTextures(2, tbo); int width = 0, height = 0; unsigned char* image; glActiveTexture(GL_TEXTURE0); //对应samplerD texKit glBindTexture(GL_TEXTURE_2D, tbo[0]); image=SOIL_load_image("sample.png", &width, &height, 0, SOIL_LOAD_RGB); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, image); SOIL_free_image_data(image); glUniform1i(glGetUniformLocation(shaderProgram,"texKit"), 0); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glActiveTexture(GL_TEXTURE1); //对应samplerD texPupy glBindTexture(GL_TEXTURE_2D, tbo[1]); image = SOIL_load_image("sample2.png", &width, &height, 0, SOIL_LOAD_RGB); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, image); SOIL_free_image_data(image); glUniform1i(glGetUniformLocation(shaderProgram, "texPupy"), 1); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glClearColor(0, 0, 0, 1); } void Display(){ glClear(GL_COLOR_BUFFER_BIT); glBindVertexArray(vao); //glDrawArrays(GL_TRIANGLES, 0, 3); glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0); //glDrawElementsBaseVertex(GL_TRIANGLES, 3, GL_UNSIGNED_INT, 0, 1); glFlush(); } int main(int argc, char** argv){ glutInit(&argc, argv); glutInitDisplayMode(GLUT_RGBA); glutInitWindowSize(512, 512); glutInitWindowPosition(100, 200); glutInitContextVersion(3, 2); glutInitContextProfile(GLUT_CORE_PROFILE); glutCreateWindow(argv[0]); glewExperimental = true; glewInit(); Init(); glutDisplayFunc(Display); glutMainLoop(); return 0; }
"https://open.gl/"教程之Textures源码(freeglut版)
标签:
原文地址:http://www.cnblogs.com/jiafenggang/p/5463453.html