This will be a long post so bear with me!
I have the following code to load a bitmap using SDL_Image:
void hbBaseMaterial::Load() { HBboolean alpha = HB_TRUE; HBint x, y; HBint pos = 0; mSurface = IMG_Load(mFilename.c_str()); if (mSurface == NULL) { HBstring error = "Error loading material " + mFilename; throw hbBaseException(error); } int dim = mSurface->w * mSurface->h * ((alpha) ? 4 : 3); mData = new HBubyte[dim]; if(!mData) { throw hbBaseException("Error creating texture."); } for(y=(mSurface->h-1); y>-1; y--) { for(x=0; x<mSurface->w; x++) { Uint8 r,g,b,a; // Note about getpixel function. It's a simple function // taken from SDL's manuals. It just returns the color // of specifig pixel in Uint32 format. Just ask me // if you want me to show the function. Uint32 color = getpixel(mSurface, x,y); if(!alpha) SDL_GetRGB(color, mSurface->format, &r,&g,&b); else SDL_GetRGBA(color, mSurface->format, &r,&g,&b,&a); mData[pos] = r; pos++; mData[pos] = g; pos++; mData[pos] = b; pos++; if(alpha) { mData[pos] = a; pos++; } } } }
The following code sets up OpenGL initially:
glClearColor(0.0f, 0.0f, 0.0f, 0.0f); glClearDepth(1.0); //glDepthFunc(GL_LESS); //glEnable(GL_DEPTH_TEST); //glShadeModel(GL_SMOOTH); //glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); glViewport(0,0,mWidth, mHeight); glMatrixMode(GL_PROJECTION); glLoadIdentity();
gluPerspective(45.0f, (GLfloat)mWidth/(GLfloat)mHeight, 0.1f, 100.0f); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glEnable(GL_TEXTURE_2D);
The following code sets up for 2D rendering:
// Setup the projection for 2D. glMatrixMode(GL_PROJECTION); glPushMatrix(); glLoadIdentity(); glOrtho(0, mWidth, mHeight, 0, 1.0, -1.0); // Initialise the model matrix. glMatrixMode(GL_MODELVIEW); glPushMatrix(); glLoadIdentity(); // Draw on top of everything. glDisable(GL_DEPTH_TEST);
The following code ends the 2D rendering:
glEnable(GL_DEPTH_TEST); // Restore the projection matrix. glMatrixMode(GL_PROJECTION); glPopMatrix(); glMatrixMode(GL_MODELVIEW); glPopMatrix();
Now I can get a white rectangle displayed on the screen, using this code:
HBint = mMaterial->GetTextureId(); HBubyte* texture = (HBubyte *)mMaterial->GetData(); glBindTexture(GL_TEXTURE_2D, id); glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, mSizeX, mSizeY, 0, GL_RGBA, GL_UNSIGNED_BYTE, texture); glBegin(GL_QUADS); glTexCoord2i(mPosX, mPosY); glVertex2i(mPosX, mPosY); glTexCoord2i(mPosX+mSizeX, mPosY); glVertex2i(mPosX+mSizeX, mPosY); glTexCoord2i(mPosX+mSizeX, mPosY+mSizeY); glVertex2i(mPosX+mSizeX, mPosY+mSizeY); glTexCoord2i(mPosX, mPosY+mSizeY); glVertex2i(mPosX, mPosY+mSizeY); glEnd();
But that’s it. I can’t get OpenGL to display the bitmap. Can you see anything blatantly wrong here? I think I’m following basically what other people appear to be doing.
Many thanks!!
[This message has been edited by stodge (edited 01-15-2003).]