07.07
Starting a new project usually calls upon looking thru previously written code. I fired up a previous class I wrote 5 years ago to load a texture into OpenGL. Right away just reading the definition I recoiled in horror:
13 14 15 16 17 18 19 20 21 22 23 | @interface WFTexture : NSObject { // Bitmap data NSBitmapImageRep *bitmap; // Bitmap information double width, height; GLenum pixfmt, bpp, datatype; // Texture ID GLuint textureID; } |
After reading thru this code a pattern emerged. I was creating objects for the sake of creating objects, instead of using objects to solve a specific problem. For example, why would I need to keep around the original bitmap data after it has already been offloaded to the GPU? To a lesser extent why would I need to keep around the bpp or pixel format of the data offloaded aside from out right wasting system resources.
I then started to look at the implementation which I will snip for brevity:
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 | - (NSBitmapImageRep *) bitmap { return bitmap; } - (void) setBitmap: (NSBitmapImageRep *) newBitmap { [newBitmap retain]; [bitmap release]; bitmap = newBitmap; } - (float) width { return width; } - (float) height { return height; } - (GLenum) bpp { return bpp; } - (GLenum) datatype { return datatype; } - (GLuint) textureID { return textureID; } - (void) setTextureID: (GLuint) texid { textureID = texid; } |
The implementation makes me certain I was thinking too much about the object and not enough about the problem it is used to solve. Aside from being able to access various bits of information about the original image data (bpp, width, height, datatype) I created set and get methods for both the bitmap data and the OpenGL texture ID.
If this was supposed to be a reusable object giving access to setting the texture ID would have be atrocious. Instead of changing the ID, a whole new texture object should be created. Also take note that once the bitmap was set to something new, no OpenGL texture data was uploaded. If I was really bent on storing the bitmap data in memory as well in VRAM, then bent on allowing the changing of this bitmap data, the set method should at least re-upload the new bitmap data to the graphics card. But I digress.
The only bit of code I thought about snagging from this entire object was this:
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 | - (void) on { if ( width != height ) { glEnable(GL_TEXTURE_RECTANGLE_EXT); glBindTexture(GL_TEXTURE_RECTANGLE_EXT, textureID); } else { glEnable(GL_TEXTURE_2D); glBindTexture(GL_TEXTURE_2D, textureID); } } - (void) off { if ( width != height ) glDisable(GL_TEXTURE_RECTANGLE_EXT); else glDisable(GL_TEXTURE_2D); } |
But I thought otherwise when I realized this code makes way to many state changes. Objective-C is very object intensive and you are rewarded for creating proper objects, but it makes it very easy to waste resources. Going over old code I wrote was a great learning exercise for me, and it drove home the point that I need to only write methods and properties as I use them.
No Comment.
Add Your Comment