-// File: OpenGl_GraphicDriver_7.cxx
-// Created: 20 October 2011
-// Author: Sergey ZERCHANINOV
-// Copyright: OPEN CASCADE 2011
+// Created on: 2011-10-20
+// Created by: Sergey ZERCHANINOV
+// Copyright (c) 2011-2012 OPEN CASCADE SAS
+//
+// The content of this file is subject to the Open CASCADE Technology Public
+// License Version 6.5 (the "License"). You may not use the content of this file
+// except in compliance with the License. Please obtain a copy of the License
+// at http://www.opencascade.org and read it completely before using this file.
+//
+// The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
+// main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
+//
+// The Original Code and all software distributed under the License is
+// distributed on an "AS IS" basis, without warranty of any kind, and the
+// Initial Developer hereby disclaims all such warranties, including without
+// limitation, any warranties of merchantability, fitness for a particular
+// purpose or non-infringement. Please see the License for the specific terms
+// and conditions governing the rights and limitations under the License.
+
#include <OpenGl_GraphicDriver.hxx>
if (aCView)
{
aCView->WS->SetBackgroundColor(ACView.DefWindow.Background.r,ACView.DefWindow.Background.g,ACView.DefWindow.Background.b);
- aCView->WS->Invalidate();
}
}
if (aCView)
{
aCView->View->SetBackgroundGradient(AColor1,AColor2,AType);
- aCView->WS->Invalidate();
}
}
// Do nothing
}
-void OpenGl_GraphicDriver::BoundaryBox (const Graphic3d_CStructure& ACStructure, const Standard_Boolean Create)
+void OpenGl_GraphicDriver::BoundaryBox (const Graphic3d_CStructure& theCStructure,
+ const Standard_Boolean toCreate)
{
- OpenGl_Structure *astructure = (OpenGl_Structure *)ACStructure.ptrStructure;
- if (!astructure)
+ OpenGl_Structure* aStructure = (OpenGl_Structure* )theCStructure.ptrStructure;
+ if (aStructure == NULL)
return;
- if ( Create )
- astructure->SetHighlightBox(ACStructure.BoundBox);
+ if (toCreate)
+ aStructure->SetHighlightBox (GetSharedContext(), theCStructure.BoundBox);
else
- astructure->ClearHighlightBox();
+ aStructure->ClearHighlightBox (GetSharedContext());
}
-void OpenGl_GraphicDriver::HighlightColor (const Graphic3d_CStructure& ACStructure, const Standard_ShortReal R, const Standard_ShortReal G, const Standard_ShortReal B, const Standard_Boolean Create)
+void OpenGl_GraphicDriver::HighlightColor (const Graphic3d_CStructure& theCStructure,
+ const Standard_ShortReal R,
+ const Standard_ShortReal G,
+ const Standard_ShortReal B,
+ const Standard_Boolean toCreate)
{
- OpenGl_Structure *astructure = (OpenGl_Structure *)ACStructure.ptrStructure;
- if (!astructure)
+ OpenGl_Structure* aStructure = (OpenGl_Structure* )theCStructure.ptrStructure;
+ if (aStructure == NULL)
return;
- if ( Create )
- astructure->SetHighlightColor(R,G,B);
+ if (toCreate)
+ aStructure->SetHighlightColor (GetSharedContext(), R, G, B);
else
- astructure->ClearHighlightColor();
+ aStructure->ClearHighlightColor (GetSharedContext());
}
void OpenGl_GraphicDriver::NameSetStructure (const Graphic3d_CStructure& ACStructure)
Standard_Integer aStatus = 0;
if (ACStructure.highlight) aStatus |= OPENGL_NS_HIGHLIGHT;
if (!ACStructure.visible) aStatus |= OPENGL_NS_HIDE;
- if (ACStructure.pick) aStatus |= OPENGL_NS_PICK;
astructure->SetNamedStatus( aStatus );
}
}
if (aCView)
{
aCView->View->SetClipLimit(ACView);
- if (!AWait && !ACView.DefBitmap.bitmap)
+ if (!AWait)
{
aCView->WS->Resize(ACView.DefWindow);
- aCView->WS->Invalidate();
}
}
}
Standard_Integer aWidth = aCView->WS->Width();
Standard_Integer aHeight = aCView->WS->Height();
-
- //if we want project something before to dump it into pixmap
- //For right copution of projection before dumping to pixmap
- if ( ACView.DefBitmap.bitmap )
- {
- aWidth = ACView.DefBitmap.width;
- aHeight = ACView.DefBitmap.height;
- }
-
Standard_ShortReal xr, yr;
if (aCView->View->ProjectObjectToRaster(aWidth, aHeight, AX, AY, AZ, xr, yr))
{
return aCView->View->ProjectRasterToObjectWithRay( aWidth, aHeight, AU, AYM-Aym-AV, Ax, Ay, Az, Dx, Dy, Dz );
}
-void OpenGl_GraphicDriver::RatioWindow (const Graphic3d_CView& ACView)
+void OpenGl_GraphicDriver::RatioWindow (const Graphic3d_CView& theCView)
{
- const OpenGl_CView *aCView = (const OpenGl_CView *)ACView.ptrView;
- if (!aCView)
- if( !ACView.DefBitmap.bitmap )
- aCView->WS->Resize(ACView.DefWindow);
+ const OpenGl_CView* aCView = (const OpenGl_CView* )theCView.ptrView;
+ if (aCView != NULL)
+ aCView->WS->Resize (theCView.DefWindow);
}
void OpenGl_GraphicDriver::Redraw (const Graphic3d_CView& ACView, const Aspect_CLayer2d& ACUnderLayer, const Aspect_CLayer2d& ACOverLayer, const Standard_Integer x, const Standard_Integer y, const Standard_Integer width, const Standard_Integer height)
aFrameBuffer->ChangeViewport (theWidth, theHeight);
}
-// OpenGL 1.2 stuff
-#ifndef GL_BGR
- #define GL_BGR 0x80E0
-#endif
-#ifndef GL_BGRA
- #define GL_BGRA 0x80E1
-#endif
-
-static inline GLenum TFormatToGLEnum (const TRawBufferDataFormat theTFormat)
-{
- switch (theTFormat)
- {
- case TRGB: return GL_RGB;
- case TBGR: return GL_BGR;
- case TRGBA: return GL_RGBA;
- case TBGRA: return GL_BGRA;
- case TDepthComponent: return GL_DEPTH_COMPONENT;
- case TRed: return GL_RED;
- case TGreen: return GL_GREEN;
- case TBlue: return GL_BLUE;
- case TAlpha: return GL_ALPHA;
- default: return 0;
- }
-}
-
-static inline GLenum TTypeToGLEnum (const TRawBufferDataType theTType)
+inline bool getDataFormat (const Image_PixMap& theData,
+ GLenum& thePixelFormat,
+ GLenum& theDataType)
{
- switch (theTType)
+ thePixelFormat = GL_RGB;
+ theDataType = GL_UNSIGNED_BYTE;
+ switch (theData.Format())
{
- case TUByte: return GL_UNSIGNED_BYTE;
- case TFloat: return GL_FLOAT;
- default: return 0;
+ case Image_PixMap::ImgGray:
+ thePixelFormat = GL_DEPTH_COMPONENT;
+ theDataType = GL_UNSIGNED_BYTE;
+ return true;
+ case Image_PixMap::ImgRGB:
+ thePixelFormat = GL_RGB;
+ theDataType = GL_UNSIGNED_BYTE;
+ return true;
+ case Image_PixMap::ImgBGR:
+ thePixelFormat = GL_BGR;
+ theDataType = GL_UNSIGNED_BYTE;
+ return true;
+ case Image_PixMap::ImgRGBA:
+ case Image_PixMap::ImgRGB32:
+ thePixelFormat = GL_RGBA;
+ theDataType = GL_UNSIGNED_BYTE;
+ return true;
+ case Image_PixMap::ImgBGRA:
+ case Image_PixMap::ImgBGR32:
+ thePixelFormat = GL_BGRA;
+ theDataType = GL_UNSIGNED_BYTE;
+ return true;
+ case Image_PixMap::ImgGrayF:
+ thePixelFormat = GL_DEPTH_COMPONENT;
+ theDataType = GL_FLOAT;
+ return true;
+ case Image_PixMap::ImgRGBF:
+ thePixelFormat = GL_RGB;
+ theDataType = GL_FLOAT;
+ return true;
+ case Image_PixMap::ImgBGRF:
+ thePixelFormat = GL_BGR;
+ theDataType = GL_FLOAT;
+ return true;
+ case Image_PixMap::ImgRGBAF:
+ thePixelFormat = GL_RGBA;
+ theDataType = GL_FLOAT;
+ return true;
+ case Image_PixMap::ImgBGRAF:
+ thePixelFormat = GL_BGRA;
+ theDataType = GL_FLOAT;
+ return true;
+ default:
+ return false;
}
}
-Standard_Boolean OpenGl_GraphicDriver::BufferDump (const Graphic3d_CView& ACView, Image_CRawBufferData& theBuffer)
+Standard_Boolean OpenGl_GraphicDriver::BufferDump (const Graphic3d_CView& theCView,
+ Image_PixMap& theImage,
+ const Graphic3d_BufferType& theBufferType)
{
- const OpenGl_CView *aCView = (const OpenGl_CView *)ACView.ptrView;
- if (aCView)
- return aCView->WS->BufferDump((OpenGl_FrameBuffer *)ACView.ptrFBO,theBuffer);
+ const OpenGl_CView* aCView = (const OpenGl_CView* )theCView.ptrView;
+ return (aCView != NULL) && aCView->WS->BufferDump ((OpenGl_FrameBuffer* )theCView.ptrFBO, theImage, theBufferType);
return Standard_False;
}
-Standard_Boolean OpenGl_Workspace::BufferDump (OpenGl_FrameBuffer *theFBOPtr, Image_CRawBufferData& theBuffer)
+Standard_Boolean OpenGl_Workspace::BufferDump (OpenGl_FrameBuffer* theFBOPtr,
+ Image_PixMap& theImage,
+ const Graphic3d_BufferType& theBufferType)
{
- GLenum aFormat = TFormatToGLEnum (theBuffer.format);
- GLenum aType = TTypeToGLEnum (theBuffer.type);
-
- // safe checks
- if (aFormat == 0 || aType == 0 ||
- theBuffer.widthPx == 0 || theBuffer.heightPx == 0 ||
- theBuffer.dataPtr == NULL)
+ GLenum aFormat, aType;
+ if (theImage.IsEmpty()
+ || !getDataFormat (theImage, aFormat, aType)
+ || ((theBufferType == Graphic3d_BT_Depth) && (aFormat != GL_DEPTH_COMPONENT))
+ || !Activate())
{
return Standard_False;
}
- // activate OpenGL context
- if (!Activate())
- return Standard_False;
-
// bind FBO if used
- OpenGl_FrameBuffer* aFrameBuffer = theFBOPtr;
GLint aReadBufferPrev = GL_BACK;
- if (aFrameBuffer != NULL && aFrameBuffer->IsValid())
+ if (theFBOPtr != NULL && theFBOPtr->IsValid())
{
- aFrameBuffer->BindBuffer (GetGlContext());
+ theFBOPtr->BindBuffer (GetGlContext());
}
else
{
GLint anAlignBack = 1;
glGetIntegerv (GL_PACK_ALIGNMENT, &anAlignBack);
- if (theBuffer.rowAligmentBytes == 0)
+ GLint anExtraBytes = (GLint )theImage.RowExtraBytes();
+ GLint anAligment = Min (GLint(theImage.MaxRowAligmentBytes()), 8); // limit to 8 bytes for OpenGL
+ glPixelStorei (GL_PACK_ALIGNMENT, anAligment);
+
+ if (anExtraBytes >= anAligment)
+ {
+ // copy row by row
+ for (Standard_Size aRow = 0; aRow < theImage.SizeY(); ++aRow)
+ {
+ glReadPixels (0, GLint(aRow), GLsizei (theImage.SizeX()), 1, aFormat, aType, theImage.ChangeRow (aRow));
+ }
+ }
+ else
{
- theBuffer.rowAligmentBytes = 1;
+ // read pixels
+ glReadPixels (0, 0, GLsizei (theImage.SizeX()), GLsizei (theImage.SizeY()), aFormat, aType, theImage.ChangeData());
+ theImage.SetTopDown (false); // image bottom-up in OpenGL
}
- glPixelStorei (GL_PACK_ALIGNMENT, theBuffer.rowAligmentBytes);
- // read pixels
- glReadPixels (0, 0, theBuffer.widthPx, theBuffer.heightPx, aFormat, aType, (GLvoid* )theBuffer.dataPtr);
glPixelStorei (GL_PACK_ALIGNMENT, anAlignBack);
- if (aFrameBuffer != NULL && aFrameBuffer->IsValid())
+ if (theFBOPtr != NULL && theFBOPtr->IsValid())
{
- aFrameBuffer->UnbindBuffer (GetGlContext());
+ theFBOPtr->UnbindBuffer (GetGlContext());
}
else
{
void OpenGl_GraphicDriver::RemoveView (const Graphic3d_CView& ACView)
{
- if (GetMapOfViews().IsBound (ACView.ViewId))
- GetMapOfViews().UnBind (ACView.ViewId);
+ if (myMapOfView.IsBound (ACView.ViewId))
+ myMapOfView.UnBind (ACView.ViewId);
- if (GetMapOfWorkspaces().IsBound (ACView.WsId))
- GetMapOfWorkspaces().UnBind (ACView.WsId);
+ if (myMapOfWS.IsBound (ACView.WsId))
+ myMapOfWS.UnBind (ACView.WsId);
OpenGl_CView *aCView = (OpenGl_CView *)ACView.ptrView;
delete aCView;
aCView->WS->Update(ACView,ACUnderLayer,ACOverLayer);
}
-Standard_Boolean OpenGl_GraphicDriver::View (Graphic3d_CView& ACView)
+Standard_Boolean OpenGl_GraphicDriver::View (Graphic3d_CView& theCView)
{
if (openglDisplay.IsNull())
return Standard_False;
- if (GetMapOfViews().IsBound (ACView.ViewId))
- GetMapOfViews().UnBind (ACView.ViewId);
+ if (myMapOfView.IsBound (theCView.ViewId))
+ myMapOfView.UnBind (theCView.ViewId);
- if (GetMapOfWorkspaces().IsBound (ACView.WsId))
- GetMapOfWorkspaces().UnBind (ACView.WsId);
+ if (myMapOfWS.IsBound (theCView.WsId))
+ myMapOfWS.UnBind (theCView.WsId);
- Handle(OpenGl_Workspace) aWS = Handle(OpenGl_Workspace)::DownCast(openglDisplay->GetWindow( ACView.DefWindow.XWindow ));
- if ( aWS.IsNull() )
+ Handle(OpenGl_Workspace) aWS = Handle(OpenGl_Workspace)::DownCast(openglDisplay->GetWindow (theCView.DefWindow.XWindow));
+ if (aWS.IsNull())
{
- aWS = new OpenGl_Workspace( openglDisplay, ACView.DefWindow, ACView.GContext );
- openglDisplay->SetWindow( ACView.DefWindow.XWindow, aWS );
+ Handle(OpenGl_Context) aShareCtx = GetSharedContext();
+ aWS = new OpenGl_Workspace (openglDisplay, theCView.DefWindow, theCView.GContext, aShareCtx);
+ openglDisplay->SetWindow (theCView.DefWindow.XWindow, aWS);
}
- GetMapOfWorkspaces().Bind (ACView.WsId, aWS);
+ myMapOfWS.Bind (theCView.WsId, aWS);
- Handle(OpenGl_View) aView = new OpenGl_View( ACView.Context );
- GetMapOfViews().Bind (ACView.ViewId, aView);
+ Handle(OpenGl_View) aView = new OpenGl_View (theCView.Context);
+ myMapOfView.Bind (theCView.ViewId, aView);
- OpenGl_CView *aCView = new OpenGl_CView;
+ OpenGl_CView* aCView = new OpenGl_CView();
aCView->View = aView;
aCView->WS = aWS;
- ACView.ptrView = aCView;
+ theCView.ptrView = aCView;
return Standard_True;
}
if (aCView)
{
aCView->View->SetMapping(ACView);
- if (!AWait && !ACView.DefBitmap.bitmap)
+ if (!AWait)
{
aCView->WS->Resize(ACView.DefWindow);
- aCView->WS->Invalidate();
}
}
}
if (aCView)
{
aCView->View->SetOrientation(ACView);
- if (!AWait && !ACView.DefBitmap.bitmap)
+ if (!AWait)
{
aCView->WS->Resize(ACView.DefWindow);
- aCView->WS->Invalidate();
}
}
}
if (aCView)
aCView->View->SetBackfacing(ACView.Backfacing);
}
+
+//=======================================================================
+//function : AddZLayer
+//purpose :
+//=======================================================================
+
+void OpenGl_GraphicDriver::AddZLayer (const Graphic3d_CView& theCView,
+ const Standard_Integer theLayerId)
+{
+ const OpenGl_CView *aCView = (const OpenGl_CView *)theCView.ptrView;
+ if (aCView)
+ aCView->View->AddZLayer (theLayerId);
+}
+
+//=======================================================================
+//function : RemoveZLayer
+//purpose :
+//=======================================================================
+
+void OpenGl_GraphicDriver::RemoveZLayer (const Graphic3d_CView& theCView,
+ const Standard_Integer theLayerId)
+{
+ const OpenGl_CView* aCView = (const OpenGl_CView *)theCView.ptrView;
+ if (aCView)
+ aCView->View->RemoveZLayer (theLayerId);
+}