There is a problem in OpenGL that when creating a large world, everything rests on the accuracy of float. Also, artifacts with lighting occur at large coordinates. How can I fix this problem without switching to double? If possible, give an example of the code, the C++ language

You have a large WORLD space, right?

The problem is large translations in the MODELING and VIEWING transformations. These end up consuming all your sig-figs for single-precision float to capture the magnitude, leaving few-to-no sigfigs for representing small differences in value.

To avoid this problem:

- Store your MODELING and VIEWING transforms using double-precision floats.
- Compute the MODELVIEW matrix (their product) using double-precision floats.
- Convert that to a single-precision float matrix, and upload that to the GPU for performing vertex transforms.
- Input geometry positions remain single-precision float (or less, depending on your needs).

That’s the standard way to solve this problem.

With this method, the GPU doesn’t have to do anything with double-precision floats … which is good, because it’s double performance is typically horrible, if it supports it at all.

3 Likes

Thank you very much! It worked, but I’m a little confused about the lighting, the lantern lighting stopped working:

Vertex shader

```
#version 330 core
layout (location = 0) in vec3 position;
layout (location = 1) in vec3 normal;
layout (location = 2) in vec2 aTexCoords;
uniform mat4 modelView;
uniform mat4 projection;
uniform vec3 lightPos;
out vec3 FragPos;
out vec3 Normal;
out vec2 TexCoords;
void main()
{
gl_Position = projection * modelView * vec4(position, 1.0);
FragPos = vec3(modelView * vec4(position, 1.0));
Normal = mat3(transpose(inverse(modelView))) * normal;
TexCoords = vec2(aTexCoords.x, aTexCoords.y);
}
```

Fragment shader

```
#version 330 core
out vec4 color;
uniform sampler2D texture_diffuse1;
uniform sampler2D texture_specular1;
struct SpotLight {
vec3 position;
vec3 direction;
float cutOff;
float outerCutOff;
float shininess;
float constant;
float linear;
float quadratic;
vec3 ambient;
vec3 diffuse;
vec3 specular;
};
in vec3 FragPos;
in vec3 Normal;
in vec2 TexCoords;
in vec3 LightPos;
uniform vec3 viewPos;
uniform SpotLight light;
vec3 calcSpotLight(SpotLight light, vec3 normal, vec3 fragPos, vec3 viewDir);
void main()
{
vec3 norm = normalize(Normal);
vec3 viewDir = normalize(-FragPos);
vec3 result = calcSpotLight(light, norm, FragPos, viewDir);
color = vec4(result, 1.0);
}
vec3 calcSpotLight(SpotLight light, vec3 normal, vec3 fragPos, vec3 viewDir)
{
vec3 lightDir = normalize(light.position - fragPos);
float diff = max(dot(normal, lightDir), 0.0);
vec3 reflectDir = reflect(-lightDir, normal);
float spec = pow(max(dot(viewDir, reflectDir), 0.0), light.shininess);
float distance = length(light.position - fragPos);
float attenuation = 1.0 / (light.constant + light.linear * distance + light.quadratic * (distance * distance));
float theta = dot(lightDir, normalize(-light.direction));
float epsilon = light.cutOff - light.outerCutOff;
float intensity = clamp((theta - light.outerCutOff) / epsilon, 0.0, 1.0);
vec3 ambient = light.ambient * vec3(texture(texture_diffuse1, TexCoords));
vec3 diffuse = light.diffuse * diff * vec3(texture(texture_diffuse1, TexCoords));
vec3 specular = light.specular * spec * vec3(texture(texture_specular1, TexCoords));
ambient *= attenuation * intensity;
diffuse *= attenuation * intensity;
specular *= attenuation * intensity;
return (ambient + diffuse + specular);
}
```

Passing it to the shader:

```
shader.setVec3("light.position", view * glm::dvec4(camera.position, 1.0));
shader.setMat4("modelView", glm::mat4(view * model));
```

I’m doing something wrong, but what?