[英]Multiple texture mapping with openGL
I tried to search online but I couldn't understand how to add a texture for my model. 我尝试在线搜索,但我无法理解如何为我的模型添加纹理。 (It can be seted from the start) I having trouble with it and I would like for some help in the changes I should make with my code. (它可以从一开始就选择)我遇到了麻烦,我想在我的代码中做出一些改变。
My texture setting function is: 我的纹理设置功能是:
void Texture::setTexture(unsigned char* data, unsigned int width, unsigned int height) {
glGenTextures(1, &m_texture[unit]); // Genatate 1 texture in m_texture
glBindTexture(GL_TEXTURE_2D, m_texture[unit]);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
My bind function: 我的绑定功能:
void Texture::Bind(unsigned int unit) {
glActiveTexture(GL_TEXTURE0 + unit);
glBindTexture(GL_TEXTURE_2D, m_texture[unit]);
And my vertex shaders looks like this: 我的顶点着色器看起来像这样:
attribute vec3 position; // Position values
attribute vec2 texCoord; // Texture coordinates values
attribute vec3 normal; // Normal
varying vec2 texCoord0;
varying vec3 normal0;
uniform mat4 transform;
void main()
{
gl_Position = transform * vec4(position, 1.0);
texCoord0 = texCoord;
normal0= (transform * vec4(normal, 0.0)).xyz;
}
fragment shader: 片段着色器:
varying vec2 texCoord0;
varying vec3 normal0;
uniform sampler2D diffuse;
void main()
{
gl_FragColor = texture2D(diffuse, texCoord0) *
clamp(dot(-vec3(0,0,1), normal0), 1.0, 1.0);
}
Shader ctor: Shader ctor:
Shader::Shader(const string& fileName)
{
m_program = glCreateProgram();
m_shaders[0] = CreateShader(LoadShader(fileName + ".vs"), GL_VERTEX_SHADER);
m_shaders[1] = CreateShader(LoadShader(fileName + ".fs"), GL_FRAGMENT_SHADER);
for (unsigned int i = 0; i < NUM_SHADERS; i++)
glAttachShader(m_program, m_shaders[i]);
glBindAttribLocation(m_program, 0, "position");
glBindAttribLocation(m_program, 1, "texCoord");
glBindAttribLocation(m_program, 2, "normal");
glLinkProgram(m_program);
glValidateProgram(m_program);
m_uniforms[TRANSFORM_U] = glGetUniformLocation(m_program, "transform");
}
I want to choose choose some vertices to use the first texture and some the other one. 我想选择一些顶点来使用第一个纹理而选择另一个顶点。 For example, a cube which each square has a different texture. 例如,每个正方形具有不同纹理的立方体。
How can I add another texture? 如何添加其他纹理? And what changes should I make in my Mesh class? 我应该在Mesh课程中做些什么改变?
Sorry for the stupid question, I'm new in openGL :) Thanks in advance! 抱歉这个愚蠢的问题,我是openGL的新手:)提前谢谢!
Don't know if this is what you're looking for. 不知道这是不是你想要的。 But, here i go.. 但是,我走了......
In the fragment shader, you would be using multiple sampler2D
for example: 在片段着色器中,您将使用多个sampler2D
,例如:
uniform sampler2D tex0;
uniform sampler2D tex1;
And you need to do some adjustment to the c++ code too assigning the sampler2D
like so: 你需要对c ++代码进行一些调整,如下所示分配sampler2D
:
glUniform1i(glGetUniformLocation(shader, "tex0"), 0);
glUniform1i(glGetUniformLocation(shader, "tex1"), 1);
The 0
and 1
is the active texture number from GL_TEXTURE0
and GL_TEXTURE1
. 0
和1
是GL_TEXTURE0
和GL_TEXTURE1
的活动纹理编号。 it would be N if it is GL_TEXTUREN
. 如果它是GL_TEXTUREN
它将是GL_TEXTUREN
So that being said, you should bind those texture before with the according glActiveTexture
like so: 所以说,你应该使用相应的glActiveTexture
绑定那些纹理, glActiveTexture
所示:
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, tex0);
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, tex1);
Which i believe you already did so. 我相信你已经这样做了。
Oh, and perhaps you can use array too in the fragment shader like so: 哦,也许您可以在片段着色器中使用数组,如下所示:
uniform sampler2D tex[2];
And the c++ code would be something like this: 而c ++代码将是这样的:
for (int i = 0; i < 2; i++) {
glUniform1i(glGetUniformLocation(shader, "tex[" + std::to_string(i) + "]"), i);
}
Edit: I haven't test if "tex[" + std::to_string(i) + "]"
works. 编辑:我没有测试"tex[" + std::to_string(i) + "]"
有效。 But i think you get the idea. 但我认为你明白了。 Might be ("tex[" + std::to_string(i) + "]").c_str()
可能是 ("tex[" + std::to_string(i) + "]").c_str()
Edit: Talking about arrays, there is also something called texture arrays. 编辑:谈论数组,还有一些叫做纹理数组的东西。 For details and more options consider seeing this 有关详细信息和更多选项,请考虑查看此
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.