4 #include <Base/Common/BIASpragma.hh>
5 #include <Utils/GlutInitWrapper.hh>
6 #include "OpenGLCanvasBase.hh"
15 #include <Base/Image/ImageIO.hh>
16 #include <Base/Math/Vector2.hh>
17 #include <Base/Math/Vector3.hh>
18 #include <Base/Math/Vector4.hh>
19 #include <Base/Common/FileHandling.hh>
20 #include <Gui/StringConv.hh>
23 #include "GeometryGL.hh"
26 #include "SettingsGL.hh"
39 #ifdef BIAS_HAVE_STEREOI
41 #endif // BIAS_HAVE_STEREOI
62 bool OpenGLCanvasBase::animationUpdateEnabled = OPENGLCANVASBASE_DEFAULT_animationUpdateEnabled;
64 #ifdef BIAS_HAVE_STEREOI
67 static StereoI* g_pStereoAPI = NULL;
68 #endif // BIAS_HAVE_STEREOI
76 OpenGLCanvasBase::~OpenGLCanvasBase()
78 #ifdef BIAS_HAVE_FONTGL
81 #endif // BIAS_HAVE_FONTGL
85 OpenGLCanvasBase::OpenGLCanvasBase(
93 ,
const wxPalette& palette )
94 : wxGLCanvas(parent,id,pos,size,style,name,attribList,palette)
96 #ifdef BIAS_HAVE_FONTGL
102 instancesTotalNo_ ++;
110 ,wxGLContext* sharedContext
115 ,
const wxString& name
117 ,
const wxPalette& palette )
118 : wxGLCanvas(parent,sharedContext,id,pos,size,style,name,attribList,palette)
120 #ifdef BIAS_HAVE_FONTGL
126 instancesTotalNo_ ++;
149 BIASERR(
"skipped because GetContext failed on frame ");
157 BIASERR(
"skipped because no context available on glGetString GL_VENDOR test");
162 #if defined(BIAS_HAVE_GLUT)
163 # if defined(__FREEGLUT_H__) || defined(__FREEGLUT_STD_H__)
173 #if defined(BIAS_HAVE_GLEW)
176 # ifdef USE_EXPERIMENTAL_SETTING_GLEW
179 BIASERR(
"WARNING: you are forcing bypassing glew extension query"<<endl
180 <<
"because USE_EXPERIMENTAL_SETTING_GLEW is defined."<<endl
181 <<
"This is *NOT* recommended in general (JW)");
182 glewExperimental=GL_TRUE;
183 # endif // USE_EXPERIMENTAL_SETTING_GLEW
185 int err = glewInit();
186 if (GLEW_OK != err) {
188 BIASERR(
"could not init GLEW. Error: "<<glewGetErrorString(err));
191 #endif // BIAS_HAVE_GLEW
194 #if defined(WIN32) && defined(BIAS_HAVE_GLEW)
196 if (WGLEW_EXT_swap_control)
201 <<
"\tDoes the driver allow application controlled VSYNC/page swapping settings?"<<endl
202 <<
"\tE.g. NVStereo will and driver tab may force overriding our settings." );
207 #ifdef BIAS_HAVE_FONTGL
218 #endif // BIAS_HAVE_FONTGL
227 if (!(GetParent()->IsShown())) {
232 BIASERR(
"Context not (yet?) available.");
236 if (!(GetParent()->IsShown()))
return false;
246 const unsigned char * p_str=NULL;
247 p_str = glGetString(GL_VENDOR);
250 BIASERR(
"No Context available (NULL ptr)");
273 if (!GetContext())
return;
282 #ifdef BIAS_HAVE_STEREOI
285 #endif //BIAS_HAVE_STEREOI
290 wxGLCanvas::SwapBuffers();
306 if (!(GetParent()->IsShown())) {
307 BIASERR(
"Parent not yet shown.");
357 static int bufferNr=0;
364 glDrawBuffer(GL_BACK);
371 glDrawBuffer(GL_LEFT);
375 glDrawBuffer(GL_RIGHT);
389 glDrawBuffer(GL_BACK_LEFT);
393 glDrawBuffer(GL_BACK_RIGHT);
432 #ifdef BIAS_HAVE_FONTGL
433 BIASASSERT(
p_font!= NULL);
441 #endif // BIAS_HAVE_FONTGL
459 GetClientSize(&w, &h);
468 GetClientSize(&w, &h);
486 BIASERR(
"no context");
492 (GLsizei)1, (GLsizei)1,
495 (GLvoid*)(&data[0]) );
506 BIASERR(
"no context");
509 glReadPixels((GLint)pos2d.x,
511 (GLsizei)1, (GLsizei)1,
514 (GLvoid*)(&data[0]) );
524 glReadPixels((GLint)pos2d.x,
526 (GLsizei)1, (GLsizei)1,
540 GLdouble modelview[16];
541 GLdouble projection[16];
542 GLfloat wx=pos2d.x, wy=pos2d.y, wz=1;
543 GLint x=pos2d.x, y=pos2d.y;
545 glGetIntegerv(GL_VIEWPORT, viewport);
549 glGetDoublev(GL_MODELVIEW_MATRIX, modelview);
550 glGetDoublev(GL_PROJECTION_MATRIX, projection);
553 glReadPixels(x, y, 1,1, GL_DEPTH_COMPONENT, GL_FLOAT, &wz);
556 gluUnProject(wx,wy,wz,
560 &pos3d[0], &pos3d[1], &pos3d[2] );
575 const GLenum & format,
576 const bool &force8Bit )
579 if (format==GL_RGBA){
586 this->
ScreenShot(img, flipY, format, force8Bit);
588 COUT(
"DBG "<<FUNCNAME<<
" is writing "<<filename<<endl<<flush);
592 BIASERR(
"could not save "<<filename<<
" result="<<result );
601 const GLenum & format,
602 const bool &force8Bit )
607 unsigned int channels=3;
608 GLenum type = GL_UNSIGNED_BYTE;
609 unsigned int ByteDepth=1;
614 case GL_DEPTH_COMPONENT:
627 case GL_STENCIL_INDEX:
630 case GL_LUMINANCE_ALPHA:
642 BIASERR(
"unsupported format");
674 glReadPixels( GLint(0), GLint(0),
683 if (flipY) dest.
Flip();
689 static unsigned int savedNr=0;
696 GLint stencil_bits=0;
697 glGetIntegerv(GL_RED_BITS, &red_bits);
698 glGetIntegerv(GL_GREEN_BITS, &green_bits);
699 glGetIntegerv(GL_BLUE_BITS, &blue_bits);
700 glGetIntegerv(GL_ALPHA_BITS, &alpha_bits);
701 glGetIntegerv(GL_DEPTH_BITS, &depth_bits);
702 glGetIntegerv(GL_STENCIL_BITS, &stencil_bits);
706 ss<<
"out_screenshot_"
713 if ((red_bits>0) || (green_bits>0) || (blue_bits>0)) {
714 this->
ScreenShot( ss.str() +string(
"_RGB") +string(BIAS_SCREENSHOT_DEFAULT_EXTENSION),
720 this->
ScreenShot( ss.str() +string(
"_ALPHA") +string(BIAS_SCREENSHOT_DEFAULT_EXTENSION),
724 if (red_bits>0 && green_bits>0 && blue_bits>0 && alpha_bits>0) {
725 this->
ScreenShot( ss.str() +string(
"_RGBA") +string(BIAS_SCREENSHOT_DEFAULT_EXTENSION),
732 this->
ScreenShot( ss.str() +string(
"_DEPTH") +string(BIAS_SCREENSHOT_DEFAULT_EXTENSION),
733 true , GL_DEPTH_COMPONENT );
738 this->
ScreenShot( ss.str() +string(
"_STENCIL") +string(BIAS_SCREENSHOT_DEFAULT_EXTENSION),
739 true , GL_STENCIL_INDEX);
749 bool statbarOK=
false;
755 if (
p_ownerframe->GetStatusBar()->GetFieldsCount()>field){
765 field = (
p_ownerframe->GetStatusBar()->GetFieldsCount()) -1;
769 BIASERR(
"p_ownerframe has no status bar!");
780 cout<<field<<
": "<<text<<endl;
803 s<<
"("<<pos.x<<
";"<<pos.y<<
") ";
805 <<std::setprecision(5)<<rgbGL[0]<<
";"
806 <<std::setprecision(5)<<rgbGL[1]<<
";"
807 <<std::setprecision(5)<<rgbGL[2]<<
";"
808 <<std::setprecision(5)<<rgbGL[3]<<
") ";
810 s<<
"depthGL="<<std::setprecision(5)<<depthGL<<
" ";
813 s<<
"["<<p3d[0]<<
";"<<p3d[1]<<
";"<<p3d[2]<<
"] ";
827 const float & minUpdateIntervalSec,
828 const unsigned int & minNewFrames,
829 const unsigned int nField)
831 static float fps=0.0f;
832 static clock_t start=clock();
833 static unsigned int startFrameNo = framecounter;
836 unsigned int nNewFrames = framecounter - startFrameNo;
840 }
else if (nNewFrames<minNewFrames) {
846 clock_t stop=clock();
847 clock_t duration = stop -start;
849 if (
float(duration)==0){
852 }
else if (duration>=
float(CLOCKS_PER_SEC)*minUpdateIntervalSec){
858 fps = (float)nNewFrames *
float(CLOCKS_PER_SEC)/(float)duration;
865 startFrameNo = framecounter;
872 <<
"("<<framecounter<<
")."
889 style = style | wxFULLSCREEN_ALL;
894 |OpenGLCanvasBase_DEFAULT_FULLSCREEN_STYLE
937 os<<
"key function assignment:"<<endl
938 <<
"F1\t this help"<<endl
939 <<
"F10, INSERT, p\t Screenshot"<<endl
940 <<
"z,y\tPOINT rendering mode"<<endl
941 <<
"x\tLINE polygon rendering mode (wireframe)"<<endl
942 <<
"c\tFILL polygon rendering mode (vertices)"<<endl
943 <<
"v\tFILL front, LINE back polygon rendering mode"<<endl
944 <<
"Z,Y\tSMOOTH shading mode"<<endl
945 <<
"X\tFLAT shading mode"<<endl
946 <<
"i\tInformation, e.g. on OpenGL status"<<endl
947 <<
"k\t toggle culling back sides"<<endl
948 <<
"K\t toggle culling front sides"<<endl
949 <<
"L\t toggle culling front and back sides, "<<endl
950 <<
" \t =draw only LINES and POINTS, no faces at all."<<endl
951 <<
"]\t increase pointsize (++)"<<endl
952 <<
"[\t decrease pointsize (--)"<<endl
953 <<
"s\tstop animation (halt)"<<endl
954 <<
"a\tanimate=run animation"<<endl
955 <<
"f,F\ttoggle Fullscreen modus (of the frame)"<<endl
956 <<
"V\ttoggle VSYNC swap control on/off"<<endl
965 switch (event.GetKeyCode())
971 ::wxMessageBox(
AsciiToWx(ss.str()), wxT(
"Help"), wxICON_INFORMATION | wxOK );
974 #ifdef BIAS_HAVE_STEREOI
979 #endif // BIAS_HAVE_STEREOI
1041 SetStatusText(wxT(
"enabled culling BACK sides (clockwise ordering) "));
1053 SetStatusText(wxT(
"enabled culling FRONT sides (counterclockwise ordering) "));
1066 SetStatusText(wxT(
"warning: Culling front and back. No faces are drawn, only LINES and POINTS are drawn, no polygones!"));
1075 cout<<ss.str()<<endl;
1078 ::wxMessageBox(wxT(
"see cout for information on current OpenGL status."), wxT(
"Information"), wxICON_INFORMATION | wxOK );
1119 msg=
"toggling failed. still: ";
1120 BIASERR(
"toggling VSync failed. Please verify: "<<endl
1121 <<
" -application controlled vsync should be set in driver tab on WIN32"<<endl
1122 <<
" -Nvidia Stereo driver may override vsync (for shutter glasses)"<<endl );
1125 msg+=
"VSYNC enabled";
1127 msg+=
"VSYNC disable";
1170 time = double(clock()) / double(CLOCKS_PER_SEC);
1180 #ifdef BIAS_HAVE_STEREOI
1183 if (g_pStereoAPI!=NULL){
1184 BIASERR(
"StereoI API already initialized through g_pStereoAPI."<<endl
1185 <<
"\tinterface creation should be called once, only. aborting." );
1189 BIASASSERT(g_pStereoAPI==NULL);
1190 if(!CreateStereoI(&g_pStereoAPI)){
1191 BIASERR(
"CreateStereoI failed. Did you install NVStereo and Nvidia GPU drivers? aborting.");
1192 g_pStereoAPI = NULL;
1195 BIASASSERT(g_pStereoAPI!=NULL);
1196 if (g_pStereoAPI->GetStereoState() == STEREO_STATE_DISABLED){
1197 BIASERR(
"Stereo is currently DISABLED. Did you install enable NVStereo in the driver control panel?");
1210 StereoI* Get_pStereoAPI(){
1211 if (g_pStereoAPI==NULL)
1213 if (g_pStereoAPI==NULL){
1214 BIASERR(
"StereoI API not avaible through g_pStereoAPI.");
1217 BIASASSERT(g_pStereoAPI!=NULL);
1218 return g_pStereoAPI;
1224 if (Get_pStereoAPI()==NULL)
1227 BIASERR(
"StereoI API not avaible through g_pStereoAPI.");
1230 os<<
"*StereoI state is:"<<endl;
1232 os<<
"CheckAPIVersion >=1: "<<bool(Get_pStereoAPI()->CheckAPIVersion(1)>=0)<<endl;
1234 tmp=Get_pStereoAPI()->GetStereoState();
1236 case STEREO_STATE_ENABLED:
1237 os<<
"STEREO_STATE_ENABLED"<<endl;
1239 case STEREO_STATE_DISABLED:
1240 os<<
"STEREO_STATE_DISABLED"<<endl;
1244 ss<<
"STEREO_STATE="<<tmp<<
" is unknown.";
1249 os<<
"Separation: "<<Get_pStereoAPI()->GetSeparation()<<endl;
1250 os<<
"Convergence: "<<Get_pStereoAPI()->GetConvergence()<<endl;
1257 if (Get_pStereoAPI()==NULL) {
1258 BIASERR(
"StereoI API not avaible through g_pStereoAPI.");
1261 return Get_pStereoAPI()->GetStereoState();
1265 if (Get_pStereoAPI()==NULL) {
1266 BIASERR(
"StereoI API not avaible through g_pStereoAPI.");
1269 return Get_pStereoAPI()->SetStereoState(val);
1273 if (Get_pStereoAPI()==NULL) {
1274 BIASERR(
"StereoI API not avaible through g_pStereoAPI.");
1277 return Get_pStereoAPI()->GetSeparation();
1281 if (Get_pStereoAPI()==NULL) {
1282 BIASERR(
"StereoI API not avaible through g_pStereoAPI.");
1285 return Get_pStereoAPI()->SetSeparation(val);
1289 if (Get_pStereoAPI()==NULL) {
1290 BIASERR(
"StereoI API not avaible through g_pStereoAPI.");
1293 return Get_pStereoAPI()->GetConvergence();
1297 if (Get_pStereoAPI()==NULL) {
1298 BIASERR(
"StereoI API not avaible through g_pStereoAPI.");
1301 return Get_pStereoAPI()->SetConvergence(val);
1304 const int & quality)
1306 if (Get_pStereoAPI()==NULL) {
1307 BIASERR(
"StereoI API not avaible through g_pStereoAPI.");
1310 return Get_pStereoAPI()->CaptureStereoImage(format, quality);
1312 #endif // BIAS_HAVE_STEREOI
static float StereoI_SetConvergence(const float &val)
virtual void DisplayRenderMode()
display default shade model, polygon mode,
BIAS::Vector3< float > GetPixelValueRGB(const wxPoint &pos2d)
void Set(const T *pv)
copy the array of vectorsize beginning at *T to this->data_
virtual void GetInfoGL(std::ostream &os)
return GL state information
static float StereoI_GetSeparation()
unsigned long framecount_
static void StereoI_GetInfo(std::ostream &os=std::cout)
query status information on current NVStereo rendering state JW
static bool animationUpdateEnabled
unsigned int GetDepth() const
returns the bytes per channel, which is the sizeof(StorageType) Should match GetSizeDepth(GetStorageT...
wxString AsciiToWx(const char *thestring)
Converts a C string to a wxString.
static int StereoI_GetStereoState()
wrappers for StereoI interface
bool IsEmpty() const
check if ImageData_ points to allocated image buffer or not
virtual void InitMembers()
any constructor should call this function to initialize all data members (e.g.
static void DisplayCoordCross(const float &size=1.0f, const float &linewidth=1.0f)
draw a coord cross as three lines in r,g,b
virtual void OnChar(wxKeyEvent &event)
void SwapBuffers()
expands parents SwapBUffers by automatic framecount increase
static void GetInfoGL(std::ostream &os)
print all GL state informatino to os including displaylist, texobj and matric info ...
static int StereoI_SetStereoState(const int &val)
static void DisplayRenderMode(const BIAS::RenderModeGL &m)
bool IsContextAvailable()
activates the current rendereing context if there is one and
static int GetVSyncInterval()
query status of vsync/swap control interval=VSYNC JW
void Set(const T &scalar)
set all elements to a scalat value
void ScreenShot()
JW automatic screenshot to file with filename based on frame, instance etc.
BIAS::RenderModeGL rendermode
GLenum cullface
glCullFace: GL_FRONT, GL_BACK, and GL_FRONT_AND_BACK
void OnSize(wxSizeEvent &event)
static void DisplayClear(const GLbitfield &mask=CLEAR_DEFAULT_MASK)
utility functions
void OnEraseBackground(wxEraseEvent &event)
void DisplaySelectBuffer()
useful for quadbuffered stereo, e.g. shutetr glasses
float GetPixelValueDepth(const wxPoint &pos2d) const
unsigned int GetWidth() const
BIAS::Vector3< float > Unproject(const wxPoint &pos2d, double &d)
computes 3D coordinate from 2D+Z by "unprojection" JW
void ShowFPS()
simple interface for the above
static int StereoI_Init()
interace to NVidias consumer Stereo NVStereo driver API.
int InitFont(void *handleDC, const std::string theFontname=std::string(DrawTextGL_DEFAULT_FONTNAME), const int fontHeight=DrawTextGL_DEFAULT_FONTSIZE)
set the font up
static bool ToggleVSync()
try to toggle VSync setting.
unsigned int GetHeight() const
GLenum polygonmodeBACK
glPoygonMode(GL_BACK...
const void * GetImageData() const
static void StereoI_CaptureStereoImage(const int &format, const int &quality)
virtual void InitWithContext()
init all things that need a context.
void DisplayCoordCross(const float &size=1.0f, const float &linewidth=1.0f)
GLenum shademodel
glShadeModel(...
static unsigned int instancesTotalNo_
static GlutInitWrapper * GetInstance()
base class for OpenGL canvases you could derive from to get base functionality (JW) ...
GLenum polygonmodeFRONT
glPoygonMode(GL_FRONT...
unsigned int GetWidth() const
unsigned int GetChannelCount() const
returns the number of Color channels, e.g.
BIAS::DrawTextGL * p_font
static std::string LeadingZeroString(const int &n, const unsigned int &digits=DEFAULT_LEADING_ZEROS)
Create a string with leading zeroes from number.
float GetAspectRatio() const
GetAspectRatio is X/Y.
unsigned int GetHeight() const
bool InitWithContextDone_
virtual void DisplayCalls()
void SetOwnerframe(wxFrame *ptr)
static bool ToggleIP(bool &value)
helper toggling a bool value in place
static int Save(const std::string &filename, const ImageBase &img, const enum TFileFormat FileFormat=FF_auto, const bool sync=BIAS_DEFAULT_SYNC, const int c_jpeg_quality=BIAS_DEFAULT_IMAGE_QUALITY, const bool forceNewID=BIAS_DEFAULT_FORCENEWID, const bool &writeMetaData=true)
Export image as file using extrnal libs.
static void SetVSyncInterval(const int &interval)
set the vsync/swap control interval JW
void SetStatusText(const wxString &text, const int c_field=0)
let an OpenGL canvas use the status bar of the containing frame and cout as fallback (JW) ...
void Release(const bool reset_storage_type=false)
Free the allocated data structures Hands off: Do !!NOT!! change the default of reset_storage_type: Im...
virtual std::ostream & PrintHelp(std::ostream &os)
pop up keyboard layout help.
void ShowValue(const wxPoint &pos)
void Init(unsigned int width, unsigned int height, unsigned int nChannels=1, enum EStorageType storageType=ST_unsignedchar, const bool interleaved=true)
Initialize image size and channels.
static void DisplaySceneExampleSimple()
BIAS::Vector4< float > GetPixelValueRGBA(const wxPoint &pos2d)
(8bit) unsigned char image storage type
static float StereoI_GetConvergence()
static void DisplayCoordCrossAxesLabel(const float &size, const BIAS::DrawTextGL &font, const std::string &LabelX=std::string("x"), const std::string &LabelY=std::string("y"), const std::string &LabelZ=std::string("z"))
draw only the axes labels for a coord cross as text in r,g,b JW
static void DisplaySampleScene()
display soem very simple Gl calls to draw something at all
virtual void Reshape()
change of size and aspect ratio.
int Flip()
flips the image vertically (row order is inverted) In place function return 0 in case of success...
static float StereoI_SetSeparation(const float &val)
BIAS::DrawbufferMode drawbuffermode
virtual void UpdateTime()
sets the internal time param, by default based on realtime wall clock
virtual void DisplayClear(const GLbitfield &mask=CLEAR_DEFAULT_MASK)
clears buffers and resets virtual camera to identity
void OnPaint(wxPaintEvent &event)
EVT handlers: ---------------------------------------—.
This is the base class for images in BIAS.
OpenGLCanvasBase(wxFrame *parent, wxWindowID id=-1, const wxPoint &pos=wxDefaultPosition, const wxSize &size=wxDefaultSize, long style=0, const wxString &name=AsciiToWx("OpenGLCanvasBase"), int *attribList=NULL, const wxPalette &palette=wxNullPalette)
static void SetVSync(const bool &trueForOn)
vertical sync control on or off (VSYNC) JW
Draw text to OpenGL as 2D bitmap font.
wxFrame * GetOwnerframe()
virtual void Display()
main display call to be overridden by child classes usually displays: -InitWithContext (must be done ...