m3g/m3gcore11/src/m3g_sprite.c
changeset 36 01a6848ebfd7
parent 0 5d03bc08d59c
child 116 171fae344dd4
equal deleted inserted replaced
0:5d03bc08d59c 36:01a6848ebfd7
   276         const Matrix *projMatrix = m3gProjectionMatrix(cam);
   276         const Matrix *projMatrix = m3gProjectionMatrix(cam);
   277         m3gTransformVec4(projMatrix, &ot);
   277         m3gTransformVec4(projMatrix, &ot);
   278         m3gTransformVec4(projMatrix, &x);
   278         m3gTransformVec4(projMatrix, &x);
   279         m3gTransformVec4(projMatrix, &y);
   279         m3gTransformVec4(projMatrix, &y);
   280     }
   280     }
   281 
   281 #ifndef M3G_USE_NGL_API
       
   282     /* Store w after projection */
       
   283     eyeSpace->w = ot.w;
       
   284 #endif
   282     m3gScaleVec4(&ot, m3gRcp(ot.w));
   285     m3gScaleVec4(&ot, m3gRcp(ot.w));
   283     m3gScaleVec4(&x, m3gRcp(x.w));
   286     m3gScaleVec4(&x, m3gRcp(x.w));
   284     m3gScaleVec4(&y, m3gRcp(y.w));
   287     m3gScaleVec4(&y, m3gRcp(y.w));
   285 
   288 
   286     m3gSubVec4(&x, &ot);
   289     m3gSubVec4(&x, &ot);
   329                              y.y));
   332                              y.y));
   330 
   333 
   331         x.x = m3gMul(x.x, (M3Gfloat) rIsect.width);
   334         x.x = m3gMul(x.x, (M3Gfloat) rIsect.width);
   332         y.y = m3gMul(y.y, (M3Gfloat) rIsect.height);
   335         y.y = m3gMul(y.y, (M3Gfloat) rIsect.height);
   333     }
   336     }
   334 
   337 #ifdef M3G_USE_NGL_API
   335     /* Store final Z */
   338     /* Store final Z */
   336     if (eyeSpace != NULL) {
   339     if (eyeSpace != NULL) {
   337         eyeSpace->w = ot.z;
   340         eyeSpace->w = ot.z;
   338     }
   341     }
   339 
   342 #endif
   340     /* Set up positions */
   343     /* Set up positions */
   341     vert[0 * 3 + 0] = (M3Gint) m3gMul(65536, m3gSub(ot.x, x.x));
   344     vert[0 * 3 + 0] = (M3Gint) m3gMul(65536, m3gSub(ot.x, x.x));
   342     vert[0 * 3 + 1] = m3gRoundToInt(m3gAdd(m3gMul(65536, m3gAdd(ot.y, y.y)), 0.5f));
   345     vert[0 * 3 + 1] = m3gRoundToInt(m3gAdd(m3gMul(65536, m3gAdd(ot.y, y.y)), 0.5f));
   343     vert[0 * 3 + 2] = m3gRoundToInt(m3gMul(65536, ot.z));
   346     vert[0 * 3 + 2] = m3gRoundToInt(m3gMul(65536, ot.z));
   344 
   347 
   462              m3gRcp((M3Gfloat) m3gGetHeight(sprite->image)),
   465              m3gRcp((M3Gfloat) m3gGetHeight(sprite->image)),
   463              1.f);
   466              1.f);
   464     glMatrixMode(GL_MODELVIEW);
   467     glMatrixMode(GL_MODELVIEW);
   465 
   468 
   466     /* Apply fog and compositing mode */
   469     /* Apply fog and compositing mode */
       
   470 #ifdef M3G_USE_NGL_API
   467     m3gApplySpriteFog(sprite->appearance->fog, eyeSpace.z, eyeSpace.w);
   471     m3gApplySpriteFog(sprite->appearance->fog, eyeSpace.z, eyeSpace.w);
       
   472 #else
       
   473     m3gApplyFog(sprite->appearance->fog);
       
   474 #endif
   468     m3gApplyCompositingMode(sprite->appearance->compositingMode, ctx);
   475     m3gApplyCompositingMode(sprite->appearance->compositingMode, ctx);
   469 
   476 
   470     {
   477     {
   471         GLfixed a = (GLfixed) (0xff * sprite->totalAlphaFactor);
   478         GLfixed a = (GLfixed) (0xff * sprite->totalAlphaFactor);
   472         a = (a >> (NODE_ALPHA_FACTOR_BITS - 8))
   479         a = (a >> (NODE_ALPHA_FACTOR_BITS - 8))
   481 
   488 
   482     /* Store current matrices, then set up an identity modelview and
   489     /* Store current matrices, then set up an identity modelview and
   483      * projection */
   490      * projection */
   484 
   491 
   485     m3gPushScreenSpace(ctx, M3G_FALSE);
   492     m3gPushScreenSpace(ctx, M3G_FALSE);
       
   493 
       
   494 #ifndef M3G_USE_NGL_API
       
   495     /* Transform the sprite vertices (in NDC) back to eye coordinates, so that 
       
   496        the fog distance will be calculated correctly in the OpenGL pipeline. */
       
   497     {
       
   498         GLfloat transform[16];
       
   499         GLfloat scaleW[16] = { 0.f, 0.f, 0.f, 0.f,
       
   500                                0.f, 0.f, 0.f, 0.f,
       
   501                                0.f, 0.f, 0.f, 0.f,
       
   502                                0.f, 0.f, 0.f, 0.f };
       
   503         Matrix invProjMatrix;
       
   504         const Matrix *projMatrix = m3gProjectionMatrix(m3gGetCurrentCamera(ctx));
       
   505 
       
   506         m3gMatrixInverse(&invProjMatrix, projMatrix);
       
   507 		m3gGetMatrixColumns(&invProjMatrix, transform);
       
   508         
       
   509         glMatrixMode(GL_MODELVIEW);
       
   510         glMultMatrixf(transform);
       
   511         scaleW[0] = scaleW[5] = scaleW[10] = scaleW[15] = eyeSpace.w;
       
   512         glMultMatrixf(scaleW);
       
   513 
       
   514         glMatrixMode(GL_PROJECTION);
       
   515         m3gGetMatrixColumns(projMatrix, transform);
       
   516         glLoadMatrixf(transform);
       
   517     }
       
   518 #endif
   486 
   519 
   487     /* Load indices -> draws the sprite */
   520     /* Load indices -> draws the sprite */
   488     M3G_BEGIN_PROFILE(M3G_INTERFACE(ctx), M3G_PROFILE_NGL_DRAW);
   521     M3G_BEGIN_PROFILE(M3G_INTERFACE(ctx), M3G_PROFILE_NGL_DRAW);
   489     glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
   522     glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
   490     M3G_END_PROFILE(M3G_INTERFACE(ctx), M3G_PROFILE_NGL_DRAW);
   523     M3G_END_PROFILE(M3G_INTERFACE(ctx), M3G_PROFILE_NGL_DRAW);