SDL, OpenGL and textures

Hello,

Currently I’ve been trying to get some knowlegde about OpenGL and SDL.
I really must say that it works beautyfull, the turorial is really
great and I made some nice progress with SDL and OpenGL!

Now my question, my code is based upon the testgl application in de SDL
test directory by using the code from DrawLogoTexture. The only
difference
is that I’ve got a SDL_Surface which acts as a buffer (and has the gfx)
and must be blit all the time. I managed to get the updates working by
changing the function DrawLogoTexture to grab the gfx from the buffer.
Only now I see that my application uses a lot of memory resulting in use
of swap and a performance downgrade.

It’s possibly that the function DrawLogoTexture from the testgl is
optimized
for a single texture load (like an image in BMP format) and not for a
SDL_Surface which is upgraded frequently. What’s the best way to make
sure
that I can use OpenGL using textures based upon a SDL_Surface (display)
and
then blit this to the main OpenGL screen (gbascreen). Below my slightly
changed source has been put. Any help is appreciated. Please understand
that
I’m a newbie on OpenGL and SDL.

Here’s a snip of my code (DrawLogoTexture was slightly changed while
others
haven’t been changed at all) :

inline void DrawLogoTexture(void)
{
static GLuint texture;
static GLfloat texMinX, texMinY;
static GLfloat texMaxX, texMaxY;
static int x = 0;
static int y = 0;
static int w, h;
static int delta_x = 1;
static int delta_y = 1;
static Uint32 last_moved = 0;

GLfloat texcoord[4];

w = gbascreen->w; // gbascreen is here the OpenGL display
h = gbascreen->h;


if ( !texture ) {
/* Convert the buffer display into an OpenGL texture */
texture = SDL_GL_LoadTexture(display, texcoord);

/* Make texture coordinates easy to understand */
texMinX = texcoord[0];
texMinY = texcoord[1];
texMaxX = texcoord[2];
texMaxY = texcoord[3];

/* Make sure that the texture conversion is okay */
if ( ! texture ) {
       printf("failed!\n");
       return;
}


}
/* Show the image on the screen */
SDL_GL_Enter2DMode();
glBindTexture(GL_TEXTURE_2D, texture);
glBegin(GL_TRIANGLE_STRIP);
glTexCoord2f(texMinX, texMinY); glVertex2i(x,   y  );
glTexCoord2f(texMaxX, texMinY); glVertex2i(x+w, y  );
glTexCoord2f(texMinX, texMaxY); glVertex2i(x,   y+h);
glTexCoord2f(texMaxX, texMaxY); glVertex2i(x+w, y+h);
glEnd();
SDL_GL_Leave2DMode();

/* display and swap the buffers */
SDL_GL_SwapBuffers( );

}

Tnx in advance!

Regards,

Niels Wagenaar

Currently I’ve been trying to get some knowlegde about OpenGL and SDL.
I really must say that it works beautyfull, the turorial is really
great and I made some nice progress with SDL and OpenGL!

Now my question, my code is based upon the testgl application in de SDL
test directory by using the code from DrawLogoTexture. The only
difference is that I’ve got a SDL_Surface which acts as a buffer (and
has the gfx) and must be blit all the time. I managed to get the updates
working by changing the function DrawLogoTexture to grab the gfx from
the buffer. Only now I see that my application uses a lot of memory
resulting in use of swap and a performance downgrade.

Unless you’re trying to create a new texture every time the buffer changes
(which your code does not do as far as my quick reading shows), it
shouldn’t be too slow really to just display the image once per frame.

It’s possibly that the function DrawLogoTexture from the testgl is
optimized for a single texture load (like an image in BMP format) and
not for a SDL_Surface which is upgraded frequently. What’s the best way
to make sure that I can use OpenGL using textures based upon a
SDL_Surface (display) and then blit this to the main OpenGL screen
(gbascreen). Below my slightly changed source has been put. Any help is
appreciated. Please understand that I’m a newbie on OpenGL and SDL.

It is designed for a single texture and it is not designed to ever change
that texture. A slightly enhanced demo which used two textures might help
show you how to do that - just use different texture ID’s, they’re just
ints and they can be completely random if you like, although obviously
linear is probably best to avoid re-using a texture slot.

The function to use when you want to change a given texture (especially if
only a small part of it) is to use glTexSubImage2D. I’ll direct you to
the manpage for that function (if you don’t have manpages for OpenGL I’ll
track down a copy for you) or to the OpenGL Reference Manual (HTML ebook
link can be found on gamedev.net if you need that…) Here’s a synopsis:

void glTexSubImage2D (GLenum target, GLint level, GLint xoffset,
GLint yoffset, GLsizei width, GLsizei height, GLenum format,
GLenum type, const GLvoid *pixels)

target - a texture ID, must be a GL_TEXTURE_2D
level - mip level - 0 for base texture (you’re not using this yet)
xoffset,
yoffset,
width,
height - position and size of the rect we’ll modify
format - how pixel data is stored, eg GL_RGBA
type - size of pixel data, eg GL_UNSIGNED_BYTE
pixels - if you can’t figure out what this is… :wink:

Here’s a snip of my code (DrawLogoTexture was slightly changed while
others
haven’t been changed at all) :

That code is probably more useful than what you’ve got here for finding
the problem I suspect.

inline void DrawLogoTexture(void)
{
static GLuint texture;
static GLfloat texMinX, texMinY;
static GLfloat texMaxX, texMaxY;
static int x = 0;
static int y = 0;
static int w, h;
static int delta_x = 1;
static int delta_y = 1;
static Uint32 last_moved = 0;

GLfloat texcoord[4];

w = gbascreen->w; // gbascreen is here the OpenGL display
h = gbascreen->h;

if ( !texture ) {
/* Convert the buffer display into an OpenGL texture */
texture = SDL_GL_LoadTexture(display, texcoord);

/* Make texture coordinates easy to understand */
texMinX = texcoord[0];
texMinY = texcoord[1];
texMaxX = texcoord[2];
texMaxY = texcoord[3];

/* Make sure that the texture conversion is okay */
if ( ! texture ) {
printf(“failed!\n”);
return;
}

}

I’d recommend doing this block in an init function, really. Especially if
you have plans for more than one texture or plans to update the texture.

/* Show the image on the screen */
SDL_GL_Enter2DMode();
glBindTexture(GL_TEXTURE_2D, texture);
glBegin(GL_TRIANGLE_STRIP);
glBegin(GL_QUADS);

Just because OpenGL uses a tri strip internally doesn’t mean that you
should use it externally. The reason is that the driver is free to make a
few assumptions when you use GL_QUADS, assuming that you’re obeying the
rules for specifying one - ie, no hourglasses, completely planar.

glTexCoord2f(texMinX, texMinY); glVertex2i(x, y );
glTexCoord2f(texMaxX, texMinY); glVertex2i(x+w, y );
glTexCoord2f(texMinX, texMaxY); glVertex2i(x, y+h);
glTexCoord2f(texMaxX, texMaxY); glVertex2i(x+w, y+h);
glEnd();
SDL_GL_Leave2DMode();

/* display and swap the buffers */

SDL_GL_SwapBuffers( );
}

Note also that you do the SDL_GL_SwapBuffers only after drawing all
textures. Sam’s testgl example is not the best-written OpenGL program in
the world, it really is just a sample to show how to get something on the
screen in OpenGL. My best advice to you is to go out and read the OpenGL
Programming Guide (old version for OpenGL 1.1 is available from gamedev in
non-downloadable HTML or quite-downloadable PDF formats - It’s a $50 book
which fits into the Get The Money™ category…)

The OpenGL Programming Guide (red book) does not use SDL. It uses GLUT,
which you should probably also learn about. SDL has more features, but
GLUT is a better learning aid. And once you have a good handle on OpenGL,
using it with SDL becomes very simple. Be sure to have a calculator handy
as you read through the book, you’ll do some trig and linear algebra when
things get interesting.On Mon, Dec 10, 2001 at 11:51:34AM +0100, nwagenaar at digitaldynamics.nl wrote:


Joseph Carter Free software developer

  • Mercury calmly removes XT-Ream’s arm…
  • Mercury then proceeds to beat XT-Ream with XT-Ream’s arm.
    wow, all this quake hacking is making Mercury violent here
  • mao is glad the quake forge project is in good hands

-------------- next part --------------
A non-text attachment was scrubbed…
Name: not available
Type: application/pgp-signature
Size: 273 bytes
Desc: not available
URL: http://lists.libsdl.org/pipermail/sdl-libsdl.org/attachments/20011210/0950a0c7/attachment.pgp