BGE: Multitexture world (mist, ambient) fix
[blender.git] / source / gameengine / Rasterizer / RAS_OpenGLRasterizer / RAS_OpenGLRasterizer.cpp
index 9d1fb3ba63c5126d55acfd41cfa9be22117213e0..14a184bbe7f59075cb455705c0eb8520d66eedcd 100644 (file)
  
 #include "RAS_OpenGLRasterizer.h"
 
-#include "GL/glew.h"
+#include "glew-mx.h"
 
+#include "RAS_ICanvas.h"
 #include "RAS_Rect.h"
 #include "RAS_TexVert.h"
 #include "RAS_MeshObject.h"
-#include "RAS_LightObject.h"
+#include "RAS_Polygon.h"
+#include "RAS_ILightObject.h"
 #include "MT_CmMatrix4x4.h"
 
+#include "RAS_OpenGLLight.h"
+
 #include "RAS_StorageIM.h"
 #include "RAS_StorageVA.h"
 #include "RAS_StorageVBO.h"
 
 #include "GPU_draw.h"
 #include "GPU_material.h"
-#include "GPU_extensions.h"
-
-#include "DNA_image_types.h"
-#include "DNA_meshdata_types.h"
-#include "DNA_material_types.h"
-#include "DNA_scene_types.h"
 
 extern "C"{
-       #include "BLI_utildefines.h"
-       #include "BKE_DerivedMesh.h"
        #include "BLF_api.h"
 }
 
 
 // XXX Clean these up <<<
-#include "BL_Material.h" // MAXTEX
 #include "Value.h"
-#include "KX_BlenderMaterial.h"
-#include "KX_PolygonMaterial.h"
-#include "KX_Light.h"
 #include "KX_Scene.h"
 #include "KX_RayCast.h"
 #include "KX_GameObject.h"
@@ -109,6 +101,7 @@ RAS_OpenGLRasterizer::RAS_OpenGLRasterizer(RAS_ICanvas* canvas, int storage)
        m_usingoverrideshader(false),
     m_clientobject(NULL),
     m_auxilaryClientInfo(NULL),
+    m_drawingmode(KX_TEXTURED),
        m_texco_num(0),
        m_attrib_num(0),
        //m_last_alphablend(GPU_BLEND_SOLID),
@@ -236,7 +229,6 @@ void RAS_OpenGLRasterizer::SetFogColor(float r,
        m_fogr = r;
        m_fogg = g;
        m_fogb = b;
-       m_fogenabled = true;
 }
 
 
@@ -244,7 +236,6 @@ void RAS_OpenGLRasterizer::SetFogColor(float r,
 void RAS_OpenGLRasterizer::SetFogStart(float start)
 {
        m_fogstart = start;
-       m_fogenabled = true;
 }
 
 
@@ -252,7 +243,6 @@ void RAS_OpenGLRasterizer::SetFogStart(float start)
 void RAS_OpenGLRasterizer::SetFogEnd(float fogend)
 {
        m_fogdist = fogend;
-       m_fogenabled = true;
 }
 
 
@@ -268,14 +258,13 @@ void RAS_OpenGLRasterizer::SetFog(float start,
        m_fogr = r;
        m_fogg = g;
        m_fogb = b;
-       m_fogenabled = true;
 }
 
 
 
-void RAS_OpenGLRasterizer::DisableFog()
+void RAS_OpenGLRasterizer::EnableFog(bool enable)
 {
-       m_fogenabled = false;
+       m_fogenabled = enable;
 }
 
 bool RAS_OpenGLRasterizer::IsFogEnabled()
@@ -288,16 +277,12 @@ void RAS_OpenGLRasterizer::DisplayFog()
 {
        if ((m_drawingmode >= KX_SOLID) && m_fogenabled)
        {
-               float params[5];
+               float params[4] = {m_fogr, m_fogg, m_fogb, 1.0f};
                glFogi(GL_FOG_MODE, GL_LINEAR);
                glFogf(GL_FOG_DENSITY, 0.1f);
                glFogf(GL_FOG_START, m_fogstart);
                glFogf(GL_FOG_END, m_fogstart + m_fogdist);
-               params[0] = m_fogr;
-               params[1] = m_fogg;
-               params[2] = m_fogb;
-               params[3] = 0.0;
-               glFogfv(GL_FOG_COLOR, params); 
+               glFogfv(GL_FOG_COLOR, params);
                glEnable(GL_FOG);
        } 
        else
@@ -339,10 +324,9 @@ void RAS_OpenGLRasterizer::Exit()
        EndFrame();
 }
 
-bool RAS_OpenGLRasterizer::BeginFrame(int drawingmode, double time)
+bool RAS_OpenGLRasterizer::BeginFrame(double time)
 {
        m_time = time;
-       SetDrawingMode(drawingmode);
 
        // Blender camera routine destroys the settings
        if (m_drawingmode < KX_SOLID)
@@ -536,6 +520,28 @@ void RAS_OpenGLRasterizer::SetRenderArea()
                                        break;
                        }
                        break;
+               case RAS_STEREO_3DTVTOPBOTTOM:
+                       switch (m_curreye) {
+                               case RAS_STEREO_LEFTEYE:
+                                       // upper half of window
+                                       area.SetLeft(0);
+                                       area.SetBottom(m_2DCanvas->GetHeight() -
+                                               m_2DCanvas->GetHeight() / 2);
+       
+                                       area.SetRight(m_2DCanvas->GetWidth());
+                                       area.SetTop(m_2DCanvas->GetHeight());
+                                       m_2DCanvas->SetDisplayArea(&area);
+                                       break;
+                               case RAS_STEREO_RIGHTEYE:
+                                       // lower half of window
+                                       area.SetLeft(0);
+                                       area.SetBottom(0);
+                                       area.SetRight(m_2DCanvas->GetWidth());
+                                       area.SetTop(m_2DCanvas->GetHeight() / 2);
+                                       m_2DCanvas->SetDisplayArea(&area);
+                                       break;
+                       }
+                       break;
                case RAS_STEREO_SIDEBYSIDE:
                        switch (m_curreye)
                        {
@@ -682,6 +688,8 @@ void RAS_OpenGLRasterizer::IndexPrimitives_3DText(RAS_MeshSlot& ms,
        MT_Vector4& rgba = ms.m_RGBAcolor;
        RAS_MeshSlot::iterator it;
 
+       const STR_String& mytext = ((CValue*)m_clientobject)->GetPropertyText("Text");
+
        // handle object color
        if (obcolor) {
                glDisableClientState(GL_COLOR_ARRAY);
@@ -732,9 +740,8 @@ void RAS_OpenGLRasterizer::IndexPrimitives_3DText(RAS_MeshSlot& ms,
                                        for (unit=0; unit<m_attrib_num; unit++)
                                                if (m_attrib[unit] == RAS_TEXCO_UV)
                                                        glattrib = unit;
-                               
-                               RenderText(polymat->GetDrawingMode(), polymat,
-                                       v[0], v[1], v[2], (numvert == 4)? v[3]: NULL, glattrib);
+
+                               GPU_render_text(polymat->GetMTFace(), polymat->GetDrawingMode(), mytext, mytext.Length(), polymat->GetMCol(), v[0], v[1], v[2], v[3], glattrib);
 
                                ClearCachingInfo();
                        }
@@ -849,6 +856,12 @@ MT_Matrix4x4 RAS_OpenGLRasterizer::GetFrustumMatrix(
                                                break;
                        }
                        // leave bottom and top untouched
+                       if (m_stereomode == RAS_STEREO_3DTVTOPBOTTOM) {
+                               // restore the vertical frustrum because the 3DTV will 
+                               // expande the top and bottom part to the full size of the screen
+                               bottom *= 2.0f;
+                               top *= 2.0f;
+                       }
        }
        
        glMatrixMode(GL_PROJECTION);
@@ -1161,8 +1174,32 @@ void RAS_OpenGLRasterizer::ProcessLighting(bool uselights, const MT_Transform& v
        m_lastauxinfo = m_auxilaryClientInfo;
 
        /* enable/disable lights as needed */
-       if (layer >= 0)
-               enable = ApplyLights(layer, viewmat);
+       if (layer >= 0) {
+               //enable = ApplyLights(layer, viewmat);
+               // taken from blender source, incompatibility between Blender Object / GameObject
+               KX_Scene* kxscene = (KX_Scene*)m_auxilaryClientInfo;
+               float glviewmat[16];
+               unsigned int count;
+               std::vector<RAS_OpenGLLight*>::iterator lit = m_lights.begin();
+
+               for (count=0; count<m_numgllights; count++)
+                       glDisable((GLenum)(GL_LIGHT0+count));
+
+               viewmat.getValue(glviewmat);
+
+               glPushMatrix();
+               glLoadMatrixf(glviewmat);
+               for (lit = m_lights.begin(), count = 0; !(lit==m_lights.end()) && count < m_numgllights; ++lit)
+               {
+                       RAS_OpenGLLight* light = (*lit);
+
+                       if (light->ApplyFixedFunctionLighting(kxscene, layer, count))
+                               count++;
+               }
+               glPopMatrix();
+
+               enable = count > 0;
+       }
 
        if (enable)
                EnableOpenGLLights();
@@ -1198,44 +1235,58 @@ void RAS_OpenGLRasterizer::DisableOpenGLLights()
        m_lastlighting = false;
 }
 
-void RAS_OpenGLRasterizer::AddLight(struct RAS_LightObject* lightobject)
+RAS_ILightObject *RAS_OpenGLRasterizer::CreateLight()
+{
+       return new RAS_OpenGLLight(this);
+}
+
+void RAS_OpenGLRasterizer::AddLight(RAS_ILightObject* lightobject)
 {
-       m_lights.push_back(lightobject);
+       RAS_OpenGLLight* gllight = dynamic_cast<RAS_OpenGLLight*>(lightobject);
+       assert(gllight);
+       m_lights.push_back(gllight);
 }
 
-void RAS_OpenGLRasterizer::RemoveLight(struct RAS_LightObject* lightobject)
+void RAS_OpenGLRasterizer::RemoveLight(RAS_ILightObject* lightobject)
 {
-       std::vector<struct      RAS_LightObject*>::iterator lit =
-               std::find(m_lights.begin(),m_lights.end(),lightobject);
+       RAS_OpenGLLight* gllight = dynamic_cast<RAS_OpenGLLight*>(lightobject);
+       assert(gllight);
+
+       std::vector<RAS_OpenGLLight*>::iterator lit =
+               std::find(m_lights.begin(),m_lights.end(),gllight);
 
        if (!(lit==m_lights.end()))
                m_lights.erase(lit);
 }
 
-bool RAS_OpenGLRasterizer::RayHit(class KX_ClientObjectInfo *client, KX_RayCast *result, void * const data)
+bool RAS_OpenGLRasterizer::RayHit(struct KX_ClientObjectInfo *client, KX_RayCast *result, void * const data)
 {
-       double* const oglmatrix = (double* const) data;
+       if (result->m_hitMesh) {
+               double* const oglmatrix = (double* const) data;
 
-       RAS_Polygon* poly = result->m_hitMesh->GetPolygon(result->m_hitPolygon);
-       if (!poly->IsVisible())
-               return false;
+               RAS_Polygon* poly = result->m_hitMesh->GetPolygon(result->m_hitPolygon);
+               if (!poly->IsVisible())
+                       return false;
 
-       MT_Point3 resultpoint(result->m_hitPoint);
-       MT_Vector3 resultnormal(result->m_hitNormal);
-       MT_Vector3 left(oglmatrix[0],oglmatrix[1],oglmatrix[2]);
-       MT_Vector3 dir = -(left.cross(resultnormal)).safe_normalized();
-       left = (dir.cross(resultnormal)).safe_normalized();
-       // for the up vector, we take the 'resultnormal' returned by the physics
-
-       double maat[16] = {left[0],         left[1],         left[2],         0,
-                          dir[0],          dir[1],          dir[2],          0,
-                          resultnormal[0], resultnormal[1], resultnormal[2], 0,
-                          0,               0,               0,               1};
-
-       glTranslated(resultpoint[0],resultpoint[1],resultpoint[2]);
-       //glMultMatrixd(oglmatrix);
-       glMultMatrixd(maat);
-       return true;
+               MT_Vector3 resultnormal(result->m_hitNormal);
+               MT_Vector3 left(oglmatrix[0],oglmatrix[1],oglmatrix[2]);
+               MT_Vector3 dir = -(left.cross(resultnormal)).safe_normalized();
+               left = (dir.cross(resultnormal)).safe_normalized();
+               // for the up vector, we take the 'resultnormal' returned by the physics
+
+               double maat[16] = {left[0],         left[1],         left[2],         0,
+                                      dir[0],          dir[1],          dir[2],          0,
+                                          resultnormal[0], resultnormal[1], resultnormal[2], 0,
+                                              0,               0,               0,               1};
+
+               glTranslated(oglmatrix[12],oglmatrix[13],oglmatrix[14]);
+               //glMultMatrixd(oglmatrix);
+               glMultMatrixd(maat);
+               return true;
+       }
+       else {
+               return false;
+       }
 }
 
 void RAS_OpenGLRasterizer::applyTransform(double* oglmatrix,int objectdrawmode )
@@ -1315,13 +1366,11 @@ void RAS_OpenGLRasterizer::applyTransform(double* oglmatrix,int objectdrawmode )
 
                        KX_Scene* kxscene = (KX_Scene*) m_auxilaryClientInfo;
                        PHY_IPhysicsEnvironment* physics_environment = kxscene->GetPhysicsEnvironment();
-                       KX_IPhysicsController* physics_controller = gameobj->GetPhysicsController();
+                       PHY_IPhysicsController* physics_controller = gameobj->GetPhysicsController();
 
                        KX_GameObject *parent = gameobj->GetParent();
                        if (!physics_controller && parent)
                                physics_controller = parent->GetPhysicsController();
-                       if (parent)
-                               parent->Release();
 
                        KX_RayCast::Callback<RAS_OpenGLRasterizer> callback(this, physics_controller, oglmatrix);
                        if (!KX_RayCast::RayTest(physics_environment, frompoint, topoint, callback))
@@ -1355,7 +1404,7 @@ static void DisableForText()
        glDisable(GL_COLOR_MATERIAL);
 
        if (GLEW_ARB_multitexture) {
-               for (int i=0; i<MAXTEX; i++) {
+               for (int i=0; i<RAS_MAX_TEXCO; i++) {
                        glActiveTextureARB(GL_TEXTURE0_ARB+i);
 
                        if (GLEW_ARB_texture_cube_map)
@@ -1422,13 +1471,9 @@ void RAS_OpenGLRasterizer::RenderBox2D(int xco,
        glEnable(GL_DEPTH_TEST);
 }
 
-void RAS_OpenGLRasterizer::RenderText3D(int fontid,
-                                                                                const char* text,
-                                                                                int size,
-                                                                                int dpi,
-                                                                                float* color,
-                                                                                double* mat,
-                                                                                float aspect)
+void RAS_OpenGLRasterizer::RenderText3D(
+        int fontid, const char *text, int size, int dpi,
+        const float color[4], const double mat[16], float aspect)
 {
        /* gl prepping */
        DisableForText();
@@ -1447,17 +1492,16 @@ void RAS_OpenGLRasterizer::RenderText3D(int fontid,
 
        BLF_size(fontid, size, dpi);
        BLF_position(fontid, 0, 0, 0);
-       BLF_draw(fontid, (char *)text, 65535);
+       BLF_draw(fontid, text, 65535);
 
        BLF_disable(fontid, BLF_MATRIX|BLF_ASPECT);
 }
 
-void RAS_OpenGLRasterizer::RenderText2D(RAS_TEXT_RENDER_MODE mode,
-                                                                                const char* text,
-                                                                                int xco,
-                                                                                int yco,
-                                                                                int width,
-                                                                                int height)
+void RAS_OpenGLRasterizer::RenderText2D(
+        RAS_TEXT_RENDER_MODE mode,
+        const char* text,
+        int xco, int yco,
+        int width, int height)
 {
        /* This is a rather important line :( The gl-mode hasn't been left
         * behind quite as neatly as we'd have wanted to. I don't know
@@ -1480,19 +1524,15 @@ void RAS_OpenGLRasterizer::RenderText2D(RAS_TEXT_RENDER_MODE mode,
                glColor3ub(0, 0, 0);
                BLF_size(blf_mono_font, 11, 72);
                BLF_position(blf_mono_font, (float)xco+1, (float)(height-yco-1), 0.0f);
-               BLF_draw(blf_mono_font, (char *)text, 65535);/* XXX, use real len */
-
-               glColor3ub(255, 255, 255);
-               BLF_position(blf_mono_font, (float)xco, (float)(height-yco), 0.0f);
-               BLF_draw(blf_mono_font, (char *)text, 65535);
-       } else {
-               /* the actual drawing */
-               glColor3ub(255, 255, 255);
-               BLF_size(blf_mono_font, 11, 72);
-               BLF_position(blf_mono_font, (float)xco, (float)(height-yco), 0.0f);
-               BLF_draw(blf_mono_font, (char *)text, 65535); /* XXX, use real len */
+               BLF_draw(blf_mono_font, text, 65535); /* XXX, use real len */
        }
 
+       /* the actual drawing */
+       glColor3ub(255, 255, 255);
+       BLF_size(blf_mono_font, 11, 72);
+       BLF_position(blf_mono_font, (float)xco, (float)(height-yco), 0.0f);
+       BLF_draw(blf_mono_font, text, 65535); /* XXX, use real len */
+
        glMatrixMode(GL_PROJECTION);
        glPopMatrix();
        glMatrixMode(GL_MODELVIEW);
@@ -1500,34 +1540,6 @@ void RAS_OpenGLRasterizer::RenderText2D(RAS_TEXT_RENDER_MODE mode,
        glEnable(GL_DEPTH_TEST);
 }
 
-/* Render Text renders text into a (series of) polygon, using a texture font,
- * Each character consists of one polygon (one quad or two triangles) */
-
-void RAS_OpenGLRasterizer::RenderText(
-       int mode,
-       RAS_IPolyMaterial* polymat,
-       float v1[3], float v2[3], float v3[3], float v4[3], int glattrib)
-{
-       const STR_String& mytext = ((CValue*)m_clientobject)->GetPropertyText("Text");
-
-       const unsigned int flag = polymat->GetFlag();
-       struct MTFace* tface = 0;
-       unsigned int *col = 0;
-
-       if (flag & RAS_BLENDERMAT) {
-               KX_BlenderMaterial *bl_mat = static_cast<KX_BlenderMaterial*>(polymat);
-               tface = bl_mat->GetMTFace();
-               col = bl_mat->GetMCol();
-       } else {
-               KX_PolygonMaterial* blenderpoly = static_cast<KX_PolygonMaterial*>(polymat);
-               tface = blenderpoly->GetMTFace();
-               col = blenderpoly->GetMCol();
-       }
-
-       GPU_render_text(tface, mode, mytext, mytext.Length(), col, v1, v2, v3, v4, glattrib);
-}
-
-
 void RAS_OpenGLRasterizer::PushMatrix()
 {
        glPushMatrix();
@@ -1538,35 +1550,6 @@ void RAS_OpenGLRasterizer::PopMatrix()
        glPopMatrix();
 }
 
-
-int RAS_OpenGLRasterizer::ApplyLights(int objectlayer, const MT_Transform& viewmat)
-{
-       // taken from blender source, incompatibility between Blender Object / GameObject
-       KX_Scene* kxscene = (KX_Scene*)m_auxilaryClientInfo;
-       float glviewmat[16];
-       unsigned int count;
-       std::vector<struct      RAS_LightObject*>::iterator lit = m_lights.begin();
-
-       for (count=0; count<m_numgllights; count++)
-               glDisable((GLenum)(GL_LIGHT0+count));
-
-       viewmat.getValue(glviewmat);
-
-       glPushMatrix();
-       glLoadMatrixf(glviewmat);
-       for (lit = m_lights.begin(), count = 0; !(lit==m_lights.end()) && count < m_numgllights; ++lit)
-       {
-               RAS_LightObject* lightdata = (*lit);
-               KX_LightObject *kxlight = (KX_LightObject*)lightdata->m_light;
-
-               if (kxlight->ApplyLight(kxscene, objectlayer, count))
-                       count++;
-       }
-       glPopMatrix();
-
-       return count;
-}
-
 void RAS_OpenGLRasterizer::MotionBlur()
 {
        int state = GetMotionBlurState();