3 * ***** BEGIN GPL LICENSE BLOCK *****
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
20 * All rights reserved.
22 * The Original Code is: all of this file.
24 * Contributor(s): none yet.
26 * ***** END GPL LICENSE BLOCK *****
29 /** \file gameengine/Rasterizer/RAS_OpenGLRasterizer/RAS_OpenGLRasterizer.cpp
37 #include "RAS_OpenGLRasterizer.h"
42 #include "RAS_TexVert.h"
43 #include "RAS_MeshObject.h"
44 #include "MT_CmMatrix4x4.h"
45 #include "RAS_IRenderTools.h" // rendering text
48 #include "GPU_material.h"
49 #include "GPU_extensions.h"
51 #include "DNA_image_types.h"
52 #include "DNA_meshdata_types.h"
53 #include "DNA_material_types.h"
54 #include "DNA_scene_types.h"
56 #include "BKE_DerivedMesh.h"
59 * 32x32 bit masks for vinterlace stereo mode
61 static GLuint left_eye_vinterlace_mask[32];
62 static GLuint right_eye_vinterlace_mask[32];
65 * 32x32 bit masks for hinterlace stereo mode.
66 * Left eye = &hinterlace_mask[0]
67 * Right eye = &hinterlace_mask[1]
69 static GLuint hinterlace_mask[33];
71 RAS_OpenGLRasterizer::RAS_OpenGLRasterizer(RAS_ICanvas* canvas)
72 :RAS_IRasterizer(canvas),
76 m_campos(0.0f, 0.0f, 0.0f),
78 m_stereomode(RAS_STEREO_NOSTEREO),
79 m_curreye(RAS_STEREO_LEFTEYE),
82 m_setfocallength(false),
85 m_motionblurvalue(-1.0),
88 //m_last_blendmode(GPU_BLEND_SOLID),
89 m_last_frontface(true),
90 m_materialCachingInfo(0)
92 m_viewmatrix.setIdentity();
93 m_viewinvmatrix.setIdentity();
95 for (int i = 0; i < 32; i++)
97 left_eye_vinterlace_mask[i] = 0x55555555;
98 right_eye_vinterlace_mask[i] = 0xAAAAAAAA;
99 hinterlace_mask[i] = (i&1)*0xFFFFFFFF;
101 hinterlace_mask[32] = 0;
103 m_prevafvalue = GPU_get_anisotropic();
108 RAS_OpenGLRasterizer::~RAS_OpenGLRasterizer()
110 // Restore the previous AF value
111 GPU_set_anisotropic(m_prevafvalue);
114 bool RAS_OpenGLRasterizer::Init()
124 glDisable(GL_ALPHA_TEST);
125 //m_last_blendmode = GPU_BLEND_SOLID;
126 GPU_set_material_blend_mode(GPU_BLEND_SOLID);
129 m_last_frontface = true;
132 m_greenback = 0.4375;
136 glClearColor(m_redback,m_greenback,m_blueback,m_alphaback);
137 glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
138 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
141 glShadeModel(GL_SMOOTH);
147 void RAS_OpenGLRasterizer::SetAmbientColor(float red, float green, float blue)
155 void RAS_OpenGLRasterizer::SetAmbient(float factor)
157 float ambient[] = { m_ambr*factor, m_ambg*factor, m_ambb*factor, 1.0f };
158 glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambient);
162 void RAS_OpenGLRasterizer::SetBackColor(float red,
175 void RAS_OpenGLRasterizer::SetFogColor(float r,
187 void RAS_OpenGLRasterizer::SetFogStart(float start)
195 void RAS_OpenGLRasterizer::SetFogEnd(float fogend)
203 void RAS_OpenGLRasterizer::SetFog(float start,
219 void RAS_OpenGLRasterizer::DisableFog()
221 m_fogenabled = false;
224 bool RAS_OpenGLRasterizer::IsFogEnabled()
230 void RAS_OpenGLRasterizer::DisplayFog()
232 if ((m_drawingmode >= KX_SOLID) && m_fogenabled)
235 glFogi(GL_FOG_MODE, GL_LINEAR);
236 glFogf(GL_FOG_DENSITY, 0.1f);
237 glFogf(GL_FOG_START, m_fogstart);
238 glFogf(GL_FOG_END, m_fogstart + m_fogdist);
243 glFogfv(GL_FOG_COLOR, params);
254 bool RAS_OpenGLRasterizer::SetMaterial(const RAS_IPolyMaterial& mat)
256 return mat.Activate(this, m_materialCachingInfo);
261 void RAS_OpenGLRasterizer::Exit()
264 glEnable(GL_CULL_FACE);
265 glEnable(GL_DEPTH_TEST);
267 glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
268 glClearColor(m_redback, m_greenback, m_blueback, m_alphaback);
269 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
270 glDepthMask (GL_TRUE);
271 glDepthFunc(GL_LEQUAL);
272 glBlendFunc(GL_ONE, GL_ZERO);
274 glDisable(GL_POLYGON_STIPPLE);
276 glDisable(GL_LIGHTING);
277 if (GLEW_EXT_separate_specular_color || GLEW_VERSION_1_2)
278 glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL, GL_SINGLE_COLOR);
283 bool RAS_OpenGLRasterizer::BeginFrame(int drawingmode, double time)
286 m_drawingmode = drawingmode;
288 // Blender camera routine destroys the settings
289 if (m_drawingmode < KX_SOLID)
291 glDisable (GL_CULL_FACE);
292 glDisable (GL_DEPTH_TEST);
296 glEnable(GL_DEPTH_TEST);
297 glEnable (GL_CULL_FACE);
301 glDisable(GL_ALPHA_TEST);
302 //m_last_blendmode = GPU_BLEND_SOLID;
303 GPU_set_material_blend_mode(GPU_BLEND_SOLID);
306 m_last_frontface = true;
308 glShadeModel(GL_SMOOTH);
310 glEnable(GL_MULTISAMPLE_ARB);
312 m_2DCanvas->BeginFrame();
319 void RAS_OpenGLRasterizer::SetDrawingMode(int drawingmode)
321 m_drawingmode = drawingmode;
323 if(m_drawingmode == KX_WIREFRAME)
324 glDisable(GL_CULL_FACE);
327 int RAS_OpenGLRasterizer::GetDrawingMode()
329 return m_drawingmode;
333 void RAS_OpenGLRasterizer::SetDepthMask(DepthMask depthmask)
335 glDepthMask(depthmask == KX_DEPTHMASK_DISABLED ? GL_FALSE : GL_TRUE);
339 void RAS_OpenGLRasterizer::ClearColorBuffer()
341 m_2DCanvas->ClearColor(m_redback,m_greenback,m_blueback,m_alphaback);
342 m_2DCanvas->ClearBuffer(RAS_ICanvas::COLOR_BUFFER);
346 void RAS_OpenGLRasterizer::ClearDepthBuffer()
348 m_2DCanvas->ClearBuffer(RAS_ICanvas::DEPTH_BUFFER);
352 void RAS_OpenGLRasterizer::ClearCachingInfo(void)
354 m_materialCachingInfo = 0;
357 void RAS_OpenGLRasterizer::FlushDebugLines()
359 if(!m_debugLines.size())
363 GLboolean light, tex;
365 light= glIsEnabled(GL_LIGHTING);
366 tex= glIsEnabled(GL_TEXTURE_2D);
368 if(light) glDisable(GL_LIGHTING);
369 if(tex) glDisable(GL_TEXTURE_2D);
372 for (unsigned int i=0;i<m_debugLines.size();i++)
374 glColor4f(m_debugLines[i].m_color[0],m_debugLines[i].m_color[1],m_debugLines[i].m_color[2],1.f);
375 const MT_Scalar* fromPtr = &m_debugLines[i].m_from.x();
376 const MT_Scalar* toPtr= &m_debugLines[i].m_to.x();
378 glVertex3dv(fromPtr);
383 if(light) glEnable(GL_LIGHTING);
384 if(tex) glEnable(GL_TEXTURE_2D);
386 m_debugLines.clear();
389 void RAS_OpenGLRasterizer::EndFrame()
395 glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
397 glDisable(GL_MULTISAMPLE_ARB);
399 m_2DCanvas->EndFrame();
402 void RAS_OpenGLRasterizer::SetRenderArea()
405 // only above/below stereo method needs viewport adjustment
406 switch (m_stereomode)
408 case RAS_STEREO_ABOVEBELOW:
411 case RAS_STEREO_LEFTEYE:
412 // upper half of window
414 area.SetBottom(m_2DCanvas->GetHeight() -
415 int(m_2DCanvas->GetHeight() - m_noOfScanlines) / 2);
417 area.SetRight(int(m_2DCanvas->GetWidth()));
418 area.SetTop(int(m_2DCanvas->GetHeight()));
419 m_2DCanvas->SetDisplayArea(&area);
421 case RAS_STEREO_RIGHTEYE:
422 // lower half of window
425 area.SetRight(int(m_2DCanvas->GetWidth()));
426 area.SetTop(int(m_2DCanvas->GetHeight() - m_noOfScanlines) / 2);
427 m_2DCanvas->SetDisplayArea(&area);
431 case RAS_STEREO_SIDEBYSIDE:
434 case RAS_STEREO_LEFTEYE:
435 // Left half of window
438 area.SetRight(m_2DCanvas->GetWidth()/2);
439 area.SetTop(m_2DCanvas->GetHeight());
440 m_2DCanvas->SetDisplayArea(&area);
442 case RAS_STEREO_RIGHTEYE:
443 // Right half of window
444 area.SetLeft(m_2DCanvas->GetWidth()/2);
446 area.SetRight(m_2DCanvas->GetWidth());
447 area.SetTop(m_2DCanvas->GetHeight());
448 m_2DCanvas->SetDisplayArea(&area);
453 // every available pixel
456 area.SetRight(int(m_2DCanvas->GetWidth()));
457 area.SetTop(int(m_2DCanvas->GetHeight()));
458 m_2DCanvas->SetDisplayArea(&area);
463 void RAS_OpenGLRasterizer::SetStereoMode(const StereoMode stereomode)
465 m_stereomode = stereomode;
468 RAS_IRasterizer::StereoMode RAS_OpenGLRasterizer::GetStereoMode()
473 bool RAS_OpenGLRasterizer::Stereo()
475 if(m_stereomode > RAS_STEREO_NOSTEREO) // > 0
481 bool RAS_OpenGLRasterizer::InterlacedStereo()
483 return m_stereomode == RAS_STEREO_VINTERLACE || m_stereomode == RAS_STEREO_INTERLACED;
486 void RAS_OpenGLRasterizer::SetEye(const StereoEye eye)
489 switch (m_stereomode)
491 case RAS_STEREO_QUADBUFFERED:
492 glDrawBuffer(m_curreye == RAS_STEREO_LEFTEYE ? GL_BACK_LEFT : GL_BACK_RIGHT);
494 case RAS_STEREO_ANAGLYPH:
495 if (m_curreye == RAS_STEREO_LEFTEYE)
497 glColorMask(GL_FALSE, GL_TRUE, GL_TRUE, GL_FALSE);
499 //glAccum(GL_LOAD, 1.0);
500 glColorMask(GL_TRUE, GL_FALSE, GL_FALSE, GL_FALSE);
504 case RAS_STEREO_VINTERLACE:
506 glEnable(GL_POLYGON_STIPPLE);
507 glPolygonStipple((const GLubyte*) ((m_curreye == RAS_STEREO_LEFTEYE) ? left_eye_vinterlace_mask : right_eye_vinterlace_mask));
508 if (m_curreye == RAS_STEREO_RIGHTEYE)
512 case RAS_STEREO_INTERLACED:
514 glEnable(GL_POLYGON_STIPPLE);
515 glPolygonStipple((const GLubyte*) &hinterlace_mask[m_curreye == RAS_STEREO_LEFTEYE?0:1]);
516 if (m_curreye == RAS_STEREO_RIGHTEYE)
525 RAS_IRasterizer::StereoEye RAS_OpenGLRasterizer::GetEye()
531 void RAS_OpenGLRasterizer::SetEyeSeparation(const float eyeseparation)
533 m_eyeseparation = eyeseparation;
536 float RAS_OpenGLRasterizer::GetEyeSeparation()
538 return m_eyeseparation;
541 void RAS_OpenGLRasterizer::SetFocalLength(const float focallength)
543 m_focallength = focallength;
544 m_setfocallength = true;
547 float RAS_OpenGLRasterizer::GetFocalLength()
549 return m_focallength;
553 void RAS_OpenGLRasterizer::SwapBuffers()
555 m_2DCanvas->SwapBuffers();
560 const MT_Matrix4x4& RAS_OpenGLRasterizer::GetViewMatrix() const
565 const MT_Matrix4x4& RAS_OpenGLRasterizer::GetViewInvMatrix() const
567 return m_viewinvmatrix;
570 void RAS_OpenGLRasterizer::IndexPrimitives_3DText(RAS_MeshSlot& ms,
571 class RAS_IPolyMaterial* polymat,
572 class RAS_IRenderTools* rendertools)
574 bool obcolor = ms.m_bObjectColor;
575 MT_Vector4& rgba = ms.m_RGBAcolor;
576 RAS_MeshSlot::iterator it;
578 // handle object color
580 glDisableClientState(GL_COLOR_ARRAY);
581 glColor4d(rgba[0], rgba[1], rgba[2], rgba[3]);
584 glEnableClientState(GL_COLOR_ARRAY);
586 for(ms.begin(it); !ms.end(it); ms.next(it)) {
588 size_t i, j, numvert;
590 numvert = it.array->m_type;
592 if(it.array->m_type == RAS_DisplayArray::LINE) {
593 // line drawing, no text
596 for(i=0; i<it.totindex; i+=2)
598 vertex = &it.vertex[it.index[i]];
599 glVertex3fv(vertex->getXYZ());
601 vertex = &it.vertex[it.index[i+1]];
602 glVertex3fv(vertex->getXYZ());
608 // triangle and quad text drawing
609 for(i=0; i<it.totindex; i+=numvert)
614 for(j=0; j<numvert; j++) {
615 vertex = &it.vertex[it.index[i+j]];
617 v[j][0] = vertex->getXYZ()[0];
618 v[j][1] = vertex->getXYZ()[1];
619 v[j][2] = vertex->getXYZ()[2];
622 // find the right opengl attribute
624 if(GLEW_ARB_vertex_program)
625 for(unit=0; unit<m_attrib_num; unit++)
626 if(m_attrib[unit] == RAS_TEXCO_UV1)
629 rendertools->RenderText(polymat->GetDrawingMode(), polymat,
630 v[0], v[1], v[2], (numvert == 4)? v[3]: NULL, glattrib);
637 glDisableClientState(GL_COLOR_ARRAY);
640 void RAS_OpenGLRasterizer::SetTexCoordNum(int num)
643 if(m_texco_num > RAS_MAX_TEXCO)
644 m_texco_num = RAS_MAX_TEXCO;
647 void RAS_OpenGLRasterizer::SetAttribNum(int num)
650 if(m_attrib_num > RAS_MAX_ATTRIB)
651 m_attrib_num = RAS_MAX_ATTRIB;
654 void RAS_OpenGLRasterizer::SetTexCoord(TexCoGen coords, int unit)
656 // this changes from material to material
657 if(unit < RAS_MAX_TEXCO)
658 m_texco[unit] = coords;
661 void RAS_OpenGLRasterizer::SetAttrib(TexCoGen coords, int unit)
663 // this changes from material to material
664 if(unit < RAS_MAX_ATTRIB)
665 m_attrib[unit] = coords;
668 void RAS_OpenGLRasterizer::TexCoord(const RAS_TexVert &tv)
672 if(GLEW_ARB_multitexture) {
673 for(unit=0; unit<m_texco_num; unit++) {
674 if(tv.getFlag() & RAS_TexVert::SECOND_UV && (int)tv.getUnit() == unit) {
675 glMultiTexCoord2fvARB(GL_TEXTURE0_ARB+unit, tv.getUV2());
678 switch(m_texco[unit]) {
681 glMultiTexCoord3fvARB(GL_TEXTURE0_ARB+unit, tv.getXYZ());
684 glMultiTexCoord2fvARB(GL_TEXTURE0_ARB+unit, tv.getUV1());
687 glMultiTexCoord3fvARB(GL_TEXTURE0_ARB+unit, tv.getNormal());
690 glMultiTexCoord4fvARB(GL_TEXTURE0_ARB+unit, tv.getTangent());
693 glMultiTexCoord2fvARB(GL_TEXTURE0_ARB+unit, tv.getUV2());
701 if(GLEW_ARB_vertex_program) {
702 for(unit=0; unit<m_attrib_num; unit++) {
703 switch(m_attrib[unit]) {
706 glVertexAttrib3fvARB(unit, tv.getXYZ());
709 glVertexAttrib2fvARB(unit, tv.getUV1());
712 glVertexAttrib3fvARB(unit, tv.getNormal());
715 glVertexAttrib4fvARB(unit, tv.getTangent());
718 glVertexAttrib2fvARB(unit, tv.getUV2());
721 glVertexAttrib4ubvARB(unit, tv.getRGBA());
731 void RAS_OpenGLRasterizer::IndexPrimitives(RAS_MeshSlot& ms)
733 IndexPrimitivesInternal(ms, false);
736 void RAS_OpenGLRasterizer::IndexPrimitivesMulti(RAS_MeshSlot& ms)
738 IndexPrimitivesInternal(ms, true);
741 static bool current_wireframe;
742 static RAS_MaterialBucket *current_bucket;
743 static RAS_IPolyMaterial *current_polymat;
744 static RAS_MeshSlot *current_ms;
745 static RAS_MeshObject *current_mesh;
746 static int current_blmat_nr;
747 static GPUVertexAttribs current_gpu_attribs;
748 static Image *current_image;
749 static int CheckMaterialDM(int matnr, void *attribs)
751 // only draw the current material
752 if (matnr != current_blmat_nr)
754 GPUVertexAttribs *gattribs = (GPUVertexAttribs *)attribs;
756 memcpy(gattribs, ¤t_gpu_attribs, sizeof(GPUVertexAttribs));
761 static int CheckTexfaceDM(void *mcol, int index)
764 // index is the original face index, retrieve the polygon
765 RAS_Polygon* polygon = (index >= 0 && index < current_mesh->NumPolygons()) ?
766 current_mesh->GetPolygon(index) : NULL;
767 if (polygon && polygon->GetMaterial() == current_bucket) {
768 // must handle color.
769 if (current_wireframe)
771 if (current_ms->m_bObjectColor) {
772 MT_Vector4& rgba = current_ms->m_RGBAcolor;
773 glColor4d(rgba[0], rgba[1], rgba[2], rgba[3]);
778 // we have to set the color from the material
779 unsigned char rgba[4];
780 current_polymat->GetMaterialRGBAColor(rgba);
781 glColor4ubv((const GLubyte *)rgba);
790 static int CheckTexDM(MTFace *tface, MCol *mcol, int matnr)
793 // index is the original face index, retrieve the polygon
794 if (matnr == current_blmat_nr &&
795 (tface == NULL || tface->tpage == current_image)) {
796 // must handle color.
797 if (current_wireframe)
799 if (current_ms->m_bObjectColor) {
800 MT_Vector4& rgba = current_ms->m_RGBAcolor;
801 glColor4d(rgba[0], rgba[1], rgba[2], rgba[3]);
806 // we have to set the color from the material
807 unsigned char rgba[4];
808 current_polymat->GetMaterialRGBAColor(rgba);
809 glColor4ubv((const GLubyte *)rgba);
817 void RAS_OpenGLRasterizer::IndexPrimitivesInternal(RAS_MeshSlot& ms, bool multi)
819 bool obcolor = ms.m_bObjectColor;
820 bool wireframe = m_drawingmode <= KX_WIREFRAME;
821 MT_Vector4& rgba = ms.m_RGBAcolor;
822 RAS_MeshSlot::iterator it;
824 if (ms.m_pDerivedMesh) {
825 // mesh data is in derived mesh,
826 current_bucket = ms.m_bucket;
827 current_polymat = current_bucket->GetPolyMaterial();
829 current_mesh = ms.m_mesh;
830 current_wireframe = wireframe;
831 // MCol *mcol = (MCol*)ms.m_pDerivedMesh->getFaceDataArray(ms.m_pDerivedMesh, CD_MCOL); /* UNUSED */
834 if (current_polymat->GetDrawingMode() & RAS_IRasterizer::KX_TWOSIDE)
835 this->SetCullFace(false);
837 this->SetCullFace(true);
839 if (current_polymat->GetFlag() & RAS_BLENDERGLSL) {
840 // GetMaterialIndex return the original mface material index,
841 // increment by 1 to match what derived mesh is doing
842 current_blmat_nr = current_polymat->GetMaterialIndex()+1;
843 // For GLSL we need to retrieve the GPU material attribute
844 Material* blmat = current_polymat->GetBlenderMaterial();
845 Scene* blscene = current_polymat->GetBlenderScene();
846 if (!wireframe && blscene && blmat)
847 GPU_material_vertex_attributes(GPU_material_from_blender(blscene, blmat), ¤t_gpu_attribs);
849 memset(¤t_gpu_attribs, 0, sizeof(current_gpu_attribs));
850 // DM draw can mess up blending mode, restore at the end
851 int current_blend_mode = GPU_get_material_blend_mode();
852 ms.m_pDerivedMesh->drawFacesGLSL(ms.m_pDerivedMesh, CheckMaterialDM);
853 GPU_set_material_blend_mode(current_blend_mode);
855 //ms.m_pDerivedMesh->drawMappedFacesTex(ms.m_pDerivedMesh, CheckTexfaceDM, mcol);
856 current_blmat_nr = current_polymat->GetMaterialIndex();
857 current_image = current_polymat->GetBlenderImage();
858 ms.m_pDerivedMesh->drawFacesTex(ms.m_pDerivedMesh, CheckTexDM);
862 // iterate over display arrays, each containing an index + vertex array
863 for(ms.begin(it); !ms.end(it); ms.next(it)) {
865 size_t i, j, numvert;
867 numvert = it.array->m_type;
869 if(it.array->m_type == RAS_DisplayArray::LINE) {
873 for(i=0; i<it.totindex; i+=2)
875 vertex = &it.vertex[it.index[i]];
876 glVertex3fv(vertex->getXYZ());
878 vertex = &it.vertex[it.index[i+1]];
879 glVertex3fv(vertex->getXYZ());
885 // triangle and quad drawing
886 if(it.array->m_type == RAS_DisplayArray::TRIANGLE)
887 glBegin(GL_TRIANGLES);
891 for(i=0; i<it.totindex; i+=numvert)
894 glColor4d(rgba[0], rgba[1], rgba[2], rgba[3]);
896 for(j=0; j<numvert; j++) {
897 vertex = &it.vertex[it.index[i+j]];
901 glColor4ubv((const GLubyte *)(vertex->getRGBA()));
903 glNormal3fv(vertex->getNormal());
908 glTexCoord2fv(vertex->getUV1());
911 glVertex3fv(vertex->getXYZ());
920 void RAS_OpenGLRasterizer::SetProjectionMatrix(MT_CmMatrix4x4 &mat)
922 glMatrixMode(GL_PROJECTION);
923 double* matrix = &mat(0,0);
924 glLoadMatrixd(matrix);
926 m_camortho= (mat(3, 3) != 0.0f);
929 void RAS_OpenGLRasterizer::SetProjectionMatrix(const MT_Matrix4x4 & mat)
931 glMatrixMode(GL_PROJECTION);
933 /* Get into argument. Looks a bit dodgy, but it's ok. */
934 mat.getValue(matrix);
935 /* Internally, MT_Matrix4x4 uses doubles (MT_Scalar). */
936 glLoadMatrixd(matrix);
938 m_camortho= (mat[3][3] != 0.0f);
941 MT_Matrix4x4 RAS_OpenGLRasterizer::GetFrustumMatrix(
954 // correction for stereo
957 float near_div_focallength;
960 // if Rasterizer.setFocalLength is not called we use the camera focallength
961 if (!m_setfocallength)
962 // if focallength is null we use a value known to be reasonable
963 m_focallength = (focallength == 0.f) ? m_eyeseparation * 30.0
966 near_div_focallength = frustnear / m_focallength;
967 offset = 0.5 * m_eyeseparation * near_div_focallength;
970 case RAS_STEREO_LEFTEYE:
974 case RAS_STEREO_RIGHTEYE:
979 // leave bottom and top untouched
982 glMatrixMode(GL_PROJECTION);
984 glFrustum(left, right, bottom, top, frustnear, frustfar);
986 glGetDoublev(GL_PROJECTION_MATRIX, mat);
987 result.setValue(mat);
992 MT_Matrix4x4 RAS_OpenGLRasterizer::GetOrthoMatrix(
1000 MT_Matrix4x4 result;
1003 // stereo is meaning less for orthographic, disable it
1004 glMatrixMode(GL_PROJECTION);
1006 glOrtho(left, right, bottom, top, frustnear, frustfar);
1008 glGetDoublev(GL_PROJECTION_MATRIX, mat);
1009 result.setValue(mat);
1015 // next arguments probably contain redundant info, for later...
1016 void RAS_OpenGLRasterizer::SetViewMatrix(const MT_Matrix4x4 &mat,
1017 const MT_Matrix3x3 & camOrientMat3x3,
1018 const MT_Point3 & pos,
1023 // correction for stereo
1024 if(Stereo() && perspective)
1026 MT_Vector3 unitViewDir(0.0, -1.0, 0.0); // minus y direction, Blender convention
1027 MT_Vector3 unitViewupVec(0.0, 0.0, 1.0);
1028 MT_Vector3 viewDir, viewupVec;
1032 viewDir = camOrientMat3x3 * unitViewDir; // this is the moto convention, vector on right hand side
1033 // actual viewup vec
1034 viewupVec = camOrientMat3x3 * unitViewupVec;
1036 // vector between eyes
1037 eyeline = viewDir.cross(viewupVec);
1041 case RAS_STEREO_LEFTEYE:
1043 // translate to left by half the eye distance
1044 MT_Transform transform;
1045 transform.setIdentity();
1046 transform.translate(-(eyeline * m_eyeseparation / 2.0));
1047 m_viewmatrix *= transform;
1050 case RAS_STEREO_RIGHTEYE:
1052 // translate to right by half the eye distance
1053 MT_Transform transform;
1054 transform.setIdentity();
1055 transform.translate(eyeline * m_eyeseparation / 2.0);
1056 m_viewmatrix *= transform;
1062 m_viewinvmatrix = m_viewmatrix;
1063 m_viewinvmatrix.invert();
1065 // note: getValue gives back column major as needed by OpenGL
1066 MT_Scalar glviewmat[16];
1067 m_viewmatrix.getValue(glviewmat);
1069 glMatrixMode(GL_MODELVIEW);
1070 glLoadMatrixd(glviewmat);
1075 const MT_Point3& RAS_OpenGLRasterizer::GetCameraPosition()
1080 bool RAS_OpenGLRasterizer::GetCameraOrtho()
1085 void RAS_OpenGLRasterizer::SetCullFace(bool enable)
1088 glEnable(GL_CULL_FACE);
1090 glDisable(GL_CULL_FACE);
1093 void RAS_OpenGLRasterizer::SetLines(bool enable)
1096 glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
1098 glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
1101 void RAS_OpenGLRasterizer::SetSpecularity(float specX,
1106 GLfloat mat_specular[] = {specX, specY, specZ, specval};
1107 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, mat_specular);
1112 void RAS_OpenGLRasterizer::SetShinyness(float shiny)
1114 GLfloat mat_shininess[] = { shiny };
1115 glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, mat_shininess);
1120 void RAS_OpenGLRasterizer::SetDiffuse(float difX,float difY,float difZ,float diffuse)
1122 GLfloat mat_diffuse [] = {difX, difY,difZ, diffuse};
1123 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, mat_diffuse);
1126 void RAS_OpenGLRasterizer::SetEmissive(float eX, float eY, float eZ, float e)
1128 GLfloat mat_emit [] = {eX,eY,eZ,e};
1129 glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, mat_emit);
1133 double RAS_OpenGLRasterizer::GetTime()
1138 void RAS_OpenGLRasterizer::SetPolygonOffset(float mult, float add)
1140 glPolygonOffset(mult, add);
1141 GLint mode = GL_POLYGON_OFFSET_FILL;
1142 if (m_drawingmode < KX_SHADED)
1143 mode = GL_POLYGON_OFFSET_LINE;
1144 if (mult != 0.0f || add != 0.0f)
1150 void RAS_OpenGLRasterizer::EnableMotionBlur(float motionblurvalue)
1152 /* don't just set m_motionblur to 1, but check if it is 0 so
1153 * we don't reset a motion blur that is already enabled */
1154 if(m_motionblur == 0)
1156 m_motionblurvalue = motionblurvalue;
1159 void RAS_OpenGLRasterizer::DisableMotionBlur()
1162 m_motionblurvalue = -1.0;
1165 void RAS_OpenGLRasterizer::SetBlendingMode(int blendmode)
1167 GPU_set_material_blend_mode(blendmode);
1169 if(blendmode == m_last_blendmode)
1172 if(blendmode == GPU_BLEND_SOLID) {
1173 glDisable(GL_BLEND);
1174 glDisable(GL_ALPHA_TEST);
1175 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1177 else if(blendmode == GPU_BLEND_ADD) {
1178 glBlendFunc(GL_ONE, GL_ONE);
1180 glDisable(GL_ALPHA_TEST);
1182 else if(blendmode == GPU_BLEND_ALPHA) {
1183 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1185 glEnable(GL_ALPHA_TEST);
1186 glAlphaFunc(GL_GREATER, 0.0f);
1188 else if(blendmode == GPU_BLEND_CLIP) {
1189 glDisable(GL_BLEND);
1190 glEnable(GL_ALPHA_TEST);
1191 glAlphaFunc(GL_GREATER, 0.5f);
1194 m_last_blendmode = blendmode;
1198 void RAS_OpenGLRasterizer::SetFrontFace(bool ccw)
1200 if(m_last_frontface == ccw)
1204 glFrontFace(GL_CCW);
1208 m_last_frontface = ccw;
1211 void RAS_OpenGLRasterizer::SetAnisotropicFiltering(short level)
1213 GPU_set_anisotropic((float)level);
1216 short RAS_OpenGLRasterizer::GetAnisotropicFiltering()
1218 return (short)GPU_get_anisotropic();