Home » U++ Library support » U++ Widgets - General questions or Mixed problems » openGL and texture
|
|
Re: openGL and texture [message #14432 is a reply to message #14429] |
Tue, 26 February 2008 15:29   |
mrjt
Messages: 705 Registered: March 2007 Location: London
|
Contributor |
|
|
Unfortunately it's not quite that easy, Upp Image formats are not standard RGBA. Depending on the platform they could be BGRA (Win32) or ARGB (see Core/Color.h). Unfortunately there is no GL unpacking format that copes with this directly, so it requires an Image copy with some byte swapping.
This code should work for all platforms (only tested on Win32), though you may wish to change some of the GL parameters:
#ifdef PLATFORM_WIN32
#define RGBA_FIX(q) Swap(q->r, q->b);
#endif
#ifdef PLATFORM_POSIX
#ifdef CPU_BE
#define RGBA_FIX(q) { Swap(q->a, q->b); Swap(q->b, q->g); Swap(q->r, q->g); }
#else
#define RGBA_FIX(q) Swap(q->r, q->b);
#endif
#endif
Image RGBAFormat(Image img)
{
ImageBuffer ib(img);
RGBA *eoi = ~ib + ib.GetLength();
for (RGBA *q = ~ib; q < eoi; q++)
RGBA_FIX(q);
return ib;
}
GLuint GLTexture(Image img)
{
GLuint texnum = 0;
Size sz = img.GetSize();
Image copy = RGBAFormat(img);
glGenTextures(1, &texnum);
glBindTexture(GL_TEXTURE_2D, texnum);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
glTexImage2D(GL_TEXTURE_2D, 0, 4, sz.cx, sz.cy, 0, GL_RGBA, GL_UNSIGNED_BYTE, ~copy);
gluBuild2DMipmaps(GL_TEXTURE_2D, 4, sz.cx, sz.cy, GL_RGBA, GL_UNSIGNED_BYTE, ~copy);
return texnum;
}
Example usage:
void Init()
{
wglMakeCurrent(GLCtrl::GetDC(), GLCtrl::GetHGLRC());
texture = GLTexture(StreamRaster::LoadFileAny("C:\\texture.png"));
wglMakeCurrent(NULL, NULL);
}
virtual void GLPaint()
{
....Set up viewports/molelview matrix as necessary
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, texture);
glPushMatrix();
glTranslatef(0, 0, -6);
glBegin(GL_QUADS);
glColor3f(1.0f, 1.0f, 1.0f);
glTexCoord2f(0.0f, 1.0f);
glVertex3f(0.0f, 0.0f, 1.0f);
glTexCoord2f(1.0f, 1.0f);
glVertex3f(1.0f, 0.0f, 1.0f);
glTexCoord2f(1.0f, 0.0f);
glVertex3f(1.0f, 1.0f, 1.0f);
glTexCoord2f(0.0f, 0.0f);
glVertex3f(0.0f, 1.0f, 1.0f);
glEnd();
glPopMatrix();
}
Obviously the Image copy will create a certain amount of overhead for very large/lots of textures, but it shouldn't be too bad unless intending to write a AAA game (in whiich case you've got other problems ).
[Updated on: Tue, 26 February 2008 16:01] Report message to a moderator
|
|
|
|
Re: openGL and texture [message #14440 is a reply to message #14439] |
Tue, 26 February 2008 18:43   |
mrjt
Messages: 705 Registered: March 2007 Location: London
|
Contributor |
|
|
AGH! The dreaded white cube!
I assume you are using Linux. After modifying it for the Windows version of GLCtrl (why aren't they the same?) it works fine, so I can't really help.
My best guess would be that it's something to do with the Linux GLCtrl though, if the byte swapping wasn't working properly you'd still see expect to see a texture, just with the wrong colors.
[Updated on: Tue, 26 February 2008 18:45] Report message to a moderator
|
|
|
|
|
|
|
Goto Forum:
Current Time: Sun May 11 14:52:32 CEST 2025
Total time taken to generate the page: 0.00827 seconds
|