www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - What am I doing Wrong (OpenGL & SDL)

reply "Sean Campbell" <sycam.inc gmail.com> writes:
I cannot figure out what is wrong with this code and why i keep 
getting object.error access violation. the code is simple 
tutorial code for SDL and OpenGL what am i doing wrong (the 
access violation seems to be with glGenBuffers)
The Code

import std.stdio;
import derelict.opengl3.gl3;
import derelict.sdl2.sdl;

float vertices[] = [
	0.0f,  0.5f, // Vertex 1 (X, Y)
	0.5f, -0.5f, // Vertex 2 (X, Y)
	-0.5f, -0.5f  // Vertex 3 (X, Y)
];

int main(string args[]){
	DerelictSDL2.load();
	DerelictGL3.load();
	SDL_Init(SDL_INIT_EVERYTHING);
	SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, 
SDL_GL_CONTEXT_PROFILE_CORE);
	SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
	SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION,2 );
	SDL_Window* window = SDL_CreateWindow("OpenGL", 100, 100, 800, 
600, SDL_WINDOW_OPENGL);
	SDL_GLContext context = SDL_GL_CreateContext(window);
	SDL_Event windowEvent;
	GLuint vbo;
	glGenBuffers(1, &vbo); // Generate 1 buffer
	glBindBuffer(GL_ARRAY_BUFFER, vbo);
	glBufferData(GL_ARRAY_BUFFER, (vertices[0]).sizeof * 
vertices.length, vertices.ptr, GL_STATIC_DRAW);
	while (true)
	{
		if (SDL_PollEvent(&windowEvent))
		{
			if (windowEvent.type == SDL_QUIT){
				return 0;
			}else if (windowEvent.type == SDL_KEYUP && 
windowEvent.key.keysym.sym == SDLK_ESCAPE){
				return 0;
			}
			SDL_GL_SwapWindow(window);
		}
	}
	return 0;
}
Jul 04 2014
next sibling parent reply "Misu" <misugi-pwnu live.fr> writes:
Can you try to add DerelictGL3.reload(); after 
SDL_GL_CreateContext ?
Jul 04 2014
parent reply "Sean Campbell" <sycam.inc gmail.com> writes:
On Friday, 4 July 2014 at 08:02:59 UTC, Misu wrote:
 Can you try to add DerelictGL3.reload(); after 
 SDL_GL_CreateContext ?
yes this solved the problem. however why? is it a problem with the SDL binding?
Jul 04 2014
next sibling parent "safety0ff" <safety0ff.dev gmail.com> writes:
On Friday, 4 July 2014 at 09:39:49 UTC, Sean Campbell wrote:
 On Friday, 4 July 2014 at 08:02:59 UTC, Misu wrote:
 Can you try to add DerelictGL3.reload(); after 
 SDL_GL_CreateContext ?
yes this solved the problem. however why? is it a problem with the SDL binding?
No. https://github.com/DerelictOrg/DerelictGL3/blob/master/README.md
Jul 04 2014
prev sibling parent reply Mike Parker <aldacron gmail.com> writes:
On 7/4/2014 6:39 PM, Sean Campbell wrote:
 On Friday, 4 July 2014 at 08:02:59 UTC, Misu wrote:
 Can you try to add DerelictGL3.reload(); after SDL_GL_CreateContext ?
yes this solved the problem. however why? is it a problem with the SDL binding?
OpenGL on Windows requires a context be created before attempting to load any extensions or any later versions of OpenGL beyond 1.1. Although this is not an issue on other platforms, the Derelict binding makes it a requirement for consistency. DerelictGL3.load loads the DLL into memory along with the 1.0 & 1.1 function addresses. You can call that at any time, before or after creating a context. If you do not call DerelictGL3.reload, you will never load the extensions and 1.2+ functions. If you attempt to call it before creating a context, Derelict will throw an exception. --- This email is free from viruses and malware because avast! Antivirus protection is active. http://www.avast.com
Jul 04 2014
parent reply "Entity325" <lonewolf325 gmail.com> writes:
I am having a problem which is similar in appearance to the OP's, 
but it does not seem to be similar in function. When I try to 
execute any of the Gl functions in the (protected) 
DerelictGL3.gl.loadSymbols function, I get an access violation. 
Checking for null reveals that the functions are indeed null and 
therefore have not been loaded into memory.

Now for where it gets weird.

Here's the code I'm using to load DerelictGL and create an OpenGL 
context in my test program(to make sure all the libraries are 
built correctly.)

     SDL_GLContext context;

     *sdlWindow = SDL_CreateWindow("New SDL Window",
                                 SDL_WINDOWPOS_UNDEFINED,
                                 SDL_WINDOWPOS_UNDEFINED,
                                 800, 600,
                                 //SDL_WINDOW_FULLSCREEN_DESKTOP |
                                 SDL_WINDOW_OPENGL);

     context = SDL_GL_CreateContext(*sdlWindow);

     /* Turn on double buffering with a 24bit Z buffer.
      * You may need to change this to 16 or 32 for your system */
     SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
     SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, bitsPerPixel);

     GLVersion glVersion = DerelictGL.reload();
     if(glVersion > GLVersion.GL20)
     {
         writeln("Sufficient OpenGL version found.");
         writeln("Loaded OGL version is: ", glVersion);
         string glVersionStr = to!(string)(glGetString( GL_VERSION 
));
         writeln("GL version reported as ", glVersionStr);
     }
     else
     {
         writeln("Error: OpenGL version ", glVersion, " not 
supported.");
         context = null;
     }
     if(glMatrixMode is null)
         writeln("*****glMatrixMode not loaded");
     else
         writeln("*****All functions loaded properly.");

("sdlWindow" is a pointer to an SDL_Window, the reference of 
which is passed into the function doing all this so that I can 
load the window into it)

Console output for this code:

Sufficient OpenGL version found.
Loaded OGL version is: GL45
GL version reported as 4.5.0 NVIDIA 347.09
*****All functions loaded properly.

So that's good.

Now in the project that I'm actually working on, something very 
different happens.

Here's the code from that project:
[insert variable passing and assignments here, I'm trying to save 
you guys some reading.]
             // Flags tell SDL about the type of window we are 
creating.
             windowFlags = 
SDL_WINDOW_OPENGL;//|SDL_WINDOW_RESIZABLE;
             if(fullscreen)
                 windowFlags |= SDL_WINDOW_FULLSCREEN_DESKTOP;


//            SDL_SetVideoMode( sdlvideoinfo.current_w, 
sdlvideoinfo.current_h, bpp, flags );
             thisWindow = SDL_CreateWindow(startTitle.toStringz,
                                           SDL_WINDOWPOS_UNDEFINED,
                                           SDL_WINDOWPOS_UNDEFINED,
                                           startSize.x, 
startSize.y,
                                           windowFlags);
             if(thisWindow is null)
                 return false;

             glContext = SDL_GL_CreateContext(thisWindow);

     GLVersion glVersion = DerelictGL.reload();
     if(glVersion > GLVersion.GL20)
     {
         writeln("Sufficient OpenGL version found.");
         writeln("Loaded OGL version is: ", glVersion);
         string glVersionStr = to!(string)(glGetString( GL_VERSION 
));
         writeln("GL version reported as ", glVersionStr);
     }
     else
     {
         writeln("Error: OpenGL version ", glVersion, " not 
supported.");
         glContext = null;
     }
     if(glMatrixMode is null)
         writeln("*****glMatrixMode not loaded");
     else
         writeln("*****All functions loaded properly.");

My, doesn't that look familiar? That's because it wasn't working 
before, so I copied over the code from my test program, which 
includes diagnostic statements such as ouptutting the GL version 
loaded.

Console output for this code:

Sufficient OpenGL version found.
Loaded OGL version is: GL45
GL version reported as 4.5.0 NVIDIA 347.09
*****glMatrixMode not loaded

What.

As far as I know, the code on both sides is functionally the 
same. The build environments are as identical as I can reasonably 
make them, and the execution directory for the second program has 
at minimum any libraries that the first program has.

And of course, I'm loading the new OpenGL versions, so that's not 
the problem.
Feb 04 2015
parent reply "Mike Parker" <aldacron gmail.com> writes:
On Thursday, 5 February 2015 at 01:51:05 UTC, Entity325 wrote:
 I am having a problem which is similar in appearance to the 
 OP's, but it does not seem to be similar in function. When I 
 try to execute any of the Gl functions in the (protected) 
 DerelictGL3.gl.loadSymbols function, I get an access violation. 
 Checking for null reveals that the functions are indeed null 
 and therefore have not been loaded into memory.
Normally, I would suggest that you make sure you're calling DerelictGL3.load, but if you weren't, then there's no way reload or glGetString would be working. I can't do much to help you without a minimal case that reproduces the problem, so if you can try to strip it down to something manageable and open an issue at the DerelictGL3 issue tracker[1], then I can go to work on it. [1] https://github.com/DerelictOrg/DerelictGL3/issues
Feb 04 2015
parent reply "Entity325" <lonewolf325 gmail.com> writes:
I will see how much I can strip away and still reproduce the 
problem. If I find the cause before then, I'll be sure to report 
back here.
Feb 04 2015
parent reply "Entity325" <lonewolf325 gmail.com> writes:
On Thursday, 5 February 2015 at 06:07:34 UTC, Entity325 wrote:
 I will see how much I can strip away and still reproduce the 
 problem. If I find the cause before then, I'll be sure to 
 report back here.
I don't know if this is relevant, but while stripping down my code, I discovered that suddenly my test code that previously worked also no longer works. I sincerely hope this isn't a driver issue, because that'll be a small nightmare to debug.
Feb 04 2015
parent reply drug <drug2004 bk.ru> writes:
On 05.02.2015 09:57, Entity325 wrote:
 On Thursday, 5 February 2015 at 06:07:34 UTC, Entity325 wrote:
 I will see how much I can strip away and still reproduce the problem.
 If I find the cause before then, I'll be sure to report back here.
I don't know if this is relevant, but while stripping down my code, I discovered that suddenly my test code that previously worked also no longer works. I sincerely hope this isn't a driver issue, because that'll be a small nightmare to debug.
Look at this https://github.com/drug007/geoviewer/blob/master/src/sdlapp.d I used here SDL and OpenGL and it worked. Ctor of SDLApp creates SDL window with OpenGL context, may be it helps.
Feb 04 2015
parent reply "Entity325" <lonewolf325 gmail.com> writes:
On Thursday, 5 February 2015 at 07:23:15 UTC, drug wrote:
 Look at this 
 https://github.com/drug007/geoviewer/blob/master/src/sdlapp.d
 I used here SDL and OpenGL and it worked. Ctor of SDLApp 
 creates SDL window with OpenGL context, may be it helps.
Tested your code. Symbols still not being loaded, though it might be wise to borrow some of your organizational conventions. Just for kicks I had a friend try to run the executable I produced, and she said she got exactly the same output I did, so it's definitely something that gets compiled into the program. A bug report has been posted on Github, including the full source code(which I probably did completely wrong) and a screenshot of the output.
Feb 04 2015
next sibling parent drug <drug2004 bk.ru> writes:
On 05.02.2015 10:53, Entity325 wrote:
 On Thursday, 5 February 2015 at 07:23:15 UTC, drug wrote:
 Look at this
 https://github.com/drug007/geoviewer/blob/master/src/sdlapp.d
 I used here SDL and OpenGL and it worked. Ctor of SDLApp creates SDL
 window with OpenGL context, may be it helps.
Tested your code. Symbols still not being loaded, though it might be wise to borrow some of your organizational conventions. Just for kicks I had a friend try to run the executable I produced, and she said she got exactly the same output I did, so it's definitely something that gets compiled into the program. A bug report has been posted on Github, including the full source code(which I probably did completely wrong) and a screenshot of the output.
Hmm, just checked it and it works. Change dependencies in package.json on this: "dependencies": { "gl3n": "==1.0.0", "glamour": "==1.0.1", "derelict-gl3": "==1.0.12", "derelict-fi": "==1.9.0", }, and install besides SDL2 developer version of freeimage and libcurl. Unfortunately data format of server has changed and there won't be properly rendered map, but correct OpenGL context will be created undoubtly.
Feb 05 2015
prev sibling next sibling parent Mike Parker <aldacron gmail.com> writes:
On 2/5/2015 4:53 PM, Entity325 wrote:
 On Thursday, 5 February 2015 at 07:23:15 UTC, drug wrote:
 Look at this
 https://github.com/drug007/geoviewer/blob/master/src/sdlapp.d
 I used here SDL and OpenGL and it worked. Ctor of SDLApp creates SDL
 window with OpenGL context, may be it helps.
Tested your code. Symbols still not being loaded, though it might be wise to borrow some of your organizational conventions. Just for kicks I had a friend try to run the executable I produced, and she said she got exactly the same output I did, so it's definitely something that gets compiled into the program. A bug report has been posted on Github, including the full source code(which I probably did completely wrong) and a screenshot of the output.
I've already replied on github [1], but for anyone else following this thread -- the symbols actually are being loaded. The ones for DerelictGL3 anyway. It's a different problem entirely. https://github.com/DerelictOrg/DerelictGL3/issues/29
Feb 05 2015
prev sibling parent "Entity325" <lonewolf325 gmail.com> writes:
Aldacron and I have determined that I'm doing something weird 
with the imports between gl.d and gl3.d, the functions I'm trying 
to access are deprecated so the problem is less that they aren't 
loading and more that I can see them at all.
Feb 05 2015
prev sibling parent "bearophile" <bearophileHUGS lycos.com> writes:
Sean Campbell:

 I cannot figure out what is wrong with this code and why i keep 
 getting object.error access violation. the code is simple 
 tutorial code for SDL and OpenGL what am i doing wrong (the 
 access violation seems to be with glGenBuffers)
I don't know where your problem is, but you can start helping yourself with more tidy code (because this makes it more easy to fix) and adding some asserts on the pointers.
 float vertices[] = [
Better to use the D syntax.
 int main(string args[]){
 	DerelictSDL2.load();
 	DerelictGL3.load();
 	SDL_Init(SDL_INIT_EVERYTHING);
Better to add a space before the { and some blank lines to separate logically distinct pieces of your code. Also, your main perhaps could be void (and use just empty returns inside it).
 	SDL_Window* window = SDL_CreateWindow("OpenGL", 100, 100, 800, 
 600, SDL_WINDOW_OPENGL);
Perhaps it's better to use auto here.
 	glBufferData(GL_ARRAY_BUFFER, (vertices[0]).sizeof * 
 vertices.length, vertices.ptr, GL_STATIC_DRAW);
Better to define a little function that computes the bytes of an array, and call it here, it's less bug-prone. As a first step you can assert that all the pointers that should not be null in your program are not null. And then run a debugger. Bye, bearophile
Jul 04 2014