BGE: Multitexture world (mist, ambient) fix
[blender.git] / source / gameengine / Rasterizer / RAS_OpenGLRasterizer / RAS_OpenGLRasterizer.cpp
index 2884983a17e3ffd77d1a269012d3b874179e0131..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_Polygon.h"
-#include "RAS_LightObject.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"
@@ -59,7 +61,6 @@ extern "C"{
 
 // XXX Clean these up <<<
 #include "Value.h"
-#include "KX_Light.h"
 #include "KX_Scene.h"
 #include "KX_RayCast.h"
 #include "KX_GameObject.h"
@@ -100,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),
@@ -227,7 +229,6 @@ void RAS_OpenGLRasterizer::SetFogColor(float r,
        m_fogr = r;
        m_fogg = g;
        m_fogb = b;
-       m_fogenabled = true;
 }
 
 
@@ -235,7 +236,6 @@ void RAS_OpenGLRasterizer::SetFogColor(float r,
 void RAS_OpenGLRasterizer::SetFogStart(float start)
 {
        m_fogstart = start;
-       m_fogenabled = true;
 }
 
 
@@ -243,7 +243,6 @@ void RAS_OpenGLRasterizer::SetFogStart(float start)
 void RAS_OpenGLRasterizer::SetFogEnd(float fogend)
 {
        m_fogdist = fogend;
-       m_fogenabled = true;
 }
 
 
@@ -259,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()
@@ -279,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
@@ -330,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)
@@ -527,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)
                        {
@@ -841,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);
@@ -1159,7 +1180,7 @@ void RAS_OpenGLRasterizer::ProcessLighting(bool uselights, const MT_Transform& v
                KX_Scene* kxscene = (KX_Scene*)m_auxilaryClientInfo;
                float glviewmat[16];
                unsigned int count;
-               std::vector<struct      RAS_LightObject*>::iterator lit = m_lights.begin();
+               std::vector<RAS_OpenGLLight*>::iterator lit = m_lights.begin();
 
                for (count=0; count<m_numgllights; count++)
                        glDisable((GLenum)(GL_LIGHT0+count));
@@ -1170,10 +1191,9 @@ void RAS_OpenGLRasterizer::ProcessLighting(bool uselights, const MT_Transform& v
                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;
+                       RAS_OpenGLLight* light = (*lit);
 
-                       if (kxlight->ApplyLight(kxscene, layer, count))
+                       if (light->ApplyFixedFunctionLighting(kxscene, layer, count))
                                count++;
                }
                glPopMatrix();
@@ -1215,15 +1235,25 @@ void RAS_OpenGLRasterizer::DisableOpenGLLights()
        m_lastlighting = false;
 }
 
-void RAS_OpenGLRasterizer::AddLight(struct RAS_LightObject* lightobject)
+RAS_ILightObject *RAS_OpenGLRasterizer::CreateLight()
 {
-       m_lights.push_back(lightobject);
+       return new RAS_OpenGLLight(this);
 }
 
-void RAS_OpenGLRasterizer::RemoveLight(struct RAS_LightObject* lightobject)
+void RAS_OpenGLRasterizer::AddLight(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);
+       m_lights.push_back(gllight);
+}
+
+void RAS_OpenGLRasterizer::RemoveLight(RAS_ILightObject* 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);
@@ -1231,28 +1261,32 @@ void RAS_OpenGLRasterizer::RemoveLight(struct RAS_LightObject* lightobject)
 
 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 )
@@ -1337,8 +1371,6 @@ void RAS_OpenGLRasterizer::applyTransform(double* oglmatrix,int objectdrawmode )
                        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))
@@ -1439,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();
@@ -1464,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
@@ -1497,14 +1524,14 @@ 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 */
+               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, (char *)text, 65535); /* XXX, use real len */
+       BLF_draw(blf_mono_font, text, 65535); /* XXX, use real len */
 
        glMatrixMode(GL_PROJECTION);
        glPopMatrix();