opengl 使用 texture2d 关键步骤


生成 texture2d 的步骤
1. 准备 纹理的像素数据  imageData
2. glGenTextures();  存放在一个 句柄 GLuint texture 
3. glBindTexture() 刚才生成的 Texture, 第一个参数是 GL_TEXTURE_2D
4. glTexImage2D() ,用 imageData 和 刚才绑定的 texture  结合起来,这样texture 生成完毕
5. 清理 imageData 内存
6. glBindTexture()  第一个参数 是 GL_TEXTURE_2D, 第二个传 0. 和 刚才的 texture 句柄 解除绑定




GLuint generateTexture(std::string filePath)
{
int width, height;
unsigned char* image = SOIL_load_image(filePath.c_str(), &width, &height, 0, SOIL_LOAD_RGB);
GLuint texture;
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
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);
return texture;
}


7. 使用 纹理时


glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, texture);
glUniform1i(glGetUniformLocation(shaderProgram, "outTexture"),0);


glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, texture);
glUniform1i(glGetUniformLocation(shaderProgram, "outTexture1"),1);


最后 glUniform1i() 的参数 0,1  指代 shader 里的  sampler2D对应 GL_TEXTURE0, GL_TEXTURE1




对应的 fragment shader:


#version 330 core
out vec4 color;
in vec4 vertexColor;
in vec2 TexCoord;
uniform sampler2D outTexture;
uniform sampler2D outTexture2;
void main()
{
"color = mix(texture(outTexture,TexCoord),texture(outTexture2,TexCoord),0.2);"
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值