Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 7 of 7

Thread: OpenGL and classes

  1. #1
    Junior Member Newbie
    Join Date
    Feb 2016
    Posts
    12

    OpenGL and classes

    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.

  2. #2
    Senior Member OpenGL Guru
    Join Date
    Jun 2013
    Posts
    2,926
    Quote Originally Posted by wotnot View Post
    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.

  3. #3
    Junior Member Newbie
    Join Date
    Feb 2016
    Posts
    24
    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.

  4. #4
    Junior Member Newbie
    Join Date
    Feb 2016
    Posts
    12
    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?
    Last edited by wotnot; 03-21-2016 at 08:13 AM.

  5. #5
    Junior Member Newbie
    Join Date
    Feb 2016
    Posts
    12
    Quote Originally Posted by wotnot View Post
    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.)

  6. #6
    Junior Member Newbie
    Join Date
    Feb 2016
    Posts
    24
    Quote Originally Posted by wotnot View Post
    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

  7. #7
    Junior Member Newbie
    Join Date
    Feb 2016
    Posts
    24
    Quote Originally Posted by mamannon View Post
    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 Take a look:

    https://www.opengl.org/wiki/Common_M...nguage_Problem

    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.
    Last edited by mamannon; 03-21-2016 at 09:49 AM. Reason: typo

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •