Direct3D 11将纹理应用于球体

时间:2013-07-30 15:23:20

标签: visual-c++ directx textures direct3d directx-11

我正在尝试将纹理应用于球体。我使用了一个3d建模软件来创建带有顶点,法线和texcoords导出为.obj文件的球体。我解析文件并使用数据创建顶点缓冲区。 IDE是Visual C ++ 2012。 纹理是一个512x256 jpg的地球(原来,是吧?) 它看起来像this 看起来并不像预期的那样。正在尝试不同的UV映射而没有运气。

加载资源:

D3DX11CreateShaderResourceViewFromFile(dev,        // the Direct3D device
    L"earth.jpg",    
    NULL,           
    NULL,           
    &pTexture,      
    NULL);          
devcon->PSSetShaderResources(0, 1, &pTexture);

解析文件:

if (fileIn) {
    while (fileIn) {
        VERTEX vertex;
        inChar = fileIn.get();
        std::wstring string;
        switch (inChar)
        {       
        case '#':
            inChar = fileIn.get();
            while(inChar != '\n')
                inChar = fileIn.get();
            break;
        case 'v':
            inChar = fileIn.get();
            if (inChar == ' ')  //v - vert position
            {
                fileIn >> x >> y >> z;                      
                vVector.push_back(D3DXVECTOR3(x, y, z));
                vertCount++;
            }
            else if(inChar == 'n')  //vn - vert normal
            {
                fileIn >> x >> y >> z;                      
                nVector.push_back(D3DXVECTOR3(x, y, z));                    
                normCount++;
            }
            else if(inChar == 't')  //vt - vert tex coords
            {           
                fileIn >> u >> v;                       
                tVector.push_back(D3DXVECTOR2(u, v));
                texCount++;
            }
            break;
        case 'f': // assuming vectors are filled..
            inChar = fileIn.get();
            if (inChar != ' ')
                break;
            while (!fileIn.eof()) {
                std::getline(fileIn, string);

                size_t pos = 0;
                while((pos = string.find(L"/", pos)) != std::string::npos)
                {
                    string.replace(pos, 1, L" ");
                    pos++;
                }
                if (string.find(L"f ") != std::wstring::npos)
                    string = string.replace(0, 2, L"");
                std::wistringstream iss(string);
                iss.seekg(0, std::ios::beg);
                while (iss >> i) {
                    iVector.push_back(i);
                }
            }
            break;
        }
    }
}

int vv, vt, vn;
for (i = 0; i < iVector.size(); i += 3) {
    vv  = iVector.at(i) - 1;
    vt = iVector.at(i + 1) - 1;
    vn = iVector.at(i + 2) - 1;
    VERTEX vertex;
    vertex.X = vVector.at(vv).x;
    vertex.Y = vVector.at(vv).y;
    vertex.Z = vVector.at(vv).z;
    vertex.U = tVector.at(vt).x;
    vertex.V = tVector.at(vt).y;
    vertex.Normal.x = nVector.at(vn).x;
    vertex.Normal.y = nVector.at(vn).y;
    vertex.Normal.z = nVector.at(vn).z;
    vector.push_back(vertex);
}

像素着色器:

float4 outputColor = (1.0f, 0.0f, 1.0f, 1.0f);
if (calc == 0) {
    outputColor = lightAmbient;
    float3 lightVector = lightPos.xyz - input.worldPosition.xyz;
    float3 normalVector = input.normal.xyz;
    float distance = length(lightVector);
    //if (distance > range) 
        //return outputColor;
    float diffuseBrightness = saturate(dot(normalize(normalVector), normalize(lightVector)));
    outputColor += diffuseBrightness;
    //outputColor *= lightAtt.x + (lightAtt.y * distance) + (lightAtt.z * (distance * distance));
    outputColor = saturate(outputColor);

    outputColor *= Texture.Sample(ss, input.texcoord);
}
return outputColor;

有什么想法吗?帮助赞赏。可以找到OBJ文件here

0 个答案:

没有答案