简体   繁体   English

GLSL - 两种纹理(不混合)

[英]GLSL - Two textures (Not mixed)

I'm working with textures with GLSL. 我正在使用GLSL进行纹理处理。 How can I handle two textures with GLSL? 如何用GLSL处理两个纹理? One guy recommended me to do two samplers2D in my GLSL... But how can GLSL knows which samplers2D should use? 一个人建议我在我的GLSL中做两个samplers2D ......但GLSL怎么知道哪个samplers2D应该使用? (I am not talking about mixing the textures...) (我不是在谈论混合纹理......)

I heard that I should use glBindTexture. 我听说我应该使用glBindTexture。 How Can I do this? 我怎样才能做到这一点? With glBindTexture? 使用glBindTexture? Anyone has an example of this? 有人有这方面的例子吗?

openGL 3.3 openGL 3.3


Edit: 编辑:

I have this: 我有这个:

uniform Sampler2D texture1;
uniform Sampler2D texture2;

I need to draw two objects, using textures, so how can GLSL know if he should use texture1 or texture2 according to the object I want to draw. 我需要使用纹理绘制两个对象,因此GLSL如何知道他是否应该根据我想要绘制的对象使用texture1或texture2。 That's my question. 那是我的问题。

You need to bind each texture to a different texture unit, and then use multitexture coords. 您需要将每个纹理绑定到不同的纹理单元,然后使用多纹理坐标。 It would look something like this (assuming you already have the textures) : 它看起来像这样(假设你已经有纹理):

glActiveTexture (GL_TEXTURE0);  // First texture is going into texture unit 0
glBindTexture (GL_TEXTURE_2D, tex0);
glEnable (GL_TEXTURE_2D);

glActiveTexture (GL_TEXTURE1);  // Second texture is going into texture unit 1
glBindTexture (GL_TEXTURE2D, tex1);
glEnable (GL_TEXTURE_2D);

glUseProgram (yourGLSLProgramID);
glUniform1i (sampler1Location, 0); // tell glsl that sampler 1 is sampling the texture in texture unit 0
glUniform1i (sampler2Location, 1); // tell glsl that sampler 2 is sampling the texture in texture unit 1
///... set the rest of your uniforms...

glBegin (GL_QUADS);
    glMultiTexCoord2f(GL_TEXTURE0, 0.0, 0.0);
    glMultiTexCoord2f(GL_TEXTURE1, 0.0, 0.0);
    glVertexCoord2f(0.0, 0.0);

    glMultiTexCoord2f(GL_TEXTURE0, 1.0, 0.0);
    glMultiTexCoord2f(GL_TEXTURE1, 1.0, 0.0);
    glVertexCoord2f(width, 0.0);

    glMultiTexCoord2f(GL_TEXTURE0, 1.0, 1.0);
    glMultiTexCoord2f(GL_TEXTURE1, 1.0, 1.0);
    glVertexCoord2f(width, height);

    glMultiTexCoord2f(GL_TEXTURE0, 0.0, 1.0);
    glMultiTexCoord2f(GL_TEXTURE1, 0.0, 1.0);
    glVertexCoord2f(0.0, height);
glEnd();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM