Error

This is where I post various problems I had while building things, and what solved them. Usually it’ll be code related.

OpenCL

Khronos Group’s cross-platform parallel operation framework specifically designed to work on graphics cards and interoperate with OpenGL.

My OpenCL program (which in this case is using OpenGL interop), sometimes causes massive screen flicker and sometimes crashes. It also doesn’t appear to do what I designed it to do.
Be quadruple careful you aren’t writing to memory outside of the bounds you created for your buffer. Buffer offsets should be re-examined.


GLSL

OpenGL’s shader language. Also check out the official GLSL: Common Mistakes

I’m modifying GLSL uniform variables that have already been created, but the values don’t change.
The program must be active (glUseProgram) to modify uniforms.

My uniform positions/uniform vectors are moving relative to the camera, not the world.
Any 3D points or vectors that aren’t already (such as light positions) MUST be multiplied by the gl_ModelViewMatrix matrix in the shader if you want them to move relative to the world.

My shaders compiled and linked fine, but the objects are all rendering black (no shader effects).
Make sure that while the shaders and program may be correct, you have the correct handles to your shaders and programs. If two different shaders have the same handle, linking will work, but your output will not (see next two problems).

glCreateShader/glGenTextures is returning duplicate names (aka handles or IDs).
Are you changing the OpenGL context? If you don’t understand the question, you can probably safely skip to the next issue.

No, I didn’t change my OpenGL context.
Were you buildings object oriented wrappers for shaders/textures? Or are you in any way calling glDeleteShader/glDeleteTextures? This happened to me when the constructed shaders were copied to the final destination, and the original was destructed, leaving the original handle available for reuse. Either make your calls to delete manual, or change the way you construct your objects.


OpenGL

The texture appears, but is all stripy or has a rainbow pattern on it.
Use mip-mapping.

How do I turn a set of points/normals/uv coordinates into data for use in an indexed vertex array?
Some modeling programs use one point for every vertex on every face, instead of using indexed arrays to eliminate duplicates. If you want to use the highly efficient indexed vertex array buffer objects, you have to index them. Loop through every vertex/normal/uv coordinate complex (from however it was exported), and copy each into three arrays, one for vertices, one for normals, and one for uv coordinates. Additionally, add the index number that you have been accumulating in the loop to an index array. However, if before you add these values you find that an EXACT copy of the current complex point in the arrays, you just add the index to the indices array (ALL of the components must be equal, or you have to duplicate the point). The data is now setup, ready to be used as a vertex array. Note that UV coordinates are not always used, so any scripts you make need to account for this. There are also other types of data that might be associated with each point (like color). Here’s a set of scripts that can get Blender object data into an indexed data file.

My textures handles work, and the image data is getting loaded, but the textures aren’t showing up (my shaders/image system works fine though).
Make sure that, when calling gluBuild2DMipmaps or glTexImage2D, the “format” component of the call uses the symbolic constants. Though it might have worked in some cases, specifying for format the same number as supplied to the “internalFormat” was incorrect. It generated no errors and ran, but textures did not display.


Physics Programming

My rigid body object explodes within seconds of applying ANY rotational momentum at all.
The rotation matrix that is applied to the actual vertices of the body is not cumulative over multiple frames, it is completely recomputed every single frame from the momentum. Also, check that your dampers are not being applied in the wrong direction (turning them into accelerators). If it’s not either of those, make sure it’s not your timestep or integration scheme. Have a look at this site for tutorials on all of this.


File IO

When using Windows standard open file dialog (OPENFILENAME, GetOpenFileName, and all that), the application sometimes crashes when I hover over files on the desktop, usually on the second time the open file dialog is used.
This is caused by the way Windows caches tooltip info. Though there is obviously a solution, I avoided the problem because it would have taken too long to learn enough Windows programming to fix this. Just be aware the problem exists. It only applies to the Desktop, thankfully.

I want to be able to copy a whole file into a string. Not line by line, the whole thing.
In C++, there’s a very easy way to do this. Credit goes to SiCrane on Gamedev.net.

First, make sure you’re importing what you need for strings and file streams:

#include <fstream>

using std::string;
using std::ifstream;
using std::istreambuf_iterator;

Then, use a function like this:

string ReadWholeFile(string path)
{
	ifstream stream(path.c_str());
	return string((istreambuf_iterator<char>(stream)), istreambuf_iterator<char>());
}

Might want to include some error checking in there, just in case, such as if the path was bad.


General C++

My variable/function/class is clearly written and available, but the compiler can’t find it.
Make sure you’ve accounted for namespaces. Otherwise, check other aspects of scope.

Compiler says it can’t convert a parameter X to parameter Y, but they are the same type!
Templates! Make sure you’re accounting for them during this operation.

My pointers become invalid after I know they were good.
Make sure the copy constructor is moving all the components. Don’t have a copy constructor? Now would probably be a great time to have one.


Visual Studio C++

Somewhere along the line, my pointers are becoming invalid and causing crashes, but I don’t know where.
You can use the _CrtCheckMemory() function to check the status of the memory at any point. Thus, you can generate crashes earlier. Placing this function at various points, you can narrow down where the actual problem occurs to a specific line.


XCode

I’m getting “no rule to process file [shader]” warnings.
XCode thinks it’s supposed to compile them and doesn’t know how to do that for shaders. Make sure, in your target, you specify that the shader files be in the “Copy Files” directory, rather than the “Compile Sources” directory. Thanks to AtomicBird for the solution.

Stringstream won’t work correctly, integers and floats can’t be read.
As of XCode 3.2, it uses GCC 4.2 as its default compiler. Change the default compiler to GCC 4.0 to fix it.


General Code Design

I have several related functions that are all share a large amount of code, but in such a way that I can’t pull out the related code into its own function(s). How can I reduce duplicate code?
Do the generalizing in reverse. Instead of pulling out similarities, send in the differences, in the form of a callback function, or member callback function.

I’m finding that a lot of my class member functions simply pass-through the parameters to a similar function in one of my private variables.
Now would be a great time to consider friend functions. It might also be appropriate to make this variable public, but that’s less likely.

I have a class that needs to call various unrelated functions in unrelated classes that might be anywhere. It’s impractical or impossible to give the class access to all things it might need to call. What do I do?
For me, this occurred when implementing a button class. Clicking the button needed to call the function in the sound manager to lower the sound, or in the screen manager to return to the title screen. To allow a function to call arbitrary functions in other classes, you need to implement the delegates. They are not native to C++, so you have to make it yourself. Here’s a tutorial.

I’m using void pointers to call the same function in several highly-related classes, there’s gotta be a better way.
There is. This is exactly the sort of thing inheritance solves. You define a base class that stores the similarities of all your classes, and your derivative classes handle the differences. If it’s a function that MUST be different for each class, make it a pure virtual class so you will have to overload it.

Last Updated: February 10, 2010