OpenGL - vértices normales en OBJ
Quiero saber cómo puedo usar las normales de vértice para el efecto del rayo. Actualmente, lo que tengo es que puedo enviar tanto los vértices como los patrones de textura al sombreador y usarlos, pero con los normales, no sé cómo usarlos en el programa del sombreador. A continuación se muestra lo que tengo hasta ahora.
// vertex shader
layout(location = 0) in vec4 vert;
layout(location = 1) in vec4 color;
layout(location = 2) in vec2 texcoord;
uniform mat4 m_model;
uniform mat4 m_view;
uniform mat4 m_proj;
void main() {
gl_Position = m_proj * m_view * m_model * vert;
}
// fragment shader
in vec2 fragtexcoord;
out vec4 color;
uniform sampler2D textureunit;
void main(void) {
color = texture(textureunit, fragtexcoord);
}
EDITAR Aquí están mis sombreadores por ahora.
sombreador de vértices
layout(location = 0) in vec4 vert;
layout(location = 1) in vec4 color;
layout(location = 2) in vec2 texcoord;
layout(location = 3) in vec4 normal;
out vec4 LightIntensity;
uniform vec4 LightPosition;
uniform vec4 Kd;
uniform vec4 Ld;
uniform mat4 m_model;
uniform mat4 m_view;
uniform mat4 m_proj;
void main() {
gl_Position = m_proj * m_view * m_model * vert;
mat4 normalmatrix = transpose(inverse(m_view));
vec4 tnorm = normalize(normalmatrix * normal);
vec4 eyeCoords = m_model * vec4(vert);
vec4 s = normalize(vec4(LightPosition - eyeCoords));
LightIntensity = Ld * Kd * max(dot(s, tnorm), 0.0);
}
Fragmento sombreador.
in vec4 LightIntensity;
out vec4 color;
void main(void) {
color = vec4(LightIntensity);
}
Actualmente obtengo un cubo negro sin sombreado. Probablemente hice algo mal aquí en el sombreador que no tengo idea de cuál :(
ACTUALIZACIÓN
vértice
layout(location = 0) in vec4 vert;
layout(location = 1) in vec4 color;
layout(location = 2) in vec2 texcoord;
layout(location = 3) in vec4 normal;
out vec2 fragtexcoord;
out vec4 fragnormal;
uniform mat4 m_model;
uniform mat4 m_view;
uniform mat4 m_proj;
void main() {
gl_Position = m_proj * m_view * m_model * vert;
fragtexcoord = texcoord;
fragnormal = normal;
}
fragmento
in vec2 fragtexcoord;
in vec4 fragnormal;
out vec4 fragment_color;
uniform sampler2D textureunit;
void main(void) {
vec4 lt_ambient = vec4(0.2, 0.2, 0.2, 1.0);
vec4 lt_direct = vec4(0.8, 0.8, 0.8, 1.0);
vec4 lt_direct_dir = vec4(1.5, 1.0, 1.0, 1.0);
vec4 color = texture(textureunit, fragtexcoord);
fragment_color = (lt_ambient + (lt_direct * dot(lt_direct_dir, -fragnormal))) * color;
}
No sé qué poner para lt_direct_dir, por eso tiene valores así :)
ACTUALIZACIÓN A continuación se muestran los sombreadores de trabajo para mí.
// vertex shader
layout(location = 0) in vec4 vert;
layout(location = 1) in vec4 color;
layout(location = 2) in vec2 texcoord;
layout(location = 3) in vec4 normal;
out vec4 fragposition;
out vec4 fragcolor;
out vec4 fragnormal;
out vec2 fragtexcoord;
uniform mat4 m_model;
uniform mat4 m_view;
uniform mat4 m_proj;
uniform vec4 lightpos;
void main() {
gl_Position = m_proj * m_view * m_model * vert;
mat4 m_normal = transpose(inverse(m_model));
fragposition = m_model * vert;
fragnormal = m_normal * normal;
fragtexcoord = texcoord;
}
// fragment shader
in vec4 fragposition;
in vec4 fragnormal;
in vec2 fragtexcoord;
out vec4 fragment_color;
uniform sampler2D textureunit;
void main() {
vec4 lt_pnt_pos = vec4(2.5, 2.5, 2.5, 1.0);
vec4 lt_pnt_col = vec4(0.8, 0.8, 0.8, 1.0);
vec4 lt_amb_col = vec4(0.2, 0.2, 0.2, 1.0);
vec4 lt_dir = normalize(lt_pnt_pos - fragposition);
float li = dot(fragnormal, lt_dir);
if(li < 0.0) {
li = 0.0;
}
vec4 color = texture(textureunit, fragtexcoord);
fragment_color = color * (lt_amb_col + (lt_pnt_col * li));
}