Overview
Examples
Screenshots
Comparisons
Applications
Download
Documentation
Tutorials
Bazaar
Status & Roadmap
FAQ
Authors & License
Forums
Funding Ultimate++
Search on this site
Search in forums












SourceForge.net Logo
Home » U++ Library support » U++ Widgets - General questions or Mixed problems » [GLCtrl] Adding an Initilisation function to GLPaint
[GLCtrl] Adding an Initilisation function to GLPaint [message #53115] Tue, 03 March 2020 11:32 Go to next message
Xemuth is currently offline  Xemuth
Messages: 387
Registered: August 2018
Location: France
Senior Member
Hello community,

When we have to deal with OpenGL, it is often interesting to load a certain amount of data (Texture / Vertex / Shaders) before rendering a scene.

Nowaday, to load thoses things, I must declare a boolean in my derived class of GLCtrl and use it to perform many load at the first loop in GLPaint, as Follow :
	bool isLoaded = false;
	virtual void GLPaint() {
		if(!isLoaded){
			/* Loading Few things  ! */
			isLoaded=true;
		}
		
		/* Code to perform draw */
		Refresh();
	}


Even if this way of working is functional, Having to write many and many code and routine of loading in GLPaint() is annoying and don't help in code readability.

That's why I started to dig out the GLCtrl to implement a virtual function called Initialisation() wich can be overwritted. The purpose of it is to be called right after OpenGL context Initialisation. My first way of doing this was Adding the call of function here (GLCtrl.cpp line 33):
void GLCtrl::Init()
{
	Transparent();
#ifdef PLATFORM_WIN32
	pane.ctrl = this;
	Add(pane.SizePos());
#endif
	restore_gl_viewport__ = SetCurrentViewport;
	Initialisation(); //--> I call Initialisation Here 
}

But (It seems) due to Ctrl creation, the function wich is called at this moment is the non overwritted Initialisation() from GLCtrl and not the one I have overwritted in my derived class from GLCtrl.

So my next try was to implement it here (Win32GLCtrl.cpp) :
void GLCtrl::GLPane::State(int reason)
{
	DHCtrl::State(reason);
	
	if(reason == OPEN) {
		HWND hwnd = GetHWND();
		CreateContext();
		HDC hDC = GetDC(hwnd);
		if(!SetPixelFormat(hDC, s_pixelFormatID, &s_pfd))
			return;
		ReleaseDC(hwnd, hDC);
		static_cast<GLCtrl*>(parent)->Initialisation();//--> I call Initialisation Here
	}
}

This time, it call the overwritted Initialisation() but OpenGL Context is not yet well initialised, consequence is every call to OpenGL api (all call to allocate some memory in vram) fail !

So far I dig, I didn't find the proper way of adding this function, so I need your help ! If someone have an idea...

Thanks in advance.
Best regard

Xemuth

[Updated on: Wed, 04 March 2020 08:40]

Report message to a moderator

Re: [GLCtrl] Adding an Initilisation function to GLPaint [message #53124 is a reply to message #53115] Wed, 04 March 2020 13:06 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 13975
Registered: November 2005
Ultimate Member
I think the problem is that context is created but not associated with current thread.

Try with ExecuteGL method...

That said, doing this in GLPaint actually seems fine to me.

Mirek
Re: [GLCtrl] Adding an Initilisation function to GLPaint [message #53125 is a reply to message #53124] Wed, 04 March 2020 13:28 Go to previous messageGo to next message
Xemuth is currently offline  Xemuth
Messages: 387
Registered: August 2018
Location: France
Senior Member
mirek wrote on Wed, 04 March 2020 13:06
I think the problem is that context is created but not associated with current thread.

Try with ExecuteGL method...



Hello Mirek, you are right, doing this kind of thing work :
void GLCtrl::GLPane::ExecuteGL(HDC hDC, Event<> paint, bool swap_buffers)
{
	static bool HaveBeenInitialised = false;
	wglMakeCurrent(hDC, s_openGLContext);
	if(!HaveBeenInitialised){
		static_cast<GLCtrl*>(parent)->Initialisation();//--> I call Initialisation Here
		HaveBeenInitialised = true;
	}
	paint();
	if(swap_buffers)
		SwapBuffers(hDC);
	else
		glFlush();
	wglMakeCurrent(NULL, NULL);
	
}


mirek wrote on Wed, 04 March 2020 13:06
That said, doing this in GLPaint actually seems fine to me.

Yes, it work aswell but the idea of having a visible boolean just to do some action at the first loop of drawing is disturbing me Twisted Evil (the fix I did in ExecuteGL(...) is basicly the same things but user don't see it Very Happy Is it possible to add it to the GLCtrl code or you prefer stay like that ?

Thanks in advance
Best regard !
Re: [GLCtrl] Adding an Initilisation function to GLPaint [message #53133 is a reply to message #53125] Fri, 06 March 2020 10:28 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 13975
Registered: November 2005
Ultimate Member
Xemuth wrote on Wed, 04 March 2020 13:28
mirek wrote on Wed, 04 March 2020 13:06
I think the problem is that context is created but not associated with current thread.

Try with ExecuteGL method...



Hello Mirek, you are right, doing this kind of thing work :
void GLCtrl::GLPane::ExecuteGL(HDC hDC, Event<> paint, bool swap_buffers)
{
	static bool HaveBeenInitialised = false;
	wglMakeCurrent(hDC, s_openGLContext);
	if(!HaveBeenInitialised){
		static_cast<GLCtrl*>(parent)->Initialisation();//--> I call Initialisation Here
		HaveBeenInitialised = true;
	}
	paint();
	if(swap_buffers)
		SwapBuffers(hDC);
	else
		glFlush();
	wglMakeCurrent(NULL, NULL);
	
}


mirek wrote on Wed, 04 March 2020 13:06
That said, doing this in GLPaint actually seems fine to me.

Yes, it work aswell but the idea of having a visible boolean just to do some action at the first loop of drawing is disturbing me Twisted Evil (the fix I did in ExecuteGL(...) is basicly the same things but user don't see it Very Happy Is it possible to add it to the GLCtrl code or you prefer stay like that ?

Thanks in advance
Best regard !


For I prefer it to stay as it is....
Re: [GLCtrl] Adding an Initilisation function to GLPaint [message #53136 is a reply to message #53133] Fri, 06 March 2020 14:20 Go to previous message
koldo is currently offline  koldo
Messages: 3354
Registered: August 2008
Senior Veteran
Do not worry Xemuth. With your control totally developed, GLPaint will be scarcely used.

Best regards
IƱaki
Previous Topic: [SOLVED]Removing glaux lib from GLctrl
Next Topic: How to draw random images in ArrayCtrl-cell using Display? [SOLVED]
Goto Forum:
  


Current Time: Thu Mar 28 13:14:20 CET 2024

Total time taken to generate the page: 0.00892 seconds