_SDL_main error and missing functions

Hi, I am new to SDL (v 1.2.15) and have two problems with this code:

Code:

#include <SDL.h>

void Mesh::createTextures() {
SDL_Init(SDL_INIT_EVERYTHING);
SDL_Surface *surface;
surface = SDL_LoadBMP("…\…\resources\textures\bla.bmp");
int mode;
if (surface->format->BytesPerPixel == 3) { // RGB 24bit
mode = GL_RGB;
} else if (surface->format->BytesPerPixel == 4) { // RGBA 32bit
mode = GL_RGBA;
}
glGenTextures(1, &_textureID);
glBindTexture(GL_TEXTURE_2D, _textureID);
glTexImage2D(GL_TEXTURE_2D, 0, mode, surface->w, surface->h, 0, mode, GL_UNSIGNED_BYTE, surface->pixels);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
}

First:
A texture which is completely red is shown blue. When I change the second mode to GL_BGR though OpenGL renders nothing.

Second:
How can I load other images than BMP? I can only find the function SDL_LoadBMP().

I hope anybody can help me out with this.

For your first, there’s a good chance that you’re not taking endianness
into account for your colour stuff. Here is how I did it (thanks to many
people here on the mailing list);

Second, your best bet is probably SDL_image, an add-on library to SDL,
found here: http://www.libsdl.org/projects/SDL_image/

I hope that helps,
-AlexOn Wed, Jun 6, 2012 at 3:47 AM, telandor wrote:

**
Hi, I am new to SDL (v 1.2.15) and have two problems with this code:

Code:

#include **

void Mesh::createTextures() {
SDL_Init(SDL_INIT_EVERYTHING);
SDL_Surface *surface;
surface = SDL_LoadBMP("…\…\resources\textures\bla.bmp");
int mode;
if (surface->format->BytesPerPixel == 3) { // RGB 24bit
mode = GL_RGB;
} else if (surface->format->BytesPerPixel == 4) { // RGBA 32bit
mode = GL_RGBA;
}
glGenTextures(1, &_textureID);
glBindTexture(GL_TEXTURE_2D, _textureID);
glTexImage2D(GL_TEXTURE_2D, 0, mode, surface->w, surface->h, 0, mode,
GL_UNSIGNED_BYTE, surface->pixels);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
}

First:
A texture which is completely red is shown blue. When I change the second
mode to GL_BGR though OpenGL renders nothing.

Second:
How can I load other images than BMP? I can only find the function
SDL_LoadBMP().

I hope anybody can help me out with this.


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

Where’s the _SDL_main error? :-POn 06/06/12 10:47, telandor wrote:

First:
A texture which is completely red is shown blue. When I change the
second mode to GL_BGR though OpenGL renders nothing.

Second:
How can I load other images than BMP? I can only find the function
SDL_LoadBMP().

For your first, there’s a good chance that you’re not taking endianness
into account for your colour stuff. Here is how I did it (thanks to many
people here on the mailing list);
https://github.com/mrozbarry/bitfighter-experiments/blob/master/bitfighter1.0/sdlutil.cpp#L349

Second, your best bet is probably SDL_image, an add-on library to SDL,
found here: http://www.libsdl.org/projects/SDL_image/

I hope that helps,
-Alex

**
Hi, I am new to SDL (v 1.2.15) and have two problems with this code:

Code:

#include **

void Mesh::createTextures() {
SDL_Init(SDL_INIT_EVERYTHING);
SDL_Surface *surface;
surface = SDL_LoadBMP("…\…\resources\textures\bla.bmp");
int mode;
if (surface->format->BytesPerPixel == 3) { // RGB 24bit
mode = GL_RGB;
} else if (surface->format->BytesPerPixel == 4) { // RGBA 32bit
mode = GL_RGBA;
}
glGenTextures(1, &_textureID);
glBindTexture(GL_TEXTURE_2D, _textureID);
glTexImage2D(GL_TEXTURE_2D, 0, mode, surface->w, surface->h, 0, mode,
GL_UNSIGNED_BYTE, surface->pixels);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
}

First:
A texture which is completely red is shown blue. When I change the second
mode to GL_BGR though OpenGL renders nothing.

Short story:
The internalFormat parameter to glTexImage2D() is wrong, should be
GL_RGB/GL_RGBA.

Long story:
The internal format is best thought of “how should the graphics card keep
it” – it isn’t meant to communicate stuff like component order, but a more
abstract “does it have alpha?” “does it use 8 bits per component?”. The
graphics card will do whatever it wants with your data to make rendering
efficient. Maybe that means 8x8 tiled textures with GARB format or
something weird. In short, your call could should be:

glTexImage2D(GL_TEXTURE_2D, 0,
surface->format->BytesPerPixel == 4? GL_RGBA : GL_RGB, /* Internal format */
, surface->w, surface->h, 0,

surface->format->BytesPerPixel == 4?, GL_BGRA : GL_BGR, /* External format
*/
GL_UNSIGNED_BYTE, surface->pixels);

Next time you get weird OpenGL issues, try glGetError();

http://www.opengl.org/sdk/docs/man/xhtml/glTexImage2D.xml

GL_INVALID_VALUE is generated if internalFormat is not 1, 2, 3, 4, or one
of the accepted resolution and format symbolic constants.

Patrick

Second:On Wed, Jun 6, 2012 at 9:43 AM, Alex Barry <alex.barry at gmail.com> wrote:

On Wed, Jun 6, 2012 at 3:47 AM, telandor wrote:

How can I load other images than BMP? I can only find the function
SDL_LoadBMP().

I hope anybody can help me out with this.


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

Thank you everybody for your fast responses.
SDL_image works so far, but the image is upside down. Is there a possibility to tell the library to change the orientation or do I have to switch the UV coordinates?

This library as well solves my problem with the color inversion. So this snipped works fine:

Code:

if (surface->format->BytesPerPixel == 3) { // RGB 24bit
mode = GL_RGB;
} else if (surface->format->BytesPerPixel == 4) { // RGBA 32bit
mode = GL_RGBA;
}
[…]
glTexImage2D(GL_TEXTURE_2D, 0, mode, surface->w, surface->h, 0, mode, GL_UNSIGNED_BYTE, surface->pixels);

Where’s the _SDL_main error?

By accident I posted this post twice. I cancelled submitting right after clicking submit, but it seems that it didn’t work. I thought id did though, changed the subject and resubmitted.

For the record, OpenGL defines the origin of the image to be the
bottom-left. BMP uses top-left, so that’s why the image will appear
inverted. If you use PNG images, this issue will disappear since they have
bottom-left origins as well.

PatrickOn Wed, Jun 6, 2012 at 1:30 PM, telandor wrote:

**
Thank you everybody for your fast responses.
SDL_image works so far, but the image is upside down. Is there a
possibility to tell the library to change the orientation or do I have to
switch the UV coordinates?

This library as well solves my problem with the color inversion. So this
snipped works fine:

Code:

if (surface->format->BytesPerPixel == 3) { // RGB 24bit
mode = GL_RGB;
} else if (surface->format->BytesPerPixel == 4) { // RGBA 32bit
mode = GL_RGBA;
}
[…]

glTexImage2D(GL_TEXTURE_2D, 0, mode, surface->w, surface->h, 0, mode,
GL_UNSIGNED_BYTE, surface->pixels);

Quote:

Where’s the _SDL_main error?

By accident I posted this post twice. I cancelled submitting right after
clicking submit, but it seems that it didn’t work. I thought id did though,
changed the subject and resubmitted.


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

Also, if you don’t want switch to a different image format, you can just
swap the rows’ pixels. Something like:

char* base; // base address of image data
size_t pitch; // size in bytes of a row of pixels
size_t rows; // the height of the image

char tmp[pitch];

for(i=0; i<rows/2; i++) {

char* r1 = base + (pitch * i); //ith row
char* r2 = base + (pitch * (rows - i - 1)) //ith from last row

memcpy(tmp, r1, pitch); //tmp <- R1
memcpy(r1, r2, pitch); //R1 <- R2
memcpy(r2, tmp, pitch); //R2 <- tmp

}On Wed, Jun 6, 2012 at 1:30 PM, telandor wrote:

**
Thank you everybody for your fast responses.
SDL_image works so far, but the image is upside down. Is there a
possibility to tell the library to change the orientation or do I have to
switch the UV coordinates?

This library as well solves my problem with the color inversion. So this
snipped works fine:

Code:

if (surface->format->BytesPerPixel == 3) { // RGB 24bit
mode = GL_RGB;
} else if (surface->format->BytesPerPixel == 4) { // RGBA 32bit
mode = GL_RGBA;
}
[…]

glTexImage2D(GL_TEXTURE_2D, 0, mode, surface->w, surface->h, 0, mode,
GL_UNSIGNED_BYTE, surface->pixels);

Quote:

Where’s the _SDL_main error?

By accident I posted this post twice. I cancelled submitting right after
clicking submit, but it seems that it didn’t work. I thought id did though,
changed the subject and resubmitted.


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

Just to put it out there, I recommend using a library that is meant for
loading image files into textures, like SOIL:
http://www.lonesock.net/soil.html

That would solve a few problems for you automatically.

Jonny D

For the record, the following graphics APIs and image formats use bottom-left:
Windows GDI
OpenGL
Direct3D10/11
.bmp (default)
.tga (default)

The following graphics APIs and image formats use top-left:
DirectDraw
Direct3D9 and below
.bmp (option, limited - no RLE compression in the top-left mode)
.tga (option)
.pcx
.png
.jpg

I’m sure others can greatly expand this list. But I want to point out that .bmp (which is a Windows GDI BITMAP structure dumped to disk) is bottom-left by default, and storing top-left is done by
storing a negative height value, and the RLE compression option becomes unsupported then.On 06/07/2012 09:37 AM, Patrick Baggett wrote:

For the record, OpenGL defines the origin of the image to be the bottom-left. BMP uses top-left, so that’s why the image will appear inverted. If you use PNG images, this issue will disappear since
they have bottom-left origins as well.

Patrick

On Wed, Jun 6, 2012 at 1:30 PM, telandor <ricowidmer at gmx.ch <mailto:ricowidmer at gmx.ch>> wrote:

__
Thank you everybody for your fast responses.
SDL_image works so far, but the image is upside down. Is there a possibility to tell the library to change the orientation or do I have to switch the UV coordinates?

This library as well solves my problem with the color inversion. So this snipped works fine:




Code:	


if (surface->format->BytesPerPixel == 3) { // RGB 24bit
   mode = GL_RGB;
} else if (surface->format->BytesPerPixel == 4) { // RGBA 32bit
   mode = GL_RGBA;
}
[...]

glTexImage2D(GL_TEXTURE_2D, 0, mode, surface->w, surface->h, 0, mode, GL_UNSIGNED_BYTE, surface->pixels);
	







Quote:	

Where's the _SDL_main error?	


By accident I posted this post twice. I cancelled submitting right after clicking submit, but it seems that it didn't work. I thought id did though, changed the subject and resubmitted.

_______________________________________________
SDL mailing list
SDL at lists.libsdl.org <mailto:SDL at lists.libsdl.org>
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org


LordHavoc
Author of DarkPlaces Quake1 engine - http://icculus.org/twilight/darkplaces
Co-designer of Nexuiz - http://alientrap.org/nexuiz
"War does not prove who is right, it proves who is left." - Unknown
"Any sufficiently advanced technology is indistinguishable from a rigged demo." - James Klass
"A game is a series of interesting choices." - Sid Meier

Thank you for all the answers.
No matter whether the image starts at the bottom left or the top left - it is upside down.
I will try the SOIL library or change the image after loading it.