diff options
Diffstat (limited to 'source/gameengine/Rasterizer')
33 files changed, 5718 insertions, 0 deletions
diff --git a/source/gameengine/Rasterizer/Makefile b/source/gameengine/Rasterizer/Makefile new file mode 100644 index 00000000000..6a1421ae90b --- /dev/null +++ b/source/gameengine/Rasterizer/Makefile @@ -0,0 +1,51 @@ +# +# $Id$ +# +# ***** BEGIN GPL/BL DUAL LICENSE BLOCK ***** +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License +# as published by the Free Software Foundation; either version 2 +# of the License, or (at your option) any later version. The Blender +# Foundation also sells licenses for use in proprietary software under +# the Blender License. See http://www.blender.org/BL/ for information +# about this. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software Foundation, +# Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +# +# The Original Code is Copyright (C) 2001-2002 by NaN Holding BV. +# All rights reserved. +# +# The Original Code is: all of this file. +# +# Contributor(s): none yet. +# +# ***** END GPL/BL DUAL LICENSE BLOCK ***** +# +# + +LIBNAME = rasterizer +DIR = $(OCGDIR)/gameengine/$(LIBNAME) + +include nan_compile.mk + +CCFLAGS += $(LEVEL_1_CPP_WARNINGS) + +CPPFLAGS += -I$(OPENGL_HEADERS) +CPPFLAGS += -I$(NAN_STRING)/include +CPPFLAGS += -I$(NAN_MOTO)/include +CPPFLAGS += -I../../kernel/gen_system + +############### + +SOURCEDIR = source/gameengine/Rasterizer +DIRS = RAS_OpenGLRasterizer + +include nan_subdirs.mk diff --git a/source/gameengine/Rasterizer/RAS_BucketManager.cpp b/source/gameengine/Rasterizer/RAS_BucketManager.cpp new file mode 100644 index 00000000000..39ec523cfa2 --- /dev/null +++ b/source/gameengine/Rasterizer/RAS_BucketManager.cpp @@ -0,0 +1,156 @@ +/** + * $Id$ + * ***** BEGIN GPL/BL DUAL LICENSE BLOCK ***** + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. The Blender + * Foundation also sells licenses for use in proprietary software under + * the Blender License. See http://www.blender.org/BL/ for information + * about this. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + * + * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV. + * All rights reserved. + * + * The Original Code is: all of this file. + * + * Contributor(s): none yet. + * + * ***** END GPL/BL DUAL LICENSE BLOCK ***** + */ + + +#ifdef WIN32 +// don't show these anoying STL warnings +#pragma warning (disable:4786) +#endif + +#include "GEN_Map.h" +#include "RAS_MaterialBucket.h" +#include "STR_HashedString.h" +#include "RAS_MeshObject.h" +#define KX_NUM_MATERIALBUCKETS 100 +#include "RAS_IRasterizer.h" +#include "RAS_IRenderTools.h" + +#include "RAS_BucketManager.h" + + +RAS_BucketManager::RAS_BucketManager() +{ + +} + +RAS_BucketManager::~RAS_BucketManager() +{ + RAS_BucketManagerClearAll(); +} + + +void RAS_BucketManager::Renderbuckets( + const MT_Transform& cameratrans, RAS_IRasterizer* rasty, RAS_IRenderTools* rendertools) +{ + int numbuckets = m_MaterialBuckets.size(); + + //default_gl_light(); + + int i; + + rasty->EnableTextures(false); + rasty->SetDepthMask(RAS_IRasterizer::KX_DEPTHMASK_ENABLED); + + // beginning each frame, clear (texture/material) caching information + rasty->ClearCachingInfo(); + + RAS_MaterialBucket::StartFrame(); + + for (i=0;i<numbuckets;i++) + { + RAS_MaterialBucket** bucketptr = m_MaterialBuckets.at(i); + if (bucketptr) + { + (*bucketptr)->ClearScheduledPolygons(); + } + } + + vector<RAS_MaterialBucket*> alphabuckets; + + // if no visibility method is define, everything is drawn + + for (i=0;i<numbuckets;i++) + { + RAS_MaterialBucket** bucketptr = m_MaterialBuckets.at(i); + if (bucketptr) + { + if (!(*bucketptr)->IsTransparant()) + { + (*bucketptr)->Render(cameratrans,rasty,rendertools); + } else + { + alphabuckets.push_back(*bucketptr); + } + } + } + + rasty->SetDepthMask(RAS_IRasterizer::KX_DEPTHMASK_DISABLED); + + int numalphabuckets = alphabuckets.size(); + for (vector<RAS_MaterialBucket*>::const_iterator it=alphabuckets.begin(); + !(it==alphabuckets.end());it++) + { + (*it)->Render(cameratrans,rasty,rendertools); + } + + alphabuckets.clear(); + + + RAS_MaterialBucket::EndFrame(); + + rasty->SetDepthMask(RAS_IRasterizer::KX_DEPTHMASK_ENABLED); +} + +RAS_MaterialBucket* RAS_BucketManager::RAS_BucketManagerFindBucket(RAS_IPolyMaterial * material) +{ + + RAS_MaterialBucket** bucketptr = m_MaterialBuckets[*material]; + RAS_MaterialBucket* bucket=NULL; + if (!bucketptr) + { + bucket = new RAS_MaterialBucket(material); + m_MaterialBuckets.insert(*material,bucket); + + } else + { + bucket = *bucketptr; + } + + return bucket; +} + +void RAS_BucketManager::RAS_BucketManagerClearAll() +{ + + int numbuckets = m_MaterialBuckets.size(); + for (int i=0;i<numbuckets;i++) + { + RAS_MaterialBucket** bucketptr = m_MaterialBuckets.at(i); + if (bucketptr) + { + delete (*bucketptr); + *bucketptr=NULL; + + } + } + m_MaterialBuckets.clear(); + +} diff --git a/source/gameengine/Rasterizer/RAS_BucketManager.h b/source/gameengine/Rasterizer/RAS_BucketManager.h new file mode 100644 index 00000000000..72427473fc1 --- /dev/null +++ b/source/gameengine/Rasterizer/RAS_BucketManager.h @@ -0,0 +1,63 @@ +/** + * $Id$ + * + * ***** BEGIN GPL/BL DUAL LICENSE BLOCK ***** + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. The Blender + * Foundation also sells licenses for use in proprietary software under + * the Blender License. See http://www.blender.org/BL/ for information + * about this. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + * + * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV. + * All rights reserved. + * + * The Original Code is: all of this file. + * + * Contributor(s): none yet. + * + * ***** END GPL/BL DUAL LICENSE BLOCK ***** + */ +// this will be put in a class later on + +#ifndef __RAS_BUCKETMANAGER +#define __RAS_BUCKETMANAGER + +#include "MT_Transform.h" +#include "RAS_MaterialBucket.h" +#include "GEN_Map.h" + +class RAS_BucketManager +{ + + GEN_Map<class RAS_IPolyMaterial,class RAS_MaterialBucket*> m_MaterialBuckets; + +public: + RAS_BucketManager(); + virtual ~RAS_BucketManager(); + + void Renderbuckets(const MT_Transform & cameratrans, + RAS_IRasterizer* rasty, + class RAS_IRenderTools* rendertools); + + RAS_MaterialBucket* RAS_BucketManagerFindBucket(RAS_IPolyMaterial * material); + + +private: + void RAS_BucketManagerClearAll(); + +}; + +#endif //__RAS_BUCKETMANAGER + diff --git a/source/gameengine/Rasterizer/RAS_CameraData.h b/source/gameengine/Rasterizer/RAS_CameraData.h new file mode 100644 index 00000000000..5c9b1dd0e88 --- /dev/null +++ b/source/gameengine/Rasterizer/RAS_CameraData.h @@ -0,0 +1,42 @@ +/** + * $Id$ + * + * ***** BEGIN GPL/BL DUAL LICENSE BLOCK ***** + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. The Blender + * Foundation also sells licenses for use in proprietary software under + * the Blender License. See http://www.blender.org/BL/ for information + * about this. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + * + * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV. + * All rights reserved. + * + * The Original Code is: all of this file. + * + * Contributor(s): none yet. + * + * ***** END GPL/BL DUAL LICENSE BLOCK ***** + */ +#ifndef __RAS_CAMERADATA_H +#define __RAS_CAMERADATA_H + +struct RAS_CameraData +{ + float m_lens; + float m_clipstart; + float m_clipend; +}; + +#endif //__RAS_CAMERADATA_H diff --git a/source/gameengine/Rasterizer/RAS_Deformer.h b/source/gameengine/Rasterizer/RAS_Deformer.h new file mode 100644 index 00000000000..9aeb85c5740 --- /dev/null +++ b/source/gameengine/Rasterizer/RAS_Deformer.h @@ -0,0 +1,56 @@ +/** + * $Id$ + * + * ***** BEGIN GPL/BL DUAL LICENSE BLOCK ***** + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. The Blender + * Foundation also sells licenses for use in proprietary software under + * the Blender License. See http://www.blender.org/BL/ for information + * about this. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + * + * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV. + * All rights reserved. + * + * The Original Code is: all of this file. + * + * Contributor(s): none yet. + * + * ***** END GPL/BL DUAL LICENSE BLOCK ***** + */ + +#ifndef RAS_DEFORMER +#define RAS_DEFORMER + +#ifdef WIN32 +#pragma warning (disable:4786) // get rid of stupid stl-visual compiler debug warning +#endif //WIN32 + +#include "GEN_Map.h" + +class RAS_Deformer +{ +public: + RAS_Deformer(){}; + virtual ~RAS_Deformer(){}; + virtual void Relink(GEN_Map<class GEN_HashedPtr, void*>*map)=0; + virtual bool Apply(class RAS_IPolyMaterial *polymat)=0; + virtual void Update(void)=0; + virtual RAS_Deformer *GetReplica()=0; +protected: + class RAS_MeshObject *m_pMesh; +}; + +#endif + diff --git a/source/gameengine/Rasterizer/RAS_FramingManager.cpp b/source/gameengine/Rasterizer/RAS_FramingManager.cpp new file mode 100644 index 00000000000..af0499bd348 --- /dev/null +++ b/source/gameengine/Rasterizer/RAS_FramingManager.cpp @@ -0,0 +1,234 @@ +/** + * $Id$ + * ***** BEGIN GPL/BL DUAL LICENSE BLOCK ***** + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. The Blender + * Foundation also sells licenses for use in proprietary software under + * the Blender License. See http://www.blender.org/BL/ for information + * about this. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + * + * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV. + * All rights reserved. + * + * The Original Code is: all of this file. + * + * Contributor(s): none yet. + * + * ***** END GPL/BL DUAL LICENSE BLOCK ***** + */ + +#include "RAS_FramingManager.h" + +#include "RAS_Rect.h" + + void +RAS_FramingManager:: +ComputeDefaultFrustum( + const float camnear, + const float camfar, + const float lens, + const float design_aspect_ratio, + RAS_FrameFrustum & frustum +){ + + /* + * Magic Blender calculation. + * Blender does not give a Field of View as lens but a size + * at 16 units away from the lens. + */ + float halfSize = 16.f * camnear / lens; + float sizeX; + float sizeY; + + if (design_aspect_ratio > 1.f) { + // halfsize defines the width + sizeX = halfSize; + sizeY = halfSize/design_aspect_ratio; + } else { + // halfsize defines the height + sizeX = halfSize * design_aspect_ratio; + sizeY = halfSize; + } + + frustum.x2 = sizeX; + frustum.x1 = -frustum.x2; + frustum.y2 = sizeY; + frustum.y1 = -frustum.y2; + frustum.camnear = camnear; + frustum.camfar = camfar; +} + + void +RAS_FramingManager:: +ComputeBestFitViewRect( + const RAS_Rect &availableViewport, + const float design_aspect_ratio, + RAS_Rect &viewport +){ + // try and honour the aspect ratio when setting the + // drawable area. If we don't do this we are liable + // to get a lot of distortion in the rendered image. + + int width = availableViewport.GetWidth(); + int height = availableViewport.GetHeight(); + float window_aspect = float(width)/float(height); + + if (window_aspect < design_aspect_ratio) { + int v_height = (int)(width / design_aspect_ratio); + int left_over = (height - v_height) / 2; + + viewport.SetLeft(availableViewport.GetLeft()); + viewport.SetBottom(availableViewport.GetBottom() + left_over); + viewport.SetRight(availableViewport.GetLeft() + width); + viewport.SetTop(availableViewport.GetBottom() + left_over + v_height); + + } else { + int v_width = (int)(height * design_aspect_ratio); + int left_over = (width - v_width) / 2; + + viewport.SetLeft(availableViewport.GetLeft() + left_over); + viewport.SetBottom(availableViewport.GetBottom()); + viewport.SetRight(availableViewport.GetLeft() + v_width + left_over); + viewport.SetTop(availableViewport.GetBottom() + height); + } +} + + void +RAS_FramingManager:: +ComputeViewport( + const RAS_FrameSettings &settings, + const RAS_Rect &availableViewport, + RAS_Rect &viewport +){ + + RAS_FrameSettings::RAS_FrameType type = settings.FrameType(); + const int winx = availableViewport.GetWidth(); + const int winy = availableViewport.GetHeight(); + + const float design_width = float(settings.DesignAspectWidth()); + const float design_height = float(settings.DesignAspectHeight()); + + float design_aspect_ratio = float(1); + + if (design_height == float(0)) { + // well this is ill defined + // lets just scale the thing + + type = RAS_FrameSettings::e_frame_scale; + } else { + design_aspect_ratio = design_width/design_height; + } + + switch (type) { + + case RAS_FrameSettings::e_frame_scale : + case RAS_FrameSettings::e_frame_extend: + { + viewport.SetLeft(availableViewport.GetLeft()); + viewport.SetBottom(availableViewport.GetBottom()); + viewport.SetRight(availableViewport.GetLeft() + int(winx)); + viewport.SetTop(availableViewport.GetBottom() + int(winy)); + + break; + } + + case RAS_FrameSettings::e_frame_bars: + { + ComputeBestFitViewRect( + availableViewport, + design_aspect_ratio, + viewport + ); + + break; + } + default : + break; + } +} + + void +RAS_FramingManager:: +ComputeFrustum( + const RAS_FrameSettings &settings, + const RAS_Rect &availableViewport, + const RAS_Rect &viewport, + const float lens, + const float camnear, + const float camfar, + RAS_FrameFrustum &frustum +){ + + const int winx = availableViewport.GetWidth(); + const int winy = availableViewport.GetHeight(); + + RAS_FrameSettings::RAS_FrameType type = settings.FrameType(); + + const float design_width = float(settings.DesignAspectWidth()); + const float design_height = float(settings.DesignAspectHeight()); + + float design_aspect_ratio = float(1); + + if (design_height == float(0)) { + // well this is ill defined + // lets just scale the thing + + type = RAS_FrameSettings::e_frame_scale; + } else { + design_aspect_ratio = design_width/design_height; + } + + ComputeDefaultFrustum( + camnear, + camfar, + lens, + design_aspect_ratio, + frustum + ); + + switch (type) { + + case RAS_FrameSettings::e_frame_extend: + { + RAS_Rect vt; + ComputeBestFitViewRect( + availableViewport, + design_aspect_ratio, + vt + ); + + // now scale the calculated frustum by the difference + // between vt and the viewport in each axis. + // These are always > 1 + + float x_scale = float(viewport.GetWidth())/float(vt.GetWidth()); + float y_scale = float(viewport.GetHeight())/float(vt.GetHeight()); + + frustum.x1 *= x_scale; + frustum.x2 *= x_scale; + frustum.y1 *= y_scale; + frustum.y2 *= y_scale; + + break; + } + case RAS_FrameSettings::e_frame_scale : + case RAS_FrameSettings::e_frame_bars: + default : + break; + } +} + + + diff --git a/source/gameengine/Rasterizer/RAS_FramingManager.h b/source/gameengine/Rasterizer/RAS_FramingManager.h new file mode 100644 index 00000000000..581c6b56fc9 --- /dev/null +++ b/source/gameengine/Rasterizer/RAS_FramingManager.h @@ -0,0 +1,255 @@ +/** + * $Id$ + * + * ***** BEGIN GPL/BL DUAL LICENSE BLOCK ***** + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. The Blender + * Foundation also sells licenses for use in proprietary software under + * the Blender License. See http://www.blender.org/BL/ for information + * about this. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + * + * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV. + * All rights reserved. + * + * The Original Code is: all of this file. + * + * Contributor(s): none yet. + * + * ***** END GPL/BL DUAL LICENSE BLOCK ***** + */ +#ifndef RAS_FRAMINGMANAGER_H +#define RAS_FRAMINGMANAGER_H + +class RAS_Rect; + +/** + * @section RAS_FrameSettings + * This is a value type describing the framing used + * by a particular scene in the game engine. + * Each KX_Scene contains a RAS_FrameSetting describing + * how the frustum and viewport are to be modified + * depending on the canvas size. + * + * e_frame_scale means that the viewport is set to the current + * canvas size. If the view frustum aspect ratio is different + * to the canvas aspect this will lead to stretching. + * + * e_frame_extend means that the best fit viewport will be + * computed based upon the design aspect ratio + * and the view frustum will be adjusted so that + * more of the scene is visible. + * + * e_frame_bars means that the best fit viewport will be + * be computed based upon the design aspect ratio. + */ + +class RAS_FrameSettings +{ +public : + + /** + * enum defining the policy to use + * in each axis. + */ + enum RAS_FrameType { + e_frame_scale, + e_frame_extend, + e_frame_bars + }; + + /** + * Contructor + */ + + RAS_FrameSettings( + RAS_FrameType frame_type, + float bar_r, + float bar_g, + float bar_b, + unsigned int design_aspect_width, + unsigned int design_aspect_height + ): + m_frame_type(frame_type), + m_bar_r(bar_r), + m_bar_g(bar_g), + m_bar_b(bar_b), + m_design_aspect_width(design_aspect_width), + m_design_aspect_height(design_aspect_height) + { + }; + + RAS_FrameSettings( + ): + m_frame_type(e_frame_scale), + m_bar_r(0), + m_bar_g(0), + m_bar_b(0), + m_design_aspect_width(1), + m_design_aspect_height(1) + { + }; + + /** + * Accessors + */ + + const + RAS_FrameType & + FrameType( + ) const { + return m_frame_type; + }; + + float + BarRed( + ) const { + return m_bar_r; + }; + + float + BarGreen( + ) const { + return m_bar_g; + }; + + float + BarBlue( + ) const { + return m_bar_b; + }; + + unsigned int + DesignAspectWidth( + ) const { + return m_design_aspect_width; + }; + + unsigned int + DesignAspectHeight( + ) const { + return m_design_aspect_height; + }; + +private : + + /** + * private to force use of public constructor + */ + + RAS_FrameSettings( + const RAS_FrameSettings & + ); + + RAS_FrameType m_frame_type; + float m_bar_r; + float m_bar_g; + float m_bar_b; + unsigned int m_design_aspect_width; + unsigned int m_design_aspect_height; +}; + +struct RAS_FrameFrustum +{ + float camnear,camfar; + float x1,y1; + float x2,y2; +}; + + +/** + * @section RAS_FramingManager + * This class helps to compute a view frustum + * and a viewport rectangle given the + * above settings and a description of the + * current canvas dimensions. + * + * You do not have to instantiate this class + * directly, it only contains static helper functions + */ + +class RAS_FramingManager +{ +public : + + /** + * Compute a viewport given + * a RAS_FrameSettings and a description of the + * canvas. + */ + + static + void + ComputeViewport( + const RAS_FrameSettings &settings, + const RAS_Rect &availableViewport, + RAS_Rect &viewport + ); + + + /** + * compute a frustrum given a valid viewport, + * RAS_FrameSettings, canvas description + * and camera description + */ + + static + void + ComputeFrustum( + const RAS_FrameSettings &settings, + const RAS_Rect &availableViewport, + const RAS_Rect &viewport, + const float lens, + const float camnear, + const float camfar, + RAS_FrameFrustum &frustum + ); + + +private : + + static + void + ComputeDefaultFrustum( + const float camnear, + const float camfar, + const float lens, + const float design_aspect_ratio, + RAS_FrameFrustum & frustum + ); + + static + void + ComputeBestFitViewRect( + const RAS_Rect &availableViewport, + const float design_aspect_ratio, + RAS_Rect &viewport + ); + + + + /** + * Private constructor - this class is not meant + * for instanciation. + */ + + RAS_FramingManager( + ); + + RAS_FramingManager( + const RAS_FramingManager & + ); +}; + +#endif diff --git a/source/gameengine/Rasterizer/RAS_ICanvas.h b/source/gameengine/Rasterizer/RAS_ICanvas.h new file mode 100644 index 00000000000..8060313b4be --- /dev/null +++ b/source/gameengine/Rasterizer/RAS_ICanvas.h @@ -0,0 +1,172 @@ +/** + * $Id$ + * + * ***** BEGIN GPL/BL DUAL LICENSE BLOCK ***** + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. The Blender + * Foundation also sells licenses for use in proprietary software under + * the Blender License. See http://www.blender.org/BL/ for information + * about this. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + * + * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV. + * All rights reserved. + * + * The Original Code is: all of this file. + * + * Contributor(s): none yet. + * + * ***** END GPL/BL DUAL LICENSE BLOCK ***** + */ +#ifndef __RAS_ICANVAS +#define __RAS_ICANVAS + +/** + * 2D rendering device context. The connection from 3d rendercontext to 2d surface. + */ + +class RAS_Rect; + + +class RAS_ICanvas +{ +public: + enum BufferType { + COLOR_BUFFER=1, + DEPTH_BUFFER=2 + }; + + enum RAS_MouseState + { + MOUSE_INVISIBLE=1, + MOUSE_WAIT, + MOUSE_NORMAL + }; + + virtual + ~RAS_ICanvas( + ) { + }; + + virtual + void + Init( + ) = 0; + + virtual + void + BeginFrame( + )=0; + + virtual + void + EndFrame( + )=0; + + /** + * Initializes the canvas for drawing. Drawing to the canvas is + * only allowed between BeginDraw() and EndDraw(). + * + * @retval false Acquiring the canvas failed. + * @retval true Acquiring the canvas succeeded. + * + */ + + virtual + bool + BeginDraw( + )=0; + + /** + * Unitializes the canvas for drawing. + */ + + virtual + void + EndDraw( + )=0; + + + /// probably needs some arguments for PS2 in future + virtual + void + SwapBuffers( + )=0; + + virtual + void + ClearBuffer( + int type + )=0; + + virtual + void + ClearColor( + float r, + float g, + float b, + float a + )=0; + + virtual + int + GetWidth( + ) const = 0; + + virtual + int + GetHeight( + ) const = 0; + + virtual + const RAS_Rect & + GetDisplayArea( + ) const = 0; + + virtual + RAS_Rect & + GetDisplayArea( + ) = 0; + + /** + * Set the visible vieport + */ + + virtual + void + SetViewPort( + int x1, int y1, + int x2, int y2 + ) = 0; + + virtual + void + SetMouseState( + RAS_MouseState mousestate + )=0; + + virtual + void + SetMousePosition( + int x, + int y + )=0; + + virtual + void + MakeScreenShot( + const char* filename + )=0; +}; +#endif //__RAS_ICANVAS diff --git a/source/gameengine/Rasterizer/RAS_IPolygonMaterial.cpp b/source/gameengine/Rasterizer/RAS_IPolygonMaterial.cpp new file mode 100644 index 00000000000..7e1017b5c39 --- /dev/null +++ b/source/gameengine/Rasterizer/RAS_IPolygonMaterial.cpp @@ -0,0 +1,125 @@ +/** + * $Id$ + * ***** BEGIN GPL/BL DUAL LICENSE BLOCK ***** + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. The Blender + * Foundation also sells licenses for use in proprietary software under + * the Blender License. See http://www.blender.org/BL/ for information + * about this. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + * + * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV. + * All rights reserved. + * + * The Original Code is: all of this file. + * + * Contributor(s): none yet. + * + * ***** END GPL/BL DUAL LICENSE BLOCK ***** + */ + +#include "RAS_IPolygonMaterial.h" + + +RAS_IPolyMaterial::RAS_IPolyMaterial(const STR_String& texname, + bool ba, + const STR_String& matname, + int tile, + int tilexrep, + int tileyrep, + int mode, + int transparant, + int lightlayer, + bool bIsTriangle, + void* clientobject=NULL) : + + m_texturename(texname), + m_materialname(matname), + m_tile(tile), + m_tilexrep(tilexrep), + m_tileyrep(tileyrep), + m_drawingmode (mode), + m_transparant(transparant), + m_lightlayer(lightlayer), + m_bIsTriangle(bIsTriangle) +{ + m_shininess = 35.0; + m_specular = MT_Vector3(0.5,0.5,0.5); + m_specularity = 1.0; + m_diffuse = MT_Vector3(0.5,0.5,0.5); +} + + +bool RAS_IPolyMaterial::Equals(const RAS_IPolyMaterial& lhs) const +{ + return ( + this->m_texturename == lhs.m_texturename && + this->m_tile == lhs.m_tile && + this->m_tilexrep == lhs.m_tilexrep && + this->m_tileyrep == lhs.m_tileyrep && + this->m_transparant == lhs.m_transparant && + this->m_drawingmode == lhs.m_drawingmode && + this->m_bIsTriangle == lhs.m_bIsTriangle && + this->m_lightlayer == lhs.m_lightlayer && + this->m_materialname == lhs.m_materialname + ); +} + + + +int RAS_IPolyMaterial::GetLightLayer() +{ + return m_lightlayer; +} + + + +bool RAS_IPolyMaterial::IsTransparant() +{ + return (m_transparant != 0); +} + + + +bool RAS_IPolyMaterial::UsesTriangles() +{ + return m_bIsTriangle; +} + + + +unsigned int RAS_IPolyMaterial::hash() const +{ + return m_texturename.hash(); +} + + + +int RAS_IPolyMaterial::GetDrawingMode() +{ + return m_drawingmode; +} + + + +const STR_String& RAS_IPolyMaterial::GetMaterialName() const +{ + return m_materialname; +} + + +const STR_String& RAS_IPolyMaterial::GetTextureName() const +{ + return m_texturename; +} diff --git a/source/gameengine/Rasterizer/RAS_IPolygonMaterial.h b/source/gameengine/Rasterizer/RAS_IPolygonMaterial.h new file mode 100644 index 00000000000..dd5fadab008 --- /dev/null +++ b/source/gameengine/Rasterizer/RAS_IPolygonMaterial.h @@ -0,0 +1,123 @@ +/** + * $Id$ + * + * ***** BEGIN GPL/BL DUAL LICENSE BLOCK ***** + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. The Blender + * Foundation also sells licenses for use in proprietary software under + * the Blender License. See http://www.blender.org/BL/ for information + * about this. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + * + * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV. + * All rights reserved. + * + * The Original Code is: all of this file. + * + * Contributor(s): none yet. + * + * ***** END GPL/BL DUAL LICENSE BLOCK ***** + */ +#ifndef __RAS_IPOLYGONMATERIAL +#define __RAS_IPOLYGONMATERIAL + +#include "STR_HashedString.h" + + +/** + * Polygon Material on which the material buckets are sorted + * + */ +#include "MT_Vector3.h" +#include "STR_HashedString.h" + +class RAS_IRasterizer; + +class RAS_IPolyMaterial +{ + //todo: remove these variables from this interface/protocol class +protected: + STR_HashedString m_texturename; + STR_String m_materialname; //also needed for touchsensor + int m_tile; + int m_tilexrep,m_tileyrep; + int m_drawingmode; // tface->mode + int m_transparant; + int m_lightlayer; + bool m_bIsTriangle; + +public: + + MT_Vector3 m_diffuse; + float m_shininess; + MT_Vector3 m_specular; + float m_specularity; + + /** Used to store caching information for materials. */ + typedef void* TCachingInfo; + + // care! these are taken from blender polygonflags, see file DNA_mesh_types.h for #define TF_BILLBOARD etc. + enum MaterialFlags + { + BILLBOARD_SCREENALIGNED = 256, + BILLBOARD_AXISALIGNED = 4096, + SHADOW =8192 + }; + + RAS_IPolyMaterial(const STR_String& texname, + bool ba, + const STR_String& matname, + int tile, + int tilexrep, + int tileyrep, + int mode, + int transparant, + int lightlayer, + bool bIsTriangle, + void* clientobject); + virtual ~RAS_IPolyMaterial() {}; + + /** + * Returns the caching information for this material, + * This can be used to speed up the rasterizing process. + * @return The caching information. + */ + virtual TCachingInfo GetCachingInfo(void) const { return 0; } + + /** + * Activates the material in the rasterizer. + * On entry, the cachingInfo contains info about the last activated material. + * On exit, the cachingInfo should contain updated info about this material. + * @param rasty The rasterizer in which the material should be active. + * @param cachingInfo The information about the material used to speed up rasterizing. + */ + virtual void Activate(RAS_IRasterizer* rasty, TCachingInfo& cachingInfo) const {} + + bool Equals(const RAS_IPolyMaterial& lhs) const; + int GetLightLayer(); + bool IsTransparant(); + bool UsesTriangles(); + unsigned int hash() const; + int GetDrawingMode(); + const STR_String& GetMaterialName() const; + const STR_String& GetTextureName() const; +}; + +inline bool operator ==( const RAS_IPolyMaterial & rhs,const RAS_IPolyMaterial & lhs) +{ + return ( rhs.Equals(lhs)); +} + + +#endif //__RAS_IPOLYGONMATERIAL diff --git a/source/gameengine/Rasterizer/RAS_IRasterizer.h b/source/gameengine/Rasterizer/RAS_IRasterizer.h new file mode 100644 index 00000000000..d3a2a74887c --- /dev/null +++ b/source/gameengine/Rasterizer/RAS_IRasterizer.h @@ -0,0 +1,191 @@ +/** + * $Id$ + * + * ***** BEGIN GPL/BL DUAL LICENSE BLOCK ***** + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. The Blender + * Foundation also sells licenses for use in proprietary software under + * the Blender License. See http://www.blender.org/BL/ for information + * about this. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + * + * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV. + * All rights reserved. + * + * The Original Code is: all of this file. + * + * Contributor(s): none yet. + * + * ***** END GPL/BL DUAL LICENSE BLOCK ***** + */ +#ifndef __RAS_IRASTERIZER +#define __RAS_IRASTERIZER + +#ifdef WIN32 +#pragma warning (disable:4786) +#endif + +#include "MT_CmMatrix4x4.h" +#include "MT_Matrix4x4.h" + +class RAS_ICanvas; +class RAS_IPolyMaterial; +#include "RAS_MaterialBucket.h" + +/** + * 3D rendering device context interface. + */ + +class RAS_IRasterizer +{ + +public: + + RAS_IRasterizer(RAS_ICanvas* canv){}; + virtual ~RAS_IRasterizer(){}; + enum { + RAS_RENDER_3DPOLYGON_TEXT = 16384 + }; + enum { + KX_BOUNDINGBOX = 1, + KX_WIREFRAME, + KX_SOLID, + KX_SHADED, + KX_TEXTURED + }; + + enum { + KX_DEPTHMASK_ENABLED =1, + KX_DEPTHMASK_DISABLED, + }; + + enum { + RAS_STEREO_NOSTEREO = 1, + RAS_STEREO_QUADBUFFERED, + RAS_STEREO_ABOVEBELOW, + RAS_STEREO_INTERLACED, + }; + enum { + RAS_STEREO_LEFTEYE = 1, + RAS_STEREO_RIGHTEYE, + }; + + virtual void SetDepthMask(int depthmask)=0; + virtual void SetMaterial(const RAS_IPolyMaterial& mat)=0; + virtual bool Init()=0; + virtual void Exit()=0; + virtual bool BeginFrame(int drawingmode, double time)=0; + virtual void ClearDepthBuffer()=0; + virtual void ClearCachingInfo(void)=0; + virtual void EndFrame()=0; + virtual void SetRenderArea()=0; + + virtual void SetStereoMode(const int stereomode)=0; + virtual bool Stereo()=0; + virtual void SetEye(const int eye)=0; + virtual void SetEyeSeparation(const float eyeseparation)=0; + virtual void SetFocalLength(const float focallength)=0; + + virtual void SwapBuffers()=0; + virtual void IndexPrimitives( const vecVertexArray& vertexarrays, + const vecIndexArrays & indexarrays, + int mode, + class RAS_IPolyMaterial* polymat, + class RAS_IRenderTools* rendertools, + bool useObjectColor, + const MT_Vector4& rgbacolor)=0; + virtual void IndexPrimitives_Ex( const vecVertexArray& vertexarrays, + const vecIndexArrays & indexarrays, + int mode, + class RAS_IPolyMaterial* polymat, + class RAS_IRenderTools* rendertools, + bool useObjectColor, + const MT_Vector4& rgbacolor)=0; + virtual void IndexPrimitives_3DText( const vecVertexArray& vertexarrays, + const vecIndexArrays & indexarrays, + int mode, + class RAS_IPolyMaterial* polymat, + class RAS_IRenderTools* rendertools, + bool useObjectColor, + const MT_Vector4& rgbacolor)=0; + + virtual void SetProjectionMatrix(MT_CmMatrix4x4 & mat)=0; + /* This one should become our final version, methinks. */ + /** + * Set the projection matrix for the rasterizer. This projects + * from camera coordinates to window coordinates. + * @param mat The projection matrix. + */ + virtual void SetProjectionMatrix(MT_Matrix4x4 & mat)=0; + virtual void SetViewMatrix(const MT_Matrix4x4 & mat, + const MT_Vector3& campos, + const MT_Point3 &camLoc, + const MT_Quaternion &camOrientQuat)=0; + virtual const MT_Point3& GetCameraPosition()=0; + virtual void LoadViewMatrix()=0; + + virtual void SetFog(float start, + float dist, + float r, + float g, + float b)=0; + + virtual void SetFogColor(float r, + float g, + float b)=0; + + virtual void SetFogStart(float start)=0; + virtual void SetFogEnd(float end)=0; + + virtual void DisplayFog()=0; + virtual void DisableFog()=0; + + virtual void SetBackColor(float red, + float green, + float blue, + float alpha)=0; + + virtual void SetDrawingMode(int drawingmode)=0; + virtual int GetDrawingMode()=0; + + virtual void EnableTextures(bool enable)=0; + + virtual void SetCullFace(bool enable)=0; + + virtual double GetTime()=0; + + virtual MT_Matrix4x4 GetFrustumMatrix( + float left, + float right, + float bottom, + float top, + float frustnear, + float frustfar + )=0; + + virtual void SetSpecularity(float specX, + float specY, + float specZ, + float specval)=0; + + virtual void SetShinyness(float shiny)=0; + virtual void SetDiffuse(float difX, + float difY, + float difZ, + float diffuse)=0; + +}; + +#endif //__RAS_IRASTERIZER + diff --git a/source/gameengine/Rasterizer/RAS_IRenderTools.cpp b/source/gameengine/Rasterizer/RAS_IRenderTools.cpp new file mode 100644 index 00000000000..d52ba72af40 --- /dev/null +++ b/source/gameengine/Rasterizer/RAS_IRenderTools.cpp @@ -0,0 +1,76 @@ +/** + * $Id$ + * ***** BEGIN GPL/BL DUAL LICENSE BLOCK ***** + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. The Blender + * Foundation also sells licenses for use in proprietary software under + * the Blender License. See http://www.blender.org/BL/ for information + * about this. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + * + * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV. + * All rights reserved. + * + * The Original Code is: all of this file. + * + * Contributor(s): none yet. + * + * ***** END GPL/BL DUAL LICENSE BLOCK ***** + */ + +#include "RAS_IRenderTools.h" + + + +void RAS_IRenderTools::SetViewMat(const MT_Transform& trans) +{ + trans.getValue(m_viewmat); +} + + + +void RAS_IRenderTools::SetClientObject(void* obj) +{ + if (m_clientobject != obj) + { + m_clientobject = obj; + m_modified = true; + } +} + + + +void RAS_IRenderTools::SetAuxilaryClientInfo(void* inf) +{ + m_auxilaryClientInfo = inf; +} + + + +void RAS_IRenderTools::AddLight(struct RAS_LightObject* lightobject) +{ + m_lights.push_back(lightobject); +} + + + +void RAS_IRenderTools::RemoveLight(struct RAS_LightObject* lightobject) +{ + std::vector<struct RAS_LightObject*>::iterator lit = + std::find(m_lights.begin(),m_lights.end(),lightobject); + + if (!(lit==m_lights.end())) + m_lights.erase(lit); + +} diff --git a/source/gameengine/Rasterizer/RAS_IRenderTools.h b/source/gameengine/Rasterizer/RAS_IRenderTools.h new file mode 100644 index 00000000000..ac9033c7186 --- /dev/null +++ b/source/gameengine/Rasterizer/RAS_IRenderTools.h @@ -0,0 +1,196 @@ +/** + * $Id$ + * + * ***** BEGIN GPL/BL DUAL LICENSE BLOCK ***** + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. The Blender + * Foundation also sells licenses for use in proprietary software under + * the Blender License. See http://www.blender.org/BL/ for information + * about this. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + * + * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV. + * All rights reserved. + * + * The Original Code is: all of this file. + * + * Contributor(s): none yet. + * + * ***** END GPL/BL DUAL LICENSE BLOCK ***** + */ +#ifndef __RAS_IRENDERTOOLS +#define __RAS_IRENDERTOOLS + +#include "MT_Transform.h" +#include "RAS_IRasterizer.h" + + +#include <vector> +#include <algorithm> + +class RAS_IPolyMaterial; +struct RAS_LightObject; + +class RAS_IRenderTools +{ + +protected: + float m_viewmat[16]; + void* m_clientobject; + void* m_auxilaryClientInfo; + + bool m_modified; + + std::vector<struct RAS_LightObject*> m_lights; + + +public: + enum RAS_TEXT_RENDER_MODE { + RAS_TEXT_RENDER_NODEF = 0, + RAS_TEXT_NORMAL, + RAS_TEXT_PADDED, + RAS_TEXT_MAX, + }; + + RAS_IRenderTools( + ) : + m_modified(true), + m_clientobject(NULL) + { + }; + + virtual + ~RAS_IRenderTools( + ) {}; + + virtual + void + BeginFrame( + RAS_IRasterizer* rasty + )=0; + + virtual + void + EndFrame( + RAS_IRasterizer* rasty + )=0; + + // the following function was formerly called 'Render' + // by it doesn't render anymore + // It only sets the transform for the rasterizer + // so must be renamed to 'applyTransform' or something + + virtual + void + applyTransform( + class RAS_IRasterizer* rasty, + double* oglmatrix, + int drawingmode + )=0; + + /** + * Renders 2D text string. + * @param mode The type of text + * @param text The string to render. + * @param xco Position on the screen (origin in lower left corner). + * @param yco Position on the screen (origin in lower left corner). + * @param width Width of the canvas to draw to. + * @param height Height of the canvas to draw to. + */ + virtual + void + RenderText2D( + RAS_TEXT_RENDER_MODE mode, + const char* text, + int xco, + int yco, + int width, + int height + ) = 0; + + // 3d text, mapped on polygon + virtual + void + RenderText( + int mode, + RAS_IPolyMaterial* polymat, + float v1[3], + float v2[3], + float v3[3], + float v4[3] + )=0; + + virtual + void + SetViewMat( + const MT_Transform& trans + ); + + virtual + int + ProcessLighting( + int layer + )=0; + + void + SetClientObject( + void* obj + ); + + void + SetAuxilaryClientInfo( + void* inf + ); + + virtual + void + PushMatrix( + )=0; + + virtual + void + PopMatrix( + )=0; + + virtual + void + AddLight( + struct RAS_LightObject* lightobject + ); + + virtual + void + RemoveLight( + struct RAS_LightObject* lightobject + ); + + virtual + class RAS_IPolyMaterial* + CreateBlenderPolyMaterial( + const STR_String &texname, + bool ba, + const STR_String& matname, + int tile, + int tilexrep, + int tileyrep, + int mode, + int transparant, + int lightlayer, + bool bIsTriangle, + void* clientobject, + void* tface + )=0; +}; + +#endif //__RAS_IRENDERTOOLS diff --git a/source/gameengine/Rasterizer/RAS_LightObject.h b/source/gameengine/Rasterizer/RAS_LightObject.h new file mode 100644 index 00000000000..cd2014ba678 --- /dev/null +++ b/source/gameengine/Rasterizer/RAS_LightObject.h @@ -0,0 +1,63 @@ +/** + * $Id$ + * + * ***** BEGIN GPL/BL DUAL LICENSE BLOCK ***** + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. The Blender + * Foundation also sells licenses for use in proprietary software under + * the Blender License. See http://www.blender.org/BL/ for information + * about this. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + * + * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV. + * All rights reserved. + * + * The Original Code is: all of this file. + * + * Contributor(s): none yet. + * + * ***** END GPL/BL DUAL LICENSE BLOCK ***** + */ +#ifndef __RAS_LIGHTOBJECT_H +#define __RAS_LIGHTOBJECT_H + +#include "MT_CmMatrix4x4.h" + +struct RAS_LightObject +{ + enum LightType{ + LIGHT_SPOT, + LIGHT_SUN, + LIGHT_NORMAL + }; + bool m_modified; + int m_layer; + + float m_energy; + float m_distance; + + float m_red; + float m_green; + float m_blue; + + float m_att1; + float m_spotsize; + float m_spotblend; + + LightType m_type; + MT_CmMatrix4x4* m_worldmatrix; + +}; + +#endif //__RAS_LIGHTOBJECT_H diff --git a/source/gameengine/Rasterizer/RAS_MaterialBucket.cpp b/source/gameengine/Rasterizer/RAS_MaterialBucket.cpp new file mode 100644 index 00000000000..ecb621c0d9d --- /dev/null +++ b/source/gameengine/Rasterizer/RAS_MaterialBucket.cpp @@ -0,0 +1,248 @@ +/** + * $Id$ + * ***** BEGIN GPL/BL DUAL LICENSE BLOCK ***** + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. The Blender + * Foundation also sells licenses for use in proprietary software under + * the Blender License. See http://www.blender.org/BL/ for information + * about this. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + * + * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV. + * All rights reserved. + * + * The Original Code is: all of this file. + * + * Contributor(s): none yet. + * + * ***** END GPL/BL DUAL LICENSE BLOCK ***** + */ + + +#include "RAS_MaterialBucket.h" + +#ifdef WIN32 +#pragma warning (disable:4786) +#include <windows.h> +#endif // WIN32 + +#include "RAS_Polygon.h" +#include "RAS_TexVert.h" +#include "RAS_IRasterizer.h" +#include "RAS_IRenderTools.h" +#include "RAS_MeshObject.h" +#include "RAS_Deformer.h" // __NLA + + + +KX_VertexIndex::KX_VertexIndex(int size) +{ + m_size = size; +} + + + +void KX_VertexIndex::SetIndex(short loc,short index) +{ + m_indexarray[loc]=index; +} + + + + +bool KX_MeshSlot::Less(const KX_MeshSlot& lhs) const +{ + bool result = ((m_mesh < lhs.m_mesh ) || + ((m_mesh == lhs.m_mesh)&&(m_OpenGLMatrix < lhs.m_OpenGLMatrix))); + + return result; +} + + + +RAS_MaterialBucket::RAS_MaterialBucket(RAS_IPolyMaterial* mat) + :m_bModified(true) +{ + m_bScheduled=true; + m_material = mat; +} + + + +void RAS_MaterialBucket::SchedulePolygons(int drawingmode) +{ + m_bScheduled = true; +} + + + +void RAS_MaterialBucket::ClearScheduledPolygons() +{ + m_bScheduled = false; +} + + + +RAS_IPolyMaterial* RAS_MaterialBucket::GetPolyMaterial() +{ + return m_material; +} + + + +void RAS_MaterialBucket::SetMeshSlot(KX_MeshSlot& ms) +{ + m_meshSlots.insert(ms); +} + + + +void RAS_MaterialBucket::RemoveMeshSlot(KX_MeshSlot& ms) +{ + T_MeshSlotList::iterator it = m_meshSlots.find(ms); + + if (!(it == m_meshSlots.end())) + m_meshSlots.erase(it); + +} + + + +void RAS_MaterialBucket::MarkVisibleMeshSlot(KX_MeshSlot& ms, + bool visible, + bool color, + const MT_Vector4& rgbavec) +{ + T_MeshSlotList::iterator it = m_meshSlots.find(ms); + + assert (!(it == m_meshSlots.end())); + (*it).m_bVisible = visible; + (*it).m_bObjectColor = color; + (*it).m_RGBAcolor= rgbavec; +} + + + +bool RAS_MaterialBucket::IsTransparant() +{ + return (m_material->IsTransparant()); +} + + + +void RAS_MaterialBucket::StartFrame() +{ +} + + + +void RAS_MaterialBucket::EndFrame() +{ +} + + + +void RAS_MaterialBucket::Render(const MT_Transform& cameratrans, + RAS_IRasterizer* rasty, + RAS_IRenderTools* rendertools) +{ + + if (m_meshSlots.begin()== m_meshSlots.end()) + return; + + rendertools->SetViewMat(cameratrans); + + rasty->SetMaterial(*m_material); + + if (m_meshSlots.size() >0) + { + + rendertools->SetClientObject((*m_meshSlots.begin()).m_clientObj); + } + + bool dolights = m_material->GetDrawingMode()&16; + + if ((rasty->GetDrawingMode() <= RAS_IRasterizer::KX_SOLID) || !dolights) + { + bool bUseLights = rendertools->ProcessLighting(-1); + } + else + { + bool bUseLights = rendertools->ProcessLighting(m_material->GetLightLayer()); + } + + int drawmode = (rasty->GetDrawingMode() < RAS_IRasterizer::KX_SOLID ? + 1: (m_material->UsesTriangles() ? 0 : 2)); + + for (T_MeshSlotList::const_iterator it = m_meshSlots.begin(); + ! (it == m_meshSlots.end());it++) + { + if ((*it).m_bVisible) + { + rendertools->SetClientObject((*it).m_clientObj); + + /* __NLA Do the deformation */ + if ((*it).m_pDeformer) + (*it).m_pDeformer->Apply(m_material); + /* End __NLA */ + + rendertools->PushMatrix(); + rendertools->applyTransform(rasty,(*it).m_OpenGLMatrix,m_material->GetDrawingMode()); + + // Use the text-specific IndexPrimitives for text faces + if (m_material->GetDrawingMode() & RAS_IRasterizer::RAS_RENDER_3DPOLYGON_TEXT) + { + rasty->IndexPrimitives_3DText( + (*it).m_mesh->GetVertexCache(m_material), + (*it).m_mesh->GetIndexCache(m_material), + drawmode, + m_material, + rendertools, // needed for textprinting on polys + (*it).m_bObjectColor, + (*it).m_RGBAcolor); + + } + // Use the (slower) IndexPrimitives_Ex which can recalc face normals & such + // for deformed objects - eventually should be extended to recalc ALL normals + else if ((*it).m_pDeformer){ + rasty->IndexPrimitives_Ex( + (*it).m_mesh->GetVertexCache(m_material), + (*it).m_mesh->GetIndexCache(m_material), + drawmode, + m_material, + rendertools, // needed for textprinting on polys + (*it).m_bObjectColor, + (*it).m_RGBAcolor + ); + } + // Use the normal IndexPrimitives + else + { + rasty->IndexPrimitives( + (*it).m_mesh->GetVertexCache(m_material), + (*it).m_mesh->GetIndexCache(m_material), + drawmode, + m_material, + rendertools, // needed for textprinting on polys + (*it).m_bObjectColor, + (*it).m_RGBAcolor + ); + } + + rendertools->PopMatrix(); + } + } +} + + diff --git a/source/gameengine/Rasterizer/RAS_MaterialBucket.h b/source/gameengine/Rasterizer/RAS_MaterialBucket.h new file mode 100644 index 00000000000..06b1261c532 --- /dev/null +++ b/source/gameengine/Rasterizer/RAS_MaterialBucket.h @@ -0,0 +1,126 @@ +/** + * $Id$ + * + * ***** BEGIN GPL/BL DUAL LICENSE BLOCK ***** + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. The Blender + * Foundation also sells licenses for use in proprietary software under + * the Blender License. See http://www.blender.org/BL/ for information + * about this. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + * + * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV. + * All rights reserved. + * + * The Original Code is: all of this file. + * + * Contributor(s): none yet. + * + * ***** END GPL/BL DUAL LICENSE BLOCK ***** + */ +#ifndef __RAS_MATERIALBUCKET +#define __RAS_MATERIALBUCKET + +#include "RAS_TexVert.h" +#include "GEN_Map.h" +#include "STR_HashedString.h" + +#include "MT_Transform.h" +#include "RAS_IPolygonMaterial.h" +#include "RAS_Deformer.h" // __NLA +#include <vector> +#include <map> +#include <set> +using namespace std; + +typedef vector< vector<class RAS_TexVert>* > vecVertexArray; +typedef vector<unsigned int> KX_IndexArray; +typedef vector< KX_IndexArray* > vecIndexArrays; + +typedef vector<RAS_TexVert> KX_VertexArray; + + + +struct KX_VertexIndex { +public: + KX_VertexIndex(int size); + void SetIndex(short loc,short index); + short m_vtxarray; + short m_indexarray[4]; + short m_size; +}; + + + +class KX_MeshSlot +{ +public: + void* m_clientObj; + RAS_Deformer* m_pDeformer; // __NLA + double* m_OpenGLMatrix; + class RAS_MeshObject* m_mesh; + mutable bool m_bVisible; // for visibility + mutable bool m_bObjectColor; + mutable MT_Vector4 m_RGBAcolor; + + KX_MeshSlot() :m_bVisible(true), m_pDeformer(NULL) {} + // KX_MeshSlot() :m_bVisible(true) {} + + bool Less(const KX_MeshSlot& lhs) const; +}; + + +inline bool operator <( const KX_MeshSlot& rhs,const KX_MeshSlot& lhs) +{ + return ( rhs.Less(lhs)); +} + + +class RAS_MaterialBucket +{ + typedef std::set<KX_MeshSlot> T_MeshSlotList; + + T_MeshSlotList m_meshSlots; + bool m_bScheduled; + bool m_bModified; + RAS_IPolyMaterial* m_material; + double* m_pOGLMatrix; + +public: + RAS_MaterialBucket(RAS_IPolyMaterial* mat); + virtual ~RAS_MaterialBucket() {} + + void Render(const MT_Transform& cameratrans, + class RAS_IRasterizer* rasty, + class RAS_IRenderTools* rendertools); + + void SchedulePolygons(int drawingmode); + void ClearScheduledPolygons(); + + RAS_IPolyMaterial* GetPolyMaterial(); + bool IsTransparant(); + + static void StartFrame(); + static void EndFrame(); + + void SetMeshSlot(KX_MeshSlot& ms); + void RemoveMeshSlot(KX_MeshSlot& ms); + void MarkVisibleMeshSlot(KX_MeshSlot& ms, + bool visible, + bool color, + const MT_Vector4& rgbavec); +}; + + +#endif //__KX_BUCKET diff --git a/source/gameengine/Rasterizer/RAS_MeshObject.cpp b/source/gameengine/Rasterizer/RAS_MeshObject.cpp new file mode 100644 index 00000000000..3c78b7cf991 --- /dev/null +++ b/source/gameengine/Rasterizer/RAS_MeshObject.cpp @@ -0,0 +1,635 @@ +/** + * $Id$ + * ***** BEGIN GPL/BL DUAL LICENSE BLOCK ***** + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. The Blender + * Foundation also sells licenses for use in proprietary software under + * the Blender License. See http://www.blender.org/BL/ for information + * about this. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + * + * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV. + * All rights reserved. + * + * The Original Code is: all of this file. + * + * Contributor(s): none yet. + * + * ***** END GPL/BL DUAL LICENSE BLOCK ***** + */ + +#include "RAS_MeshObject.h" + +#include "RAS_IRasterizer.h" +#include "MT_MinMax.h" + +STR_String RAS_MeshObject::s_emptyname = ""; + + + +KX_ArrayOptimizer::~KX_ArrayOptimizer() +{ + int i = 0; + + for (vector<KX_VertexArray*>::iterator itv = m_VertexArrayCache1.begin(); + !(itv == m_VertexArrayCache1.end());itv++) + { + delete (*itv); + } + + for (vector<KX_IndexArray*>::iterator iti = m_IndexArrayCache1.begin(); + !(iti == m_IndexArrayCache1.end());iti++) + { + delete (*iti); + } + + m_TriangleArrayCount.clear(); + m_VertexArrayCache1.clear(); + m_IndexArrayCache1.clear(); + + +} + + + +RAS_MeshObject::RAS_MeshObject(int lightlayer) + : m_bModified(true), + m_lightlayer(lightlayer) +{ +} + + + +RAS_MeshObject::~RAS_MeshObject() +{ + for (vector<RAS_Polygon*>::iterator it=m_Polygons.begin();!(it==m_Polygons.end());it++) + { + delete (*it); + } + + ClearArrayData(); +} + + + +int RAS_MeshObject::GetLightLayer() +{ + return m_lightlayer; +} + + + +int RAS_MeshObject::NumMaterials() +{ + return m_materials.size(); +} + + + +const STR_String& RAS_MeshObject::GetMaterialName(int matid) +{ + if (m_materials.size() > 0 && (matid < m_materials.size())) + { + BucketMaterialSet::iterator it = m_materials.begin(); + + for (int i = 1; i < m_materials.size(); i++) + { + it++; + } + return (*it)->GetPolyMaterial()->GetMaterialName(); + } + + return s_emptyname; +} + + + +RAS_MaterialBucket* RAS_MeshObject::GetMaterialBucket(int matid) +{ + RAS_MaterialBucket* bucket = NULL; + + if (m_materials.size() > 0 && (matid < m_materials.size())) + { + BucketMaterialSet::iterator it = m_materials.begin(); + int i = matid; + while (i > 0) + { + i--; + it++; + } + bucket = *it; + } + + return bucket; +} + + + +int RAS_MeshObject::NumPolygons() +{ + return m_Polygons.size(); +} + + + +RAS_Polygon* RAS_MeshObject::GetPolygon(int num) +{ + return m_Polygons[num]; +} + + + +set<RAS_MaterialBucket*>::iterator RAS_MeshObject::GetFirstMaterial() +{ + return m_materials.begin(); +} + + + +set<RAS_MaterialBucket*>::iterator RAS_MeshObject::GetLastMaterial() +{ + return m_materials.end(); +} + + + +void RAS_MeshObject::SetName(STR_String name) +{ + m_name = name; +} + + + +const STR_String& RAS_MeshObject::GetName() +{ + return m_name; +} + + + +const STR_String& RAS_MeshObject::GetTextureName(int matid) +{ + if (m_materials.size() > 0 && (matid < m_materials.size())) + { + BucketMaterialSet::iterator it = m_materials.begin(); + for (int i = 1; i < m_materials.size(); i++) + { + it++; + } + + return (*it)->GetPolyMaterial()->GetTextureName(); + } + + return s_emptyname; +} + + + +void RAS_MeshObject::AddPolygon(RAS_Polygon* poly) +{ + m_Polygons.push_back(poly); +} + + + +void RAS_MeshObject::DebugColor(unsigned int abgr) +{ +/* + int numpolys = NumPolygons(); + for (int i=0;i<numpolys;i++) + { + RAS_Polygon* poly = m_polygons[i]; + for (int v=0;v<poly->VertexCount();v++) + { + RAS_TexVert* vtx = poly->GetVertex(v); + vtx->setDebugRGBA(abgr); + } + } + */ + + m_debugcolor = abgr; +} + + + +void RAS_MeshObject::SchedulePoly(const KX_VertexIndex& idx, + int numverts, + RAS_IPolyMaterial* mat) +{ + //int indexpos = m_IndexArrayCount[idx.m_vtxarray]; + int indexidx = 0; + //m_IndexArrayCount[idx.m_vtxarray] = indexpos + 3; + + KX_ArrayOptimizer* ao = GetArrayOptimizer(mat); + ao->m_IndexArrayCache1[idx.m_vtxarray]->push_back(idx.m_indexarray[0]); + ao->m_IndexArrayCache1[idx.m_vtxarray]->push_back(idx.m_indexarray[1]); + ao->m_IndexArrayCache1[idx.m_vtxarray]->push_back(idx.m_indexarray[2]); + if (!mat->UsesTriangles()) //if (!m_bUseTriangles) + { + //m_IndexArrayCount[idx.m_vtxarray] = indexpos+4; + ao->m_IndexArrayCache1[idx.m_vtxarray]->push_back(idx.m_indexarray[3]); + } +} + + + +void RAS_MeshObject::ScheduleWireframePoly(const KX_VertexIndex& idx, + int numverts, + int edgecode, + RAS_IPolyMaterial* mat) +{ + //int indexpos = m_IndexArrayCount[idx.m_vtxarray]; + int edgetrace = 1<<(numverts-1); + bool drawedge = (edgecode & edgetrace)!=0; + edgetrace = 1; + int prevvert = idx.m_indexarray[numverts-1]; + KX_ArrayOptimizer* ao = GetArrayOptimizer(mat); + + for (int v = 0; v < numverts; v++) + { + unsigned int curvert = idx.m_indexarray[v]; + if (drawedge) + { + ao->m_IndexArrayCache1[idx.m_vtxarray]->push_back(prevvert); + ao->m_IndexArrayCache1[idx.m_vtxarray]->push_back(curvert); + } + prevvert = curvert; + drawedge = (edgecode & edgetrace)!=0; + edgetrace*=2; + } + //m_IndexArrayCount[idx.m_vtxarray] = indexpos; +} + +int RAS_MeshObject::FindOrAddVertex(int vtxarray, + const MT_Point3& xyz, + const MT_Point2& uv, + const unsigned int rgbacolor, + const MT_Vector3& normal, + RAS_IPolyMaterial* mat, + int orgindex) +{ + short newnormal[3]; + + newnormal[0]=(short)((normal[0])*32767.0); + newnormal[1]=(short)((normal[1])*32767.0); + newnormal[2]=(short)((normal[2])*32767.0); + + KX_ArrayOptimizer* ao = GetArrayOptimizer(mat);//*(m_matVertexArrays[*mat]); + + int numverts = ao->m_VertexArrayCache1[vtxarray]->size();//m_VertexArrayCount[vtxarray]; + +#define KX_FIND_SHARED_VERTICES +#ifdef KX_FIND_SHARED_VERTICES + + std::vector<RAS_MatArrayIndex>::iterator it = m_xyz_index_to_vertex_index_mapping[orgindex].begin(); + int index=-1; + while (index < 0 && !(it == m_xyz_index_to_vertex_index_mapping[orgindex].end())) + { + if ((*it).m_arrayindex1 == ao->m_index1 && + ((*it).m_array == vtxarray) && + (RAS_IPolyMaterial*) (*it).m_matid == mat + ) + { + return (*it).m_index; + } + it++; + } + + if (index >= 0) + return index; +#endif // KX_FIND_SHARED_VERTICES + + // no vertex found, add one + ao->m_VertexArrayCache1[vtxarray]->push_back(RAS_TexVert (xyz,uv,rgbacolor,newnormal, 0)); + // printf("(%f,%f,%f) ",xyz[0],xyz[1],xyz[2]); + RAS_MatArrayIndex idx; + idx.m_arrayindex1 = ao->m_index1; + idx.m_array = vtxarray; + idx.m_index = numverts; + idx.m_matid = (int) mat; + m_xyz_index_to_vertex_index_mapping[orgindex].push_back(idx); + + return numverts; +} + + + +const vecVertexArray& RAS_MeshObject::GetVertexCache (RAS_IPolyMaterial* mat) +{ + KX_ArrayOptimizer* ao = GetArrayOptimizer(mat);//*(m_matVertexArrays[*mat]); + + return ao->m_VertexArrayCache1; +} + + + +int RAS_MeshObject::GetVertexArrayLength(RAS_IPolyMaterial* mat) +{ + int len = 0; + + const vecVertexArray & vertexvec = GetVertexCache(mat); + vector<KX_VertexArray*>::const_iterator it = vertexvec.begin(); + + for (; it != vertexvec.end(); it++) + { + len += (*it)->size(); + } + + return len; +} + + + +RAS_TexVert* RAS_MeshObject::GetVertex(int matid, + int index) +{ + RAS_TexVert* vertex = NULL; + + RAS_MaterialBucket* bucket = GetMaterialBucket(matid); + if (bucket) + { + RAS_IPolyMaterial* mat = bucket->GetPolyMaterial(); + if (mat) + { + const vecVertexArray & vertexvec = GetVertexCache(mat); + vector<KX_VertexArray*>::const_iterator it = vertexvec.begin(); + + for (int len = 0; it != vertexvec.end(); it++) + { + if (index < len + (*it)->size()) + { + vertex = &(*(*it))[index-len]; + break; + } + else + { + len += (*it)->size(); + } + } + } + } + + return vertex; +} + + + +const vecIndexArrays& RAS_MeshObject::GetIndexCache (RAS_IPolyMaterial* mat) +{ + KX_ArrayOptimizer* ao = GetArrayOptimizer(mat);//*(m_matVertexArrays[*mat]); + + return ao->m_IndexArrayCache1; +} + + + +KX_ArrayOptimizer* RAS_MeshObject::GetArrayOptimizer(RAS_IPolyMaterial* polymat) +{ + KX_ArrayOptimizer** aop = (m_matVertexArrayS[*polymat]); + + if (aop) + return *aop; + + int numelements = m_matVertexArrayS.size(); + m_sortedMaterials.push_back(polymat); + + KX_ArrayOptimizer* ao = new KX_ArrayOptimizer(numelements); + m_matVertexArrayS.insert(*polymat,ao); + + return ao; +} + + + +void RAS_MeshObject::Bucketize(double* oglmatrix, + void* clientobj, + bool useObjectColor, + const MT_Vector4& rgbavec) +{ + KX_MeshSlot ms; + ms.m_clientObj = clientobj; + ms.m_mesh = this; + ms.m_OpenGLMatrix = oglmatrix; + ms.m_bObjectColor = useObjectColor; + ms.m_RGBAcolor = rgbavec; + + int i=0; + for (BucketMaterialSet::iterator it = m_materials.begin();it!=m_materials.end();it++) + { + RAS_MaterialBucket* bucket = *it; + bucket->SchedulePolygons(0); + KX_ArrayOptimizer* oa = GetArrayOptimizer(bucket->GetPolyMaterial()); + bucket->SetMeshSlot(ms); + } + +} + + + +void RAS_MeshObject::MarkVisible(double* oglmatrix, + void* clientobj, + bool visible, + bool useObjectColor, + const MT_Vector4& rgbavec) +{ + KX_MeshSlot ms; + ms.m_clientObj = clientobj; + ms.m_mesh = this; + ms.m_OpenGLMatrix = oglmatrix; + ms.m_RGBAcolor = rgbavec; + ms.m_bObjectColor= useObjectColor; + + for (BucketMaterialSet::iterator it = m_materials.begin();it!=m_materials.end();it++) + { + RAS_MaterialBucket* bucket = *it; + bucket->SchedulePolygons(0); + KX_ArrayOptimizer* oa = GetArrayOptimizer(bucket->GetPolyMaterial()); + bucket->MarkVisibleMeshSlot(ms,visible,useObjectColor,rgbavec); + } +} + + + +void RAS_MeshObject::RemoveFromBuckets(double* oglmatrix, + void* clientobj) +{ + KX_MeshSlot ms; + ms.m_clientObj = clientobj; + ms.m_mesh = this; + ms.m_OpenGLMatrix = oglmatrix; + + for (BucketMaterialSet::iterator it = m_materials.begin();it!=m_materials.end();it++) + { + RAS_MaterialBucket* bucket = *it; + RAS_IPolyMaterial* polymat = bucket->GetPolyMaterial(); + bucket->SchedulePolygons(0); + //KX_ArrayOptimizer* oa = GetArrayOptimizer(polymat); + bucket->RemoveMeshSlot(ms); + } + +} + + + +/* + * RAS_MeshObject::GetVertex returns the vertex located somewhere in the vertexpool + * it is the clients responsibility to make sure the array and index are valid + */ +RAS_TexVert* RAS_MeshObject::GetVertex(short array, + short index, + RAS_IPolyMaterial* polymat) +{ + KX_ArrayOptimizer* ao = GetArrayOptimizer(polymat);//*(m_matVertexArrays[*polymat]); + return &((*(ao->m_VertexArrayCache1)[array])[index]); +} + + + +void RAS_MeshObject::ClearArrayData() +{ + for (int i=0;i<m_matVertexArrayS.size();i++) + { KX_ArrayOptimizer** ao = m_matVertexArrayS.at(i); + if (ao) + delete *ao; + } +} + + + +/** + * RAS_MeshObject::CreateNewVertices creates vertices within sorted pools of vertices that share same material +*/ +int RAS_MeshObject::FindVertexArray(int numverts, + RAS_IPolyMaterial* polymat) +{ + bool found=false; + int array=-1; + + KX_ArrayOptimizer* ao = GetArrayOptimizer(polymat); + + for (int i=0;i<ao->m_VertexArrayCache1.size();i++) + { + if ( (ao->m_TriangleArrayCount[i] + (numverts-2)) < BUCKET_MAX_TRIANGLES) + { + if((ao->m_VertexArrayCache1[i]->size()+numverts < BUCKET_MAX_INDICES)) + { + array = i; + ao->m_TriangleArrayCount[array]+=numverts-2; + break; + } + } + else + { + int i=0; + } + } + + if (array == -1) + { + array = ao->m_VertexArrayCache1.size(); + vector<RAS_TexVert>* va = new vector<RAS_TexVert>; + ao->m_VertexArrayCache1.push_back(va); + KX_IndexArray *ia = new KX_IndexArray(); + ao->m_IndexArrayCache1.push_back(ia); + ao->m_TriangleArrayCount.push_back(numverts-2); + } + + return array; +} + + + + +//void RAS_MeshObject::Transform(const MT_Transform& trans) +//{ + //m_trans.translate(MT_Vector3(0,0,1));//.operator *=(trans); + +// for (int i=0;i<m_Polygons.size();i++) +// { +// m_Polygons[i]->Transform(trans); +// } +//} + + +/* +void RAS_MeshObject::RelativeTransform(const MT_Vector3& vec) +{ + for (int i=0;i<m_Polygons.size();i++) + { + m_Polygons[i]->RelativeTransform(vec); + } +} +*/ + + + +void RAS_MeshObject::UpdateMaterialList() +{ + m_materials.clear(); + int numpolys = m_Polygons.size(); + // for all polygons, find out which material they use, and add it to the set of materials + for (int i=0;i<numpolys;i++) + { + m_materials.insert(m_Polygons[i]->GetMaterial()); + } + int nummaterials = m_materials.size(); + +} + + + +void RAS_MeshObject::SchedulePolygons(int drawingmode,RAS_IRasterizer* rasty) +{ + int nummaterials = m_materials.size(); + int i; + + if (m_bModified) + { + for (BucketMaterialSet::iterator it = m_materials.begin();it!=m_materials.end();it++) + { + RAS_MaterialBucket* bucket = *it; + + bucket->SchedulePolygons(drawingmode); + } + + int numpolys = m_Polygons.size(); + + if ((rasty->GetDrawingMode() > RAS_IRasterizer::KX_BOUNDINGBOX) && + (rasty->GetDrawingMode() < RAS_IRasterizer::KX_SOLID)) + { + for (i=0;i<numpolys;i++) + { + RAS_Polygon* poly = m_Polygons[i]; + if (poly->IsVisible()) + ScheduleWireframePoly(poly->GetVertexIndexBase(),poly->VertexCount(),poly->GetEdgeCode() + ,poly->GetMaterial()->GetPolyMaterial()); + + } + } + else + { + for (i=0;i<numpolys;i++) + { + RAS_Polygon* poly = m_Polygons[i]; + if (poly->IsVisible()) + { + SchedulePoly(poly->GetVertexIndexBase(),poly->VertexCount(),poly->GetMaterial()->GetPolyMaterial()); + } + } + } + + m_bModified = false; + } + // } +} diff --git a/source/gameengine/Rasterizer/RAS_MeshObject.h b/source/gameengine/Rasterizer/RAS_MeshObject.h new file mode 100644 index 00000000000..95a2858d7bf --- /dev/null +++ b/source/gameengine/Rasterizer/RAS_MeshObject.h @@ -0,0 +1,230 @@ +/** + * $Id$ + * + * ***** BEGIN GPL/BL DUAL LICENSE BLOCK ***** + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. The Blender + * Foundation also sells licenses for use in proprietary software under + * the Blender License. See http://www.blender.org/BL/ for information + * about this. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + * + * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV. + * All rights reserved. + * + * The Original Code is: all of this file. + * + * Contributor(s): none yet. + * + * ***** END GPL/BL DUAL LICENSE BLOCK ***** + */ +#ifndef __RAS_MESHOBJECT +#define __RAS_MESHOBJECT + + +#ifdef WIN32 +// disable the STL warnings ("debug information length > 255") +#pragma warning (disable:4786) +#endif + +#include <vector> +#include <set> + +#include "RAS_Polygon.h" +#include "MT_Transform.h" + +#include "GEN_HashedPtr.h" + +class KX_ArrayOptimizer +{ +public: + KX_ArrayOptimizer(int index) + : m_index1(index) + {}; + virtual ~KX_ArrayOptimizer(); + + vector<KX_VertexArray*> m_VertexArrayCache1; + vector<int> m_TriangleArrayCount; + vector<KX_IndexArray*> m_IndexArrayCache1; + + /** + order in which they are stored into the mesh + */ + int m_index1; +}; + +struct RAS_TriangleIndex +{ +public: + int m_index[3]; + bool m_collider; +}; + +class RAS_MatArrayIndex +{ +public: + + int m_arrayindex1; + int m_matid; + int m_array; + int m_index; + + inline bool Less(const RAS_MatArrayIndex& lhs) const { + bool result = + ( (m_matid < lhs.m_matid) || + ((m_matid == lhs.m_matid)&&(m_array < lhs.m_array)) || + ((m_matid == lhs.m_matid) && (m_array == lhs.m_array) && + (m_index < lhs.m_index)) + + ); + return result; + + } + + +}; +inline bool operator <( const RAS_MatArrayIndex& rhs,const RAS_MatArrayIndex& lhs) +{ + return ( rhs.Less(lhs)); +} + + +class RAS_MeshObject +{ + + enum { BUCKET_MAX_INDICES = 2048 };//2048};//8192}; + enum { BUCKET_MAX_TRIANGLES = 1024 }; + + // GEN_Map<class RAS_IPolyMaterial,KX_ArrayOptimizer*> m_matVertexArrayS; + //vector<class RAS_IPolyMaterial*,KX_ArrayOptimizer> m_vertexArrays; + virtual KX_ArrayOptimizer* GetArrayOptimizer(RAS_IPolyMaterial* polymat); + //vector<RAS_Polygon*> m_polygons; + + unsigned int m_debugcolor; + bool m_bModified; + int m_lightlayer; + + vector<class RAS_Polygon*> m_Polygons; + STR_String m_name; + static STR_String s_emptyname; + +protected: + GEN_Map<class RAS_IPolyMaterial,KX_ArrayOptimizer*> m_matVertexArrayS; + typedef set<class RAS_MaterialBucket*> BucketMaterialSet; + + BucketMaterialSet m_materials; +public: + // for now, meshes need to be in a certain layer (to avoid sorting on lights in realtime) + RAS_MeshObject(int lightlayer); + virtual ~RAS_MeshObject(); + + vector<RAS_IPolyMaterial*> m_sortedMaterials; + vector<vector<RAS_MatArrayIndex> > m_xyz_index_to_vertex_index_mapping; + vector<RAS_TriangleIndex > m_triangle_indices; + + int GetLightLayer(); + int NumMaterials(); + const STR_String& GetMaterialName(int matid); + RAS_MaterialBucket* GetMaterialBucket(int matid); + const STR_String& GetTextureName(int matid); + virtual void AddPolygon(RAS_Polygon* poly); + void UpdateMaterialList(); + + int NumPolygons(); + RAS_Polygon* GetPolygon(int num); + + virtual void Bucketize( + double* oglmatrix, + void* clientobj, + bool useObjectColor, + const MT_Vector4& rgbavec + ); + + void RemoveFromBuckets( + double* oglmatrix, + void* clientobj + ); + + void MarkVisible( + double* oglmatrix, + void* clientobj, + bool visible, + bool useObjectColor, + const MT_Vector4& rgbavec + ); + + void DebugColor(unsigned int abgr); + + void SchedulePolygons( + int drawingmode, + class RAS_IRasterizer* rasty + ); + + void ClearArrayData(); + + set<RAS_MaterialBucket*>::iterator GetFirstMaterial(); + set<RAS_MaterialBucket*>::iterator GetLastMaterial(); + + virtual RAS_TexVert* GetVertex( + short array, + short index, + RAS_IPolyMaterial* polymat + ); + + virtual int FindVertexArray( + int numverts, + RAS_IPolyMaterial* polymat + ); + + void SchedulePoly( + const KX_VertexIndex& idx, + int numverts, + RAS_IPolyMaterial* mat + ); + + void ScheduleWireframePoly( + const KX_VertexIndex& idx, + int numverts, + int edgecode, + RAS_IPolyMaterial* mat + ); + + // find (and share) or add vertices + // for some speedup, only the last 20 added vertices are searched for equality + + virtual int FindOrAddVertex( + int vtxarray, + const MT_Point3& xyz, + const MT_Point2& uv, + const unsigned int rgbacolor, + const MT_Vector3& normal, + RAS_IPolyMaterial* mat, + int orgindex + ); + + const vecVertexArray& GetVertexCache (RAS_IPolyMaterial* mat); + + int GetVertexArrayLength(RAS_IPolyMaterial* mat); + + RAS_TexVert* GetVertex( + int matid, + int index + ); + + const vecIndexArrays& GetIndexCache (RAS_IPolyMaterial* mat); + void SetName(STR_String name); + const STR_String& GetName(); +}; + +#endif //__RAS_MESHOBJECT diff --git a/source/gameengine/Rasterizer/RAS_ObjectColor.h b/source/gameengine/Rasterizer/RAS_ObjectColor.h new file mode 100644 index 00000000000..217ca9c8343 --- /dev/null +++ b/source/gameengine/Rasterizer/RAS_ObjectColor.h @@ -0,0 +1,41 @@ +/** + * $Id$ + * + * ***** BEGIN GPL/BL DUAL LICENSE BLOCK ***** + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. The Blender + * Foundation also sells licenses for use in proprietary software under + * the Blender License. See http://www.blender.org/BL/ for information + * about this. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + * + * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV. + * All rights reserved. + * + * The Original Code is: all of this file. + * + * Contributor(s): none yet. + * + * ***** END GPL/BL DUAL LICENSE BLOCK ***** + */ +#ifndef __RAS_OBJECTCOLOR_H +#define __RAS_OBJECTCOLOR_H + +struct RAS_ObjectColor { + float m_red; + float m_green; + float m_blue; +}; + +#endif //__RAS_OBJECTCOLOR_H diff --git a/source/gameengine/Rasterizer/RAS_OpenGLRasterizer/Makefile b/source/gameengine/Rasterizer/RAS_OpenGLRasterizer/Makefile new file mode 100644 index 00000000000..c03aab3422e --- /dev/null +++ b/source/gameengine/Rasterizer/RAS_OpenGLRasterizer/Makefile @@ -0,0 +1,47 @@ +# +# $Id$ +# +# ***** BEGIN GPL/BL DUAL LICENSE BLOCK ***** +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License +# as published by the Free Software Foundation; either version 2 +# of the License, or (at your option) any later version. The Blender +# Foundation also sells licenses for use in proprietary software under +# the Blender License. See http://www.blender.org/BL/ for information +# about this. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software Foundation, +# Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +# +# The Original Code is Copyright (C) 2001-2002 by NaN Holding BV. +# All rights reserved. +# +# The Original Code is: all of this file. +# +# Contributor(s): none yet. +# +# ***** END GPL/BL DUAL LICENSE BLOCK ***** +# +# Bounce make to subdirectories. +# + +LIBNAME = OpenGLrasterizer +DIR = $(OCGDIR)/gameengine/$(LIBNAME) + +include nan_compile.mk + +CCFLAGS += $(LEVEL_1_CPP_WARNINGS) + +CPPFLAGS += -I$(OPENGL_HEADERS) +CPPFLAGS += -I$(NAN_STRING)/include +CPPFLAGS += -I$(NAN_MOTO)/include +CPPFLAGS += -I../../../kernel/gen_system +CPPFLAGS += -I.. + diff --git a/source/gameengine/Rasterizer/RAS_OpenGLRasterizer/RAS_CheckVertexArrays.cpp b/source/gameengine/Rasterizer/RAS_OpenGLRasterizer/RAS_CheckVertexArrays.cpp new file mode 100644 index 00000000000..ff57e7ab102 --- /dev/null +++ b/source/gameengine/Rasterizer/RAS_OpenGLRasterizer/RAS_CheckVertexArrays.cpp @@ -0,0 +1,61 @@ +/** + * $Id$ + * + * ***** BEGIN GPL/BL DUAL LICENSE BLOCK ***** + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. The Blender + * Foundation also sells licenses for use in proprietary software under + * the Blender License. See http://www.blender.org/BL/ for information + * about this. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + * + * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV. + * All rights reserved. + * + * The Original Code is: all of this file. + * + * Contributor(s): none yet. + * + * ***** END GPL/BL DUAL LICENSE BLOCK ***** + */ +#include "RAS_CheckVertexArrays.h" + +#ifdef WIN32 +#include <windows.h> +#endif // WIN32 +#include "GL/gl.h" + +#include "STR_String.h" + + +bool RAS_SystemSupportsVertexArrays() { + + bool result = false; + + char* ext = (char*) glGetString(GL_EXTENSIONS); + STR_String extensions; + + if (ext) + extensions = STR_String(ext); + +#ifdef WIN32 + if (extensions.Find("GL_EXT_compiled_vertex_array") >= 0) + { + result=true; + } +#endif //WIN32 + + return result; + +} diff --git a/source/gameengine/Rasterizer/RAS_OpenGLRasterizer/RAS_CheckVertexArrays.h b/source/gameengine/Rasterizer/RAS_OpenGLRasterizer/RAS_CheckVertexArrays.h new file mode 100644 index 00000000000..f9edafb7d2e --- /dev/null +++ b/source/gameengine/Rasterizer/RAS_OpenGLRasterizer/RAS_CheckVertexArrays.h @@ -0,0 +1,37 @@ +/** + * $Id$ + * + * ***** BEGIN GPL/BL DUAL LICENSE BLOCK ***** + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. The Blender + * Foundation also sells licenses for use in proprietary software under + * the Blender License. See http://www.blender.org/BL/ for information + * about this. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + * + * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV. + * All rights reserved. + * + * The Original Code is: all of this file. + * + * Contributor(s): none yet. + * + * ***** END GPL/BL DUAL LICENSE BLOCK ***** + */ +#ifndef __RAS_CHECKVERTEXARRAYS +#define __RAS_CHECKVERTEXARRAYS + +bool RAS_SystemSupportsVertexArrays(); + +#endif //__RAS_CHECKVERTEXARRAYS diff --git a/source/gameengine/Rasterizer/RAS_OpenGLRasterizer/RAS_OpenGLRasterizer.cpp b/source/gameengine/Rasterizer/RAS_OpenGLRasterizer/RAS_OpenGLRasterizer.cpp new file mode 100644 index 00000000000..32b5852137b --- /dev/null +++ b/source/gameengine/Rasterizer/RAS_OpenGLRasterizer/RAS_OpenGLRasterizer.cpp @@ -0,0 +1,1226 @@ +#include "RAS_OpenGLRasterizer.h" + +#ifdef WIN32 +#include <windows.h> +#endif // WIN32 +#include "GL/gl.h" + +#include "RAS_Rect.h" +#include "RAS_TexVert.h" +#include "MT_CmMatrix4x4.h" +#include "RAS_IRenderTools.h" // rendering text + + +RAS_OpenGLRasterizer::RAS_OpenGLRasterizer(RAS_ICanvas* canvas) + :RAS_IRasterizer(canvas), + m_2DCanvas(canvas), + m_fogenabled(false), + m_materialCachingInfo(0), + m_noOfScanlines(32) +{ + m_viewmatrix.Identity(); + m_stereomode = RAS_STEREO_NOSTEREO; +} + + + +RAS_OpenGLRasterizer::~RAS_OpenGLRasterizer() +{ +} + + + +void Myinit_gl_stuff(void) +{ + float mat_specular[] = { 0.5, 0.5, 0.5, 1.0 }; + float mat_shininess[] = { 35.0 }; +/* float one= 1.0; */ + int a, x, y; + GLubyte pat[32*32]; + const GLubyte *patc= pat; + + glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, mat_specular); + glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, mat_specular); + glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, mat_shininess); + + +#if defined(__FreeBSD) || defined(__linux__) + glDisable(GL_DITHER); /* op sgi/sun hardware && 12 bits */ +#endif + + /* no local viewer, looks ugly in ortho mode */ + /* glLightModelfv(GL_LIGHT_MODEL_LOCAL_VIEWER, &one); */ + + glDepthFunc(GL_LEQUAL); + /* scaling matrices */ + glEnable(GL_NORMALIZE); + + glShadeModel(GL_FLAT); + + glDisable(GL_ALPHA_TEST); + glDisable(GL_BLEND); + glDisable(GL_DEPTH_TEST); + glDisable(GL_FOG); + glDisable(GL_LIGHTING); + glDisable(GL_LOGIC_OP); + glDisable(GL_STENCIL_TEST); + glDisable(GL_TEXTURE_1D); + glDisable(GL_TEXTURE_2D); + + glPixelTransferi(GL_MAP_COLOR, GL_FALSE); + glPixelTransferi(GL_RED_SCALE, 1); + glPixelTransferi(GL_RED_BIAS, 0); + glPixelTransferi(GL_GREEN_SCALE, 1); + glPixelTransferi(GL_GREEN_BIAS, 0); + glPixelTransferi(GL_BLUE_SCALE, 1); + glPixelTransferi(GL_BLUE_BIAS, 0); + glPixelTransferi(GL_ALPHA_SCALE, 1); + glPixelTransferi(GL_ALPHA_BIAS, 0); + + a = 0; + for(x=0; x<32; x++) + { + for(y=0; y<4; y++) + { + if( (x) & 1) pat[a++]= 0x88; + else pat[a++]= 0x22; + } + } + + glPolygonStipple(patc); +} + + + +bool RAS_OpenGLRasterizer::Init() +{ + + Myinit_gl_stuff(); + + m_redback = 0.4375; + m_greenback = 0.4375; + m_blueback = 0.4375; + m_alphaback = 0.0; + + // enable both vertexcolor AND lighting color + glEnable(GL_COLOR_MATERIAL); + + glClearColor(m_redback,m_greenback,m_blueback,m_alphaback); + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + + glShadeModel(GL_SMOOTH); + + return true; +} + + + +void RAS_OpenGLRasterizer::SetBackColor(float red, + float green, + float blue, + float alpha) +{ + m_redback = red; + m_greenback = green; + m_blueback = blue; + m_alphaback = alpha; +} + + + +void RAS_OpenGLRasterizer::SetFogColor(float r, + float g, + float b) +{ + m_fogr = r; + m_fogg = g; + m_fogb = b; + m_fogenabled = true; +} + + + +void RAS_OpenGLRasterizer::SetFogStart(float start) +{ + m_fogstart = start; + m_fogenabled = true; +} + + + +void RAS_OpenGLRasterizer::SetFogEnd(float fogend) +{ + m_fogdist = fogend; + m_fogenabled = true; +} + + + +void RAS_OpenGLRasterizer::SetFog(float start, + float dist, + float r, + float g, + float b) +{ + m_fogstart = start; + m_fogdist = dist; + m_fogr = r; + m_fogg = g; + m_fogb = b; + m_fogenabled = true; +} + + + +void RAS_OpenGLRasterizer::DisableFog() +{ + m_fogenabled = false; +} + + + +void RAS_OpenGLRasterizer::DisplayFog() +{ + if ((m_drawingmode >= KX_SOLID) && m_fogenabled) + { + float params[5]; + 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); + glEnable(GL_FOG); + } + else + { + glDisable(GL_FOG); + } +} + + + +void RAS_OpenGLRasterizer::SetMaterial(const RAS_IPolyMaterial& mat) +{ + if (mat.GetCachingInfo() != m_materialCachingInfo) + { + mat.Activate(this, m_materialCachingInfo); + } +} + + + +void RAS_OpenGLRasterizer::Exit() +{ + + glEnable(GL_CULL_FACE); + glEnable(GL_DEPTH_TEST); + glClearDepth(1.0); + glClearColor(m_redback, m_greenback, m_blueback, m_alphaback); + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + glDepthMask (GL_TRUE); + glDepthFunc(GL_LEQUAL); + glBlendFunc(GL_ONE, GL_ZERO); + + glDisable(GL_LIGHTING); + + EndFrame(); +} + + + +bool RAS_OpenGLRasterizer::BeginFrame(int drawingmode, double time) +{ + m_time = time; + m_drawingmode = drawingmode; + + m_2DCanvas->ClearColor(m_redback,m_greenback,m_blueback,m_alphaback); + m_2DCanvas->ClearBuffer(RAS_ICanvas::COLOR_BUFFER); + + // Blender camera routine destroys the settings + if (m_drawingmode < KX_SOLID) + { + glDisable (GL_CULL_FACE); + glDisable (GL_DEPTH_TEST); + } + else + { + glEnable(GL_DEPTH_TEST); + glEnable (GL_CULL_FACE); + } + + glShadeModel(GL_SMOOTH); + + m_2DCanvas->BeginFrame(); + + return true; +} + + + +void RAS_OpenGLRasterizer::SetDrawingMode(int drawingmode) +{ + m_drawingmode = drawingmode; + + switch (m_drawingmode) + { + case KX_BOUNDINGBOX: + { + } + case KX_WIREFRAME: + { + glDisable (GL_CULL_FACE); + break; + } + case KX_TEXTURED: + { + } + case KX_SHADED: + { + } + case KX_SOLID: + { + } + default: + { + } + } +} + + + +int RAS_OpenGLRasterizer::GetDrawingMode() +{ + return m_drawingmode; +} + + + +void RAS_OpenGLRasterizer::SetDepthMask(int depthmask) +{ + switch (depthmask) + { + case KX_DEPTHMASK_ENABLED: + { + glDepthMask(GL_TRUE); + break; + }; + case KX_DEPTHMASK_DISABLED: + { + glDepthMask(GL_FALSE); + break; + }; + default: + { + //printf("someone made a mistake, RAS_OpenGLRasterizer::SetDepthMask(int depthmask)\n"); + exit(0); + } + } +} + + + +void RAS_OpenGLRasterizer::ClearDepthBuffer() +{ + m_2DCanvas->ClearBuffer(RAS_ICanvas::DEPTH_BUFFER); +} + + +void RAS_OpenGLRasterizer::ClearCachingInfo(void) +{ + m_materialCachingInfo = 0; +} + + +void RAS_OpenGLRasterizer::EndFrame() +{ + m_2DCanvas->EndFrame(); +} + + +void RAS_OpenGLRasterizer::SetRenderArea() +{ + // only above/below stereo method needs viewport adjustment + if(m_stereomode == RAS_STEREO_ABOVEBELOW) + { + switch(m_curreye) + { + case RAS_STEREO_LEFTEYE: + // upper half of window + m_2DCanvas->GetDisplayArea().SetLeft(0); + m_2DCanvas->GetDisplayArea().SetBottom(m_2DCanvas->GetHeight() - + int(m_2DCanvas->GetHeight() - m_noOfScanlines) / 2); + + m_2DCanvas->GetDisplayArea().SetRight(int(m_2DCanvas->GetWidth())); + m_2DCanvas->GetDisplayArea().SetTop(int(m_2DCanvas->GetHeight())); + break; + case RAS_STEREO_RIGHTEYE: + // lower half of window + m_2DCanvas->GetDisplayArea().SetLeft(0); + m_2DCanvas->GetDisplayArea().SetBottom(0); + m_2DCanvas->GetDisplayArea().SetRight(int(m_2DCanvas->GetWidth())); + m_2DCanvas->GetDisplayArea().SetTop(int(m_2DCanvas->GetHeight() - m_noOfScanlines) / 2); + break; + } + } + else + { + // every available pixel + m_2DCanvas->GetDisplayArea().SetLeft(0); + m_2DCanvas->GetDisplayArea().SetBottom(0); + m_2DCanvas->GetDisplayArea().SetRight(int(m_2DCanvas->GetWidth())); + m_2DCanvas->GetDisplayArea().SetTop(int(m_2DCanvas->GetHeight())); + } +} + + +void RAS_OpenGLRasterizer::SetStereoMode(const int stereomode) +{ + m_stereomode = stereomode; +} + + + +bool RAS_OpenGLRasterizer::Stereo() +{ + if(m_stereomode == RAS_STEREO_NOSTEREO) + return false; + else + return true; +} + + +void RAS_OpenGLRasterizer::SetEye(int eye) +{ + m_curreye = eye; + if(m_stereomode == RAS_STEREO_QUADBUFFERED) { + if(m_curreye == RAS_STEREO_LEFTEYE) + glDrawBuffer(GL_BACK_LEFT); + else + glDrawBuffer(GL_BACK_RIGHT); + } +} + + +void RAS_OpenGLRasterizer::SetEyeSeparation(float eyeseparation) +{ + m_eyeseparation = eyeseparation; +} + + +void RAS_OpenGLRasterizer::SetFocalLength(float focallength) +{ + m_focallength = focallength; +} + + +void RAS_OpenGLRasterizer::SwapBuffers() +{ + m_2DCanvas->SwapBuffers(); +} + + + +void RAS_OpenGLRasterizer::IndexPrimitives(const vecVertexArray & vertexarrays, + const vecIndexArrays & indexarrays, + int mode, + class RAS_IPolyMaterial* polymat, + class RAS_IRenderTools* rendertools, + bool useObjectColor, + const MT_Vector4& rgbacolor + ) +{ + static const GLsizei vtxstride = sizeof(RAS_TexVert); + GLenum drawmode; + switch (mode) + { + case 0: + drawmode = GL_TRIANGLES; + break; + case 1: + drawmode = GL_LINES; + break; + case 2: + drawmode = GL_QUADS; + break; + default: + drawmode = GL_LINES; + break; + } + + const RAS_TexVert* vertexarray ; + int numindices,vt; + + for (vt=0;vt<vertexarrays.size();vt++) + { + vertexarray = &((*vertexarrays[vt]) [0]); + const KX_IndexArray & indexarray = (*indexarrays[vt]); + numindices = indexarray.size(); + + int numverts = vertexarrays[vt]->size(); + + if (!numindices) + break; + + int vindex=0; + switch (mode) + { + case 1: + { + glBegin(GL_LINES); + vindex=0; + for (int i=0;i<numindices;i+=2) + { + glVertex3fv(vertexarray[(indexarray[vindex++])].getLocalXYZ()); + glVertex3fv(vertexarray[(indexarray[vindex++])].getLocalXYZ()); + } + glEnd(); + } + break; + case 2: + { + glBegin(GL_QUADS); + vindex=0; + if (useObjectColor) + { + for (int i=0;i<numindices;i+=4) + { + + glColor4d(rgbacolor[0], rgbacolor[1], rgbacolor[2], rgbacolor[3]); + + glNormal3sv(vertexarray[(indexarray[vindex])].getNormal()); + glTexCoord2fv(vertexarray[(indexarray[vindex])].getUV1()); + glVertex3fv(vertexarray[(indexarray[vindex])].getLocalXYZ()); + vindex++; + + glNormal3sv(vertexarray[(indexarray[vindex])].getNormal()); + glTexCoord2fv(vertexarray[(indexarray[vindex])].getUV1()); + glVertex3fv(vertexarray[(indexarray[vindex])].getLocalXYZ()); + vindex++; + + glNormal3sv(vertexarray[(indexarray[vindex])].getNormal()); + glTexCoord2fv(vertexarray[(indexarray[vindex])].getUV1()); + glVertex3fv(vertexarray[(indexarray[vindex])].getLocalXYZ()); + vindex++; + + glNormal3sv(vertexarray[(indexarray[vindex])].getNormal()); + glTexCoord2fv(vertexarray[(indexarray[vindex])].getUV1()); + glVertex3fv(vertexarray[(indexarray[vindex])].getLocalXYZ()); + vindex++; + } + } + else + { + for (int i=0;i<numindices;i+=4) + { + char *cp= (char *)&(vertexarray[(indexarray[vindex])].getRGBA()); + // This looks curiously endian unsafe to me. + // However it depends on the way the colors are packed into + // the m_rgba field of RAS_TexVert + + glColor4ub(cp[0], cp[1], cp[2], cp[3]); + glNormal3sv(vertexarray[(indexarray[vindex])].getNormal()); + glTexCoord2fv(vertexarray[(indexarray[vindex])].getUV1()); + glVertex3fv(vertexarray[(indexarray[vindex])].getLocalXYZ()); + vindex++; + + cp= (char *)&(vertexarray[(indexarray[vindex])].getRGBA()); + glColor4ub(cp[0], cp[1], cp[2], cp[3]); + glNormal3sv(vertexarray[(indexarray[vindex])].getNormal()); + glTexCoord2fv(vertexarray[(indexarray[vindex])].getUV1()); + glVertex3fv(vertexarray[(indexarray[vindex])].getLocalXYZ()); + vindex++; + + cp= (char *)&(vertexarray[(indexarray[vindex])].getRGBA()); + glColor4ub(cp[0], cp[1], cp[2], cp[3]); + glNormal3sv(vertexarray[(indexarray[vindex])].getNormal()); + glTexCoord2fv(vertexarray[(indexarray[vindex])].getUV1()); + glVertex3fv(vertexarray[(indexarray[vindex])].getLocalXYZ()); + vindex++; + + cp= (char *)&(vertexarray[(indexarray[vindex])].getRGBA()); + glColor4ub(cp[0], cp[1], cp[2], cp[3]); + glNormal3sv(vertexarray[(indexarray[vindex])].getNormal()); + glTexCoord2fv(vertexarray[(indexarray[vindex])].getUV1()); + glVertex3fv(vertexarray[(indexarray[vindex])].getLocalXYZ()); + vindex++; + } + } + glEnd(); + break; + } + case 0: + { + glBegin(GL_TRIANGLES); + vindex=0; + if (useObjectColor) + { + for (int i=0;i<numindices;i+=3) + { + + glColor4d(rgbacolor[0], rgbacolor[1], rgbacolor[2], rgbacolor[3]); + + glNormal3sv(vertexarray[(indexarray[vindex])].getNormal()); + glTexCoord2fv(vertexarray[(indexarray[vindex])].getUV1()); + glVertex3fv(vertexarray[(indexarray[vindex])].getLocalXYZ()); + vindex++; + + glNormal3sv(vertexarray[(indexarray[vindex])].getNormal()); + glTexCoord2fv(vertexarray[(indexarray[vindex])].getUV1()); + glVertex3fv(vertexarray[(indexarray[vindex])].getLocalXYZ()); + vindex++; + + glNormal3sv(vertexarray[(indexarray[vindex])].getNormal()); + glTexCoord2fv(vertexarray[(indexarray[vindex])].getUV1()); + glVertex3fv(vertexarray[(indexarray[vindex])].getLocalXYZ()); + vindex++; + } + } + else + { + for (int i=0;i<numindices;i+=3) + { + + char *cp= (char *)&(vertexarray[(indexarray[vindex])].getRGBA()); + glColor4ub(cp[0], cp[1], cp[2], cp[3]); + glNormal3sv(vertexarray[(indexarray[vindex])].getNormal()); + glTexCoord2fv(vertexarray[(indexarray[vindex])].getUV1()); + glVertex3fv(vertexarray[(indexarray[vindex])].getLocalXYZ()); + vindex++; + + cp= (char *)&(vertexarray[(indexarray[vindex])].getRGBA()); + glColor4ub(cp[0], cp[1], cp[2], cp[3]); + glNormal3sv(vertexarray[(indexarray[vindex])].getNormal()); + glTexCoord2fv(vertexarray[(indexarray[vindex])].getUV1()); + glVertex3fv(vertexarray[(indexarray[vindex])].getLocalXYZ()); + vindex++; + + cp= (char *)&(vertexarray[(indexarray[vindex])].getRGBA()); + glColor4ub(cp[0], cp[1], cp[2], cp[3]); + glNormal3sv(vertexarray[(indexarray[vindex])].getNormal()); + glTexCoord2fv(vertexarray[(indexarray[vindex])].getUV1()); + glVertex3fv(vertexarray[(indexarray[vindex])].getLocalXYZ()); + vindex++; + } + } + glEnd(); + break; + } + default: + { + } + + } // switch + } // for each vertexarray + +} + +void RAS_OpenGLRasterizer::IndexPrimitives_Ex(const vecVertexArray & vertexarrays, + const vecIndexArrays & indexarrays, + int mode, + class RAS_IPolyMaterial* polymat, + class RAS_IRenderTools* rendertools, + bool useObjectColor, + const MT_Vector4& rgbacolor + ) +{ + bool recalc; + static const GLsizei vtxstride = sizeof(RAS_TexVert); + GLenum drawmode; + switch (mode) + { + case 0: + drawmode = GL_TRIANGLES; + break; + case 1: + drawmode = GL_LINES; + break; + case 2: + drawmode = GL_QUADS; + break; + default: + drawmode = GL_LINES; + break; + } + + const RAS_TexVert* vertexarray ; + int numindices,vt; + + for (vt=0;vt<vertexarrays.size();vt++) + { + vertexarray = &((*vertexarrays[vt]) [0]); + const KX_IndexArray & indexarray = (*indexarrays[vt]); + numindices = indexarray.size(); + + int numverts = vertexarrays[vt]->size(); + + if (!numindices) + break; + + int vindex=0; + switch (mode) + { + case 1: + { + glBegin(GL_LINES); + vindex=0; + for (int i=0;i<numindices;i+=2) + { + glVertex3fv(vertexarray[(indexarray[vindex++])].getLocalXYZ()); + glVertex3fv(vertexarray[(indexarray[vindex++])].getLocalXYZ()); + } + glEnd(); + } + break; + case 2: + { + glBegin(GL_QUADS); + vindex=0; + if (useObjectColor) + { + for (int i=0;i<numindices;i+=4) + { + MT_Point3 mv1, mv2, mv3, mv4, fnor; + /* Calc a new face normal */ + + if (vertexarray[(indexarray[vindex])].getFlag() & TV_CALCFACENORMAL) + recalc= true; + else + recalc=false; + + if (recalc){ + mv1 = MT_Point3(vertexarray[(indexarray[vindex])].getLocalXYZ()); + mv2 = MT_Point3(vertexarray[(indexarray[vindex+1])].getLocalXYZ()); + mv3 = MT_Point3(vertexarray[(indexarray[vindex+2])].getLocalXYZ()); + mv4 = MT_Point3(vertexarray[(indexarray[vindex+2])].getLocalXYZ()); + + fnor = (((mv2-mv1).cross(mv3-mv2))+((mv4-mv3).cross(mv1-mv4))).safe_normalized(); + + glNormal3f(fnor[0], fnor[1], fnor[2]); + } + + glColor4d(rgbacolor[0], rgbacolor[1], rgbacolor[2], rgbacolor[3]); + + if (!recalc) + glNormal3sv(vertexarray[(indexarray[vindex])].getNormal()); + glTexCoord2fv(vertexarray[(indexarray[vindex])].getUV1()); + glVertex3fv(vertexarray[(indexarray[vindex])].getLocalXYZ()); + vindex++; + + if (!recalc) + glNormal3sv(vertexarray[(indexarray[vindex])].getNormal()); + glTexCoord2fv(vertexarray[(indexarray[vindex])].getUV1()); + glVertex3fv(vertexarray[(indexarray[vindex])].getLocalXYZ()); + vindex++; + + if (!recalc) + glNormal3sv(vertexarray[(indexarray[vindex])].getNormal()); + glTexCoord2fv(vertexarray[(indexarray[vindex])].getUV1()); + glVertex3fv(vertexarray[(indexarray[vindex])].getLocalXYZ()); + vindex++; + + if (!recalc) + glNormal3sv(vertexarray[(indexarray[vindex])].getNormal()); + glTexCoord2fv(vertexarray[(indexarray[vindex])].getUV1()); + glVertex3fv(vertexarray[(indexarray[vindex])].getLocalXYZ()); + vindex++; + } + } + else + { + for (int i=0;i<numindices;i+=4) + { + char *cp= (char *)&(vertexarray[(indexarray[vindex])].getRGBA()); + // This looks curiously endian unsafe to me. + // However it depends on the way the colors are packed into + // the m_rgba field of RAS_TexVert + MT_Point3 mv1, mv2, mv3, mv4, fnor; + /* Calc a new face normal */ + + if (vertexarray[(indexarray[vindex])].getFlag() & TV_CALCFACENORMAL) + recalc= true; + else + recalc=false; + + + if (recalc){ + mv1 = MT_Point3(vertexarray[(indexarray[vindex])].getLocalXYZ()); + mv2 = MT_Point3(vertexarray[(indexarray[vindex+1])].getLocalXYZ()); + mv3 = MT_Point3(vertexarray[(indexarray[vindex+2])].getLocalXYZ()); + mv4 = MT_Point3(vertexarray[(indexarray[vindex+2])].getLocalXYZ()); + + fnor = (((mv2-mv1).cross(mv3-mv2))+((mv4-mv3).cross(mv1-mv4))).safe_normalized(); + + glNormal3f(fnor[0], fnor[1], fnor[2]); + } + + glColor4ub(cp[0], cp[1], cp[2], cp[3]); + if (!recalc) + glNormal3sv(vertexarray[(indexarray[vindex])].getNormal()); + glTexCoord2fv(vertexarray[(indexarray[vindex])].getUV1()); + glVertex3fv(vertexarray[(indexarray[vindex])].getLocalXYZ()); + vindex++; + + cp= (char *)&(vertexarray[(indexarray[vindex])].getRGBA()); + glColor4ub(cp[0], cp[1], cp[2], cp[3]); + if (!recalc) + glNormal3sv(vertexarray[(indexarray[vindex])].getNormal()); + glTexCoord2fv(vertexarray[(indexarray[vindex])].getUV1()); + glVertex3fv(vertexarray[(indexarray[vindex])].getLocalXYZ()); + vindex++; + + cp= (char *)&(vertexarray[(indexarray[vindex])].getRGBA()); + glColor4ub(cp[0], cp[1], cp[2], cp[3]); + if (!recalc) + glNormal3sv(vertexarray[(indexarray[vindex])].getNormal()); + glTexCoord2fv(vertexarray[(indexarray[vindex])].getUV1()); + glVertex3fv(vertexarray[(indexarray[vindex])].getLocalXYZ()); + vindex++; + + cp= (char *)&(vertexarray[(indexarray[vindex])].getRGBA()); + glColor4ub(cp[0], cp[1], cp[2], cp[3]); + if (!recalc) + glNormal3sv(vertexarray[(indexarray[vindex])].getNormal()); + glTexCoord2fv(vertexarray[(indexarray[vindex])].getUV1()); + glVertex3fv(vertexarray[(indexarray[vindex])].getLocalXYZ()); + vindex++; + } + } + glEnd(); + break; + } + case 0: + { + glBegin(GL_TRIANGLES); + vindex=0; + if (useObjectColor) + { + for (int i=0;i<numindices;i+=3) + { + MT_Point3 mv1, mv2, mv3, fnor; + /* Calc a new face normal */ + + if (vertexarray[(indexarray[vindex])].getFlag() & TV_CALCFACENORMAL) + recalc= true; + else + recalc=false; + + if (recalc){ + mv1 = MT_Point3(vertexarray[(indexarray[vindex])].getLocalXYZ()); + mv2 = MT_Point3(vertexarray[(indexarray[vindex+1])].getLocalXYZ()); + mv3 = MT_Point3(vertexarray[(indexarray[vindex+2])].getLocalXYZ()); + + fnor = ((mv2-mv1).cross(mv3-mv2)).safe_normalized(); + glNormal3f(fnor[0], fnor[1], fnor[2]); + } + + glColor4d(rgbacolor[0], rgbacolor[1], rgbacolor[2], rgbacolor[3]); + + if (!recalc) + glNormal3sv(vertexarray[(indexarray[vindex])].getNormal()); + glTexCoord2fv(vertexarray[(indexarray[vindex])].getUV1()); + glVertex3fv(vertexarray[(indexarray[vindex])].getLocalXYZ()); + vindex++; + + if (!recalc) + glNormal3sv(vertexarray[(indexarray[vindex])].getNormal()); + glTexCoord2fv(vertexarray[(indexarray[vindex])].getUV1()); + glVertex3fv(vertexarray[(indexarray[vindex])].getLocalXYZ()); + vindex++; + + if (!recalc) + glNormal3sv(vertexarray[(indexarray[vindex])].getNormal()); + glTexCoord2fv(vertexarray[(indexarray[vindex])].getUV1()); + glVertex3fv(vertexarray[(indexarray[vindex])].getLocalXYZ()); + vindex++; + } + } + else + { + for (int i=0;i<numindices;i+=3) + { + MT_Point3 mv1, mv2, mv3, fnor; + /* Calc a new face normal */ + + if (vertexarray[(indexarray[vindex])].getFlag() & TV_CALCFACENORMAL) + recalc= true; + else + recalc=false; + + + if (recalc){ + mv1 = MT_Point3(vertexarray[(indexarray[vindex])].getLocalXYZ()); + mv2 = MT_Point3(vertexarray[(indexarray[vindex+1])].getLocalXYZ()); + mv3 = MT_Point3(vertexarray[(indexarray[vindex+2])].getLocalXYZ()); + + fnor = ((mv2-mv1).cross(mv3-mv2)).safe_normalized(); + glNormal3f(fnor[0], fnor[1], fnor[2]); + } + + char *cp= (char *)&(vertexarray[(indexarray[vindex])].getRGBA()); + glColor4ub(cp[0], cp[1], cp[2], cp[3]); + if (!recalc) + glNormal3sv(vertexarray[(indexarray[vindex])].getNormal()); + glTexCoord2fv(vertexarray[(indexarray[vindex])].getUV1()); + glVertex3fv(vertexarray[(indexarray[vindex])].getLocalXYZ()); + vindex++; + + cp= (char *)&(vertexarray[(indexarray[vindex])].getRGBA()); + glColor4ub(cp[0], cp[1], cp[2], cp[3]); + if (!recalc) + glNormal3sv(vertexarray[(indexarray[vindex])].getNormal()); + glTexCoord2fv(vertexarray[(indexarray[vindex])].getUV1()); + glVertex3fv(vertexarray[(indexarray[vindex])].getLocalXYZ()); + vindex++; + + cp= (char *)&(vertexarray[(indexarray[vindex])].getRGBA()); + glColor4ub(cp[0], cp[1], cp[2], cp[3]); + if (!recalc) + glNormal3sv(vertexarray[(indexarray[vindex])].getNormal()); + glTexCoord2fv(vertexarray[(indexarray[vindex])].getUV1()); + glVertex3fv(vertexarray[(indexarray[vindex])].getLocalXYZ()); + vindex++; + } + } + glEnd(); + break; + } + default: + { + } + + } // switch + } // for each vertexarray + +} + + + +void RAS_OpenGLRasterizer::IndexPrimitives_3DText(const vecVertexArray & vertexarrays, + const vecIndexArrays & indexarrays, + int mode, + class RAS_IPolyMaterial* polymat, + class RAS_IRenderTools* rendertools, + bool useObjectColor, + const MT_Vector4& rgbacolor + ) +{ + unsigned char* mypointer=NULL; + static const GLsizei vtxstride = sizeof(RAS_TexVert); + GLenum drawmode; + switch (mode) + { + case 0: + drawmode = GL_TRIANGLES; + break; + case 1: + drawmode = GL_LINES; + break; + case 2: + drawmode = GL_QUADS; + break; + default: + drawmode = GL_LINES; + break; + } + + const RAS_TexVert* vertexarray ; + + int numindices ; + int vt; + + if (useObjectColor) + { + glDisableClientState(GL_COLOR_ARRAY); + glColor4d(rgbacolor[0], rgbacolor[1], rgbacolor[2], rgbacolor[3]); + } + else + { + glEnableClientState(GL_COLOR_ARRAY); + } + + for (vt=0;vt<vertexarrays.size();vt++) + { + vertexarray = &((*vertexarrays[vt]) [0]); + const KX_IndexArray & indexarray = (*indexarrays[vt]); + numindices = indexarray.size(); + + int numverts = vertexarrays[vt]->size(); + + if (!numindices) + break; + + int vindex=0; + switch (mode) + { + case 1: + { + glBegin(GL_LINES); + vindex=0; + for (int i=0;i<numindices;i+=2) + { + glVertex3fv(vertexarray[(indexarray[vindex++])].getLocalXYZ()); + glVertex3fv(vertexarray[(indexarray[vindex++])].getLocalXYZ()); + } + glEnd(); + } + break; + case 2: + { + vindex=0; + for (int i=0;i<numindices;i+=4) + { + float v1[3],v2[3],v3[3],v4[3]; + + char *cp= (char *)&(vertexarray[(indexarray[vindex])].getRGBA()); + v1[0] = vertexarray[(indexarray[vindex])].getLocalXYZ()[0]; + v1[1] = vertexarray[(indexarray[vindex])].getLocalXYZ()[1]; + v1[2] = vertexarray[(indexarray[vindex])].getLocalXYZ()[2]; + vindex++; + + cp= (char *)&(vertexarray[(indexarray[vindex])].getRGBA()); + v2[0] = vertexarray[(indexarray[vindex])].getLocalXYZ()[0]; + v2[1] = vertexarray[(indexarray[vindex])].getLocalXYZ()[1]; + v2[2] = vertexarray[(indexarray[vindex])].getLocalXYZ()[2]; + vindex++; + + cp= (char *)&(vertexarray[(indexarray[vindex])].getRGBA()); + v3[0] = vertexarray[(indexarray[vindex])].getLocalXYZ()[0]; + v3[1] = vertexarray[(indexarray[vindex])].getLocalXYZ()[1]; + v3[2] = vertexarray[(indexarray[vindex])].getLocalXYZ()[2]; + vindex++; + + cp= (char *)&(vertexarray[(indexarray[vindex])].getRGBA()); + v4[0] = vertexarray[(indexarray[vindex])].getLocalXYZ()[0]; + v4[1] = vertexarray[(indexarray[vindex])].getLocalXYZ()[1]; + v4[2] = vertexarray[(indexarray[vindex])].getLocalXYZ()[2]; + + vindex++; + + rendertools->RenderText(polymat->GetDrawingMode(),polymat,v1,v2,v3,v4); + ClearCachingInfo(); + } + break; + } + case 0: + { + glBegin(GL_TRIANGLES); + vindex=0; + for (int i=0;i<numindices;i+=3) + { + float v1[3],v2[3],v3[3]; + + v1[0] = vertexarray[(indexarray[vindex])].getLocalXYZ()[0]; + v1[1] = vertexarray[(indexarray[vindex])].getLocalXYZ()[1]; + v1[2] = vertexarray[(indexarray[vindex])].getLocalXYZ()[2]; + vindex++; + + v2[0] = vertexarray[(indexarray[vindex])].getLocalXYZ()[0]; + v2[1] = vertexarray[(indexarray[vindex])].getLocalXYZ()[1]; + v2[2] = vertexarray[(indexarray[vindex])].getLocalXYZ()[2]; + vindex++; + + v3[0] = vertexarray[(indexarray[vindex])].getLocalXYZ()[0]; + v3[1] = vertexarray[(indexarray[vindex])].getLocalXYZ()[1]; + v3[2] = vertexarray[(indexarray[vindex])].getLocalXYZ()[2]; + vindex++; + + rendertools->RenderText(polymat->GetDrawingMode(),polymat,v1,v2,v3,NULL); + ClearCachingInfo(); + } + glEnd(); + break; + } + default: + { + } + } //switch + } //for each vertexarray +} + + + +void RAS_OpenGLRasterizer::SetProjectionMatrix(MT_CmMatrix4x4 &mat) +{ + glMatrixMode(GL_PROJECTION); + double* matrix = &mat(0,0); + glLoadMatrixd(matrix); +} + + +void RAS_OpenGLRasterizer::SetProjectionMatrix(MT_Matrix4x4 & mat) +{ + glMatrixMode(GL_PROJECTION); + double matrix[16]; + /* Get into argument. Looks a bit dodgy, but it's ok. */ + mat.getValue(matrix); + /* Internally, MT_Matrix4x4 uses doubles (MT_Scalar). */ + glLoadMatrixd(matrix); +} + +MT_Matrix4x4 RAS_OpenGLRasterizer::GetFrustumMatrix( + float left, + float right, + float bottom, + float top, + float frustnear, + float frustfar +){ + MT_Matrix4x4 result; + double mat[16]; + + // correction for stereo + if(m_stereomode != RAS_STEREO_NOSTEREO) + { + float near_div_focallength; + // next 2 params should be specified on command line and in Blender publisher + m_focallength = 1.5 * right; // derived from example + m_eyeseparation = 0.18 * right; // just a guess... + + near_div_focallength = frustnear / m_focallength; + switch(m_curreye) + { + case RAS_STEREO_LEFTEYE: + left += 0.5 * m_eyeseparation * near_div_focallength; + right += 0.5 * m_eyeseparation * near_div_focallength; + break; + case RAS_STEREO_RIGHTEYE: + left -= 0.5 * m_eyeseparation * near_div_focallength; + right -= 0.5 * m_eyeseparation * near_div_focallength; + break; + } + // leave bottom, top, bottom and top untouched + } + + glMatrixMode(GL_PROJECTION); + glLoadIdentity(); + glFrustum(left, right, bottom, top, frustnear, frustfar); + glGetDoublev(GL_PROJECTION_MATRIX, mat); + result.setValue(mat); + + return result; +} + + +// next arguments probably contain redundant info, for later... +void RAS_OpenGLRasterizer::SetViewMatrix(const MT_Matrix4x4 &mat, const MT_Vector3& campos, + const MT_Point3 &camLoc, const MT_Quaternion &camOrientQuat) +{ + MT_Matrix4x4 viewMat = mat; + + // correction for stereo + if(m_stereomode != RAS_STEREO_NOSTEREO) + { + MT_Matrix3x3 camOrientMat3x3(camOrientQuat); + MT_Vector3 unitViewDir(0.0, -1.0, 0.0); // minus y direction, Blender convention + MT_Vector3 unitViewupVec(0.0, 0.0, 1.0); + MT_Vector3 viewDir, viewupVec; + MT_Vector3 eyeline; + + // actual viewDir + viewDir = camOrientMat3x3 * unitViewDir; // this is the moto convention, vector on right hand side + // actual viewup vec + viewupVec = camOrientMat3x3 * unitViewupVec; + + // vector between eyes + eyeline = viewDir.cross(viewupVec); + + switch(m_curreye) + { + case RAS_STEREO_LEFTEYE: + { + // translate to left by half the eye distance + MT_Transform transform; + transform.setIdentity(); + transform.translate(-(eyeline * m_eyeseparation / 2.0)); + viewMat *= transform; + } + break; + case RAS_STEREO_RIGHTEYE: + { + // translate to right by half the eye distance + MT_Transform transform; + transform.setIdentity(); + transform.translate(eyeline * m_eyeseparation / 2.0); + viewMat *= transform; + } + break; + } + } + + // convert row major matrix 'viewMat' to column major for OpenGL + MT_Scalar cammat[16]; + viewMat.getValue(cammat); + MT_CmMatrix4x4 viewCmmat = cammat; + + glMatrixMode(GL_MODELVIEW); + m_viewmatrix = viewCmmat; + glLoadMatrixd(&m_viewmatrix(0,0)); + m_campos = campos; +} + + +const MT_Point3& RAS_OpenGLRasterizer::GetCameraPosition() +{ + return m_campos; +} + + + +void RAS_OpenGLRasterizer::LoadViewMatrix() +{ + glLoadMatrixd(&m_viewmatrix(0,0)); +} + + + +void RAS_OpenGLRasterizer::EnableTextures(bool enable) +{ +} + + + +void RAS_OpenGLRasterizer::SetCullFace(bool enable) +{ + if (enable) + glEnable(GL_CULL_FACE); + else + glDisable(GL_CULL_FACE); +} + + + +void RAS_OpenGLRasterizer::SetSpecularity(float specX, + float specY, + float specZ, + float specval) +{ + GLfloat mat_specular[] = {specX, specY, specZ, specval}; + glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, mat_specular); +} + + + +void RAS_OpenGLRasterizer::SetShinyness(float shiny) +{ + GLfloat mat_shininess[] = { shiny }; + glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, mat_shininess); +} + + + +void RAS_OpenGLRasterizer::SetDiffuse(float difX,float difY,float difZ,float diffuse) +{ + GLfloat mat_diffuse [] = {difX, difY,difZ, diffuse}; + glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, mat_diffuse); +} + +double RAS_OpenGLRasterizer::GetTime() +{ + return m_time; +} + + diff --git a/source/gameengine/Rasterizer/RAS_OpenGLRasterizer/RAS_OpenGLRasterizer.h b/source/gameengine/Rasterizer/RAS_OpenGLRasterizer/RAS_OpenGLRasterizer.h new file mode 100644 index 00000000000..9bbb19e8aa5 --- /dev/null +++ b/source/gameengine/Rasterizer/RAS_OpenGLRasterizer/RAS_OpenGLRasterizer.h @@ -0,0 +1,226 @@ +/** + * $Id$ + * + * ***** BEGIN GPL/BL DUAL LICENSE BLOCK ***** + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. The Blender + * Foundation also sells licenses for use in proprietary software under + * the Blender License. See http://www.blender.org/BL/ for information + * about this. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + * + * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV. + * All rights reserved. + * + * The Original Code is: all of this file. + * + * Contributor(s): none yet. + * + * ***** END GPL/BL DUAL LICENSE BLOCK ***** + */ +#ifndef __RAS_OPENGLRASTERIZER +#define __RAS_OPENGLRASTERIZER + +#ifdef WIN32 +#pragma warning (disable:4786) +#endif + +#include "MT_CmMatrix4x4.h" +#include <vector> +using namespace std; + + +#include "RAS_IRasterizer.h" +#include "RAS_MaterialBucket.h" +#include "RAS_ICanvas.h" + +/** + * 3D rendering device context. + */ +class RAS_OpenGLRasterizer : public RAS_IRasterizer +{ + + RAS_ICanvas* m_2DCanvas; + + // fogging vars + bool m_fogenabled; + float m_fogstart; + float m_fogdist; + float m_fogr; + float m_fogg; + float m_fogb; + + float m_redback; + float m_greenback; + float m_blueback; + float m_alphaback; + + bool m_bEXT_compiled_vertex_array; + + double m_time; + MT_CmMatrix4x4 m_viewmatrix; + MT_Point3 m_campos; + + int m_stereomode; + int m_curreye; + float m_eyeseparation; + float m_focallength; + int m_noOfScanlines; + +protected: + int m_drawingmode; + /** Stores the caching information for the last material activated. */ + RAS_IPolyMaterial::TCachingInfo m_materialCachingInfo; + +public: + double GetTime(); + RAS_OpenGLRasterizer(RAS_ICanvas* canv); + virtual ~RAS_OpenGLRasterizer(); + + + + enum + { + KX_BOUNDINGBOX = 1, + KX_WIREFRAME, + KX_SOLID, + KX_SHADED, + KX_TEXTURED + }; + + enum + { + KX_DEPTHMASK_ENABLED =1, + KX_DEPTHMASK_DISABLED, + }; + virtual void SetDepthMask(int depthmask); + + virtual void SetMaterial(const RAS_IPolyMaterial& mat); + virtual bool Init(); + virtual void Exit(); + virtual bool BeginFrame(int drawingmode, double time); + virtual void ClearDepthBuffer(); + virtual void ClearCachingInfo(void); + virtual void EndFrame(); + virtual void SetRenderArea(); + + virtual void SetStereoMode(const int stereomode); + virtual bool Stereo(); + virtual void SetEye(const int eye); + virtual void SetEyeSeparation(const float eyeseparation); + virtual void SetFocalLength(const float focallength); + + virtual void SwapBuffers(); + virtual void IndexPrimitives( + const vecVertexArray& vertexarrays, + const vecIndexArrays & indexarrays, + int mode, + class RAS_IPolyMaterial* polymat, + class RAS_IRenderTools* rendertools, + bool useObjectColor, + const MT_Vector4& rgbacolor + ); + + virtual void IndexPrimitives_Ex( + const vecVertexArray& vertexarrays, + const vecIndexArrays & indexarrays, + int mode, + class RAS_IPolyMaterial* polymat, + class RAS_IRenderTools* rendertools, + bool useObjectColor, + const MT_Vector4& rgbacolor + ); + + virtual void IndexPrimitives_3DText( + const vecVertexArray& vertexarrays, + const vecIndexArrays & indexarrays, + int mode, + class RAS_IPolyMaterial* polymat, + class RAS_IRenderTools* rendertools, + bool useObjectColor, + const MT_Vector4& rgbacolor + ); + + virtual void SetProjectionMatrix(MT_CmMatrix4x4 & mat); + virtual void SetProjectionMatrix(MT_Matrix4x4 & mat); + virtual void SetViewMatrix( + const MT_Matrix4x4 & mat, + const MT_Vector3& campos, + const MT_Point3 &camLoc, + const MT_Quaternion &camOrientQuat + ); + + virtual const MT_Point3& GetCameraPosition(); + virtual void LoadViewMatrix(); + + virtual void SetFog( + float start, + float dist, + float r, + float g, + float b + ); + + virtual void SetFogColor( + float r, + float g, + float b + ); + + virtual void SetFogStart(float fogstart); + virtual void SetFogEnd(float fogend); + + void DisableFog(); + virtual void DisplayFog(); + + virtual void SetBackColor( + float red, + float green, + float blue, + float alpha + ); + + virtual void SetDrawingMode(int drawingmode); + virtual int GetDrawingMode(); + + virtual void EnableTextures(bool enable); + virtual void SetCullFace(bool enable); + + virtual MT_Matrix4x4 GetFrustumMatrix( + float left, + float right, + float bottom, + float top, + float frustnear, + float frustfar + ); + + virtual void SetSpecularity( + float specX, + float specY, + float specZ, + float specval + ); + + virtual void SetShinyness(float shiny); + virtual void SetDiffuse( + float difX, + float difY, + float difZ, + float diffuse + ); + +}; + +#endif //__RAS_OPENGLRASTERIZER diff --git a/source/gameengine/Rasterizer/RAS_OpenGLRasterizer/RAS_VAOpenGLRasterizer.cpp b/source/gameengine/Rasterizer/RAS_OpenGLRasterizer/RAS_VAOpenGLRasterizer.cpp new file mode 100644 index 00000000000..fe582a3d2ee --- /dev/null +++ b/source/gameengine/Rasterizer/RAS_OpenGLRasterizer/RAS_VAOpenGLRasterizer.cpp @@ -0,0 +1,215 @@ +#ifdef WIN32 + + +#include "RAS_VAOpenGLRasterizer.h" + + +#include <windows.h> +#include "GL/gl.h" + +typedef void (APIENTRY *GLLOCKARRAYSEXTPTR)(GLint first,GLsizei count); +typedef void (APIENTRY *GLUNLOCKARRAYSEXTPTR)(void); +void APIENTRY RAS_lockfunc(GLint first,GLsizei count) {}; +void APIENTRY RAS_unlockfunc() {}; +GLLOCKARRAYSEXTPTR glLockArraysEXT=RAS_lockfunc; +GLUNLOCKARRAYSEXTPTR glUnlockArraysEXT=RAS_unlockfunc; + + + + +#include "STR_String.h" +#include "RAS_TexVert.h" +#include "MT_CmMatrix4x4.h" +#include "RAS_IRenderTools.h" // rendering text + + +RAS_VAOpenGLRasterizer::RAS_VAOpenGLRasterizer(RAS_ICanvas* canvas) +:RAS_OpenGLRasterizer(canvas) +{ + int i = 0; +} + + + +RAS_VAOpenGLRasterizer::~RAS_VAOpenGLRasterizer() +{ +} + + + +bool RAS_VAOpenGLRasterizer::Init() +{ + + bool result = RAS_OpenGLRasterizer::Init(); + + if (result) + { + // if possible, add extensions to other platforms too, if this + // rasterizer becomes messy just derive one for each platform + // (ie. KX_Win32Rasterizer, KX_LinuxRasterizer etc.) + + glUnlockArraysEXT = reinterpret_cast<GLUNLOCKARRAYSEXTPTR>(wglGetProcAddress("glUnlockArraysEXT")); + if (!glUnlockArraysEXT) + result = false; + + glLockArraysEXT = reinterpret_cast<GLLOCKARRAYSEXTPTR>(wglGetProcAddress("glLockArraysEXT")); + if (!glLockArraysEXT) + result=false; + + glEnableClientState(GL_VERTEX_ARRAY); + glDisableClientState(GL_TEXTURE_COORD_ARRAY); + glDisableClientState(GL_NORMAL_ARRAY); + glDisableClientState(GL_COLOR_ARRAY); + + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + } + + return result; +} + + + +void RAS_VAOpenGLRasterizer::SetDrawingMode(int drawingmode) +{ + m_drawingmode = drawingmode; + + switch (m_drawingmode) + { + case KX_BOUNDINGBOX: + { + } + case KX_WIREFRAME: + { + glDisable (GL_CULL_FACE); + break; + } + case KX_TEXTURED: + { + } + case KX_SHADED: + { + glEnableClientState(GL_COLOR_ARRAY); + } + case KX_SOLID: + { + break; + } + default: + { + } + } +} + + + +void RAS_VAOpenGLRasterizer::Exit() +{ + glDisableClientState(GL_VERTEX_ARRAY); + glDisableClientState(GL_TEXTURE_COORD_ARRAY); + glDisableClientState(GL_COLOR_ARRAY); + glDisableClientState(GL_NORMAL_ARRAY); + glDisable(GL_COLOR_MATERIAL); + + RAS_OpenGLRasterizer::Exit(); +} + + + +void RAS_VAOpenGLRasterizer::IndexPrimitives( const vecVertexArray& vertexarrays, + const vecIndexArrays & indexarrays, + int mode, + class RAS_IPolyMaterial* polymat, + class RAS_IRenderTools* rendertools, + bool useObjectColor, + const MT_Vector4& rgbacolor) +{ + unsigned char* mypointer=NULL; + static const GLsizei vtxstride = sizeof(RAS_TexVert); + GLenum drawmode; + switch (mode) + { + case 0: + { + drawmode = GL_TRIANGLES; + break; + } + case 2: + { + drawmode = GL_QUADS; + break; + } + case 1: //lines + { + } + default: + { + drawmode = GL_LINES; + break; + } + } + const RAS_TexVert* vertexarray; + int numindices,vt; + if (drawmode != GL_LINES) + { + if (useObjectColor) + { + glDisableClientState(GL_COLOR_ARRAY); + glColor4d(rgbacolor[0], rgbacolor[1], rgbacolor[2], rgbacolor[3]); + } else + { + glColor4d(0,0,0,1.0); + glEnableClientState(GL_COLOR_ARRAY); + } + } + else + { + glColor3d(0,0,0); + } + // use glDrawElements to draw each vertexarray + for (vt=0;vt<vertexarrays.size();vt++) + { + vertexarray = &((*vertexarrays[vt]) [0]); + const KX_IndexArray & indexarray = (*indexarrays[vt]); + numindices = indexarray.size(); + int numverts = vertexarrays[vt]->size(); + + if (!numindices) + break; + + mypointer = (unsigned char*)(vertexarray); + glVertexPointer(3,GL_FLOAT,vtxstride,mypointer); + mypointer+= 3*sizeof(float); + glTexCoordPointer(2,GL_FLOAT,vtxstride,mypointer); + mypointer+= 2*sizeof(float); + glColorPointer(4,GL_UNSIGNED_BYTE,vtxstride,mypointer); + mypointer += sizeof(int); + glNormalPointer(GL_SHORT,vtxstride,mypointer); + glLockArraysEXT(0,numverts); + // here the actual drawing takes places + glDrawElements(drawmode,numindices,GL_UNSIGNED_INT,&(indexarray[0])); + glUnlockArraysEXT(); + } +} + + +void RAS_VAOpenGLRasterizer::EnableTextures(bool enable) +{ + if (enable) + glEnableClientState(GL_TEXTURE_COORD_ARRAY); + else + glDisableClientState(GL_TEXTURE_COORD_ARRAY); +} + +bool RAS_VAOpenGLRasterizer::Stereo() +{ +/* + if(m_stereomode == RAS_STEREO_NOSTEREO) + return false; + else + return true; +*/ + return false; +} + + +#endif //WIN32 diff --git a/source/gameengine/Rasterizer/RAS_OpenGLRasterizer/RAS_VAOpenGLRasterizer.h b/source/gameengine/Rasterizer/RAS_OpenGLRasterizer/RAS_VAOpenGLRasterizer.h new file mode 100644 index 00000000000..f2500e02dde --- /dev/null +++ b/source/gameengine/Rasterizer/RAS_OpenGLRasterizer/RAS_VAOpenGLRasterizer.h @@ -0,0 +1,62 @@ +/** + * $Id$ + * + * ***** BEGIN GPL/BL DUAL LICENSE BLOCK ***** + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. The Blender + * Foundation also sells licenses for use in proprietary software under + * the Blender License. See http://www.blender.org/BL/ for information + * about this. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + * + * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV. + * All rights reserved. + * + * The Original Code is: all of this file. + * + * Contributor(s): none yet. + * + * ***** END GPL/BL DUAL LICENSE BLOCK ***** + */ +#ifndef __KX_VERTEXARRAYOPENGLRASTERIZER +#define __KX_VERTEXARRAYOPENGLRASTERIZER + +#include "RAS_OpenGLRasterizer.h" + +class RAS_VAOpenGLRasterizer : public RAS_OpenGLRasterizer +{ +public: + RAS_VAOpenGLRasterizer(RAS_ICanvas* canvas); + virtual ~RAS_VAOpenGLRasterizer(); + + virtual bool Init(); + virtual void Exit(); + + virtual bool Stereo(); + virtual void SetDrawingMode(int drawingmode); + + virtual void IndexPrimitives( const vecVertexArray& vertexarrays, + const vecIndexArrays & indexarrays, + int mode, + class RAS_IPolyMaterial* polymat, + class RAS_IRenderTools* rendertools, + bool useObjectColor, + const MT_Vector4& rgbacolor); + + + virtual void EnableTextures(bool enable); + +}; + +#endif //__KX_VERTEXARRAYOPENGLRASTERIZER diff --git a/source/gameengine/Rasterizer/RAS_Polygon.cpp b/source/gameengine/Rasterizer/RAS_Polygon.cpp new file mode 100644 index 00000000000..c495ef9679d --- /dev/null +++ b/source/gameengine/Rasterizer/RAS_Polygon.cpp @@ -0,0 +1,142 @@ +/** + * $Id$ + * ***** BEGIN GPL/BL DUAL LICENSE BLOCK ***** + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. The Blender + * Foundation also sells licenses for use in proprietary software under + * the Blender License. See http://www.blender.org/BL/ for information + * about this. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + * + * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV. + * All rights reserved. + * + * The Original Code is: all of this file. + * + * Contributor(s): none yet. + * + * ***** END GPL/BL DUAL LICENSE BLOCK ***** + */ + +#ifdef WIN32 + +#pragma warning (disable:4786) +#endif + +#include "RAS_Polygon.h" + +/* +RAS_TexVert* RAS_Polygon::GetVertex(int index) +{ + if (m_bucket) + return m_bucket->GetVertex(m_vertexindexbase.m_vtxarray, m_vertexindexbase.m_indexarray[index]); + else + return NULL; +} +*/ + +/*void RAS_Polygon::Bucketize(double* oglmatrix) +{ + //Transform(trans); + if (m_bucket) + m_bucket->AddPolygon(this,oglmatrix); +} +*/ + +RAS_Polygon::RAS_Polygon(RAS_MaterialBucket* bucket, + bool visible, + int numverts, + int vtxarrayindex) + :m_bucket(bucket), + m_vertexindexbase(numverts), + m_numverts(numverts), + m_edgecode(0) +{ + int size = sizeof(RAS_Polygon); + m_vertexindexbase.m_vtxarray = vtxarrayindex ;//m_bucket->FindVertexArray(numverts); + m_polyFlags.Visible = visible; +} + + + +int RAS_Polygon::VertexCount() +{ + return m_numverts; +} + + + +void RAS_Polygon::SetVertex(int i, + int vertexindex ) //const MT_Point3& xyz,const MT_Point2& uv,const unsigned int rgbacolor,const MT_Vector3& normal) +{ + m_vertexindexbase.SetIndex(i,vertexindex); //m_bucket->FindOrAddVertex(m_vertexindexbase.m_vtxarray, + //xyz,uv,rgbacolor,normal)); +} + + + +const KX_VertexIndex& RAS_Polygon::GetIndexBase() +{ + return m_vertexindexbase; +} + + + +void RAS_Polygon::SetVisibleWireframeEdges(int edgecode) +{ + m_edgecode = edgecode; +} + + + +// each bit is for a visible edge, starting with bit 1 for the first edge, bit 2 for second etc. +int RAS_Polygon::GetEdgeCode() +{ + return m_edgecode; +} + + + +bool RAS_Polygon::IsVisible() +{ + return m_polyFlags.Visible; +} + + + +bool RAS_Polygon::IsCollider() +{ + return m_polyFlags.Collider; +} + + + +void RAS_Polygon::SetCollider(bool col) +{ + m_polyFlags.Collider = col; +} + + + +KX_VertexIndex& RAS_Polygon::GetVertexIndexBase() +{ + return m_vertexindexbase; +} + + + +RAS_MaterialBucket* RAS_Polygon::GetMaterial() +{ + return m_bucket; +} diff --git a/source/gameengine/Rasterizer/RAS_Polygon.h b/source/gameengine/Rasterizer/RAS_Polygon.h new file mode 100644 index 00000000000..d5b6123e153 --- /dev/null +++ b/source/gameengine/Rasterizer/RAS_Polygon.h @@ -0,0 +1,92 @@ +/** + * $Id$ + * + * ***** BEGIN GPL/BL DUAL LICENSE BLOCK ***** + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. The Blender + * Foundation also sells licenses for use in proprietary software under + * the Blender License. See http://www.blender.org/BL/ for information + * about this. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + * + * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV. + * All rights reserved. + * + * The Original Code is: all of this file. + * + * Contributor(s): none yet. + * + * ***** END GPL/BL DUAL LICENSE BLOCK ***** + */ +#ifndef __RAS_POLYGON +#define __RAS_POLYGON + +#include "RAS_TexVert.h" +//#include "KX_BoundingVolumes.h" +#include "RAS_MaterialBucket.h" + +#include <vector> +using namespace std; + + +// +// Bitfield that stores the flags for each CValue derived class +// +struct PolygonFlags { + PolygonFlags() : + Visible(true), + Collider(true) + { + } + unsigned char Visible : 1; + unsigned char Collider : 1; + //int Visible : 1; + //int Collider : 1; +}; + +class RAS_Polygon +{ + RAS_MaterialBucket* m_bucket; + KX_VertexIndex m_vertexindexbase; + int m_numverts; + int m_edgecode; + PolygonFlags m_polyFlags; + + +public: + RAS_Polygon(RAS_MaterialBucket* bucket, + bool visible, + int numverts, + int vtxarrayindex) ; + virtual ~RAS_Polygon() {}; + +// RAS_TexVert* GetVertex(int index); + int VertexCount(); + void SetVertex(int i,int vertexindex); //const MT_Point3& xyz,const MT_Point2& uv,const unsigned int rgbacolor,const MT_Vector3& normal) + + const KX_VertexIndex& GetIndexBase(); + + void SetVisibleWireframeEdges(int edgecode); + // each bit is for a visible edge, starting with bit 1 for the first edge, bit 2 for second etc. + int GetEdgeCode(); + + bool IsVisible(); + bool IsCollider(); + void SetCollider(bool col); + + KX_VertexIndex& GetVertexIndexBase(); + RAS_MaterialBucket* GetMaterial(); + +}; +#endif diff --git a/source/gameengine/Rasterizer/RAS_Rect.h b/source/gameengine/Rasterizer/RAS_Rect.h new file mode 100644 index 00000000000..448a5ec73c1 --- /dev/null +++ b/source/gameengine/Rasterizer/RAS_Rect.h @@ -0,0 +1,99 @@ +/** + * $Id$ + * + * ***** BEGIN GPL/BL DUAL LICENSE BLOCK ***** + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. The Blender + * Foundation also sells licenses for use in proprietary software under + * the Blender License. See http://www.blender.org/BL/ for information + * about this. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + * + * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV. + * All rights reserved. + * + * The Original Code is: all of this file. + * + * Contributor(s): none yet. + * + * ***** END GPL/BL DUAL LICENSE BLOCK ***** + */ + +#ifndef _RAS_RECT +#define _RAS_RECT + + +/** + * @section interface class. + * RAS_Rect just encodes a simple rectangle. + * Should be part of a generic library + */ + +class RAS_Rect +{ +public: // todo: make a decent class, and make private + int m_x1, m_y1; + int m_x2, m_y2; + +public: + RAS_Rect() : m_x1(0), m_y1(0), m_x2(0), m_y2(0) {} + int GetWidth( + ) const { + return m_x2 - m_x1; + } + int GetHeight( + ) const { + return m_y2 - m_y1; + } + int GetLeft( + ) const { + return m_x1; + } + int GetRight( + ) const { + return m_x2; + } + int GetBottom( + ) const { + return m_y1; + } + int GetTop( + ) const { + return m_y2; + } + + void SetLeft( + int x1) + { + m_x1 = x1; + } + void SetBottom( + int y1) + { + m_y1 = y1; + } + void SetRight( + int x2) + { + m_x2 = x2; + } + void SetTop( + int y2) + { + m_y2 = y2; + } +}; + + +#endif // _RAS_RECT diff --git a/source/gameengine/Rasterizer/RAS_TexMatrix.h b/source/gameengine/Rasterizer/RAS_TexMatrix.h new file mode 100644 index 00000000000..c4d02c49154 --- /dev/null +++ b/source/gameengine/Rasterizer/RAS_TexMatrix.h @@ -0,0 +1,45 @@ +/** + * $Id$ + * + * ***** BEGIN GPL/BL DUAL LICENSE BLOCK ***** + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. The Blender + * Foundation also sells licenses for use in proprietary software under + * the Blender License. See http://www.blender.org/BL/ for information + * about this. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + * + * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV. + * All rights reserved. + * + * The Original Code is: all of this file. + * + * Contributor(s): none yet. + * + * ***** END GPL/BL DUAL LICENSE BLOCK ***** + */ +#ifndef __RASTEXMATRIX +#define __RASTEXMATRIX + +#include "MT_Matrix3x3.h" +#include "MT_Point3.h" +#include "MT_Point2.h" + + + +#include "RAS_TexVert.h" + +void RAS_CalcTexMatrix(RAS_TexVert p[3],MT_Point3& origin,MT_Vector3& udir,MT_Vector3& vdir); + +#endif //__RASTEXMATRIX diff --git a/source/gameengine/Rasterizer/RAS_TexVert.cpp b/source/gameengine/Rasterizer/RAS_TexVert.cpp new file mode 100644 index 00000000000..20a4f0d2570 --- /dev/null +++ b/source/gameengine/Rasterizer/RAS_TexVert.cpp @@ -0,0 +1,152 @@ +/** + * $Id$ + * ***** BEGIN GPL/BL DUAL LICENSE BLOCK ***** + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. The Blender + * Foundation also sells licenses for use in proprietary software under + * the Blender License. See http://www.blender.org/BL/ for information + * about this. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + * + * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV. + * All rights reserved. + * + * The Original Code is: all of this file. + * + * Contributor(s): none yet. + * + * ***** END GPL/BL DUAL LICENSE BLOCK ***** + */ + +#include "RAS_TexVert.h" + + +RAS_TexVert::RAS_TexVert(const MT_Point3& xyz, + const MT_Point2& uv, + const unsigned int rgba, + const short *normal, + const short flag) +{ + xyz.getValue(m_localxyz); + uv.getValue(m_uv1); + SetRGBA(rgba); + m_normal[0] = normal[0]; + m_normal[1] = normal[1]; + m_normal[2] = normal[2]; + m_flag = flag; +} + + + +const float* RAS_TexVert::getUV1 () const +{ + return m_uv1; +} + + + +const MT_Point3& RAS_TexVert::xyz() +{ + g_pt3.setValue(m_localxyz); + return g_pt3; +} + +void RAS_TexVert::SetXYZ(const MT_Point3& xyz) +{ + xyz.getValue(m_localxyz); +} + + + +void RAS_TexVert::SetUV(const MT_Point2& uv) +{ + uv.getValue(m_uv1); +} + + + +void RAS_TexVert::SetRGBA(const unsigned int rgba) +{ + m_rgba = rgba; +} + + +void RAS_TexVert::SetFlag(const short flag) +{ + m_flag = flag; +} +void RAS_TexVert::SetNormal(const MT_Vector3& normal) +{ + m_normal[0] = short(normal.x()*32767.0); + m_normal[1] = short(normal.y()*32767.0); + m_normal[2] = short(normal.z()*32767.0); +} + + + +// leave multiline for debugging +const short* RAS_TexVert::getNormal() const +{ + return m_normal; +} + + + +const float* RAS_TexVert::getLocalXYZ() const +{ + return m_localxyz; +} + + + +const unsigned int& RAS_TexVert::getRGBA() const +{ + return m_rgba; +} + + + +// compare two vertices, and return TRUE if both are almost identical (they can be shared) +bool RAS_TexVert::closeTo(const RAS_TexVert* other) +{ + return ((MT_Vector3(m_localxyz) - MT_Vector3(other->m_localxyz)).fuzzyZero() && + (MT_Vector2(m_uv1) - MT_Vector2(other->m_uv1)).fuzzyZero() && + m_normal[0] == other->m_normal[0] && + m_normal[1] == other->m_normal[1] && + m_normal[2] == other->m_normal[2] && + m_flag == other->m_flag && + m_rgba == other->m_rgba) ; + +} + + + +bool RAS_TexVert::closeTo(const MT_Point3& otherxyz, + const MT_Point2& otheruv, + const unsigned int otherrgba, + short othernormal[3]) const +{ + return ((MT_Vector3(m_localxyz) - otherxyz).fuzzyZero() && + (MT_Vector2(m_uv1) - otheruv).fuzzyZero() && + m_normal[0] == othernormal[0] && + m_normal[1] == othernormal[1] && + m_normal[2] == othernormal[2] && + m_rgba == otherrgba) ; +} + + +short RAS_TexVert::getFlag() const +{ + return m_flag; +} diff --git a/source/gameengine/Rasterizer/RAS_TexVert.h b/source/gameengine/Rasterizer/RAS_TexVert.h new file mode 100644 index 00000000000..ebbd35fb91c --- /dev/null +++ b/source/gameengine/Rasterizer/RAS_TexVert.h @@ -0,0 +1,98 @@ +/** + * $Id$ + * + * ***** BEGIN GPL/BL DUAL LICENSE BLOCK ***** + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. The Blender + * Foundation also sells licenses for use in proprietary software under + * the Blender License. See http://www.blender.org/BL/ for information + * about this. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + * + * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV. + * All rights reserved. + * + * The Original Code is: all of this file. + * + * Contributor(s): none yet. + * + * ***** END GPL/BL DUAL LICENSE BLOCK ***** + */ +#ifndef __RAS_TEXVERT +#define __RAS_TEXVERT + + +#include "MT_Point3.h" +#include "MT_Point2.h" +#include "MT_Transform.h" + + +static MT_Point3 g_pt3; +static MT_Point2 g_pt2; + +#define TV_CALCFACENORMAL 0x0001 + +class RAS_TexVert +{ + + float m_localxyz[3]; // 3*4=12 = 24 + float m_uv1[2]; // 2*4=8 = 24 + 16 = 40 + unsigned int m_rgba; //4 = 40 + 4 = 44 + short m_normal[3]; //3*2=6 = 50 + short m_flag; //32 bytes total size, fits nice = 52 = not fit nice + + +public: + short getFlag() const; + RAS_TexVert()// :m_xyz(0,0,0),m_uv(0,0),m_rgba(0) + {} + RAS_TexVert(const MT_Point3& xyz, + const MT_Point2& uv, + const unsigned int rgba, + const short *normal, + const short flag); + ~RAS_TexVert() {}; + + // leave multiline for debugging + const float* getUV1 () const; + + //const float* getUV1 () const { + // return m_uv1; + //}; + + const MT_Point3& xyz(); + + void SetXYZ(const MT_Point3& xyz); + void SetUV(const MT_Point2& uv); + void SetRGBA(const unsigned int rgba); + void SetNormal(const MT_Vector3& normal); + void SetFlag(const short flag); + // leave multiline for debugging + const short* getNormal() const; + //const float* getLocalXYZ() const { + // return m_localxyz; + //}; + + const float* getLocalXYZ() const; + const unsigned int& getRGBA() const; + // compare two vertices, and return TRUE if both are almost identical (they can be shared) + bool closeTo(const RAS_TexVert* other); + + bool closeTo(const MT_Point3& otherxyz, + const MT_Point2& otheruv, + const unsigned int otherrgba, + short othernormal[3]) const; +}; + +#endif //__RAS_TEXVERT diff --git a/source/gameengine/Rasterizer/RAS_texmatrix.cpp b/source/gameengine/Rasterizer/RAS_texmatrix.cpp new file mode 100644 index 00000000000..515f05b8303 --- /dev/null +++ b/source/gameengine/Rasterizer/RAS_texmatrix.cpp @@ -0,0 +1,133 @@ +/** + * $Id$ + * ***** BEGIN GPL/BL DUAL LICENSE BLOCK ***** + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. The Blender + * Foundation also sells licenses for use in proprietary software under + * the Blender License. See http://www.blender.org/BL/ for information + * about this. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + * + * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV. + * All rights reserved. + * + * The Original Code is: all of this file. + * + * Contributor(s): none yet. + * + * ***** END GPL/BL DUAL LICENSE BLOCK ***** + */ + + + +#include "RAS_TexMatrix.h" + + +void RAS_CalcTexMatrix(RAS_TexVert p[3],MT_Point3& origin,MT_Vector3& udir,MT_Vector3& vdir) +{ +// precondition: 3 vertices are non-colinear + + MT_Vector3 vec1 = p[1].xyz()-p[0].xyz(); + MT_Vector3 vec2 = p[2].xyz()-p[0].xyz(); + MT_Vector3 normal = vec1.cross(vec2); + normal.normalize(); + + // determine which coordinate we drop, ie. max coordinate in the normal + + + int ZCOORD = normal.closestAxis(); + int XCOORD = (ZCOORD+1)%3; + int YCOORD = (ZCOORD+2)%3; + + // ax+by+cz+d=0 + MT_Scalar d = -p[0].xyz().dot(normal); + + + MT_Matrix3x3 mat3( p[0].getUV1()[0],p[0].getUV1()[1], 1, + p[1].getUV1()[0],p[1].getUV1()[1], 1, + p[2].getUV1()[0],p[2].getUV1()[1], 1); + + + MT_Matrix3x3 mat3inv = mat3.inverse(); + + MT_Vector3 p123x(p[0].xyz()[XCOORD],p[1].xyz()[XCOORD],p[2].xyz()[XCOORD]); + MT_Vector3 resultx = mat3inv*p123x; + MT_Vector3 p123y(p[0].xyz()[YCOORD],p[1].xyz()[YCOORD],p[2].xyz()[YCOORD]); + MT_Vector3 resulty = mat3inv*p123y; + + // normal[ZCOORD] is not zero, because it's chosen to be maximal (absolute), and normal has length 1, + // so at least on of the coords is <> 0 + + //droppedvalue udir.dot(normal) =0 + MT_Scalar droppedu = -(resultx.x()*normal[XCOORD]+resulty.x()*normal[YCOORD])/normal[ZCOORD]; + udir[XCOORD] = resultx.x(); + udir[YCOORD] = resulty.x(); + udir[ZCOORD] = droppedu; + MT_Scalar droppedv = -(resultx.y()*normal[XCOORD]+resulty.y()*normal[YCOORD])/normal[ZCOORD]; + vdir[XCOORD] = resultx.y(); + vdir[YCOORD] = resulty.y(); + vdir[ZCOORD] = droppedv; + // droppedvalue b = -(ax+cz+d)/y; + MT_Scalar droppedvalue = -((resultx.z()*normal[XCOORD] + resulty.z()*normal[YCOORD]+d))/normal[ZCOORD]; + origin[XCOORD] = resultx.z(); + origin[YCOORD] = resulty.z(); + origin[ZCOORD] = droppedvalue; + + +} + +#ifdef _TEXOWNMAIN + +int main() +{ + + MT_Point2 puv0={0,0}; + MT_Point3 pxyz0 (0,0,128); + + MT_Scalar puv1[2]={1,0}; + MT_Point3 pxyz1(128,0,128); + + MT_Scalar puv2[2]={1,1}; + MT_Point3 pxyz2(128,0,0); + + RAS_TexVert p0(pxyz0,puv0); + RAS_TexVert p1(pxyz1,puv1); + RAS_TexVert p2(pxyz2,puv2); + + RAS_TexVert vertices[3] = + { + p0, + p1, + p2 + }; + + MT_Vector3 udir,vdir; + MT_Point3 origin; + CalcTexMatrix(vertices,origin,udir,vdir); + + MT_Point3 testpoint(128,32,64); + + MT_Scalar lenu = udir.length2(); + MT_Scalar lenv = vdir.length2(); + + MT_Scalar testu=((pxyz2-origin).dot(udir))/lenu; + MT_Scalar testv=((pxyz2-origin).dot(vdir))/lenv; + + + + + return 0; +} + +#endif // _TEXOWNMAIN |