Re: Compiling and linking libraries
On Wednesday, 16 November 2016 at 16:05:06 UTC, Mike Parker wrote: On Wednesday, 16 November 2016 at 14:59:40 UTC, Edwin van Leeuwen wrote: Thank you for this! Great information. So dub dynamically "add" code from the dll into the source code at runtime? Also will I ever need to learn how to use static libraries and is there a reason to?
Compiling and linking libraries
Hey all, This is a very beginner problem, but not one I know how to do on my own. Could anyone give a step-by-step guide on how to compile libraries, and then use them in my project with DUB? For example, I've been using this guide for graphics: http://www.learnopengl.com/#!Getting-started/Creating-a-window In this, it says to use CMake, configure for an IDE, etc. But I am not sure how to use this with D, and I'm not using an IDE (only a text editor and dub). I've gotten by with just copy-pasting pre-compiled binaries into my project folder, but I should learn how to use static libraries (I hope I'm using the right terminology). Thanks!
Re: Using OpenGL
On Tuesday, 4 October 2016 at 16:09:34 UTC, Darren wrote: Back again with another little problem that isn't specifically OpenGL related, but is a result of getting such code to work. I actually figured it out; my own mistakes.
Re: Using OpenGL
Back again with another little problem that isn't specifically OpenGL related, but is a result of getting such code to work. Code I'm working on: https://dfcode.wordpress.com/2016/10/04/linker-problem/ What I'm learning from: http://www.learnopengl.com/#!Getting-started/Camera, http://www.learnopengl.com/code_viewer.php?type=header&code=camera The problem is I'm trying to move camera code into a module and import it, but when I try to build I'm getting the following error messages: source\app.d(256,30): Error: function 'fpscamera.fpsCamera.processMouseMovement' is not nothrow source\app.d(243,7): Error: function 'app.mouse_callback' is nothrow yet may throw If I add nothrow to processMouseMovement, like I did for some other functions, I get the following: .dub\build\application-debug-windows-x86-dmd_2071-3EF635850CA47CC4E927BFC9336E0233\ogl.obj(ogl) Error 42: Symbol Undefined _D9fpscamera9fpsCamera13getViewMatrixMxFNdZS4gl3n6linalg21__T6MatrixTfVii4Vii4Z6Matrix .dub\build\application-debug-windows-x86-dmd_2071-3EF635850CA47CC4E927BFC9336E0233\ogl.obj(ogl) Error 42: Symbol Undefined _D9fpscamera9fpsCamera20processMouseMovementMFNbffhZv .dub\build\application-debug-windows-x86-dmd_2071-3EF635850CA47CC4E927BFC9336E0233\ogl.obj(ogl) Error 42: Symbol Undefined _D9fpscamera9fpsCamera18processMouseScrollMFNbfZv .dub\build\application-debug-windows-x86-dmd_2071-3EF635850CA47CC4E927BFC9336E0233\ogl.obj(ogl) Error 42: Symbol Undefined _D9fpscamera9fpsCamera15processKeyboardMFE9fpscamera14CameraMovementfZv .dub\build\application-debug-windows-x86-dmd_2071-3EF635850CA47CC4E927BFC9336E0233\ogl.obj(ogl) Error 42: Symbol Undefined _D9fpscamera12__ModuleInfoZ --- errorlevel 5 It seems to happen if I make both processMouseMovement and processMouseScroll nothrow to get rid of the error messages. If one or the other is nothrow, I get the nothrow message for that function.
Re: Using Libraries
Also I've been making a bit of a mess in dub apparently. I'm getting: Locally registered package gl3n ~master was not found. Please run "dub remove-local C:\Users\Darren\D stuff\opengl\lib". whenever dub gets used. Then if I run what it says I get: "Missing path to package." Is there a way to return this to a default setting?
Re: Using Libraries
On Tuesday, 20 September 2016 at 19:45:57 UTC, Karabuta wrote: On Tuesday, 20 September 2016 at 15:38:55 UTC, Darren wrote: On Tuesday, 20 September 2016 at 15:07:53 UTC, rikki cattermole wrote: Ok lets start at the very beginning... I think I need to start before that, haha. I might need more of a step-by-step guide. I'm a complete beginner to programming, not just D. I worked through Programming in D, where I was just compiling with dmd, then when I decided to learn OpenGL I seem to be using dub for everything. There have been a few libraries I've wanted to use but couldn't because they didn't have a pre-compiled binary, which is all I've been able to get working through sheer trial and error. Some sites say to use things like CMake and cygwin, but I'm uncomfortable using things I have no idea about. Dub is like a package manager for D (like what npm is to node.js). All dub libraries are hosted at code.dlang.org. When you see a library at code.dlang.org you want to use, you could either type "dub install packagename" whilst in the dub project ROOT or specify dependencies in the dub.json file. You can then run "dub run" which will take care of fetching and building dependencies/libraries from code.dlang.org (including linking and running the binary). For example, there is a web framework called vibe.d. If I want to use vide.d, I can specify dependencies as; dependencies: { "vide-d":"^0.7.29" } In my app.d file (which is available for any dub project created using "dub init projectname") I can import vibe.d using; import vide.d; void main() { ... } I can now compile and run the program with "dub run" or "dub build" to only build and link without running. Thank you! This does seem to work for packages listed on the dub page (tested it with gl3n). Would you be able to tell me how to install libraries that aren't written in D? A lot of what I need to use are written in C/C++, and I've use dub for bindings to those binaries. But what if I need to compile/build those libraries from scratch, or use/link a static library? I'm not sure if there's a simple answer to this question but I could do with guidance with how to use those libraries with D (other tutorials just focus on C++ with Visual Studio, etc).
Re: Using Libraries
On Tuesday, 20 September 2016 at 15:07:53 UTC, rikki cattermole wrote: Ok lets start at the very beginning... I think I need to start before that, haha. I might need more of a step-by-step guide. I'm a complete beginner to programming, not just D. I worked through Programming in D, where I was just compiling with dmd, then when I decided to learn OpenGL I seem to be using dub for everything. There have been a few libraries I've wanted to use but couldn't because they didn't have a pre-compiled binary, which is all I've been able to get working through sheer trial and error. Some sites say to use things like CMake and cygwin, but I'm uncomfortable using things I have no idea about.
Using Libraries
Hey, all I keep hitting roadblocks and that's mainly due to not knowing how to include libraries. So far I've been getting by with downloading .dll's and including the necessary dependencies in the dub.json file and having that build/run my project. I'm sure I'm making a mess of that, too, but it works and now I need to learn how to include static libraries (and probably understand github and other dub features). Right now, for example, I want to use the gl3n package: https://github.com/Dav1dde/gl3n What do I need in order to build libraries, and have dub include them when I import modules? Can I keep all of the libraries in one place so I'm not copy-pasting them (like in lib and bin folders that I keep seeing)? As you can tell, I'm still very new to all of this and I have no idea where to start. Thank you for your time!
Re: Using OpenGL
On Friday, 16 September 2016 at 01:54:50 UTC, Mike Parker wrote: snip Okay,I actually had GL_RGB for those two fields and it didn't work, but I guess I didn't try them again after I fixed the crash issue because now it works fine. Thanks again for the guidance!
Re: Using OpenGL
On Thursday, 15 September 2016 at 02:11:03 UTC, Mike Parker wrote: //snip Okay the crashing was my fault, more or less a copy-paste error. The program now runs but has a black rectangle where a texture should be. This is the code I'm using: https://dfcode.wordpress.com/2016/09/15/texcodewip/ (The code for the shaders is at the bottom) For comparison, this is the code I'm trying to make work: http://www.learnopengl.com/code_viewer.php?code=getting-started/textures
Re: Using OpenGL
Kind of resurrecting this thread; hope that's okay. I'm working through this tutorial: http://www.learnopengl.com/#!Getting-started/Textures It uses SOIL to load images, but I haven't seen any SOIL bindings in dub. I tried using SDL and SDL_Image but when the program ran it just crashed. I guess I was doing something wrong. While googling, the idea seemed to be to create and SDL_Surface* and pass that (or surface.pixels) as the last argument for glTexImage2D. Didn't work for me, however. Does anyone have any tips?
Re: Using OpenGL
On Saturday, 3 September 2016 at 16:07:52 UTC, Mike Parker wrote: On Saturday, 3 September 2016 at 16:01:34 UTC, Mike Parker wrote: The following compiles, runs, and shows the triangle. It's the code you posted above with the corrected call to glBufferData along with more D style (as I would write it anyway) and less C. The dynamic array! Thank you so much, I changed that on another file and it finally drew the triangle. And I ran your code and it works brilliantly. I should now be in a comfortable position to digest all this information now. Can't thank you enough. One thing I overlooked. In lines where a variable is both declared and initialized, like this one: GLFWwindow* window = glfwCreateWindow(...); I normally let the compiler use type inference as I did in the manifest constant declarations: auto window = glfwCreateWindow(...); IMO, when you're dealing with long or ugly type names, it makes the code look cleaner. Yeah, it is nicer to read. Now I wonder if I can load shaders from separate files (à la http://www.opengl-tutorial.org/beginners-tutorials/tutorial-2-the-first-triangle/).
Re: Using OpenGL
On Saturday, 3 September 2016 at 11:33:10 UTC, Mike Parker wrote: On Saturday, 3 September 2016 at 11:02:11 UTC, Lodovico Giaretta wrote: glGetShaderInfoLog(vertexShader, 512, null, &infoLog[0]); I prefer: glGetShaderInfoLog(vertexShader, 512, null, infoLog.ptr); That is a good tip to know. I went through another tutorial. Changed the source code and left out the shaders. I get another coloured background but still no triangle. I have a feeling that glBufferData(GL_ARRAY_BUFFER, cast(int)g_vertex_buffer_data.sizeof, cast(void*)g_vertex_buffer_data, GL_STATIC_DRAW); in one example, or glBufferData(GL_ARRAY_BUFFER, vertices.sizeof, cast(void*)vertices, GL_STATIC_DRAW); glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * GLfloat.sizeof, cast(GLvoid*)0); in the other might be responsible, since they seem to be related to drawing the triangle and I'm casting arguments in order to get the program to compile. (In the first line, glBufferData wants an int, and .sizeof returns a uint, apparently.) I'm sure there's something simple I'm missing but I just don't have the experience to recognise it.
Re: Using OpenGL
On Saturday, 3 September 2016 at 11:27:09 UTC, Mike Parker wrote: On Saturday, 3 September 2016 at 11:13:30 UTC, Lodovico Giaretta wrote: Ah! Well, providing error messages is always useful. Now I see your issue: your callback has D linkage, but OpenGL expects a function with C linkage. So you have to put `extern(C)` on your callback declaration. Well, it's GLFW, not OpenGL, but yes they do need to be extern (C) and also nothrow, as that is how the callback types are declared in Derrlict: ``` extern(C) nothrow void key_callback(GLFWwindow* window, int key, int scancode, int action, int mode) { if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS) glfwSetWindowShouldClose(window, GL_TRUE); } ``` Hey, it worked! Thanks a lot, I know what to do in the future now. Just need to figure out why this triangle isn't showing up and I should be well on my way.
Re: Using OpenGL
On Saturday, 3 September 2016 at 11:02:11 UTC, Lodovico Giaretta wrote: //glGetShaderInfoLog(vertexShader, 512, null, infoLog); glGetShaderInfoLog(vertexShader, 512, null, &infoLog[0]); Thank you, I knew I had to do something like this! //glfwSetKeyCallback(window, key_callback); glfwSetKeyCallback(window, &key_callback); I actually tried this before but it doesn't work. I get the following error: Error: function pointer glfwSetKeyCallback (GLFWwindow*, extern (C) void function(GLFWwindow*, int, int, int, int) nothrow) is not callable using argument types (GLFWwindow*, void function(GLFWwindow* window, int key, int scancode, int action, int mode))
Re: Using OpenGL
It's not quite in a practically-usable state yet, but the SDL2 & OpenGL wrapper I'm working on may interest you as an example implementation if nothing else. https://github.com/pineapplemachine/mach.d/tree/master/mach/sdl I'm going to take a look at this, once I get my bearings, on the merits of the name alone! I'm sure there are a few tutorials that make use of SDL2 that I can use. My hope is that once I know how to set up, learning OpenGL will be more a matter of D-ifying the C-code. I'll post the modified code I'm trying to get work in full below. Some code is commented out because I couldn't make it work. Also any tips for making the code nicer would be welcome (e.g. would I change the const GLunit into enums?). # import derelict.glfw3.glfw3; import derelict.opengl3.gl3; import std.stdio; void key_callback(GLFWwindow* window, int key, int scancode, int action, int mode) { if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS) glfwSetWindowShouldClose(window, GL_TRUE); } const GLuint WIDTH = 800, HEIGHT = 600; const GLchar* vertexShaderSource = "#version 330 core\n" "layout (location = 0) in vec3 position;\n" "void main()\n" "{\n" "gl_Position = vec4(position.x, position.y, position.z, 1.0);\n" "}\0"; const GLchar* fragmentShaderSource = "#version 330 core\n" "out vec4 color;\n" "void main()\n" "{\n" "color = vec4(1.0f, 0.5f, 0.2f, 1.0f);\n" "}\n\0"; void main() { DerelictGLFW3.load(); DerelictGL3.load(); glfwInit(); glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); glfwWindowHint(GLFW_RESIZABLE, GL_FALSE); GLFWwindow* window = glfwCreateWindow(WIDTH, HEIGHT, "LearnOpenGL", null, null); glfwMakeContextCurrent(window); DerelictGL3.reload(); //glfwSetKeyCallback(window, key_callback); int width, height; glfwGetFramebufferSize(window, &width, &height); glViewport(0, 0, width, height); GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER); glShaderSource(vertexShader, 1, &vertexShaderSource, null); glCompileShader(vertexShader); GLint success; GLchar[512] infoLog; glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &success); if (!success) { //glGetShaderInfoLog(vertexShader, 512, null, infoLog); writeln("ERROR::SHADER::VERTEX::COMPILATION_FAILED\n", infoLog); } GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER); glShaderSource(fragmentShader, 1, &fragmentShaderSource, null); glCompileShader(fragmentShader); glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &success); if (!success) { //glGetShaderInfoLog(fragmentShader, 512, null, infoLog); writeln("ERROR::SHADER::FRAGMENT::COMPILATION_FAILED\n", infoLog); } GLuint shaderProgram = glCreateProgram(); glAttachShader(shaderProgram, vertexShader); glAttachShader(shaderProgram, fragmentShader); glLinkProgram(shaderProgram); glGetProgramiv(shaderProgram, GL_LINK_STATUS, &success); if (!success) { //glGetProgramInfoLog(shaderProgram, 512, null, infoLog); writeln("ERROR::SHADER::PROGRAM::LINKING_FAILED\n", infoLog); } glDeleteShader(vertexShader); glDeleteShader(fragmentShader); GLfloat[] vertices = [ -0.5f, -0.5f, 0.0f, 0.5f, -0.5f, 0.0f, 0.0f, 0.5f, 0.0f ]; GLuint VBO, VAO; glGenVertexArrays(1, &VAO); glGenBuffers(1, &VBO); glBindVertexArray(VAO); glBindBuffer(GL_ARRAY_BUFFER, VBO); glBufferData(GL_ARRAY_BUFFER, vertices.sizeof, cast(void*)vertices, GL_STATIC_DRAW); glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * GLfloat.sizeof, cast(GLvoid*)0); glEnableVertexAttribArray(0); glBindBuffer(GL_ARRAY_BUFFER, 0); glBindVertexArray(0); while (!glfwWindowShouldClose(window)) { glfwPollEvents(); glClearColor(0.2f, 0.3f, 0.3f, 1.0f); glClear(GL_COLOR_BUFFER_BIT); glUseProgram(shaderProgram); glBindVertexArray(VAO); glDrawArrays(GL_TRIANGLES, 0, 3); glBindVertexArray(0); glfwSwapBuffers(window); } glDeleteVertexArrays(1, &VAO); glDeleteBuffers(1, &VBO); glfwTerminate(); }
Re: Using OpenGL
Thanks for the information. The errors for the tutorial I _was_ trying to make work are as follows: source\app.d(9,5): Error: undefined identifier 'Window', did you mean variable 'window'? source\app.d(98,12): Error: undefined identifier 'Window', did you mean variable 'window'? source\app.d(101,14): Error: undefined identifier 'GLBuffer' source\app.d(104,14): Error: undefined identifier 'Shader' source\app.d(107,13): Error: undefined identifier 'Program', did you mean variable 'program'? source\app.d(110,15): Error: undefined identifier 'Attribute' I thought I might have needed another package for these, and gfm seemed to contain what I need in the form of the opengl sub-package. But even after importing that, it only gets rid of the GLBuffer error. I tried to follow another tutorial. Link to source code: http://www.learnopengl.com/code_viewer.php?code=getting-started/hellotriangle I'm having more success with this one. I pretty much hacked away at this and did my best to convert from C-style code to D. The window gets made and it has the green-ish background, but it's not drawing any triangles. Should I copy/paste the code I'm using in case I made a mistake?
Using OpenGL
I'm trying to teach myself OpenGL but I'm not sure how to set it up exactly. I used "dub init" to create a project, I downloaded glew32.dll and glfw.dll and put them in this folder. I added "derelict-gl3": "~>1.0.19" and "derelict-glfw3": "~>3.1.0" to dependencies, and imported them in the app.d file. I add the load() and reload() functions where appropriate (I assume). I can run a simple window program and it seems to work fine, but I notice that's when all the functions begin with "glfw". If I try to follow a tutorial for loading a triangle, I get errors when trying to build. Do I need to link an opengl.dll file, too?
Re: Converting int to dchar?
That's a really informative response. Thank you!
Converting int to dchar?
Hey, all. I'm pretty much a programming novice, so I hope you can bear with me. Does anyone know how I can change an int into a char equivalent? e.g. int i = 5; dchar value; ? assert(value == '5'); If I try and cast it to dchar, I get messed up output, and I'm not sure how to use toChars (if that can accomplish this). I can copy+paste the little exercise I'm working on if that helps? Thanks in advance!
Re: Segfault games with factorials
On Thursday, 24 July 2014 at 14:39:12 UTC, H. S. Teoh via Digitalmars-d-learn wrote: On Thu, Jul 24, 2014 at 01:14:40PM +, Darren via Digitalmars-d-learn wrote: I have the following code in fac.d (modified from the factorial examples on RosettaCode): #!/usr/bin/rdmd import std.bigint; pure BigInt factorial(BigInt n) { static pure BigInt inner(BigInt n, BigInt acc) { return n == 0 ? acc : inner(n - 1, acc * n); } return inner(n, BigInt("1")); } void main(string[] args) { import std.stdio; BigInt input = args[1]; writeln(factorial(input)); return; } It (more or less consistently) on my machine will calculate 'fac 47610', and (more or less consistently) will core dump with a segfault on 'fac 47611'. [...] You're probably running out of stack space because of your recursive function. Write it as a loop instead, and you should be able to go farther: pure BigInt factorial(BigInt n) { auto result = BigInt(1); while (n > 1) result *= n; return result; } T It does seem that's the case. Which is odd, as I thought that DMD and LDC did TCO. Not in this case obviously. PS. This was a slightly silly program, but in the general case, is there a way to use a core dump to diagnose a stack overflow?
Segfault games with factorials
I have the following code in fac.d (modified from the factorial examples on RosettaCode): #!/usr/bin/rdmd import std.bigint; pure BigInt factorial(BigInt n) { static pure BigInt inner(BigInt n, BigInt acc) { return n == 0 ? acc : inner(n - 1, acc * n); } return inner(n, BigInt("1")); } void main(string[] args) { import std.stdio; BigInt input = args[1]; writeln(factorial(input)); return; } It (more or less consistently) on my machine will calculate 'fac 47610', and (more or less consistently) will core dump with a segfault on 'fac 47611'. Interestingly, if I redirect stdout to a file it will usually manage to get to 47612. To satisfy my own curiosity about what's happening, are there any resources I can use to analyse the core dump? Thanks.
Re: Convert a hex string into a ubyte[] or OutBuffer
On Monday, 19 May 2014 at 12:28:14 UTC, anonymous wrote: On Monday, 19 May 2014 at 11:36:43 UTC, Darren wrote: Is there an idiomatic/simple way to do that? import std.conv: parse; import std.array: array; import std.range: chunks; import std.algorithm: map; ubyte[] bytes = hexnum /* "16D8..." */ .chunks(2) /* "16", "D8", ... */ .map!(twoDigits => twoDigits.parse!ubyte(16)) /* 0x16, 0xD8, ... */ .array(); Nice, thanks! I've added a slightly edited version as the answer to this question on stackoverflow: http://stackoverflow.com/a/23741556/47481
Convert a hex string into a ubyte[] or OutBuffer
Hi, I'm trying to do something very basic with large numbers: Let's say I have a hex representation of a large number: String hexnum = "16D81B16E091F31BEF"; I'd like to convert it into a ubyte[] in order to Base64 encode it (or, indeed ASCII85 or Base32). eg, [16, D8, 1B, 16, E0, 91, F3, 1B, EF] Is there an idiomatic/simple way to do that? For example, node's Buffer class supports the following API: var buffer = new Buffer(hexnum, "hex"); It also supports "base64" and "utf8". This seems like a very nice way to get string representations of binary data into a buffer :) Is there a D equivalent? I was looking at how the uuid module does it, I can use that as a roadmap, I just wanted to check if there was a shorter way already present in the libs. Many thanks, -Darren