pspGL Texture problem

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

Moderators: cheriff, TyRaNiD

Post Reply
User avatar
Thanhda
Posts: 331
Joined: Sat Apr 09, 2005 2:08 am
Location: Canada
Contact:

pspGL Texture problem

Post by Thanhda »

i'm trying to port a basic texturing mapping code into pspgl, but when i run it, it, it seems to crash. giving me some errors. "Exception - Bus error<data>"

Here the code

Code: Select all

#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <GL/glut.h>

/* ascii code for the escape key */
#define ESCAPE 27

/* The number of our GLUT window */
int window; 

/* floats for x rotation, y rotation, z rotation */
float xrot, yrot, zrot;

/* storage for one texture  */
int texture&#91;1&#93;;

/* Image type - contains height, width, and data */
struct Image &#123;
    unsigned long sizeX;
    unsigned long sizeY;
    char *data;
&#125;;
typedef struct Image Image;

/******* PSP specific debugging ********************************************/
extern void __psp_log &#40;const char *fmt, ...&#41;;
/* enable GLerror logging to "ms0&#58;/log.txt" */
#if 1
        #define GLCHK&#40;x&#41;						\
        do &#123;								\
                GLint errcode;						\
                x;							\
                errcode = glGetError&#40;&#41;;					\
                if &#40;errcode != GL_NO_ERROR&#41; &#123;				\
                        __psp_log&#40;"%s &#40;%d&#41;&#58; GL error 0x%04x\n",		\
                                __FUNCTION__, __LINE__,			\
                                &#40;unsigned int&#41; errcode&#41;;		\
                &#125;							\
        &#125; while &#40;0&#41;
#else
        #define GLCHK&#40;x&#41; x
#endif
/******* end of PSP specific debugging *************************************/
// quick and dirty bitmap loader...for 24 bit bitmaps with 1 plane only.  
int ImageLoad&#40;char *filename, Image *image&#41; &#123;
    FILE *file;
    unsigned long size;                 // size of the image in bytes.
    unsigned long i;                    // standard counter.
    unsigned short int planes;          // number of planes in image &#40;must be 1&#41; 
    unsigned short int bpp;             // number of bits per pixel &#40;must be 24&#41;
    char temp;                          // temporary color storage for bgr-rgb conversion.

    // make sure the file is there.
    if &#40;&#40;file = fopen&#40;filename, "rb"&#41;&#41;==NULL&#41;
    &#123;
	printf&#40;"File Not Found &#58; %s\n",filename&#41;;
	//return 0;
    &#125;
    
    // seek through the bmp header, up to the width/height&#58;
    fseek&#40;file, 18, SEEK_CUR&#41;;

    // read the width
    if &#40;&#40;i = fread&#40;&image->sizeX, 4, 1, file&#41;&#41; != 1&#41; &#123;
	printf&#40;"Error reading width from %s.\n", filename&#41;;
	//return 0;
    &#125;
    printf&#40;"Width of %s&#58; %lu\n", filename, image->sizeX&#41;;
    
    // read the height 
    if &#40;&#40;i = fread&#40;&image->sizeY, 4, 1, file&#41;&#41; != 1&#41; &#123;
	printf&#40;"Error reading height from %s.\n", filename&#41;;
	//return 0;
    &#125;
    printf&#40;"Height of %s&#58; %lu\n", filename, image->sizeY&#41;;
    
    // calculate the size &#40;assuming 24 bits or 3 bytes per pixel&#41;.
    size = image->sizeX * image->sizeY * 3;

    // read the planes
    if &#40;&#40;fread&#40;&planes, 2, 1, file&#41;&#41; != 1&#41; &#123;
	printf&#40;"Error reading planes from %s.\n", filename&#41;;
	//return 0;
    &#125;
    if &#40;planes != 1&#41; &#123;
	printf&#40;"Planes from %s is not 1&#58; %u\n", filename, planes&#41;;
	//return 0;
    &#125;

    // read the bpp
    if &#40;&#40;i = fread&#40;&bpp, 2, 1, file&#41;&#41; != 1&#41; &#123;
	printf&#40;"Error reading bpp from %s.\n", filename&#41;;
	//return 0;
    &#125;
    if &#40;bpp != 24&#41; &#123;
	printf&#40;"Bpp from %s is not 24&#58; %u\n", filename, bpp&#41;;
	//return 0;
    &#125;
	
    // seek past the rest of the bitmap header.
    fseek&#40;file, 24, SEEK_CUR&#41;;

    // read the data. 
    image->data = &#40;char *&#41; malloc&#40;size&#41;;
    if &#40;image->data == NULL&#41; &#123;
	printf&#40;"Error allocating memory for color-corrected image data"&#41;;
	//return 0;	
    &#125;

    if &#40;&#40;i = fread&#40;image->data, size, 1, file&#41;&#41; != 1&#41; &#123;
	printf&#40;"Error reading image data from %s.\n", filename&#41;;
	//return 0;
    &#125;

    for &#40;i=0;i<size;i+=3&#41; &#123; // reverse all of the colors. &#40;bgr -> rgb&#41;
	temp = image->data&#91;i&#93;;
	image->data&#91;i&#93; = image->data&#91;i+2&#93;;
	image->data&#91;i+2&#93; = temp;
    &#125;
    
    // we're done.
    return 1;
&#125;
// Load Bitmaps And Convert To Textures
void LoadGLTextures&#40;&#41; &#123;	
    // Load Texture
    Image *image1;
    
    // allocate space for texture
    image1 = &#40;Image *&#41; malloc&#40;sizeof&#40;Image&#41;&#41;;
    if &#40;image1 == NULL&#41; &#123;
	printf&#40;"Error allocating space for image"&#41;;
	//exit&#40;0&#41;;
    &#125;

    if &#40;!ImageLoad&#40;"Image.bmp", image1&#41;&#41; &#123;
	//exit&#40;1&#41;;
    &#125;        

    // Create Texture	
    GLCHK&#40;glGenTextures&#40;1, &texture&#91;0&#93;&#41;&#41;;
    GLCHK&#40;glBindTexture&#40;GL_TEXTURE_2D, texture&#91;0&#93;&#41;&#41;;   // 2d texture &#40;x and y size&#41;
    GLCHK&#40;glTexParameteri&#40;GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR&#41;&#41;; // scale linearly when image bigger than texture
    GLCHK&#40;glTexParameteri&#40;GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR&#41;&#41;; // scale linearly when image smalled than texture

    // 2d texture, level of detail 0 &#40;normal&#41;, 3 components &#40;red, green, blue&#41;, x size from image, y size from image, 
    // border 0 &#40;normal&#41;, rgb color data, unsigned byte data, and finally the data itself.
    GLCHK&#40;glTexImage2D&#40;GL_TEXTURE_2D, 0, 3, image1->sizeX, image1->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, image1->data&#41;&#41;;
&#125;;

/* A general OpenGL initialization function.  Sets all of the initial parameters. */
void InitGL&#40;int Width, int Height&#41;	        // We call this right after our OpenGL window is created.
&#123;
    //LoadGLTextures&#40;&#41;;					// Load The Texture&#40;s&#41; 
    GLCHK&#40;glEnable&#40;GL_TEXTURE_2D&#41;&#41;;			// Enable Texture Mapping
    GLCHK&#40;glClearColor&#40;0.0f, 0.0f, 1.0f, 0.0f&#41;&#41;;	// Clear The Background Color To Blue 
    GLCHK&#40;glClearDepth&#40;1.0&#41;&#41;;				// Enables Clearing Of The Depth Buffer
    GLCHK&#40;glDepthFunc&#40;GL_LESS&#41;&#41;;			// The Type Of Depth Test To Do
    GLCHK&#40;glEnable&#40;GL_DEPTH_TEST&#41;&#41;;			// Enables Depth Testing
    GLCHK&#40;glShadeModel&#40;GL_SMOOTH&#41;&#41;;			// Enables Smooth Color Shading
    
    GLCHK&#40;glMatrixMode&#40;GL_PROJECTION&#41;&#41;;
    GLCHK&#40;glLoadIdentity&#40;&#41;&#41;;				// Reset The Projection Matrix
    
    GLCHK&#40;gluPerspective&#40;45.0f,&#40;GLfloat&#41;Width/&#40;GLfloat&#41;Height,0.1f,100.0f&#41;&#41;;	// Calculate The Aspect Ratio Of The Window
    
    GLCHK&#40;glMatrixMode&#40;GL_MODELVIEW&#41;&#41;;
&#125;

static
void reshape &#40;int w, int h&#41;
&#123;
        GLCHK&#40;glViewport&#40;0, 0, w, h&#41;&#41;;
        GLCHK&#40;glMatrixMode&#40;GL_PROJECTION&#41;&#41;;
        GLCHK&#40;glLoadIdentity&#40;&#41;&#41;;
        GLCHK&#40;glOrtho&#40;-2, 2, -2, 2, -2, 2&#41;&#41;;
	GLCHK&#40;gluPerspective&#40;45.0f,&#40;GLfloat&#41;w/&#40;GLfloat&#41;h,0.1f,100.0f&#41;&#41;;
        GLCHK&#40;glMatrixMode&#40;GL_MODELVIEW&#41;&#41;;
        GLCHK&#40;glLoadIdentity&#40;&#41;&#41;;
&#125;

static float delta = 1.0;

static
void display &#40;void&#41;
&#123;
    GLCHK&#40;glShadeModel&#40;GL_SMOOTH&#41;&#41;;
    GLCHK&#40;glClear&#40;GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT&#41;&#41;;		// Clear The Screen And The Depth Buffer
    GLCHK&#40;glLoadIdentity&#40;&#41;&#41;;				// Reset The View

    GLCHK&#40;glTranslatef&#40;0.0f,0.0f,-5.0f&#41;&#41;;              // move 5 units into the screen.
    
    GLCHK&#40;glRotatef&#40;xrot,1.0f,0.0f,0.0f&#41;&#41;;		// Rotate On The X Axis
    GLCHK&#40;glRotatef&#40;yrot,0.0f,1.0f,0.0f&#41;&#41;;		// Rotate On The Y Axis
    GLCHK&#40;glRotatef&#40;zrot,0.0f,0.0f,1.0f&#41;&#41;;		// Rotate On The Z Axis

    GLCHK&#40;glBindTexture&#40;GL_TEXTURE_2D, texture&#91;0&#93;&#41;&#41;;   // choose the texture to use.

    GLCHK&#40;glBegin&#40;GL_QUADS&#41;&#41;;		                // begin drawing a cube
    
    // Front Face &#40;note that the texture's corners have to match the quad's corners&#41;
    GLCHK&#40;glTexCoord2f&#40;0.0f, 0.0f&#41;&#41;; GLCHK&#40;glVertex3f&#40;-1.0f, -1.0f,  1.0f&#41;&#41;;	// Bottom Left Of The Texture and Quad
    GLCHK&#40;glTexCoord2f&#40;1.0f, 0.0f&#41;&#41;; GLCHK&#40;glVertex3f&#40; 1.0f, -1.0f,  1.0f&#41;&#41;;	// Bottom Right Of The Texture and Quad
    GLCHK&#40;glTexCoord2f&#40;1.0f, 1.0f&#41;&#41;; GLCHK&#40;glVertex3f&#40; 1.0f,  1.0f,  1.0f&#41;&#41;;	// Top Right Of The Texture and Quad
    GLCHK&#40;glTexCoord2f&#40;0.0f, 1.0f&#41;&#41;; GLCHK&#40;glVertex3f&#40;-1.0f,  1.0f,  1.0f&#41;&#41;;	// Top Left Of The Texture and Quad
    
    // Back Face
    GLCHK&#40;glTexCoord2f&#40;1.0f, 0.0f&#41;&#41;; GLCHK&#40;glVertex3f&#40;-1.0f, -1.0f, -1.0f&#41;&#41;;	// Bottom Right Of The Texture and Quad
    GLCHK&#40;glTexCoord2f&#40;1.0f, 1.0f&#41;&#41;; GLCHK&#40;glVertex3f&#40;-1.0f,  1.0f, -1.0f&#41;&#41;;	// Top Right Of The Texture and Quad
    GLCHK&#40;glTexCoord2f&#40;0.0f, 1.0f&#41;&#41;; GLCHK&#40;glVertex3f&#40; 1.0f,  1.0f, -1.0f&#41;&#41;;	// Top Left Of The Texture and Quad
    GLCHK&#40;glTexCoord2f&#40;0.0f, 0.0f&#41;&#41;; GLCHK&#40;glVertex3f&#40; 1.0f, -1.0f, -1.0f&#41;&#41;;	// Bottom Left Of The Texture and Quad
	
    // Top Face
    GLCHK&#40;glTexCoord2f&#40;0.0f, 1.0f&#41;&#41;; GLCHK&#40;glVertex3f&#40;-1.0f,  1.0f, -1.0f&#41;&#41;;	// Top Left Of The Texture and Quad
    GLCHK&#40;glTexCoord2f&#40;0.0f, 0.0f&#41;&#41;; GLCHK&#40;glVertex3f&#40;-1.0f,  1.0f,  1.0f&#41;&#41;;	// Bottom Left Of The Texture and Quad
    GLCHK&#40;glTexCoord2f&#40;1.0f, 0.0f&#41;&#41;; GLCHK&#40;glVertex3f&#40; 1.0f,  1.0f,  1.0f&#41;&#41;;	// Bottom Right Of The Texture and Quad
    GLCHK&#40;glTexCoord2f&#40;1.0f, 1.0f&#41;&#41;; GLCHK&#40;glVertex3f&#40; 1.0f,  1.0f, -1.0f&#41;&#41;;	// Top Right Of The Texture and Quad
    
    // Bottom Face       
    GLCHK&#40;glTexCoord2f&#40;1.0f, 1.0f&#41;&#41;; GLCHK&#40;glVertex3f&#40;-1.0f, -1.0f, -1.0f&#41;&#41;;	// Top Right Of The Texture and Quad
    GLCHK&#40;glTexCoord2f&#40;0.0f, 1.0f&#41;&#41;; GLCHK&#40;glVertex3f&#40; 1.0f, -1.0f, -1.0f&#41;&#41;;	// Top Left Of The Texture and Quad
    GLCHK&#40;glTexCoord2f&#40;0.0f, 0.0f&#41;&#41;; GLCHK&#40;glVertex3f&#40; 1.0f, -1.0f,  1.0f&#41;&#41;;	// Bottom Left Of The Texture and Quad
    GLCHK&#40;glTexCoord2f&#40;1.0f, 0.0f&#41;&#41;; GLCHK&#40;glVertex3f&#40;-1.0f, -1.0f,  1.0f&#41;&#41;;	// Bottom Right Of The Texture and Quad
    
    // Right face
    GLCHK&#40;glTexCoord2f&#40;1.0f, 0.0f&#41;&#41;; GLCHK&#40;glVertex3f&#40; 1.0f, -1.0f, -1.0f&#41;&#41;;	// Bottom Right Of The Texture and Quad
    GLCHK&#40;glTexCoord2f&#40;1.0f, 1.0f&#41;&#41;; GLCHK&#40;glVertex3f&#40; 1.0f,  1.0f, -1.0f&#41;&#41;;	// Top Right Of The Texture and Quad
    GLCHK&#40;glTexCoord2f&#40;0.0f, 1.0f&#41;&#41;; GLCHK&#40;glVertex3f&#40; 1.0f,  1.0f,  1.0f&#41;&#41;;	// Top Left Of The Texture and Quad
    GLCHK&#40;glTexCoord2f&#40;0.0f, 0.0f&#41;&#41;; GLCHK&#40;glVertex3f&#40; 1.0f, -1.0f,  1.0f&#41;&#41;;	// Bottom Left Of The Texture and Quad
    
    // Left Face
    GLCHK&#40;glTexCoord2f&#40;0.0f, 0.0f&#41;&#41;; GLCHK&#40;glVertex3f&#40;-1.0f, -1.0f, -1.0f&#41;&#41;;	// Bottom Left Of The Texture and Quad
    GLCHK&#40;glTexCoord2f&#40;1.0f, 0.0f&#41;&#41;; GLCHK&#40;glVertex3f&#40;-1.0f, -1.0f,  1.0f&#41;&#41;;	// Bottom Right Of The Texture and Quad
    GLCHK&#40;glTexCoord2f&#40;1.0f, 1.0f&#41;&#41;; GLCHK&#40;glVertex3f&#40;-1.0f,  1.0f,  1.0f&#41;&#41;;	// Top Right Of The Texture and Quad
    GLCHK&#40;glTexCoord2f&#40;0.0f, 1.0f&#41;&#41;; GLCHK&#40;glVertex3f&#40;-1.0f,  1.0f, -1.0f&#41;&#41;;	// Top Left Of The Texture and Quad
    
    GLCHK&#40;glEnd&#40;&#41;&#41;;                                    // done with the polygon.

    xrot+=15.0f;		                // X Axis Rotation	
    yrot+=15.0f;		                // Y Axis Rotation
    zrot+=15.0f;		                // Z Axis Rotation

    // since this is double buffered, swap the buffers to display what just got drawn.
    glutSwapBuffers&#40;&#41;;
    glutPostRedisplay&#40;&#41;;
&#125;

static
void keydown &#40;unsigned char key, int x, int y&#41;
&#123;
        switch &#40;key&#41; &#123;
        case 'd'&#58;			/* delta, triangle */
                break;
        case 'o'&#58;			/* round */
                delta = 0.0f;
                break;
        case 'q'&#58;			/* square*/
                break;
        case 'x'&#58;			/* cross button */
                exit&#40;0&#41;;
                break;
        default&#58;
                ;
        &#125;
&#125;

static
void keyup &#40;unsigned char key, int x, int y&#41;
&#123;
        switch &#40;key&#41; &#123;
        case 'o'&#58;
                delta = 1.0f;
                break;
        default&#58;
                ;
        &#125;
&#125;

static
void joystick &#40;unsigned int buttonMask, int x, int y, int z&#41;
&#123;
        GLCHK&#40;glClearColor&#40;x * 1.0f/2000.0f + 0.5f, y * 1.0f/2000.0f + 0.5f, 1.0f, 1.0f&#41;&#41;;
&#125;

int main&#40;int argc, char* argv&#91;&#93;&#41;
&#123;
        glutInit&#40;&argc, argv&#41;;
        glutCreateWindow&#40; __FILE__ &#41;;
        glutKeyboardFunc&#40;keydown&#41;;
        glutKeyboardUpFunc&#40;keyup&#41;;
        glutJoystickFunc&#40;joystick, 0&#41;;
        glutReshapeFunc&#40;reshape&#41;;
        glutDisplayFunc&#40;display&#41;;
	InitGL&#40;480, 272&#41;;
        glutMainLoop&#40;&#41;;
        return 0;

&#125;
If i comment out the LoadGLTextures() function, it seems to work, but instead of a rotating cube, its a blur of boxes. that dont resemble a cube at all.

edit: Okay, figure out the weird shaped object problem. its because pspgl only read in triangles not quads. that problem has now been fix. but the texture problem is still there.
There are 10 types of people in the world: Those who understand binary, and those who don't...
Ryu
Posts: 19
Joined: Mon Oct 17, 2005 8:36 pm

Post by Ryu »

the crash probably occurs when you're trying to load the image, try to identify the problem by commenting less and less stuff in the image loader;

and if the problem is not there (i just read the code it seems ok) then comment the pspgl lines in the LoadTextures function instead, that way you might be able to find exactly where it crashes.
User avatar
Thanhda
Posts: 331
Joined: Sat Apr 09, 2005 2:08 am
Location: Canada
Contact:

Post by Thanhda »

this is the exact function that crashes the program. dont know why.

Code: Select all

if &#40;!ImageLoad&#40;"Image.bmp", image1&#41;&#41; &#123; 
   //exit&#40;1&#41;; 
    &#125;    
does psp have a problem loading bmp images?
There are 10 types of people in the world: Those who understand binary, and those who don't...
holger
Posts: 204
Joined: Thu Aug 18, 2005 10:57 am

Post by holger »

maybe you want to try an absolute path, including the memory stick device prefix.
User avatar
Thanhda
Posts: 331
Joined: Sat Apr 09, 2005 2:08 am
Location: Canada
Contact:

Post by Thanhda »

holger wrote:maybe you want to try an absolute path, including the memory stick device prefix.
well what is the root path for the ms in the psp? "f:/" or should i just use "/" since "/" is consider to be root.

But i dont know if that is the problem. if it cant find the image, it will load fine, and will not display it. but if the image was found, it will give an error. here link to screens of errors.

http://geocities.com/tsune_l/180658.jpg
http://geocities.com/tsune_l/180705.jpg

get the same error with the OBJ viewer i wrote
http://forums.ps2dev.org/viewtopic.php?t=4109

Here link to download the bin and src direct.
http://geocities.com/tsune_l/pspgl_obj.zip

Edit: It doesnt make a difference. i've set the absolute path, and still i get an error.
There are 10 types of people in the world: Those who understand binary, and those who don't...
User avatar
dot_blank
Posts: 498
Joined: Wed Sep 28, 2005 8:47 am
Location: Brasil

Post by dot_blank »

there are many known memorystick root
absolute paths you can use

different name, same story ;)

ms0:/
fatms0:/
ftms0:/
ms1:/
etc...

i generally just use ms0:/

OT: i like your coding style more more people
should comment properly as you do ....really
adds class in my opinion to some sources ;)
10011011 00101010 11010111 10001001 10111010
User avatar
Thanhda
Posts: 331
Joined: Sat Apr 09, 2005 2:08 am
Location: Canada
Contact:

Post by Thanhda »

dot_blank wrote:there are many known memorystick root
absolute paths you can use

different name, same story ;)

ms0:/
fatms0:/
ftms0:/
ms1:/
etc...

i generally just use ms0:/

OT: i like your coding style more more people
should comment properly as you do ....really
adds class in my opinion to some sources ;)
none the less, it still finds the file. when the file is found, it crashes. because if its not found, it does not display image. plus the "/" symbol means the root of that directory.
There are 10 types of people in the world: Those who understand binary, and those who don't...
jsgf
Posts: 254
Joined: Tue Jul 12, 2005 11:02 am
Contact:

Post by jsgf »

Does it crash in a GL call, or in some other place? Does it still crash if you remove all the GL calls?
User avatar
Thanhda
Posts: 331
Joined: Sat Apr 09, 2005 2:08 am
Location: Canada
Contact:

Post by Thanhda »

it crashes when trying to load a file. every time i try to load any file whether it is a bmp or obj file it crashes. So i believe the source of the problem is the io, working with pspgl. I'm going to try to write a simple app using pspgl, that loads a simple txt file and see if i get the same problem.
There are 10 types of people in the world: Those who understand binary, and those who don't...
jsgf
Posts: 254
Joined: Tue Jul 12, 2005 11:02 am
Contact:

Post by jsgf »

It would be more interesting to try your program with .bmp IO with the GL calls commented out. That would help isolate the problem.
User avatar
Thanhda
Posts: 331
Joined: Sat Apr 09, 2005 2:08 am
Location: Canada
Contact:

Post by Thanhda »

i know its not the problem of binding the texture to the geometry, but its the loading itself. but yes i will try many test to run txt files, obj and bmp images without any gl call.
There are 10 types of people in the world: Those who understand binary, and those who don't...
Post Reply