用OpenGl渲染.obj文件

Render .obj Files with OpenGl

本文关键字:文件 obj 渲染 OpenGl      更新时间:2023-10-16

我想用c++加载。obj文件,用OpenGl绘制它的形状和材质到场景中。为了完成加载,我使用了syoyo的tinyobjectloader。加载效果非常好,我可以用静态颜色或静态漫反射纹理来绘制物体。所以我能够给所有的真实性和纹理坐标到着色器使用缓冲区。我现在的问题是如何在像素着色器中设置正确的颜色。加载器类给出的形状有一个网格和一个材质,定义如下:

typedef struct {
    std::string name;
    float ambient[3];
    float diffuse[3];
    float specular[3];
    float transmittance[3];
    float emission[3];
    float shininess;
    float ior;                // index of refraction
    float dissolve;           // 1 == opaque; 0 == fully transparent
    // illumination model (see http://www.fileformat.info/format/material/)
    int illum;
    std::string ambient_texname;
    std::string diffuse_texname;
    std::string specular_texname;
    std::string normal_texname;
    std::map<std::string, std::string> unknown_parameter;
} material_t;
typedef struct
{
    std::vector<float>          positions;
    std::vector<float>          normals;
    std::vector<float>          texcoords;
    std::vector<unsigned int>   indices;
} mesh_t;

我想设置所有的颜色和不同的纹理作为统一。例如,漫反射颜色可以这样设置:

GLuint diffColLocation = glGetUniformLocation(programmId, "diffuseColor");
glUniform3f(diffColLocation, this->material.diffuse[0], this->material.diffuse[1], this->material.diffuse[2]);

和这样的纹理:

glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, this->ambientTextureID);
glUniform1i(this->diffuseTextureUniformIndex, 0);
glActiveTexture(GL_TEXTURE0 + 1);
glBindTexture(GL_TEXTURE_2D, this->diffuseTextureID);
glUniform1i(this->diffuseTextureUniformIndex, 0);
etc.

我还能够加载一个对象,其中所有形状都有一个漫反射纹理。所以这里没有问题。我的问题列表,如何处理未设置的纹理。例如,许多对象没有镜面纹理集。我可以创建一个默认纹理吗?

我的像素着色器现在看起来是这样的:

#version 150 core
#extension GL_ARB_explicit_attrib_location: enable
// output to the (default) FBO in color buffer 0
layout (location = 0) out vec4 outColor;
in vec2 fragTextCoords;
uniform sampler2D ambientTex;
uniform sampler2D diffuseTex;
uniform sampler2D specularTex;
uniform vec3 ambientColor;
uniform vec3 diffuseColor;
uniform vec3 specularColor;
// main entry point for the fragment shader
void main() {
    //vec3 tempColor ambientColor + diffuseTex * specularTex;     //Is that correct?
    //outcolor = tempColor * texture(diffuseTex, fragTextCoords); //Then this?
      outcolor = texture(diffuseTex, fragTextCoords);
}

所以只有纹理被显示,这只有在每个形状都有漫反射纹理时才有效。但我如何将每种颜色和纹理组合在一起,使物体看起来就像我的3d软件一样?如果其中一种颜色为空或sampler2D为空会发生什么?

您提到的值是您模型的各种材料的配置值。它们在Phong照明模型中用于在渲染模型时计算像素的最终颜色。你必须事先使用glMaterial()和glLight()函数分别设置灯光和材质设置。

关于打开和关闭设置,尽量避免使用if语句,除非没有其他方法可以完成您想要做的事情。您还希望避免每次要设置制服时都要查找制服的位置。位置不变,操作成本相当高。

另一个注意事项是,如果您多次设置uniform变量的纹理ID,就像在您的示例中一样,您不需要每次都将其设置为0。该变量将保留其值,并简单地引用您设置的最后一个纹理作为您使用glBindTexture() 绑定的最后一个纹理。

如果有帮助的话,这里是我用于手机照明的着色器组合。你应该能够很容易地修改它们来打开或关闭纹理。否则,lighthouse3d是开始使用GLSL的一个很好的资源。

顶点着色器:

varying vec3 normal;
varying vec3 position;
void main( void )
{
    gl_Position = ftransform();
    gl_TexCoord[0] = gl_MultiTexCoord0;
    normal = gl_NormalMatrix * gl_Normal;
    position = ( gl_ModelViewMatrix * gl_Vertex ).xyz;
}

片段着色器:

varying vec3 normal;
varying vec3 position;
uniform sampler2D diffuseTexture;
vec4 lightSource(vec3 norm, vec3 view, gl_LightSourceParameters light)
{
    vec3 lightVector = normalize(light.position.xyz - view);
    vec3 reflection = normalize(lightVector - normalize(view.xyz));
    float diffuseFactor = max(0, dot(norm, lightVector));
    float specularDot = max(0, dot(norm, reflection));
    float specularFactor = pow(specularDot, gl_FrontMaterial.shininess);
    return 
        gl_FrontMaterial.ambient * light.ambient +
        gl_FrontMaterial.diffuse * light.diffuse * diffuseFactor +
        gl_FrontMaterial.specular * light.specular * specularFactor;
}
vec4 lighting()
{
    // normal might be damaged by linear interpolation.
    vec3 norm = normalize(normal);
    return
        gl_FrontMaterial.emission +
        gl_FrontMaterial.ambient * gl_LightModel.ambient +
        lightSource(norm, position, gl_LightSource[0]);
}
void main()
{
    gl_FragColor = texture2D(diffuseTexture, gl_TexCoord[0].st) * lighting();
}