OpenGL Z-Fighting问题

时间:2019-11-01 19:35:21

标签: android c++ opengl-es

我正在用OpenGL和C ++实现一个Android应用,我渲染了一些具有不同Y位置的三角形,它们都在X-Z平面上,例如,照相机是正投影,其值接近3。

glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LESS);

正投影为:

projectionMatrix = glm::ortho(-(width/2.f)/zoom, (width/2.f)/zoom, -(height/2.f)/zoom,(height/2.f)/zoom,near,far);

这些值是

projectionMatrix = glm::ortho(-100, 100, -197,197,3,50);

有时候我得到的三角形似乎具有Z角战斗力,尽管它们具有不同的Y值,相机从上方看了他们。

            vertexShader = "#version 300 es\n"
                "precision highp float;\n"

                "layout (location = 0) in vec3 position;\n"
                "layout (location = 1) in vec3 normal;\n"
                "layout (location = 2) in vec2 texCoord;\n"
                "\n"
                "uniform mat4 model;\n"
                "uniform mat4 view;\n"
                "uniform mat4 projection;\n"
                "\n"
                "out vec2 TexCoords;\n"
                "out vec3 Normal;\n"
                "out vec3 FragPos;\n"
                "\n"

                "void main(){\n"
                "   Normal = mat3(transpose(inverse(model))) * normal;\n"
                "   TexCoords = texCoord;\n"
                "   FragPos = vec3(model * vec4(position, 1.0));\n"
                "   gl_Position = projection * view * model * vec4(position, 1.0);\n"
                "}";

fragmentShader = "#version 300 es\n"
                "precision highp float;\n"
                "\n"
                "struct DirectLight{\n"
                "   vec3 Direction;"
                "   vec3 color;\n"
                "   float intensity;\n"
                "};\n"
                "out vec4 glFragColor;\n"
                "\n"
                "in vec2 TexCoords;\n"
                "in vec3 Normal;\n"
                "in vec3 FragPos;\n"
                "\n"
                "uniform int hasTexture;\n"
                "uniform int hasSpecular;\n"
                "uniform float alpha;\n"
                "uniform vec3 ViewPos;\n"
                "\n"
                "uniform sampler2D map_diffuse;\n"
                "uniform sampler2D map_specular;\n"
                "\n"
                "uniform vec3 diffuse;\n"
                "uniform vec3 specular;\n"
                "uniform float shininess;\n"
                "uniform DirectLight Sun1;\n"

                "vec3 calcLight(DirectLight light,vec3 diffColor,vec3 specColor){"
                "   vec3 normal      = normalize(Normal);\n"
                "   vec3 lightDir     = normalize(-light.Direction);\n"
                "   float diffFactor  = max(dot(normal,lightDir), 0.0);\n"
                "   vec3 _diffuse     = light.intensity * light.color * diffFactor * diffColor;\n"
                "   vec3 viewDir      = normalize(ViewPos - FragPos);\n"
                "   vec3 halfwayDir   = normalize(lightDir + viewDir);\n"
                "   float specFactor  = pow(max(dot(normal, halfwayDir), 0.0), shininess);\n"
                "   vec3 _specular    = specColor * light.intensity * light.color * specFactor;\n"
                "   return _diffuse + _specular ;\n"
                "}"

                "void main(){\n"
                "   vec4 diffColor;\n"
                "   vec4 specColor;\n"
                "   if(hasTexture == 1)\n"
                "       diffColor = texture(map_diffuse, TexCoords);\n"
                "   else\n"
                "       diffColor = vec4(diffuse,alpha);\n"
                "   if(hasSpecular == 1)\n"
                "       specColor = texture(map_specular, TexCoords);\n"
                "   else\n"
                "       specColor = vec4(specular,alpha);\n"
                "   vec4 _ambient    = vec4(0.4,0.4,0.4,alpha) * diffColor;\n"
                "   vec4 result = vec4("
                "        calcLight(Sun1,diffColor.xyz,specColor.xyz),alpha)"
                "       + _ambient;"
                "   float gamma = 2.2;\n"
                "   result = pow(result, vec4(1.0/gamma));\n"
                "   glFragColor = result;\n"
                "}";

enter image description here

0 个答案:

没有答案