- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我试图对我的地形进行纹理处理,但它似乎要么采用纹理的平均颜色,要么从纹理中选取一种颜色并在所有地方使用它。我看过其他解决方案,但它们对我没有帮助。我是新手所以这可能是一个简单的错误
纹理是从 SOIL2 生成的
生成纹理
int texID = SOIL_load_OGL_texture(
filename.c_str(),
SOIL_LOAD_AUTO,
SOIL_CREATE_NEW_ID,
SOIL_FLAG_MIPMAPS | SOIL_FLAG_INVERT_Y | SOIL_FLAG_NTSC_SAFE_RGB | SOIL_FLAG_COMPRESS_TO_DXT
);
生成 UV 坐标(删除了对我的问题不重要的内容)
int i=0;
for (int z = 0; z < m_Size; z++) // loop through columns
{
for (int x = 0; x < m_Size; x++) // loop through rows
{
model.mesh.uvs[i] = Math::Vector2(x / m_Size, z / m_Size); //Terrain is square
i++;
}
创建纹理缓冲区
if (model->mesh.numOfUVs != 0)
{
glGenBuffers(1, &model->textureBufferID);
glBindBuffer(GL_ARRAY_BUFFER, model->textureBufferID);
glBufferData(GL_ARRAY_BUFFER, model->mesh.numOfUVs * sizeof(Math::Vector2), model->mesh.uvs, GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(Math::Vector2), (void*)(0));
}
绘制地形(非完整代码)
glBindVertexArray(ID);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, model->textureID1);
unsigned int textureLocation = glGetUniformLocation(shaderID, "grassTex");
glUniform1i(textureLocation, 0);
glUniformMatrix4fv(glGetUniformLocation(shaderID, "MVP"), 1, false, &newMVP.mat[0][0]);
glDrawElements(GL_TRIANGLES, model->mesh.numOfIndices, GL_UNSIGNED_INT, (void*)0);
glBindVertexArray(0);
垂直着色器
#version 330 core
layout(location = 0) in vec2 texturecoord;
uniform mat4 MVP;
out vec2 texcoord;
void main(){
texcoord = texturecoord;
gl_Position = MVP * vec4(in_position, 1);
}
片段着色器
#version 330 core
layout(location = 0) out vec4 out_colour;
in vec2 texcoord;
uniform sampler2D grassTex;
void main()
{
out_colour = texture2D(grassTex, texcoord);
}
如果有些事情没有意义,那可能是因为我删除了很多代码以尝试减少我发布的代码量
它看起来像什么(图片中有 3 个纹理,但我删除了这篇文章中的代码,因为我认为它与我使用的纹理数量没有任何关系) https://imgur.com/a/rSPp7Ru
最佳答案
问题是整数除法。当你除以整数时,你得到一个整数。这是您生成 UV 坐标的地方。
for (int z = 0; z < m_Size; z++) // loop through columns
{
for (int x = 0; x < m_Size; x++) // loop through rows
{
model.mesh.uvs[i] = Math::Vector2(x / m_Size, z / m_Size); //Terrain is square
i++;
}
}
x
和 z
总是小于 m_Size
。这意味着 x/m_Size
和 z/m_Size
始终是 0
因为您不能将 0.2
存储在整数
。由于所有 UV 坐标都是 0, 0
,因此您只能看到整个三角形中纹理的一个角(我认为是左下角)。
要解决此问题,请将其中一个操作数转换为 float
或 double
。
model.mesh.uvs[i] = Math::Vector2(
static_cast<float>(x) / m_Size,
static_cast<float>(z) / m_Size
);
如果将 float
除以 int
,int
将提升为 float
,因此 float
将执行除法。
关于c++ - OpenGL,纹理输出为纯色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55770999/
我正在尝试更改iOS背景颜色,但是我认为透明度(alpha:1)似乎在起作用并改变了预期的颜色。 In **AppDelegate.m** rootView.backgroundColor = [
https://www.dropbox.com/s/4zkhtdv4yaqhpxy/Screenshot%20from%202015-01-28%2010%3A42%3A02%201.png?dl=0
照明不工作,对象显示为纯色。颜色本身随 Material 或照明参数而变化,但没有阴影或任何纯灰色。 部分代码如下: glPolygonMode(GL_FRONT_AND_BACK, GL_FILL)
ppt幻灯片生成时,系统默认是无色背景填充,幻灯片设计需要手动设置背景效果,可设置颜色填充或者图片背景填充。本文将对此介绍具体实现方法。 jar文件导入方法(参考): 步骤1:在java程序中可
我是一名优秀的程序员,十分优秀!