使用glium点亮旋转立方体的所有面。

huangapple go评论71阅读模式
英文:

How to light all faces of rotating cube using glium

问题

我使用glium制作了旋转的立方体,但只有一个面被着色。
我使用 Gouraud阴影(来自glium教程)来实现光照。
这个立方体由24个顶点和6个法线定义。

旋转是通过两个矩阵实现的:

        let m = [
            [1.0, 0.0, 0.0, 0.0],
            [0.0, t.cos(), -t.sin(), 0.0],
            [0.0, t.sin(), t.cos(), 0.0],
            [0.0, 0.0, 0.0, 1.0f32]
        ];

        let n = [
            [t.cos(), 0.0, t.sin(), 0.0],
            [0.0, 1.0, 0.0, 0.0],
            [-t.sin(), 0.0, t.cos(), 0.0],
            [0.0, 0.0, 0.0, 1.0f32]
        ];

片段着色器(glsl):

#version 150

in vec3 v_normal;
out vec4 color;
uniform vec3 u_light;

void main() {
    float brightness = dot(normalize(v_normal), normalize(u_light));
    vec3 dark_color = vec3(0.6, 0.0, 0.0);
    vec3 regular_color = vec3(1.0, 0.0, 0.0);
    color = vec4(mix(dark_color, regular_color, brightness), 1.0);
}

光源:

let light = [-1.0, 0.4, 0.9f32];

光应该在旋转的某一点照射到立方体的另一个面上。
我尝试改变光的方向,但没有改变任何东西。
有人有什么想法吗?

有关该立方体的更多信息,请参见此处:我的以前的问题。 (一些信息,如旋转,在此处不是最新的)

立方体的图像:
使用glium点亮旋转立方体的所有面。
使用glium点亮旋转立方体的所有面。

英文:

I made rotating cube using glium, but only one face is colored.
I am using Gouraud shading(from glium tutorial) for lighting.

The cube is made by defined 24 vertices and 6 normals.

Rotation is made by two matrices:

        let m = [
            [1.0, 0.0, 0.0, 0.0],
            [0.0, t.cos(), -t.sin(), 0.0],
            [0.0, t.sin(), t.cos(), 0.0],
            [0.0, 0.0, 0.0, 1.0f32]
        ];

        let n = [
            [t.cos(), 0.0, t.sin(), 0.0],
            [0.0, 1.0, 0.0, 0.0],
            [-t.sin(), 0.0, t.cos(), 0.0],
            [0.0, 0.0, 0.0, 1.0f32]
        ];

Fragment shader(glsl):

#version 150

in vec3 v_normal;
out vec4 color;
uniform vec3 u_light;

void main() {
    float brightness = dot(normalize(v_normal), normalize(u_light));
    vec3 dark_color = vec3(0.6, 0.0, 0.0);
    vec3 regular_color = vec3(1.0, 0.0, 0.0);
    color = vec4(mix(dark_color, regular_color, brightness), 1.0);
}

light:

let light = [-1.0, 0.4, 0.9f32];

The light should hit another face of cube in some point of rotation.
I try changing direction of light, that don't change anything.
Have anybody some idea?

for more info about the cube see this: my previous q. (some info like rotation is not actual here)

Images of the cube:
使用glium点亮旋转立方体的所有面。
使用glium点亮旋转立方体的所有面。

答案1

得分: 1

只要翻译代码部分:

normalize(VIEW_MATRIX * light)
max(dot(N, L), 0.0)
英文:

I think you have your normal direction in eye space and light direction in world space. So you have to convert light direction into camera (eye) space before passing into the shader, something like that (pseudo code):

normalize(VIEW_MATRIX * light)

Also, for the brightness I think you don't want to have negative value, so it have to be in a form

max(dot(N, L), 0.0)

I would recommend to read an introduction article about lighting, for example on learnOpenGL - https://learnopengl.com/Lighting/Basic-Lighting where you can see a description on what direction are for and how to prepare them.
or some GLSL tutorials about having light direction in camera space - https://www.lighthouse3d.com/tutorials/glsl-tutorial/directional-lights/

huangapple
  • 本文由 发表于 2023年2月20日 00:07:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/75501501.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定