java对opengl绑定_OpenGL 4.0纹理绑定

我正在尝试将多个纹理绑定到片段着色器中的采样器 . 加载代码似乎运行良好 . ATI的CodeXL显示正确加载的纹理 .

但是,当我将模型的纹理绑定到活动纹理0和1时,我无法将其发送到我的着色器 . 当我将着色器制服标记为usampler2D并使用uvec4存储颜色时,就像我应该将纹理作为无符号字节提供一样,我得到一个全白模型 . 当我将着色器均匀更改为sampler2D并使用vec4存储颜色时,我的glUniform1i调用无法再获取着色器变量的位置,因此没有为活动纹理设置任何内容 . 这导致可以使用漫反射纹理,但我无法获得正常纹理 . 从好的方面来说,漫反射纹理就是以这种方式在模型上绘制的 .

我不确定问题是什么 . 我在线检查了几个地方试图解决这个问题,我查看了红皮书 . 我知道我错过了一些东西,或者说某些状态设置错了,但我似乎无法找到它 . 提前感谢您提供任何帮助以解决此问题 .

纹理创建

int[] testWidth;

testWidth = new int[1];

testWidth[0] = 1000;

// First bind the texture.

bind();

// Make sure that textures are enabled.

// I read that ATI cards need this before MipMapping.

glEnable(GL_TEXTURE_2D);

// Test to make sure we can create a texture like this.

glTexImage2D(GL_PROXY_TEXTURE_2D, 0, format, width, height,

0, format, GL_UNSIGNED_BYTE, null);

glGetTexLevelParameteriv(GL_PROXY_TEXTURE_2D, 0, GL_TEXTURE_WIDTH,

testWidth);

if (testWidth[0] == 0)

{

message("Could not load texture onto the graphics card.");

}

else

{

// Not so sure about this part....but it seems to work.

glPixelStorei(GL_PACK_ALIGNMENT, 1);

glPixelStorei(GL_UNPACK_ALIGNMENT, 1);

// Load the texture data.

glTexImage2D(texture_type, 0, format, width, height,

0, format, GL_UNSIGNED_BYTE, (GLvoid[]?)value);

// Smaller mipmaps need linear mipmap coords.

// Larger just uses linear of the main texture.

glTexParameterf(texture_type, GL_TEXTURE_MIN_FILTER,

GL_LINEAR_MIPMAP_LINEAR);

glTexParameterf(texture_type, GL_TEXTURE_MAG_FILTER,

GL_LINEAR);

// Clamp the texture to the edges.

glTexParameterf(texture_type, GL_TEXTURE_WRAP_S,

GL_CLAMP_TO_EDGE);

glTexParameterf(texture_type, GL_TEXTURE_WRAP_T,

GL_CLAMP_TO_EDGE);

glTexParameterf(texture_type, GL_TEXTURE_WRAP_R,

GL_CLAMP_TO_EDGE);

// Generate the mipmaps. The tex parameter is there

// for ATI cards. Again, it's something I read online.

glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_TRUE);

glGenerateMipmap(texture_type);

}

// Now unbind the texture.

unbind();

纹理绑定

if (currentShader != null)

{

currentShader.set_uniform_matrix("model_matrix", ref model_matrix,

true);

if (material != null)

{

if (material.diffuse_texture != null)

{

glActiveTexture(GL_TEXTURE0);

material.diffuse_texture.bind();

currentShader.set_uniform_texture("diffuse_texture",

Constants.DIFFUSE_TEXTURE);

if (material.normal_testure != null)

{

glActiveTexture(GL_TEXTURE1);

material.normal_texture.bind();

currentShader.set_uniform_texture("normal_texture",

Constants.NORMAL_TEXTURE);

}

}

}

// If there is a renderable then render it.

if (renderable != null)

{

renderable.render(1.0);

}

if (material != null)

{

material.unbind();

}

片段着色器

#version 400 core

/**

* Smooth the inward vertex color. Smooth it so that the fragments

* which will be in between the vertices as well can get a value close

* to where they are positioned after being rasterized.

*/

smooth in vec4 vertex_color;

/**

* Smooth the inward texture coordinates. Smooth it so that the

* fragments which will be in between the vertices as well can get a

* value close to where they are positioned after being rasterized.

*/

smooth in vec2 out_texture_coordinate;

/**

* The color to make this fragment.

*/

out vec4 frag_color;

/**

* The models diffuse texture. This will be mapped to index 0.

*/

uniform usampler2D diffuse_texture;

/**

* The models normal texture. This will be mapped to index 1.

*/

uniform usampler2D normal_texture;

/**

* The starting function of the shader.

*/

void main(void)

{

uvec4 diffuseColor;

uvec4 normalModifier;

diffuseColor = texture(diffuse_texture, out_texture_coordinate);

normalModifier = texture(normal_texture, out_texture_coordinate);

// Discard any fragments that have an alpha color less than 0.05.

if (diffuseColor.a < 1.0)

{

// This works as part of depth testing to remove the fragments that

// are not useful.

discard;

}

frag_color = diffuseColor;

}

统一设定

/**

* Sets the uniform value for a texture in the shader.

*

* @param name The name of the uniform to bind this texture to.

* This must have already been registered.

*

* @param textureUnit The id for the texture unit to bind to the uniform.

* This is not the texture's id/reference, but the OpenGL texture unit

* that the reference is bound to.

* This is set by calling glActiveTexture.

*/

public void set_uniform_texture(string name, int textureUnit)

{

// Check to make sure the uniform was given a location already.

if (register_uniform(name) == true)

{

// Set the data for this uniform then.

glUniform1i(uniform_mapping.get(name), textureUnit);

}

else

{

message("Texture was not set. %s", name);

}

}

/**

* Register a uniform for passing data to the shader program.

*

* @return true if the uniform was found with a valid location;

* otherwise, false.

*

* @param name The name for the parameter to get a uniform location for.

* Use this name for the variable in your shader.

*/

public bool register_uniform(string name)

{

int location;

// Make sure we didn't already get the location of the uniform value.

if (uniform_mapping.has_key(name) == false)

{

location = Constants.OPENGL_INVALID_INDEX;

// We have no information about this uniform, so try

// to get it's location.

location = glGetUniformLocation(reference, name);

// The location will 0 or higher if we found the uniform.

if (location != Constants.OPENGL_INVALID_INDEX)

{

uniform_mapping.set(name, location);

return true;

}

}

else

{

// The uniform was previously found and can be used.

return true;

}

debug("Uniform %s not found!!!!!", name);

return false;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
OpenGL,可以使用纹理映射(texture mapping)来绘制多边形。下面是一个基本的多边形绑定纹理的过程: 1. 创建纹理对象 使用`glGenTextures`创建一个纹理对象,并使用`glBindTexture`将其绑定到一个纹理目标上(如`GL_TEXTURE_2D`)。 ```c++ GLuint texture; glGenTextures(1, &texture); glBindTexture(GL_TEXTURE_2D, texture); ``` 2. 加载纹理数据 使用`glTexImage2D`或`glTexStorage2D`将纹理数据加载到纹理对象。 ```c++ int width, height, nrChannels; unsigned char* data = stbi_load("texture.jpg", &width, &height, &nrChannels, 0); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data); stbi_image_free(data); ``` 在这个例子使用了stb_image库加载了一个名为"texture.jpg"的图片作为纹理数据。 3. 设置纹理过滤方式 使用`glTexParameteri`设置纹理的过滤方式。 ```c++ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); ``` 4. 绑定纹理 在绘制多边形之前,使用`glBindTexture`将纹理对象绑定纹理单元上。 ```c++ glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, texture); ``` 在这个例子使用纹理单元0。 5. 绘制多边形 在绘制多边形时,使用纹理坐标(texture coordinates)来指定纹理在多边形上的位置和方向。 ```c++ GLfloat vertices[] = { // 位置 // 纹理坐标 0.5f, 0.5f, 0.0f, 1.0f, 1.0f, // 右上角 0.5f, -0.5f, 0.0f, 1.0f, 0.0f, // 右下角 -0.5f, -0.5f, 0.0f, 0.0f, 0.0f, // 左下角 -0.5f, 0.5f, 0.0f, 0.0f, 1.0f // 左上角 }; GLuint indices[] = { 0, 1, 3, 1, 2, 3 }; ``` 在这个例子使用了一个矩形。 6. 使用纹理坐标 在顶点着色器使用纹理坐标作为输出变量,传递给片段着色器。 ```c++ #version 330 core layout (location = 0) in vec3 aPos; layout (location = 1) in vec2 aTexCoord; out vec2 TexCoord; void main() { gl_Position = vec4(aPos, 1.0); TexCoord = aTexCoord; } ``` 7. 在片段着色器使用纹理 在片段着色器使用`texture`函数获取纹理颜色。 ```c++ #version 330 core out vec4 FragColor; in vec2 TexCoord; uniform sampler2D texture1; void main() { FragColor = texture(texture1, TexCoord); } ``` 在这个例子使用了一个简单的片段着色器,直接将纹理颜色输出。 完整的多边形绑定纹理的代码示例可以参考以下链接: https://learnopengl.com/code_viewer.php?code=getting-started/textures-exercise2
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值