Loading textures causes crash.

Discuss the development of new homebrew software, tools and libraries.

Moderators: cheriff, TyRaNiD

Post Reply
longjoel
Posts: 14
Joined: Thu Jan 29, 2009 4:06 am

Loading textures causes crash.

Post by longjoel »

I can't figure out what's causing the crash when I call glEnable(GL_TEXTURE_2D).
Is there something wrong with how I'm creating the textures?

Code: Select all

glBindTexture(GL_TEXTURE_2D, surfaceTex[0]);

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);

glTexImage2D(GL_TEXTURE_2D, 0, 3, 64, 64, 0, GL_BGR, GL_UNSIGNED_BYTE, t0->pixels);
This works on windows. So i know it's not a normal OpenGL glitch. If anybody wants the full source, I can paste it.

Code: Select all

#include <SDL/SDL.h>
#include <SDL/SDL_OpenGL.h>
#include <pspkernel.h>
#include <pspdebug.h>
#include <pspdisplay.h>

#include <GL/gl.h>
#include <GL/glu.h>
#include <math.h>

#define PI 3.14159265


// This will determine what kind of tile we are looking at
static int level&#91;8&#93;&#91;8&#93; =
&#123;
	&#123;1,0,1,0,1,0,1,0&#125;,
	&#123;0,1,0,1,0,1,0,1&#125;,
	&#123;0,0,1,0,1,0,1,0&#125;,
	&#123;1,0,0,1,0,1,0,1&#125;,
	&#123;0,1,0,0,1,0,1,0&#125;,
	&#123;1,0,1,0,0,1,0,1&#125;,
	&#123;0,1,0,1,0,0,1,0&#125;,
	&#123;1,0,1,0,1,0,0,1&#125;
&#125;;

// this will determine the height of each tile
static int height&#91;8&#93;&#91;8&#93; =
&#123;
	&#123;8,8,8,8,8,8,8,8&#125;,
	&#123;8,2,2,3,4,4,3,6&#125;,
	&#123;8,2,3,3,4,3,2,4&#125;,
	&#123;8,3,4,4,3,2,1,2&#125;,
	&#123;8,3,4,3,2,1,1,2&#125;,
	&#123;8,4,3,2,1,2,1,4&#125;,
	&#123;8,3,2,1,1,1,1,6&#125;,
	&#123;9,8,2,8,4,8,0,8&#125;
&#125;;

// these two arrays contain the texture information
// relevant to the demonstration.
GLuint surfaceTex&#91;2&#93;;
GLuint edgeTex&#91;1&#93;;

// angle of rotation
float rAngle = 0;

// camera position
float xCam = 0;
float yCam = 0;

// This code creates the texture and loads it into memory
void BuildTextures&#40;&#41;
&#123;
	// Load textures from file.
	SDL_Surface *t0 = SDL_LoadBMP&#40;"000.bmp"&#41;;
	SDL_Surface *t1 = SDL_LoadBMP&#40;"001.bmp"&#41;;
	SDL_Surface *h0 = SDL_LoadBMP&#40;"100.bmp"&#41;;


	// allocate the texture slots.
	glGenTextures&#40;2, surfaceTex&#41;;
	glGenTextures&#40;1, edgeTex&#41;;


	// setup the texture parameter
	glBindTexture&#40;GL_TEXTURE_2D, surfaceTex&#91;0&#93;&#41;;

	glTexParameteri&#40;GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST&#41;;
	glTexParameteri&#40;GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST&#41;;
	glTexParameteri&#40;GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT&#41;;
	glTexParameteri&#40;GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT&#41;;

	glTexImage2D&#40;GL_TEXTURE_2D, 0, 3, 64, 64, 0, GL_BGR, GL_UNSIGNED_BYTE, t0->pixels&#41;;

	// more texture loading

	// Release the surfaces.
	SDL_FreeSurface&#40;t0&#41;;
	SDL_FreeSurface&#40;t1&#41;;
	SDL_FreeSurface&#40;h0&#41;;


&#125;

// This code frees up the allocated textures.
void DeleteTextures&#40;&#41;
&#123;
	glDeleteTextures&#40;2, surfaceTex&#41;;
	glDeleteTextures&#40;1, edgeTex&#41;;

&#125;

void DrawGridPiece&#40;int x, int y, int h, GLint tileTex, GLint borderTex&#41;
&#123;

	glPushMatrix&#40;&#41;;
	glBindTexture&#40;GL_TEXTURE_2D, tileTex&#41;;

	glTranslatef&#40;x*64, h*16, y*64&#41;;

	glBegin&#40;GL_QUADS&#41;;

	glTexCoord2f&#40;0,0&#41;;
	glVertex3f&#40;0,0,0&#41;;

	glTexCoord2f&#40;0,1&#41;;
	glVertex3f&#40;0,0,64&#41;;

	glTexCoord2f&#40;1,1&#41;;
	glVertex3f&#40;64,0,64&#41;;

	glTexCoord2f&#40;1,0&#41;;
	glVertex3f&#40;64,0,0&#41;;

	glEnd&#40;&#41;;

	glPopMatrix&#40;&#41;;

// more drawing code

&#125;

void SetupScene&#40;&#41;
&#123;
	glViewport&#40;0,0,480,272&#41;;

//	glEnable&#40;GL_TEXTURE_2D&#41;;        // uncommenting this causes a crash
	glEnable&#40;GL_CULL_FACE&#41;;

	glCullFace&#40;GL_BACK&#41;;
	glEnable&#40;GL_DEPTH_TEST&#41;;

	glMatrixMode&#40;GL_PROJECTION&#41;;
	glLoadIdentity&#40;&#41;;
	glOrtho&#40;-100,100,-200,200,-300,300&#41;;

	glMatrixMode&#40;GL_MODELVIEW&#41;;
	glLoadIdentity&#40;&#41;;

	glRotatef&#40;35, 1, 0, 0&#41;;
&#125;

void Render&#40;&#41;
&#123;
	glClearColor&#40;0,0,0,0&#41;;
	glClear&#40;GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT&#41;;
	glPushMatrix&#40;&#41;;

	glRotatef&#40;rAngle, 0,1,0&#41;;

	glTranslatef&#40;xCam, 0, yCam&#41;;
	glTranslatef&#40;0,-30,0&#41;;

	for&#40;int y = 0; y < 8; y +=1&#41;
	&#123;
		for&#40;int x = 0; x < 8; x += 1&#41;
		&#123;
			DrawGridPiece&#40;x,y,height&#91;y&#93;&#91;x&#93;, surfaceTex&#91;level&#91;y&#93;&#91;x&#93;&#93;,edgeTex&#91;0&#93;&#41;;
			//DrawGridPiece&#40;x,y,height&#91;y&#93;&#91;x&#93;, surfaceTex&#91;0&#93;,0&#41;;
		&#125;
	&#125;

	glPopMatrix&#40;&#41;;

	SDL_GL_SwapBuffers&#40;&#41;;
&#125;

PSP_MAIN_THREAD_ATTR&#40;THREAD_ATTR_USER | THREAD_ATTR_VFPU&#41;;
extern "C"
int SDL_main&#40;int argc, char *argv&#91;&#93;&#41;
&#123;
	SDL_Init&#40;SDL_INIT_EVERYTHING&#41;;

	//SDL_GL_SetAttribute&#40;SDL_GL_DOUBLEBUFFER, 1&#41;;

	SDL_SetVideoMode&#40;480,272,32,SDL_OPENGL&#41;;

	SetupScene&#40;&#41;;
	BuildTextures&#40;&#41;;

	SDL_Event e;
	while&#40;true&#41;
	&#123;
		SDL_PollEvent&#40;&e&#41;;

		if&#40;e.type == SDL_QUIT&#41;
			break;


		Render&#40;&#41;;

	&#125;

	DeleteTextures&#40;&#41;;

	SDL_Quit&#40;&#41;;

	return 0;

&#125;
Last edited by longjoel on Wed Feb 11, 2009 3:47 pm, edited 1 time in total.
longjoel
Posts: 14
Joined: Thu Jan 29, 2009 4:06 am

Post by longjoel »

Can I please get some help with this? I've been digging through the psgl source and i can't seem to find anything that i'm doing wrong.
longjoel
Posts: 14
Joined: Thu Jan 29, 2009 4:06 am

Post by longjoel »

pretty please?
J.F.
Posts: 2906
Joined: Sun Feb 22, 2004 11:41 am

Post by J.F. »

You need to give more info, like where it actually blows up. Put logging in between all those commands and see what is the last thing to work. Or get it going in psplink if you can. You know - debug the thing. It LOOKS fine, which means you'll just have to get your hands dirty and log the hell out of it until you narrow it down.
PosX100
Posts: 98
Joined: Wed Aug 15, 2007 1:02 am

Post by PosX100 »

In your "buildTextures" function , you're creating 2 textures( 1 with size of 3 elements , and another one with 2), but only 1(Just 1 out of 5) actually
gets binded.

Its simple , you have no idea what you're doing ...
kuroneko
Posts: 24
Joined: Thu Dec 08, 2005 11:32 am
Location: Chigasaki, Japan

Post by kuroneko »

PosX100 wrote:In your "buildTextures" function , you're creating 2 textures( 1 with size of 3 elements , and another one with 2), but only 1(Just 1 out of 5) actually
gets binded.

Its simple , you have no idea what you're doing ...
Could it be that it wasn't the complete source? There is a comment saying // more texture loading.
longjoel
Posts: 14
Joined: Thu Jan 29, 2009 4:06 am

Post by longjoel »

PosX100 wrote:In your "buildTextures" function , you're creating 2 textures( 1 with size of 3 elements , and another one with 2), but only 1(Just 1 out of 5) actually
gets binded.

Its simple , you have no idea what you're doing ...
Thank you for your constructive feedback, PosX100. Your post has helped me more than I could ever imagine. You are truly an amazing resource.

@kuroneko:
I've been doing line item commenting and testing some of the code in other projects. The image loading code works. I'm not sure if my glTexParameteri values are valid though.

Weird, if i change this :
glTexImage2D(GL_TEXTURE_2D, 0, 3, 64, 64, 0, GL_BGR, GL_UNSIGNED_BYTE, t0->pixels);

to this:

glTexImage2D(GL_TEXTURE_2D, 0, 3, 64, 64, 0, GL_RGB, GL_UNSIGNED_BYTE, t0->pixels);

It executes, but shows the textures as the wrong color. I think i'm getting closer...

Is there a resource for the PSP version of OpenGL so I can know what's implemented and whats not? I haven't been able to find much on google about it.
longjoel
Posts: 14
Joined: Thu Jan 29, 2009 4:06 am

Post by longjoel »

I've narrowed it down, it is infact glTexImage that is causing the issue. It only seems to want to work with GL_RGB, trying to use GL_BGR causes it to crash.

Is there a way to get GL_BGR to work or am I going to have to alter my artwork or swap the red and blue channels when I load the texture?
kuroneko
Posts: 24
Joined: Thu Dec 08, 2005 11:32 am
Location: Chigasaki, Japan

Post by kuroneko »

longjoel wrote:I've narrowed it down, it is infact glTexImage that is causing the issue. It only seems to want to work with GL_RGB, trying to use GL_BGR causes it to crash.
Does glTexImage actually accept GL_BGR, i.e. what is the status reported from glGetError() before and after this call?
longjoel
Posts: 14
Joined: Thu Jan 29, 2009 4:06 am

Post by longjoel »

kuroneko wrote:
longjoel wrote:I've narrowed it down, it is infact glTexImage that is causing the issue. It only seems to want to work with GL_RGB, trying to use GL_BGR causes it to crash.
Does glTexImage actually accept GL_BGR, i.e. what is the status reported from glGetError() before and after this call?
I'll tell you after i get psplink working. lol
kuroneko
Posts: 24
Joined: Thu Dec 08, 2005 11:32 am
Location: Chigasaki, Japan

Post by kuroneko »

I just noticed something, you use

glTexImage2D(GL_TEXTURE_2D, 0, 3, 64, 64, 0, GL_BGR, GL_UNSIGNED_BYTE, t0->pixels);

Internal format 3 and external being GL_BGR. They have to be the same. When you changed it to GL_RGB it worked because 3 is mapped to GL_RGB internally. So assuming GL_BGR is actually supported you should use

glTexImage2D(GL_TEXTURE_2D, 0, GL_BGR, 64, 64, 0, GL_BGR, GL_UNSIGNED_BYTE, t0->pixels);

HTH
longjoel
Posts: 14
Joined: Thu Jan 29, 2009 4:06 am

Post by longjoel »

kuroneko wrote:I just noticed something, you use

glTexImage2D(GL_TEXTURE_2D, 0, 3, 64, 64, 0, GL_BGR, GL_UNSIGNED_BYTE, t0->pixels);

Internal format 3 and external being GL_BGR. They have to be the same. When you changed it to GL_RGB it worked because 3 is mapped to GL_RGB internally. So assuming GL_BGR is actually supported you should use

glTexImage2D(GL_TEXTURE_2D, 0, GL_BGR, 64, 64, 0, GL_BGR, GL_UNSIGNED_BYTE, t0->pixels);

HTH
Still crashes. going to try and get PSPLink working so i can grab the glErrors.
Post Reply