SOIL OpenGL Library crashes inside classes - opengl

I'm working on a simple 3D model loader using OpenGL and I wanted to add texture support. However, not wanting to needlessly rewrite an image library, I downloaded SOIL (Simple OpenGL Image Library) that I learned about in the NeHe tutorial on texture mapping. It has a few very useful functions that take care of loading images of many times and passing them to OpenGL.
The problem is this: In the NeHe tutorial code, the SOIL function
SOIL_load_OGL_texture(char *, param,param,param)
exactly as promised and the polygons are mapped with the correct picture. However, I've made a class that loads OBJ and MTL files (3D model files). When I try to use the SOIL function to load a texture inside this class, my program crashes.
I am using GNU C++ with Code::Blocks (I also tried this code in Dev-C++)
I receive no errors or warnings from the compiler with -g -Wall.
I have linked and included everything properly in my project (if I copy/paste the NeHe code, it runs properly).
The SOIL command does not work inside any class (I tried making another class that only executed the SOIL function and I got the same problem).
The file paths passed to the function are all correct. I have tried relative and absolute, with all formats of forward and back slashes. I copied and pasted absolute paths that worked in the NeHe code to my code and it does not work.
The rest of my code works fine - I can get my shape rendered with its ambient/diffuse/etc. colors and other settings applied properly.

Sounds almost like you don't have a valid OpenGL context at the time when you're calling that function.
Are you certain that SOIL is called only after your opengl context is initialized? (e.g. no static construction of objects that are constructed at program initialization). A quick way to check this is to put a breakpoint on your opengl initialization and see if you hit the BP or if your program crashes first.
Also, make sure that SOIL call is on the same thread as your opengl context.

Related

How to implement 3d texturing in OpenGL 3.3

So I have just realized that the code I was working on for 3d textures was for OpenGL 1.1 or something and is no longer supported in OpenGL 3.3. Is there another way to do this without glTexture3D? Perhaps through a library or another function in OpenGL 3.3 that I do not know about?
EDIT:
I am not sure where I read that 3d texturing was taken out of OpenGL in newer versions (been googling a lot today), but consider this:
I have been following the tutorial/guide here. The program compiles without a hitch. Now read the following quote from the article:
The potential exists that the environment the program is being run on does not support 3D texturing, which would cause us to get a NULL address back, and attempting to use a NULL pointer is A Bad Thing so make sure to check for it and respond appropriately (the provided example exits with an error).
That quote is referring to the following function:
glTexImage3D = (PFNGLTEXIMAGE3DPROC) wglGetProcAddress("glTexImage3D");
When running my program on my computer (which has OpenGL 3.3) that same function returns null for me. When my friend runs it on his computer (which has OpenGL 1.2) it does not return null.
The way one uploads 3D textures has not changes since OpenGL-1.2. The functions for this are still named
glTexImage3D
glTexSubImage3D
glCopyTexSubImage3D

BMP texture doesn't show

I've encountered a problem whilst learning how to texture in OpenGL (from OpenGL 5th Edition Bible). The problem is that I can't display my texture. I load it from a BMP file with a function I've written myself.
Since code is quite lengthy I uploaded all neccessery files. You can download it from here.
EDIT: I've tried copying code from the mentioned book (TGA loading described there) and it doesn't work as well. It seems that loading stucks at loading image data from file (strange). I suppose it isn't relevant as far as my problem is concern.
There are general debug steps for checking for problems. I will list them below in the hope you might find the bug (and for future readers):
Does the file exist? Is the file being read properly according to the format? Did you get it to gel well with OpenGL? My suggestion: Do not mess with these things yourself. Use a library. There's an amazingly light weight SOIL (Simple OpenGL Image library)
Are you loading the textures before initialising OpenGL? This was always my problem because I used to "load" and do a glBindTexture(..) in the constructor (of some class). The objects were created before OpenGL was initialised.
Have you enabled GL_TEXTURE_2D? Check the initialise code, use: glEnable(GL_TEXTURE_2D)
Did you map the textures properly with the quad or the shape you want it on? Expanding on this here is (sort of) out of scope. There are great tutorials out there.
(Would seem stupid, but happens sometimes) Are you blacking out the object by glColor3f(0,0,0) ?
There could be more checks, but these are the ones hitting my head now

no-render with OpenGL --> contexts

i have a program that does some GPU computing with Optional OpenGL rendering.
The use dynamic is as follow:
init function (init GLEW being the most relevant).
load mesh from file to GPU (use glGenBuffers are related functions to make VBO).
process this mesh in parallel (GPU Computing API).
save mesh into file.
my problem is that when mesh is loading i use opengl calls and wihout context created i just
get segmentation fault.
Edit: Evolution of the problem:
I was missing GL/glx.h I thought that GL/glxew.h included it, thanks to the answers that got fixed.
I was missing glXMakeCurrent; and therefore it was having zero contexts.
After this fixes, it works :).
also thanks for the tools suggestions, i would gladly use them it is just that i needed the low level code for this particular case.
i tried making a context with this code ( i am using glew, so i change the header to GL/glxew.h but the rest of this code remains the same)
Don'd do it. glxew is used for loading glx functions. You probably don't need it.
If you want to use GLEW, replace GL/gl.h with GL/glew.h leave GL/glx.h as it is.
X11 and GLX are quite complex, consider using sdl of glfw instead.
Just wildly guessing here, but could it be that GLEW redefined glXChooseFBConfig with something custom? Something in the call of glXChooseFBConfig dereferences an invalid pointer. So either glXChooseFBConfig itself is invalid, or fbcount to so small, or visual_attribs not properly terminated.
GLEW has nothing to do with context creation. It is an OpenGL loading library; it loads OpenGL functions. It needs you to have an OpenGL context in order for it to function.
Since you're not really using this context for drawing stuff, I would suggest using an off-the-shelf tool for context creation. GLFW or FreeGLUT would be the most light-weight alternatives. Just use them to create a context, do what you need to do, then destroy the windows they create.

Rendering Wavefront OBJ and associated material MTL using OpenGL

I have these files: sample.obj, sample.mtl and a folder sample containing image jpg. Can somebody give me an example code of opengl texture mapping using above input.
Your problem is not just texture mapping. What you are asking for is loading an Wavefront OBJ model and its material library, then passing this data to OpenGL rendering commands. Depending on how many preexisting code you want (not) to use this takes different amounts of code. However there's no such thing like a simple OpenGL function that does it all in a black box.
Although this is a FAQ and Google brings you some results I didn't find a tutorial that really explains what's going on.
If you want it to be really simple, use a Wavefront loader and render library:
http://www.evl.uic.edu/pape/sw/libwave.html

Importing 3D objects and its animation to iphone openGL

I am trying to develop a 3D game in openGL and i need to create many 3D objects.. I am a begginner in openGL.. I have tried with many 3D softwares like Blender , MODO, Unity 3D and Cheetah.
I am easily able to create my objects with these and exporting as Wavefront .OBJ, and converting it to a header file using a perl script. This header file is added to my openGL project..
The 3D objects are seen, but its not perfect. The script i used is to convert the .OBJ to .h using TRIANGLES.. And the object is seen with triangles. Its not full.. No way when i used TRIANGLE STRIP,FAN..? Problems with the vertices..
Is the problem with my Script or is it the wrong way i have gone..?? Or is there any other best ways to directly import 3D objects to openGL..??
The below link is the best one which you can get for 3D objects to openGL.. i got the scripts from these..
http://www.heikobehrens.net/2009/08/27/obj2opengl/
please help..
You don't want to go that way. Direct drawing mode (using TRIANGLE and friends) is extremely slow in OpenGL.
Instead, you should pick a decent format and write a loader for it (or use one found on the web). Good formats would be 3ds, obj if gzipped, collada.
Here's an example tutorial on loading from Milkshape files.
Once you load your objects programatically, you can use Vertex Arrays, or even better VBO's to display them. This is waaay faster.
Google for a mesh loader for your favorite format, or write one yourself.
I have written a reader/renderer for AC3D files that works fine on the iPhone (OpenGL ES)
Feel free to have a look at it here.
There is also an obj loader by Jeff Lamarche at google code.
AC3D can reduce the triangle count pretty good and as an alternative I ported QVis to the mac. My reader/renderer also tries to build tri-strips.
About VBO's. I have not seen any gained performance when using them in the iPhone. I'm not the only one.