PDA

View Full Version : OpenGL and classes



wotnot
03-20-2016, 08:13 AM
Hoping for a bit of advice about structuring -

I have written an OpenGL application using OpenTK, which is mostly in one class. It has a number of functions relating to reading geometry from a file and building a display list from it. With a view to good programming practice I thought I should separate this out and put it in its own class. Accordingly I created a 'ModelBuilder' class, which takes a filename as an argument, parses the file and creates a display list. This display list is then used in the other class. Since OpenGL is a state machine I hadn't thought it would matter where I made GL calls from, but when structured this way I get a blank screen. I presume the problem is that I am creating a display list in one class and using it in another(?)

Would this be the case, and if so is there an accepted way of organising OpenGL code in classes? Not a big deal but I would like to try to get into good habits, and I get the feeling there may be a better way.

Using: OpenTK, C#, Visual Studio, Windows 10

Thanks in advance.

GClements
03-20-2016, 05:59 PM
Since OpenGL is a state machine I hadn't thought it would matter where I made GL calls from, but when structured this way I get a blank screen. I presume the problem is that I am creating a display list in one class and using it in another(?)

OpenGL doesn't know anything about your classes. All that it sees is the sequence of API calls with their parameters. If refactoring the code changes the behaviour, it's because you're no longer calling the same functions in the same order with the same parameters.

Also, it's best to avoid using OOP if you think that you might need other people to look at your code.

mamannon
03-21-2016, 04:49 AM
There may be some organization problems, if you code using OOP and define gl function pointers as class members. My OpenGL implementation is inside a DLL file (I use only OpenGL, no GLEW etc), and I found it to be best, if you define your gl function pointers, for example

PFNGLDRAWARRAYSPROC glDrawArrays;

outside all classes on a separate cpp file, and then use

extern PFNGLDRAWARRAYSPROC glDrawArrays;

on top of any file which needs that function.

wotnot
03-21-2016, 06:39 AM
I am still finding that it still doesn't work in a separate class. I have tried running pretty much identical code side-by-side, with one set of methods in the main class and a duplicate set in another class. The code in the main class works, whereas the code in the other class results in a blank screen. I could be missing something, of course, but not convinced.

It has occurred to me that using OpenTK I do not actually refer explicitly to the GLControl where things are being plotted. There is no requirement for a handle or other reference. That being the case, does the code perhaps have to reside within the same class (i.e. Form) as the GLControl?

wotnot
03-21-2016, 07:34 AM
It has occurred to me that using OpenTK I do not actually refer explicitly to the GLControl where things are being plotted.

(Well... that said, I do call glControl1.SwapBuffers(). However, I do not specify - and am not apparently required/expected to specify - the buffer that is being plotted to.)

mamannon
03-21-2016, 08:21 AM
I am still finding that it still doesn't work in a separate class. I have tried running pretty much identical code side-by-side, with one set of methods in the main class and a duplicate set in another class. The code in the main class works, whereas the code in the other class results in a blank screen. I could be missing something, of course, but not convinced.

It has occurred to me that using OpenTK I do not actually refer explicitly to the GLControl where things are being plotted. There is no requirement for a handle or other reference. That being the case, does the code perhaps have to reside within the same class (Form, in this case) as the GLControl?

Okay, I also had same experience in the past... And when you say that, I'm going to believe that there's really something which prevents using OpenGL functions defined as a member of one class by another class... However inside the same class GL functions will work. So you have two choices:

-define OpenGL function pointers as global outside all classes in a separate file and use extern keyword (if there is such a possibility with C#)

-define OpenGL function pointers as a member of one class and write wrapper functions/methods you use outside that class, when you need some GL function in another class

mamannon
03-21-2016, 09:45 AM
define OpenGL function pointers as a member of one class and write wrapper functions/methods you use outside that class, when you need some GL function in another class

Ooops, this wasn't a good idea :o Take a look:

https://www.opengl.org/wiki/Common_Mistakes#The_Object_Oriented_Language_Probl em

However, I have defined GL function pointers inside a class constructor like below

glDrawArrays=(PFNGLDRAWARRAYSPROC)wglGetProcAddres s("glDrawArrays");

and it worked. Just make them private.