4D星宇

c++

  C++博客 :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理 ::
  57 随笔 :: 0 文章 :: 39 评论 :: 0 Trackbacks

2008年12月8日 #

              改写代码心得
       由于BLOOD渲染器的API由DX9转向DX10,在转化架构和改写代码的过程中,出现很多异常。
在测试过程中,发现很多错误都是由于没有进行正确的初始化造成的。比如分配内存的容间,变量的正确赋值等等。已调完VB,IB,TEXTURE,RENDERTARGET等部分,还有MESH等大部未调。
       特此MARK,以作记录。
posted @ 2008-12-08 10:21 bloodbao 阅读(120) | 评论 (0)编辑 收藏

2008年11月9日 #

DX9下使用
D3DXPLANE plane; 
 D3DXPlaneFromPointNormal( &plane, &vPoint, &vNormal ); //生成这个平面
D3DXMatrixReflect( &matReflect, &plane ); //取得该平面的反射矩阵
//设置剪切平面,使反射面上的内容被渲染,面下的被丢弃
 m_pd3dDevice->SetClipPlane( 0, plane );
 m_pd3dDevice->SetRenderState( D3DRS_CLIPPLANEENABLE, 0x01 );
DX10下模拟:
User Clip PlanesUser Clip Planes are emulated by specifying a clip distance output from the Vertex Shader with the SV_ClipDistance[n] flag, where n is either 0 or 1. Each component can hold up to 4 clip distances in x, y, z, and w giving a total of 8 clip distances.

    用户裁减平面通过在VS里设定一个SV_ClipDistance[n]标记,定义一个裁减距离输出得到,n为0或1。这里一共能存放8个Clip Plane距离,分别使用数组两个元素的x,y,z,w通道。

In this scenario, each clip planes is defined by a plane equation of the form:

在这个场景里,每个clip plane被一个平面方程定义:

Ax + By + Cz + D =0;

Where <A,B,C> is the normal of the plane, and D is the distance of the plane from the origin. Plugging in any point <x,y,z> into this equation gives its distance from the plane. Therefore, all points <x,y,z> that satisfy the equation Ax + By + Cz + D = 0 are on the plane. All points that satisfy Ax + By + Cz + D < 0 are below the plane. All points that satisfy Ax + By + Cz + D > 0 are above the plane.

<A,B,C>是平面法向,D是平面到原点的距离。把任意点<x,y,z>代入方程能得到它到平面的距离。所有满足方程=0的点在平面上,<0的点在平面下而 >0的点在平面上。

In the Vertex Shader, each vertex is tested against each plane equation to produce a distance to the clip plane. Each of the three clip distances are stored in the first three components of the output component with the semantic SV_ClipDistance0. These clip distances get interpolated over the triangle during rasterization and clipped if the value every goes below 0.

    在VS中,每个顶点会带入平面方程做测试。每个三角形的Clip距离存在SV_ClipDistance0语义输出的前三个通道中。这个距离在光栅化中被线性插值,所有小于0的像素被剔除。

posted @ 2008-11-09 20:47 bloodbao 阅读(729) | 评论 (0)编辑 收藏

2008年10月23日 #

近期较闲,主要修改rendering架构,减少DX9的接口,全面使用DX10的接口,修改变动非常大,初调试三千多个ERROR,现减为850多个,不过,内部错误还是很多。希望能在DX11发布前,全部改完。第二步,会建立更庞大的ISCENENODE,以满足快速测试和新版SCENEEDIT需要。暂记下。
posted @ 2008-10-23 21:55 bloodbao 阅读(104) | 评论 (0)编辑 收藏

2008年9月18日 #

不知不觉,九月已经过去太半了,而我却由于沉迷于游戏,反而没学到什么东西,这样浪费时间,真的是一种罪过。 过了一个多月时间,只简单的支持了XML和一点点LIGHT。小猫的引擎,离我承诺的精彩的水面,漂亮的粒子系统,复杂的天气系统,流畅的骨骼动画和人工智能,越来越远。 革命远未成功,小猫还需努力,为了钱和生活,多一点自制,少玩一些游戏吧! 退路已断,前方无路,人生岂能如些堕落。猫行天下,人生何缺悲壮,也需几多拼搏。十分耕耘,万分努力,或许成功! 期小猫自勉!
posted @ 2008-09-18 21:58 bloodbao 阅读(107) | 评论 (0)编辑 收藏

2008年8月11日 #

                     Main Features in my blood engine
Direct3D 9 is used to render the scene
User input is gathered though DirectInput
Single pass multi-texturing using the fixed-function pipeline (FFP)
Light-mapping
Frustum culling
Simple skybox
Keyframe Animation with GPU-based frame interpolation through a custom vertex and pixel shader written in HLSL. This technique is also known as Vertex Tweening or even Morph Target
Support for Quake2 models (.md2)
Support for DirectX models (.x)
Support for PCX texture loading in addition to the other image formats already supported by Direct3D
Support for Quake3:Arena levels
Lightmaps
Curved surfaces using Bezier patches
Partial support of Quake3 materials and effects (.shader)
Uses the BSP/PVS to quickly discard non visible geometry
Collision detection using the BSP tree (supports ray, sphere and box sweeps)
A flexible scene graph system where entities can be attached to each other in order to perform hierarchical transformations and geometry culling
Quake-like player movement physics
A powerful in-game console system:
Outputs vital information
Can take command inputs from the user
Console variables can be dynamically edited at runtime
Commands and console variable settings can be loaded from a user-specified text file (cfg)
Garbage collection of unused resources through reference counting
Control keys can be dynamically changed in-game through the console, using the bind command
User input is abstracted through an Action Manager which maps inputs to actions
A smart chase-camera controller that will detect collisions with the world to prevent the view from being occluded by other pieces of world geometry.
Error handling through exceptions
The in-game HUD can display vital performance statistics about the game, such as frames per second, total number of triangles on the screen, number of textures loaded, etc…
posted @ 2008-08-11 15:05 bloodbao 阅读(127) | 评论 (0)编辑 收藏

2008年8月6日 #

       为了这次面试,我休息了很多天,都没工作。结果,得到的又是一个失败的结局。分析原因,都认为我的C++基础很薄弱吧,要不就是因为我以前从事的工作和现在的工作差别太大了。
      作为一名新手,我很迷茫,在学习的道路我没有被打败,却在两次面试中,深受打击。我不仅在想,这是否是我要选择的路。仅剩半个多月的时间,暑期就结束了,我再不出去,就没机会了。事实上,做不做游戏,我无所谓,但现在的生活一直不是我想要的。我不断地在否定自已的路上,越走越远,找不到真实的我。
      两个月很快就过去了,我只能静下心来去做事情,把这些烦人的事,都抛于脑后。路还是要走,人总是要成长的,希望能从失败中走出来。
      记下今天的失败,以作自勉。

posted @ 2008-08-06 10:36 bloodbao 阅读(297) | 评论 (10)编辑 收藏

2008年7月20日 #


主要是用模板缓冲区来实现。
DepthStencilState StencilShadow
{
    DepthEnable = true;
    DepthWriteMask = ZERO;
    DepthFunc = LESS;
   
    StencilEnable = true;
    StencilReadMask = 0xFFFFFFFF;
    StencilWriteMask = 0xFFFFFFFF;
   
    FrontFaceStencilFunc = ALWAYS;
    FrontFaceStencilPass = INCR;
    FrontFaceStencilFail = Keep;
   
    BackFaceStencilFunc = ALWAYS;
    BackFaceStencilPass = DECR;
  BackFaceStencilFail = Keep;
};
BlendState AdditiveBlending
{
    AlphaToCoverageEnable = FALSE;
    BlendEnable[0] = TRUE;
    SrcBlend = SRC_ALPHA ;
    DestBlend = INV_SRC_ALPHA ;
    BlendOp = ADD;
    SrcBlendAlpha = ZERO;
    DestBlendAlpha = ZERO;
    BlendOpAlpha = ADD;
    RenderTargetWriteMask[0] = 0x0F;
};

Rendering Shadows

At the top level, the rendering steps look like the following:

  • If ambient lighting is enabled, render the entire scene with ambient only.
  • For each light in the scene, do the following:
    • Disable depth-buffer and frame-buffer writing.
    • Prepare the stencil buffer render states for rendering the shadow volume.
    • Render the shadow volume mesh with a vertex extruding shader. This sets up the stencil buffer according to whether or not the pixels are in the shadow volume.
    • Prepare the stencil buffer render states for lighting.
    • Prepare the additive blending mode.
    • Render the scene for lighting with only the light being processed.

Shadow Volume并不是一个没有缺陷的技术。除了高像素填充率和阴影边界检测外,在绘制过程中还可能出现错误。错误的主要原因是当一个几何模型在计算自阴影时,它的面通常被完全照亮或者完全变暗,这取决于这个面是否朝向光源。光照计算必须使用顶点法向而非表面法向。对于接近平行光源的面,它会被完全照亮或者完全变暗,而实际上应该部分处于光源中。这是从stencil shadow volume中继承来的问题,在计算阴影时必须被考虑。这个问题可以通过增加mesh密度来解决,这也增加了处理mesh的时间。顶点法向和面法向越接近,表面的问题就越少。如果程序不能把问题限制在可接受的范围内,就必须考虑使用其他的算法,比如PRT或者Shadow Map。
posted @ 2008-07-20 10:48 bloodbao 阅读(226) | 评论 (0)编辑 收藏

Fixed-function Lighting Pipeline
ColorsOutput CalcLighting( float3 worldNormal, float3 worldPos, float3 cameraPos )
{
    ColorsOutput output = (ColorsOutput)0.0;
   
    for(int i=0; i<8; i++)
{
     //光线方向
        float3 toLight = g_lights[i].Position.xyz - worldPos;
         //离光源距离
        float lightDist = length( toLight );
         //atten=(1/(a[2]*d*d+a[1]*d+a[1])
        float fAtten = 1.0/dot( g_lights[i].Atten, float4(1,lightDist,lightDist*lightDist,0) );
        float3 lightDir = normalize( toLight );
         //H
        float3 halfAngle = normalize( normalize(-cameraPos) + lightDir );
       
         //Phong方程,逐顶点光照
        output.Diffuse += max(0,dot( lightDir, worldNormal ) * g_lights[i].Diffuse * fAtten) + g_lights[i].Ambient;
        output.Specular += max(0,pow( dot( halfAngle, worldNormal ), 64 ) * g_lights[i].Specular * fAtten );
    }
   
    return output;
}
简单ALPHA测试
//
// PS for rendering with alpha test
//
float4 PSAlphaTestmain(PSSceneIn input) : COLOR0
{       
         float4 color =  tex2D( g_samLinear, g_txDiffuse, input.tex ) * input.colorD;
         if( color.a < 0.5 )
                 discard;
         return color;
}
雾化:

//
// Calculates fog factor based upon distance
//
// E is defined as the base of the natural logarithm (2.71828)
float CalcFogFactor( float d )
{
         float fogCoeff = 1.0;
        
         if( FOGMODE_LINEAR == g_fogMode )
         {
                 fogCoeff = (g_fogEnd - d)/(g_fogEnd - g_fogStart);
         }
         else if( FOGMODE_EXP == g_fogMode )
         {
                 fogCoeff = 1.0 / pow( E, d*g_fogDensity );
         }
         else if( FOGMODE_EXP2 == g_fogMode )
         {
                 fogCoeff = 1.0 / pow( E, d*d*g_fogDensity*g_fogDensity );
         }
        
         return clamp( fogCoeff, 0, 1 );
}

Finally, the pixel shader uses the fog factor to determine how much of the original color and how much of the fog color to output to the pixel.

最后,PS使用雾化参数确定雾颜色和纹理颜色的混合程度。
return fog * normalColor + (1.0 - fog)*g_fogColor;

posted @ 2008-07-20 10:31 bloodbao 阅读(213) | 评论 (0)编辑 收藏

2008年7月17日 #

刚开始学习DX10,发觉模型文件已经从原来的X格式变为SDKMESH格式,也就是说DX10不直接支持X文件了,
那现在该怎么办,我在NVSDK下找到了他的解决方案,先用DX9的接口打开X文件,再用DX10接口来渲染文件。
         在DX10下,缺少了很多以前在DX9下的元素。比如,光照,材质等。
         要实现这些元素,就必须在SHADER下手动去实现,那就意味着你必须熟悉图形学的内容,特别是其中的光照模型等内容。
比如,方向光的实现:
    //directional light-----------------------------------------------------------------
    float3 lightDir = g_lightPos - In.worldPos;
    float3 lightDirNorm = normalize(lightDir);
    float3 SDir = normalize( g_lightPos - g_eyePos);
    float cosGammaDir = dot(SDir, V);
    float dirLighting = g_Kd*dirLightIntensity*saturate( dot( N,lightDirNorm ) );
    //diffuse
    float3 diffuseDirLight = dirLighting*exDir;       
    //airlight
    float3 dirAirLight = phaseFunctionSchlick(cosGammaDir)* dirLightIntensity*float3(1-exDir.x,1-exDir.y,1-exDir.z);
    //specular
    float3 specularDirLight = saturate( pow(  dot(lightDirNorm,reflVect),g_specPower)) * dirLightIntensity * g_KsDir * exDir;
点光源的实现:
  //point light 1---------------------------------------------------------------------
    //diffuse surface radiance and airlight due to point light
    float3 pointLightDir = g_PointLightPos - In.worldPos;
    //diffuse
    float3 diffusePointLight1 = calculateDiffusePointLight(0.1,Dvp,g_DSVPointLight,pointLightDir,N,V);
    //airlight
    float3 airlight1 = calculateAirLightPointLight(Dvp,g_DSVPointLight,g_VecPointLightEye,V);
    //specular
    float3 specularPointLight = Specular(g_PointLightIntensity, g_KsPoint, length(pointLightDir), Dvp, g_specPower, normalize(pointLightDir), reflVect);
计算点光源的漫射光:
float3 calculateDiffusePointLight(float Kd,float Dvp,float Dsv,float3 pointLightDir,float3 N,float3 V)
{

    float Dsp = length(pointLightDir);
    float3 L = pointLightDir/Dsp;
    float thetas = acos(dot(N, L));
    float lightIntensity = g_PointLightIntensity * 100;
   
    //spotlight
    float angleToSpotLight = dot(-L, g_SpotLightDir);
    if(g_useSpotLight)
    {    if(angleToSpotLight > g_cosSpotlightAngle)
             lightIntensity *= abs((angleToSpotLight - g_cosSpotlightAngle)/(1-g_cosSpotlightAngle));
         else
             lightIntensity = 0;        
    }  
   
    //diffuse contribution
    float t1 = exp(-g_beta.x*Dsp)*max(cos(thetas),0)/Dsp;
    float4 t2 = g_beta.x*Gtable.SampleLevel(samLinearClamp, float2((g_beta.x*Dsp-g_diffXOffset)*g_diffXScale, (thetas-g_diffYOffset)*g_diffYScale),0)/(2*PI);
    float rCol = (t1+t2.x)*exp(-g_beta.x*Dvp)*Kd*lightIntensity/Dsp;
    float diffusePointLight = float3(rCol,rCol,rCol); 
    return diffusePointLight.xxx;
}
计算高光:
float3 Specular(float lightIntensity, float Ks, float Dsp, float Dvp, float specPow, float3 L, float3 VReflect)
{
    lightIntensity = lightIntensity * 100;
    float LDotVReflect = dot(L,VReflect);
    float thetas = acos(LDotVReflect);

    float t1 = exp(-g_beta*Dsp)*pow(max(LDotVReflect,0),specPow)/Dsp;
    float4 t2 = g_beta.x*G_20table.SampleLevel(samLinearClamp, float2((g_beta.x*Dsp-g_20XOffset)*g_20XScale, (thetas-g_20YOffset)*g_20YScale),0)/(2*PI);
    float specular = (t1+t2.x)*exp(-g_beta.x*Dvp)*Ks*lightIntensity/Dsp;
    return specular.xxx;
}
下一步,考虑如何不通过DX9接口,直接导入X文件。

posted @ 2008-07-17 10:26 bloodbao 阅读(695) | 评论 (2)编辑 收藏

2008年7月15日 #

       唉,终于放署假了,我应该做些什么了。
      本来想写个程序的,可是我发觉自已的自控力太差了。刚摸到一本小说,就被迷住了,以至于这一周都在看那本小说,程序方面是一点进度都有。那是本关于SC的小说,但为一个星际迷,我深深地被吸引了,每当我一摸到电脑,就不自觉地点进小说的网页中,进入那个SC的虚幻空间。
看完小说,本以为还能干点什么,结果又下了本DOOM启示录,又迷进去了。哎,看来小说是不能碰啦,我可不想永远呆在这个鬼地方。
等找到工作,再看小说吧。
posted @ 2008-07-15 15:53 bloodbao 阅读(79) | 评论 (0)编辑 收藏

仅列出标题  下一页