Освещение кажется перевернутым в моей сцене OpenGLC++

Программы на C++. Форум разработчиков
Ответить
Anonymous
 Освещение кажется перевернутым в моей сцене OpenGL

Сообщение Anonymous »

Я делаю сцену со светом и Icosphere в OpenGL. Треугольники сферы, обращенной к свету, по -видимому, не освещаются (по какой -то причине)

спереди вида

< /p>
, однако, на Speergles on triang. Свет) ярко освещен странным способом

Back View

< /p>
(я выбрал фронт и использую CCW) < /p>
(я работаю перед фронтами и использованием CCW) < /p>
. Проверьте нормы, и они кажутся правильными

normals

< /p>
Я также уверен, что треурииргиconstexpr auto t = glm::golden_ratio();
positions = {
{-1, t, 0}, { 1, t, 0}, {-1, -t, 0}, { 1, -t, 0},
{ 0, -1, t}, { 0, 1, t}, { 0, -1, -t}, { 0, 1, -t},
{ t, 0, -1}, { t, 0, 1}, {-t, 0, -1}, {-t, 0, 1}
};
for (auto &p : positions) p = glm::normalize(p);

std::vector init_indices = {
0,5,11, 0,1,5, 0,7,1, 0,10,7, 0,11,10,
1,9,5, 5,4,11, 11,2,10, 10,6,7, 7,8,1,
3,4,9, 3,2,4, 3,6,2, 3,8,6, 3,9,8,
4,5,9, 2,11,4, 6,10,2, 8,7,6, 9,1,8
};

indices.assign(std::begin(init_indices), std::end(init_indices));

// Map for midpoint cache
std::map mid_cache;

auto get_mid_point = [&](GLuint a, GLuint b) -> GLuint {
auto key = std::minmax(a, b);
if (mid_cache.contains(key)) return mid_cache[key];
glm::vec3 mid = glm::normalize((positions[a] + positions) * 0.5f);
positions.push_back(mid);
GLuint idx = positions.size() - 1;
mid_cache[key] = idx;

return idx;
};

for (int i = 0; i < subdivisions; ++i) {
std::vector new_indices;
new_indices.reserve(indices.size() * 4);
for (size_t j = 0; j < indices.size(); j += 3) {
GLuint a = indices[j];
GLuint b = indices[j+1];
GLuint c = indices[j+2];

GLuint ab = get_mid_point(a, b);
GLuint bc = get_mid_point(b, c);
GLuint ca = get_mid_point(c, a);

new_indices.insert(new_indices.end(), {a, ca, ab});
new_indices.insert(new_indices.end(), {b, ab, bc});
new_indices.insert(new_indices.end(), {c, bc, ca});
new_indices.insert(new_indices.end(), {ab, ca, bc});
}
indices = std::move(new_indices);
}

// Flatten vertices
std::vector vertex_data;
vertex_data.reserve(positions.size() * (has_normals ? 3 : 0));

for (auto const &p : positions) {
glm::vec3 normal = glm::normalize(p);
glm::vec3 scaled = p * radius;

vertex_data.push_back(scaled.x);
vertex_data.push_back(scaled.y);
vertex_data.push_back(scaled.z);

if (has_normals) {
vertex_data.push_back(normal.x);
vertex_data.push_back(normal.y);
vertex_data.push_back(normal.z);
}
}
< /code>
vertex shader: < /p>
#version 330 core

struct Material {
vec3 ambient;
vec3 diffuse;
vec3 specular;
float shininess;
};

struct Light {
vec3 position;
vec3 color;
float intensity;
};

#define MAX_LIGHTS 4
uniform Light lights[MAX_LIGHTS];
uniform int lightCount;

uniform Material material;

uniform vec3 viewPos;

uniform bool shouldDrawBaseTexture;
uniform sampler2D baseTexture;

in vec3 FragPos;
in vec3 Normal;
in vec2 TexCoord;

out vec4 FragColor;

void main()
{
vec3 norm = normalize(Normal);
vec3 viewDir = normalize(viewPos - FragPos);

vec3 lighting = vec3(0.0);

for (int i = 0; i < lightCount; ++i) {
// Ambient
vec3 ambient = material.ambient * lights.color * lights.intensity;

// Diffuse
vec3 lightDir = normalize(lights.position - FragPos);
float diff = max(dot(norm, lightDir), 0.0);
vec3 diffuse = material.diffuse * diff * lights.color * lights.intensity;

// Specular
vec3 reflectDir = reflect(-lightDir, norm);
float spec = pow(max(dot(viewDir, reflectDir), 0.0), material.shininess);
vec3 specular = material.specular * spec * lights.color * lights.intensity;

lighting += ambient + diffuse + specular;
}

if (shouldDrawBaseTexture) {
vec3 texColor = texture(baseTexture, TexCoord).rgb;
FragColor = vec4(clamp(lighting * texColor, 0.0, 1.0), 1.0);
} else {
FragColor = vec4(clamp(lighting, 0.0, 1.0), 1.0);
}
}

< /code>
Ожидается ли это поведение? Какова причина, по которой спины зажжены?

Подробнее здесь: https://stackoverflow.com/questions/797 ... engl-scene
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «C++»