首先生成纹理索引
GLuint textures[] = { -1, -1 };
glGenTextures(2, textures);
glGenTextures第一个参数是生成texture索引的个数,第二个是索引的指针.
通过生成索引之后会发现textures的值变成{1,2}
//绑定纹理
glBindTexture(GL_TEXTURE_2D, textures[0]);
// Set the texture wrapping parameters
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); // Set texture wrapping to GL_REPEAT (usually basic 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);
int width, height;
unsigned char* image = SOIL_load_image("textures/container.jpg", &width, &height, 0, SOIL_LOAD_RGB);
//openGL是个状态机 直接把数据传给已经绑定的纹理
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, image);
//生成MipMap
glGenerateMipmap(GL_TEXTURE_2D);
SOIL_free_image_data(image);
//解除绑定
glBindTexture(GL_TEXTURE_2D, 0);
通过glUniform1i函数把纹理传给片段着色器
ourShader.Use();
// Bind Textures using texture units
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, textures[0]);
glUniform1i(glGetUniformLocation(ourShader.Program, "ourTexture1"), 0);
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, textures[1]);
glUniform1i(glGetUniformLocation(ourShader.Program, "ourTexture2"), 1);