2013-11-25 27 views
1

我有一個基本的模板陰影在我的遊戲引擎功能。我試圖變形基於照明方向上的影子,這是我有:OpenGL的:變形(規模)模板黑影從光源

/* 
* @brief Applies translation, rotation and scale for the shadow of the specified 
* entity. In order to reuse the vertex arrays from the primary rendering 
* pass, the shadow origin must transformed into model-view space. 
*/ 
static void R_RotateForMeshShadow_default(const r_entity_t *e) { 
    vec3_t origin, delta; 

    if (!e) { 
     glPopMatrix(); 
     return; 
    } 

    R_TransformForEntity(e, e->lighting->shadow_origin, origin); 

    VectorSubtract(e->lighting->shadow_origin, e->origin, delta); 
    const vec_t scale = 1.0 + VectorLength(delta)/LIGHTING_MAX_SHADOW_DISTANCE; 

    /*const vec_t dot = DotProduct(e->lighting->shadow_normal, e->lighting->dir); 

    const vec_t sy = sin(Radians(e->angles[YAW])); 
    const vec_t cy = cos(Radians(e->angles[YAW]));*/ 

    glPushMatrix(); 

    glTranslatef(origin[0], origin[1], origin[2] + 1.0); 

    glRotatef(-e->angles[PITCH], 0.0, 1.0, 0.0); 

    glScalef(scale, scale, 0.0); 

}

我註釋掉地平面(shadow_normal)和照明方向的點積,如以及模型偏航的罪惡和成因,因爲雖然我非常肯定他們是我需要增加陰影的比例,但我不知道正確的公式是什麼產生透視正確的變形。對於更好地理解預測的人來說,這可能是孩子們的玩法。對我而言,我在黑暗中刺傷。

回答

0

我最終能夠通過管理自己的矩陣,並從SGI的OpenGL食譜適應代碼來達到預期的效果。該代碼使用來自DarkPlaces Quake引擎的LordHavoc的矩陣庫。內聯評論提出了主要步驟。下面是完整的代碼:

/* 
* @brief Projects the model view matrix for the given entity onto the shadow 
* plane. A perspective shear is then applied using the standard planar shadow 
* deformation from SGI's cookbook, adjusted for Quake's negative planes: 
* 
* ftp://ftp.sgi.com/opengl/contrib/blythe/advanced99/notes/node192.html 
*/ 
static void R_RotateForMeshShadow_default(const r_entity_t *e, r_shadow_t *s) { 
    vec4_t pos, normal; 
    matrix4x4_t proj, shear; 
    vec_t dot; 

    if (!e) { 
     glPopMatrix(); 
     return; 
    } 

    const cm_bsp_plane_t *p = &s->plane; 

    // project the entity onto the shadow plane 
    vec3_t vx, vy, vz, t; 
    Matrix4x4_ToVectors(&e->matrix, vx, vy, vz, t); 

    dot = DotProduct(vx, p->normal); 
    VectorMA(vx, -dot, p->normal, vx); 

    dot = DotProduct(vy, p->normal); 
    VectorMA(vy, -dot, p->normal, vy); 

    dot = DotProduct(vz, p->normal); 
    VectorMA(vz, -dot, p->normal, vz); 

    dot = DotProduct(t, p->normal) - p->dist; 
    VectorMA(t, -dot, p->normal, t); 

    Matrix4x4_FromVectors(&proj, vx, vy, vz, t); 

    glPushMatrix(); 

    glMultMatrixf((GLfloat *) proj.m); 

    // transform the light position and shadow plane into model space 
    Matrix4x4_Transform(&e->inverse_matrix, s->illumination->light.origin, pos); 
    pos[3] = 1.0; 

    const vec_t *n = p->normal; 
    Matrix4x4_TransformPositivePlane(&e->inverse_matrix, n[0], n[1], n[2], p->dist, normal); 

    // calculate shearing, accounting for Quake's negative plane equation 
    normal[3] = -normal[3]; 
    dot = DotProduct(pos, normal) + pos[3] * normal[3]; 

    shear.m[0][0] = dot - pos[0] * normal[0]; 
    shear.m[1][0] = 0.0 - pos[0] * normal[1]; 
    shear.m[2][0] = 0.0 - pos[0] * normal[2]; 
    shear.m[3][0] = 0.0 - pos[0] * normal[3]; 
    shear.m[0][1] = 0.0 - pos[1] * normal[0]; 
    shear.m[1][1] = dot - pos[1] * normal[1]; 
    shear.m[2][1] = 0.0 - pos[1] * normal[2]; 
    shear.m[3][1] = 0.0 - pos[1] * normal[3]; 
    shear.m[0][2] = 0.0 - pos[2] * normal[0]; 
    shear.m[1][2] = 0.0 - pos[2] * normal[1]; 
    shear.m[2][2] = dot - pos[2] * normal[2]; 
    shear.m[3][2] = 0.0 - pos[2] * normal[3]; 
    shear.m[0][3] = 0.0 - pos[3] * normal[0]; 
    shear.m[1][3] = 0.0 - pos[3] * normal[1]; 
    shear.m[2][3] = 0.0 - pos[3] * normal[2]; 
    shear.m[3][3] = dot - pos[3] * normal[3]; 

    glMultMatrixf((GLfloat *) shear.m); 

    Matrix4x4_Copy(&s->matrix, &proj); 
} 

全面落實這一住在這裏: https://github.com/jdolan/quake2world/blob/master/src/client/renderer/r_mesh_shadow.c