www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Using OpenGL (custom port)

reply David Butler <xadrian gmail.com> writes:
I'm a bit new to D, but seem to be mostly getting the hang of things. 
For a learning project, I decided to do something in OpenGL from scratch 
(including porting the headers).  As far as I can tell, things are 
mostly ported properly... I can create an OpenGL 2.1 context, basic 
functions work, etc.  However, I've run into a snag in creating a 3+ 
context.

I have this working in C++ and it should be a straight-forward 
conversion, and since everything but this is working I'm starting to 
suspect it may be a difference in C arrays and D arrays.

Here's the relevant area (warning, incoming code!):

// Create an old-style context
HGLRC tmpContext = wglCreateContext(m_dc);
wglMakeCurrent(m_dc, context);

int[] attribs =
[
    WGL_CONTEXT_MAJOR_VERSION_ARB, 3,
    WGL_CONTEXT_MINOR_VERSION_ARB, 0,
    WGL_CONTEXT_FLAGS_ARB, WGL_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB, 0
];

PFNWGLCREATECONTEXTATTRIBSARBPROC wglCreateContextAttribsARB = null;
wglCreateContextAttribsARB = 
cast(PFNWGLCREATECONTEXTATTRIBSARBPROC)wglGetProcAddress("wglCreateContextAttribsARB");

HGLRC newContext;

if (wglCreateContextAttribsARB != null)
{
    // Failing for some reason
    newContext = wglCreateContextAttribsARB(m_dc, cast(HANDLE)0, 
attribs.ptr);

    if (!newContext)
    {
       newContext = context;
    }
    else
    {
       wglMakeCurrent(null, null);
       wglDeleteContext(context);
       wglMakeCurrent(m_dc, newContext);
    }
}
else
{
    newContext = context;
}

assert(newContext);

// End

In the C++ version, I get a 3.0 context without an issue.  The D version 
here ends up with a null 'newContext', and an error that the context 
requested is invalid.

Is there a difference between a pointer to a D array and an int* in C? 
How do I convert between the two?  Am I even looking in the right place?

Thanks for any help!

-Dave Butler
Oct 04 2009
next sibling parent David Butler <xadrian gmail.com> writes:
Oops, I edited part of the code.  Just substitute in 'tmpContext' 
anywhere you see 'context'.
Oct 04 2009
prev sibling next sibling parent reply Moritz Warning <moritzwarning web.de> writes:
On Sun, 04 Oct 2009 19:52:12 -0400, David Butler wrote:

[..]
 Is there a difference between a pointer to a D array and an int* in C?
 How do I convert between the two?  Am I even looking in the right place?
 
You can think of a D array as a struct of a length value and a C pointer. struct Array { size_t length; void* ptr; } Use someOpenGLFunction(array.ptr) instead of someOpenGLFunction(&array) Please also keep in mind that only array literals are \0 terminated.
Oct 04 2009
parent bearophile <bearophileHUGS lycos.com> writes:
Moritz Warning:

 Please also keep in mind that only array literals are \0 terminated.
char/wchar/dchar array literals only, I think. Bye, bearophile
Oct 05 2009
prev sibling parent reply div0 <div0 users.sourceforge.net> writes:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

David Butler wrote:
 
 Is there a difference between a pointer to a D array and an int* in C?
 How do I convert between the two?  Am I even looking in the right place?
 
 Thanks for any help!
 
 -Dave Butler
There's nothing wrong with your int array. What's GetLastError tell you? According to the spec you can just pass null as the attrs anyway to get a default, try that. Only other thing I can suggest is maybe you've got the calling convention of PFNWGLCREATECONTEXTATTRIBSARBPROC wrong. But you only need to declare it in an extern (windows) block. Just out of curiosity where did you get the c headers for opengl 3? My card doesn't support it unfortunately so I've not bothered getting hold of it so far. - -- My enormous talent is exceeded only by my outrageous laziness. http://www.ssTk.co.uk -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.7 (MingW32) Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/ iD8DBQFKy4fbT9LetA9XoXwRAkUiAKCi/APnNEJDDVYx0jv6vyo03ji86wCfTXns Sa178i0Ry3U+tD1o2awy8d8= =t3Cb -----END PGP SIGNATURE-----
Oct 06 2009
parent David Butler <xadrian gmail.com> writes:
I got it working, it did turn out to be a slight error in the type 
declaration.  I had this:

typedef HGLRC function (HDC hDC, HGLRC hShareContext, in int 
*attribList) PFNWGLCREATECONTEXTATTRIBSARBPROC;

And it needed to be:

typedef extern (Windows) ...everything else...

Whoops.  But I can now successfully create a 3.0 context. Yay!

You can get the latest extension headers for OpenGL from 
http://www.opengl.org/registry/#headers (glext.h, wglext.h, glxext.h). 
You should only need two of them, depending on the platform (I think) 
and just use the standard, old-timey gl.h from your system.  glext.h is 
huge, but pretty easy to convert if you have a regex search/replace tool.





div0 wrote:
 -----BEGIN PGP SIGNED MESSAGE-----
 Hash: SHA1

 David Butler wrote:
 Is there a difference between a pointer to a D array and an int* in C?
 How do I convert between the two?  Am I even looking in the right place?

 Thanks for any help!

 -Dave Butler
There's nothing wrong with your int array. What's GetLastError tell you? According to the spec you can just pass null as the attrs anyway to get a default, try that. Only other thing I can suggest is maybe you've got the calling convention of PFNWGLCREATECONTEXTATTRIBSARBPROC wrong. But you only need to declare it in an extern (windows) block. Just out of curiosity where did you get the c headers for opengl 3? My card doesn't support it unfortunately so I've not bothered getting hold of it so far. - -- My enormous talent is exceeded only by my outrageous laziness. http://www.ssTk.co.uk -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.7 (MingW32) Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/ iD8DBQFKy4fbT9LetA9XoXwRAkUiAKCi/APnNEJDDVYx0jv6vyo03ji86wCfTXns Sa178i0Ry3U+tD1o2awy8d8= =t3Cb -----END PGP SIGNATURE-----
Oct 06 2009