我有一个由仅有8个顶点的立方体组成的模型,我遇到了每个顶点定向照明的问题。而不是一次点亮整个模型,每个立方体似乎都像this一样单独点亮。
这是我的顶点着色器:
layout(location = 0)in vec3 vp;
layout(location = 1)in vec3 color;
layout(location = 2)in vec3 normal;
out vec3 fColor;
uniform mat4 model;
uniform mat3 nm;
uniform mat3 partNM;
uniform mat4 modelPart;
uniform mat4 view;
uniform mat4 projection;
void main () {
gl_Position = modelPart * vec4(vp, 1.0f);
gl_Position = model * gl_Position;
gl_Position = view * gl_Position;
gl_Position = projection * gl_Position;
mat3 normalMatrix = partNM*nm;
vec3 normalDirection = normalize(normalMatrix*normal);
vec3 lightDirection = normalize(vec3(-1.0, 1.0, -1.0));
vec3 diffuseReflection = clamp(dot(normalDirection, lightDirection),0.0,1.0);
fColor = color+diffuseReflection;
}
和我的片段着色器:
in vec3 fColor;
out vec4 frag_colour;
void main () {
frag_colour = vec4(fColor.xyz,1.0);
}
这是我用来设置普通矩阵的函数:
void Shader::setNormalMatrix(string name,glm::mat4 matrix) {
glm::mat3 nm = glm::transpose(glm::inverse(glm::mat3(matrix)));
unsigned int location = glGetUniformLocation(program, name.c_str());
glUniformMatrix3fv(location, 1, false, &nm[0][0]);
}
和为我的立方体生成顶点和法线的函数:
std::vector<float> Cube::createCube(float size,float x,float y,float z,float r, float g, float b) {
VertexType points[8];
points[0].x = (x*size)+0.0f;
points[0].y = (y*size)+0.0f;
points[0].z = (z*size)+size;
points[0].nx = 0.577350;
points[0].ny = 0.577350;
points[0].nz = -0.577350;
points[0].r = r;
points[0].g = g;
points[0].b = b;
points[1].x = (x*size)+size;
points[1].y = (y*size)+0.0f;
points[1].z = (z*size)+size;
points[1].nx = -0.577350;
points[1].ny = 0.577350;
points[1].nz = -0.577350;
points[1].r = r;
points[1].g = g;
points[1].b = b;
points[2].x = (x*size)+size;
points[2].y = (y*size)+size;
points[2].z = (z*size)+size;
points[2].nx = -0.577350;
points[2].ny = -0.577350;
points[2].nz = -0.577350;
points[2].r = r;
points[2].g = g;
points[2].b = b;
points[3].x = (x*size)+0.0f;
points[3].y = (y*size)+size;
points[3].z = (z*size)+size;
points[3].nx = 0.577350;
points[3].ny = -0.577350;
points[3].nz = -0.577350;
points[3].r = r;
points[3].g = g;
points[3].b = b;
points[4].x = (x*size)+0.0f;
points[4].y = (y*size)+0.0f;
points[4].z = (z*size)+0.0f;
points[4].nx = 0.577350;
points[4].ny = 0.577350;
points[4].nz = 0.577350;
points[4].r = r;
points[4].g = g;
points[4].b = b;
points[5].x = (x*size)+size;
points[5].y = (y*size)+0.0f;
points[5].z = (z*size)+0.0f;
points[5].nx = -0.577350;
points[5].ny = 0.577350;
points[5].nz = 0.577350;
points[5].r = r;
points[5].g = g;
points[5].b = b;
points[6].x = (x*size)+size;
points[6].y = (y*size)+size;
points[6].z = (z*size)+0.0f;
points[6].nx = -0.577350;
points[6].ny = -0.577350;
points[6].nz = 0.577350;
points[6].r = r;
points[6].g = g;
points[6].b = b;
points[7].x = (x*size)+0.0f;
points[7].y = (y*size)+size;
points[7].z = (z*size)+0.0f;
points[7].nx = 0.577350;
points[7].ny = -0.577350;
points[7].nz = 0.577350;
points[7].r = r;
points[7].g = g;
points[7].b = b;
std::vector<float> rPoint;
for(VertexType p:points) {
rPoint.push_back(p.x);
rPoint.push_back(p.y);
rPoint.push_back(p.z);
rPoint.push_back(p.r);
rPoint.push_back(p.g);
rPoint.push_back(p.b);
rPoint.push_back(p.nx);
rPoint.push_back(p.ny);
rPoint.push_back(p.nz);
}
return rPoint;
}
模型分为几个部分,这就是我有两个普通矩阵和模型矩阵的原因;一个用于整个模型,一个用于模型的单个部分。我的代码是否有问题,或者我是否需要使用每片段光照来修复此错误?
答案 0 :(得分:3)
您的问题是网格的拓扑。在立方体的角落3面相遇。这些面部中的每一个都具有不同的法线。这会在法线拓扑中产生不连续性。或者将其简化为更简单的术语。每个角必须使用3个顶点,每个面一个,面法线指向正确的方向。
当你在它的时候,你可以移除那些不可见的立方体面。
答案 1 :(得分:0)
原因是您将每个多维数据集渲染为单独的模型。因此,对于每个模型,着色器将运行一次,在每种情况下,每个立方体运行一次。要解决这个问题,您需要将整个模型(机器人)渲染为一个模型,使用一组顶点,而不是一组立方体。