如何使用OpenGL将纹理映射应用于Maya对象?

问题描述

我目前正在学习如何使用GLSL将2d纹理映射到3d对象。我有一个main.cpp,片段着色器和顶点着色器来实现此目的,还有我使用Maya和一些PNG图像制作的Sphere.obj。

我刚刚在Maya中创建了一个基本的球面多边形模型,然后将其导出为“ .obj”。

下面列出了我的片段着色器代码以供参考:

#version 410

// Inputs from application.
// Generally,"in" like the eye and normal vectors for things that change frequently,// and "uniform" for things that change less often (think scene versus vertices).  
in vec3 position_eye,normal_eye;
uniform mat4 view_mat;

// This light setup would usually be passed in from the application.
vec3 light_position_world  = vec3 (10.0,25.0,10.0);
vec3 Ls = vec3 (1.0,1.0,1.0);    // neutral,full specular color of light
vec3 Ld = vec3 (0.8,0.8,0.8);    // neutral,lessened diffuse light color of light
vec3 La = vec3 (0.12,0.12,0.12); // ambient color of light - just a bit more than dk gray bg

// Surface reflectance properties for phong or Blinn-phong shading models below.
vec3 Ks = vec3 (1.0,1.0);    // fully reflect specular light
vec3 Kd = vec3 (0.32,0.18,0.5);  // purple diffuse surface reflectance
vec3 Ka = vec3 (1.0,1.0);    // fully reflect ambient light
float specular_exponent = 400.0;   // specular 'power' -- controls "roll-off"

// These come from the VAO for texture coordinates.
in vec2 texture_coords;

// And from the uniform outputs for the textures setup in main.cpp.
uniform sampler2D texture00;
uniform sampler2D texture01;

out vec4 fragment_color;           // color of surface to draw

void main () 
{
    // Ambient intensity
    vec3 Ia = La * Ka;

    // These next few lines sample the current texture coord (s,t) in texture00 and 01 and mix.
    vec4 texel_a = texture (texture00,fract(texture_coords*2.0));
    vec4 texel_b = texture (texture01,fract(texture_coords*2.0));
    //vec4 mixed   = mix (texel_a,texel_b,texture_coords.x);
    vec4 mixed   = mix (texel_a,texture_coords.x);
    Kd.x = mixed.x;
    Kd.y = mixed.y;
    Kd.z = mixed.z; 

    // Transform light position to view space.
    // Vectors here are appended with _eye as a reminder once in view space versus world space.
    // "Eye" is used instead of "camera" since reflectance models often phrased that way.
    vec3 light_position_eye = vec3 (view_mat * vec4 (light_position_world,1.0));
    vec3 distance_to_light_eye = light_position_eye - position_eye;
    vec3 direction_to_light_eye = normalize (distance_to_light_eye);

    // Diffuse intensity
    float dot_prod = dot (direction_to_light_eye,normal_eye);
    dot_prod = max (dot_prod,0.0);
    vec3 Id = Ld * Kd * dot_prod; // final diffuse intensity

    // specular is view dependent; get vector toward camera.
    vec3 surface_to_viewer_eye = normalize (-position_eye);

    // phong    
    //vec3 reflection_eye = reflect (-direction_to_light_eye,normal_eye);
    //float dot_prod_specular = dot (reflection_eye,surface_to_viewer_eye);
    //dot_prod_specular = max (dot_prod_specular,0.0);
    //float specular_factor = pow (dot_prod_specular,specular_exponent);

    // Blinn
    vec3 half_way_eye = normalize (surface_to_viewer_eye + direction_to_light_eye);
    float dot_prod_specular = max (dot (half_way_eye,normal_eye),0.0);
    float specular_factor = pow (dot_prod_specular,specular_exponent);

    // specular intensity
    vec3 Is = Ls * Ks * specular_factor; // final specular intensity

    // final color
    fragment_color = vec4 (Is + Id + Ia,1.0);
}

我在终端中输入以下命令来运行我的程序包: ./go fs.glsl与.glsl Sphere.obj image.png image2.png

我正在尝试使用此方法将world map.jpg映射到我的球体,而忽略第二个图像输入。但它不会运行。有人可以告诉我需要在片段着色器中注释掉什么以忽略第二个纹理输入,以便我的代码运行吗?

PS:我将如何修改片段着色器以实现各种类型的“平铺”?我也对此感到迷茫。任何示例或提示都表示赞赏。

这是我的main.cpp代码的纹理部分。

// load textures
gluint tex00;
int tex00location = glGetUniformlocation (shader_programme,"texture00");
gluniform1i (tex00location,0);
glActiveTexture (GL_TEXTURE0);
assert (load_texture (argv[4],&tex00));
//assert (load_texture ("ship.png",&tex00));

gluint tex01;
int tex01location = glGetUniformlocation (shader_programme,"texture01");
gluniform1i (tex01location,1);
glActiveTexture (GL_TEXTURE1);
assert (load_texture (argv[5],&tex01));

/*---------------------------SET RENDERING DEFAULTS---------------------------*/

// Choose vertex and fragment shaders to use as well as view and proj matrices.

gluniformMatrix4fv (view_mat_location,1,GL_FALSE,view_mat.m);
gluniformMatrix4fv (proj_mat_location,proj_mat.m);

// The model matrix stores the position and orientation transformations for the mesh.
mat4 model_mat;   
model_mat = translate (identity_mat4 () * scale(identity_mat4(),vec3(0.5,0.5,0.5)),vec3(0,-0.5,0)) * rotate_y_deg (identity_mat4 (),90 );

// Setup basic GL display attributes.   
glEnable (GL_DEPTH_TEST);   // enable depth-testing
glDepthFunc (GL_LESS);      // depth-testing interprets a smaller value as "closer"
glEnable (GL_CULL_FACE);    // cull face
glCullFace (GL_BACK);       // cull back face
glFrontFace (GL_ccw);       // set counter-clock-wise vertex order to mean the front
glClearColor (0.1,0.1,1.0);   // non-black background to help spot mistakes
glViewport (0,g_gl_width,g_gl_height); // make sure correct aspect ratio

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)