简体   繁体   English

通过ASSIMP了解OBJ加载

[英]Understanding OBJ loading via ASSIMP

I am using ASSIMP 4.0.1 by compiling my own binaries. 我通过编译自己的二进制文件来使用ASSIMP 4.0.1。 I keep getting the normals loaded incorrectly. 我一直没有正确加载法线。 I have reduced my cube.obj down to just a single triangle ... and I would like to confirm that it is doing it wrong and that I'm not insane. 我已将我的cube.obj减小到仅一个三角形...,我想确认它做错了并且我不是疯了。 Finally how can I fix it would also be a question. 最后,我该如何解决它也是一个问题。 Here's what the OBJ file looks like: OBJ文件如下所示:

mtllib cube.mtl
g cube Mesh
usemtl cube Mesh_cube

v -0.500000 -0.500000 0.500000
v 0.500000 -0.500000 0.500000
v -0.500000 0.500000 0.500000
v 0.500000 0.500000 0.500000
v -0.500000 0.500000 -0.500000
v 0.500000 0.500000 -0.500000
v -0.500000 -0.500000 -0.500000
v 0.500000 -0.500000 -0.500000
vn 0.000000 0.000000 1.000000
vn 0.000000 1.000000 0.000000
vn 0.000000 0.000000 -1.000000
vn 0.000000 -1.000000 0.000000
vn 1.000000 0.000000 0.000000
vn -1.000000 0.000000 0.000000
vt 0.000000 0.000000
vt 1.000000 0.000000
vt 0.000000 1.000000
vt 1.000000 1.000000
f 1/1/1 2/2/1 3/3/1

now given the last line, I'm loading a single triangle with the attributes: 现在给出最后一行,我正在加载具有属性的单个三角形:

装样

What I'm drawing is the normal of the triangle and this clearly says it's (0.0, 1.0, 0.0) since it's RGB with green being the color. 我正在绘制的是三角形的法线,这显然是(0.0,1.0,0.0),因为它是RGB,绿色是颜色。 The triangle is created with an offset (translation) that's -30.0f away from origin in the x-axis and shaded directly with normal passed into fragment shader from vertex shader. 创建的三角形的偏移量(平移)与x轴的原点相距-30.0f,并使用法线直接从顶点着色器传递到片段着色器中进行着色。 At this point, I'm suspecting that this obj file is incorrect? 此时,我怀疑此obj文件不正确? Notice the last normal, (number 3 that is), why is it (0.0, 0.0, -1.0) ? 注意最后一个法线(即3),为什么是(0.0,0.0,-1.0)? Shouldn't this be (0.0, 0.0, 1.0) (pointing outwards from screen)? 这不是(0.0,0.0,1.0)(从屏幕向外指向)吗? Finally why is this green instead of red from vertex 1 and green to vertex 2 and black at vertex 3? 最后,为什么从顶点1到顶点2是绿色而不是绿色,而顶点3是绿色? I am using the following vertex and fragment shaders: 我正在使用以下顶点和片段着色器:

#version 430
in vec3 POSITION;
in vec4 NORMAL;
in vec2 TEXCOORD0;

out vec4 normal;
out vec2 uv0;

uniform mat4 ModelMatrix;
uniform mat4 ViewProjectionMatrix;
uniform mat4 NormalMatrix;
uniform vec3 CameraPosition;

void main()
{
    //normal = normalize(ModelMatrix * NORMAL);
    normal = normalize(NORMAL);
    uv0 = TEXCOORD0;

    vec4 worldPosition = ModelMatrix * vec4(POSITION, 1.0);
    //toCamera = normalize(CameraPosition - worldPosition.xyz);

    gl_Position = ViewProjectionMatrix * worldPosition;
}

-- -

#version 430
in vec4 normal;
in vec2 uv0;

out vec4 outFragColor;

uniform sampler2D TEXTURE_0;

uniform vec3 DirectionalLightDirection;
uniform vec4 DiffuseReflectance;
uniform float Shininess;

//Blinn-Phong lighting ... TODO make this a macro

float calculateDiffuseTerm(vec3 N, vec3 L)
{
    return clamp(dot(N, L), 0.0, 1.0);
}

float calculateSpecularTermWithHalfVector(vec3 N, vec3 L, vec3 V)
{
    float term = 0.0;
    if(dot(N, L) > 0.0)
    {
        vec3 H = normalize(L+V);
        term = pow(dot(N, H), Shininess);
    }
    return term;
}

void main()
{
    //Calculate diffuse
    vec4 textureColor = texture(TEXTURE_0, uv0);

    vec3 directionalLight = -1.0 * DirectionalLightDirection;
    float diffuseTerm = calculateDiffuseTerm(normalize(normal.xyz), directionalLight);

    outFragColor = normalize(normal);// diffuseTerm * DiffuseReflectance;
}

At this point, I'm suspecting that this obj file is incorrect? 此时,我怀疑此obj文件不正确? Notice the last normal, (number 3 that is), why is it (0.0, 0.0, -1.0) ? 注意最后一个法线(即3),为什么是(0.0,0.0,-1.0)?

Why do you say that? 为什么这么说 Your OBJ file defines a triangle with all 3 vertices sharing the normal 1 (in OBJs one-based indexing), which is clearly (0,0,1) , and is also what assimp did load. 您的OBJ文件定义了一个三角形,其中所有3个顶点共享法线1(在OBJ中基于一个索引的索引中),显然是(0,0,1) ,这也是assimp加载的内容。

Wavefront OBJ uses the convention Wavefront OBJ使用约定

vertex/texcoord/normal 

and not vertex/normal/texcoord as you seem to expect. 而不是您期望的vertex/normal/texcoord

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM