Hirdetés

Aktív témák

  • 3man

    csendes tag

    A szamitogepes jatekok ket fontos eleme a fenyek es a mappok. Ezek hatarozzak meg leginkabb a latvanyt.
    Uj utasitas lep be, a saturate(). Ez 0 es 1 koze limitalja az erteket. Shaderekben ezzel helyettesitheto v=min(max(v,0),1)

    A form_to_tex2D segitsegevel felepitek egy teszmappot. Ez egy 2 dimenzios kepnek felel meg, ami majd a magassag map lesz a parallaxhoz.Ebbol a create_normalmap() egy normalmappot general , ami termeszetesen a normalokat fogja tarolni.

    normal=normal*0.5+float3(0.5,0.5,0.5);
    Ez a sor eltunteti a negativ ertekeket, de ugy, hogy kesobb vissza lehessen hozni oket. Igy
    float3 normal=(normal_map-float3(0.5,0.5,0.5))*(-2.0);

    A shaderekben hasonloan lehet elerni a texturakat a programbol.
    float3 normal_map=tex2D[1][txcoord_hw];

    Igazan erdekes akkor tortenik, amikor aktivalom ezt a sort.
    //#define enable_lighting
    A felulet bevilagitodik, es nem is akar hogy. A normalmappot, mint egyszeru bumpot hasznalja a program. Lehetne meg onarnyekot szamolni a feluletre, talan majd kesobb.

    float saturate(float par)
    {
    if(par>1.0) return 1.0;
    if(par<0.0) return 0.0;
    return par;
    }

    const int map_size=512;
    float3 tex2D[4][map_size*map_size];

    void form_to_tex2D(int xcenter,int ycenter,int size,int type)
    {
    size/=2;

    for(int y=ycenter-size;y<ycenter+size;y++)
    for(int x=xcenter-size;x<xcenter+size;x++)
    {
    int kx=abs(x-xcenter);
    int ky=abs(y-ycenter);
    float z=tex2D[0][y*map_size+x].r;

    switch(type)
    {
    case 0:
    z=0.0;
    break;
    case 1:
    z=1.0;
    break;
    case 2:
    z=1.0-(float)((kx>ky)? kx:ky)/50.0 ;
    break;
    case 3:
    float rad=sqrt(kx*kx+ky*ky)/50.0;
    if(rad<1.0)
    z=1.0-rad ;
    break;
    }
    tex2D[0][y*map_size+x]=float3(z,z,z);
    }
    }
    void create_normalmap()
    {
    for(int y=0;y<map_size-1;y++)
    for(int x=0;x<map_size-1;x++)
    {
    float scale=30.0;
    float3 pixel =float3(x ,tex2D[0][y*map_size+x].r*scale, y);
    float3 pixel_u=float3(x+1,tex2D[0][y*map_size+x+1].r*scale, y);
    float3 pixel_v=float3(x ,tex2D[0][y*map_size+x+map_size].r*scale, y+1);

    float3 temp=(pixel_u-pixel);
    float3 normal=temp.cross(pixel_v-pixel);
    normal.Normalize();

    normal=normal*0.5+float3(0.5,0.5,0.5);

    tex2D[1][y*map_size+x]=normal;
    }
    }
    void mapsetup()
    {
    form_to_tex2D(map_size/2,map_size/2,map_size,1);
    form_to_tex2D(map_size/2,map_size/2,map_size-100,0);

    form_to_tex2D(180,180,100,2);
    form_to_tex2D(320,320,100,2);
    form_to_tex2D(180,320,100,3);
    form_to_tex2D(320,180,100,3);

    create_normalmap();
    }

    //#define enable_lighting

    void draw_function()

    {
    float3 LookAt=float3(0,0,0);
    float3 Eyes=float3(350,250,130);
    float3 ViewMatrix_z=LookAt-Eyes;
    ViewMatrix_z.Normalize();
    float3 ViewMatrix_x=ViewMatrix_z.cross(float3(0,1,0));
    float3 ViewMatrix_y=ViewMatrix_x.cross(ViewMatrix_z);

    float3 light_position=float3(-300,320,-200);
    mapsetup();

    for(int y=-y_size/2;y<y_size/2;y++)
    for(int x=-x_size/2;x<x_size/2;x++)
    {
    float3 screen=float3(x,y,300);
    float3 ray=ViewMatrix_x*screen.x + ViewMatrix_y*screen.y + ViewMatrix_z*screen.z;
    ray.Normalize();

    float3 plane_origo=float3(0,0,0);
    float3 plane_normal=float3(0,1,0).Normalize();
    float dist=plane_normal.dot(plane_origo-Eyes)/plane_normal.dot(ray);

    float3 plane=Eyes + ray*dist;

    plane.y=0.0;
    float3 color=plane*0.002;
    float3 txcoord=plane*0.002;

    int txcoord_hw=(((int)(txcoord.z*map_size))&(map_size-1))*map_size + (((int)(txcoord.x*map_size))&(map_size-1));
    float3 height_map=tex2D[0][txcoord_hw];
    float3 normal_map=tex2D[1][txcoord_hw];
    color=normal_map;

    #ifdef enable_lighting
    float3 normal=(normal_map-float3(0.5,0.5,0.5))*(-2.0);
    float3 plane2light=light_position-plane;
    plane2light.Normalize();

    float diffuse_light=saturate(plane2light.dot(normal));
    color*=diffuse_light;

    float3 eyes2plane=plane-Eyes;
    eyes2plane.Normalize();
    float3 reflection_dir=eyes2plane - normal*normal.dot(eyes2plane)*2.0;
    float3 light_color=float3(1.0,0.9,0.5);
    float specular_light=pow(saturate(reflection_dir.dot(plane2light)),20.0);
    color=lerp(color,light_color,specular_light);
    #endif

    if(dist<0.0) color=lerp(float3(0,1,1),float3(0,0,1),ray.y);//sky_color

    pixelv(screen,color);
    }
    }

Aktív témák