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[1];
/* Image type - contains height, width, and data */
struct Image {
    unsigned long sizeX;
    unsigned long sizeY;
    char *data;
};
typedef struct Image Image;
/******* PSP specific debugging ********************************************/
extern void __psp_log (const char *fmt, ...);
/* enable GLerror logging to "ms0:/log.txt" */
#if 1
        #define GLCHK(x)						\
        do {								\
                GLint errcode;						\
                x;							\
                errcode = glGetError();					\
                if (errcode != GL_NO_ERROR) {				\
                        __psp_log("%s (%d): GL error 0x%04x\n",		\
                                __FUNCTION__, __LINE__,			\
                                (unsigned int) errcode);		\
                }							\
        } while (0)
#else
        #define GLCHK(x) x
#endif
/******* end of PSP specific debugging *************************************/
// quick and dirty bitmap loader...for 24 bit bitmaps with 1 plane only.  
int ImageLoad(char *filename, Image *image) {
    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 (must be 1) 
    unsigned short int bpp;             // number of bits per pixel (must be 24)
    char temp;                          // temporary color storage for bgr-rgb conversion.
    // make sure the file is there.
    if ((file = fopen(filename, "rb"))==NULL)
    {
	printf("File Not Found : %s\n",filename);
	//return 0;
    }
    
    // seek through the bmp header, up to the width/height:
    fseek(file, 18, SEEK_CUR);
    // read the width
    if ((i = fread(&image->sizeX, 4, 1, file)) != 1) {
	printf("Error reading width from %s.\n", filename);
	//return 0;
    }
    printf("Width of %s: %lu\n", filename, image->sizeX);
    
    // read the height 
    if ((i = fread(&image->sizeY, 4, 1, file)) != 1) {
	printf("Error reading height from %s.\n", filename);
	//return 0;
    }
    printf("Height of %s: %lu\n", filename, image->sizeY);
    
    // calculate the size (assuming 24 bits or 3 bytes per pixel).
    size = image->sizeX * image->sizeY * 3;
    // read the planes
    if ((fread(&planes, 2, 1, file)) != 1) {
	printf("Error reading planes from %s.\n", filename);
	//return 0;
    }
    if (planes != 1) {
	printf("Planes from %s is not 1: %u\n", filename, planes);
	//return 0;
    }
    // read the bpp
    if ((i = fread(&bpp, 2, 1, file)) != 1) {
	printf("Error reading bpp from %s.\n", filename);
	//return 0;
    }
    if (bpp != 24) {
	printf("Bpp from %s is not 24: %u\n", filename, bpp);
	//return 0;
    }
	
    // seek past the rest of the bitmap header.
    fseek(file, 24, SEEK_CUR);
    // read the data. 
    image->data = (char *) malloc(size);
    if (image->data == NULL) {
	printf("Error allocating memory for color-corrected image data");
	//return 0;	
    }
    if ((i = fread(image->data, size, 1, file)) != 1) {
	printf("Error reading image data from %s.\n", filename);
	//return 0;
    }
    for (i=0;i<size;i+=3) { // reverse all of the colors. (bgr -> rgb)
	temp = image->data[i];
	image->data[i] = image->data[i+2];
	image->data[i+2] = temp;
    }
    
    // we're done.
    return 1;
}
// Load Bitmaps And Convert To Textures
void LoadGLTextures() {	
    // Load Texture
    Image *image1;
    
    // allocate space for texture
    image1 = (Image *) malloc(sizeof(Image));
    if (image1 == NULL) {
	printf("Error allocating space for image");
	//exit(0);
    }
    if (!ImageLoad("Image.bmp", image1)) {
	//exit(1);
    }        
    // Create Texture	
    GLCHK(glGenTextures(1, &texture[0]));
    GLCHK(glBindTexture(GL_TEXTURE_2D, texture[0]));   // 2d texture (x and y size)
    GLCHK(glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR)); // scale linearly when image bigger than texture
    GLCHK(glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR)); // scale linearly when image smalled than texture
    // 2d texture, level of detail 0 (normal), 3 components (red, green, blue), x size from image, y size from image, 
    // border 0 (normal), rgb color data, unsigned byte data, and finally the data itself.
    GLCHK(glTexImage2D(GL_TEXTURE_2D, 0, 3, image1->sizeX, image1->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, image1->data));
};
/* A general OpenGL initialization function.  Sets all of the initial parameters. */
void InitGL(int Width, int Height)	        // We call this right after our OpenGL window is created.
{
    //LoadGLTextures();					// Load The Texture(s) 
    GLCHK(glEnable(GL_TEXTURE_2D));			// Enable Texture Mapping
    GLCHK(glClearColor(0.0f, 0.0f, 1.0f, 0.0f));	// Clear The Background Color To Blue 
    GLCHK(glClearDepth(1.0));				// Enables Clearing Of The Depth Buffer
    GLCHK(glDepthFunc(GL_LESS));			// The Type Of Depth Test To Do
    GLCHK(glEnable(GL_DEPTH_TEST));			// Enables Depth Testing
    GLCHK(glShadeModel(GL_SMOOTH));			// Enables Smooth Color Shading
    
    GLCHK(glMatrixMode(GL_PROJECTION));
    GLCHK(glLoadIdentity());				// Reset The Projection Matrix
    
    GLCHK(gluPerspective(45.0f,(GLfloat)Width/(GLfloat)Height,0.1f,100.0f));	// Calculate The Aspect Ratio Of The Window
    
    GLCHK(glMatrixMode(GL_MODELVIEW));
}
static
void reshape (int w, int h)
{
        GLCHK(glViewport(0, 0, w, h));
        GLCHK(glMatrixMode(GL_PROJECTION));
        GLCHK(glLoadIdentity());
        GLCHK(glOrtho(-2, 2, -2, 2, -2, 2));
	GLCHK(gluPerspective(45.0f,(GLfloat)w/(GLfloat)h,0.1f,100.0f));
        GLCHK(glMatrixMode(GL_MODELVIEW));
        GLCHK(glLoadIdentity());
}
static float delta = 1.0;
static
void display (void)
{
    GLCHK(glShadeModel(GL_SMOOTH));
    GLCHK(glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT));		// Clear The Screen And The Depth Buffer
    GLCHK(glLoadIdentity());				// Reset The View
    GLCHK(glTranslatef(0.0f,0.0f,-5.0f));              // move 5 units into the screen.
    
    GLCHK(glRotatef(xrot,1.0f,0.0f,0.0f));		// Rotate On The X Axis
    GLCHK(glRotatef(yrot,0.0f,1.0f,0.0f));		// Rotate On The Y Axis
    GLCHK(glRotatef(zrot,0.0f,0.0f,1.0f));		// Rotate On The Z Axis
    GLCHK(glBindTexture(GL_TEXTURE_2D, texture[0]));   // choose the texture to use.
    GLCHK(glBegin(GL_QUADS));		                // begin drawing a cube
    
    // Front Face (note that the texture's corners have to match the quad's corners)
    GLCHK(glTexCoord2f(0.0f, 0.0f)); GLCHK(glVertex3f(-1.0f, -1.0f,  1.0f));	// Bottom Left Of The Texture and Quad
    GLCHK(glTexCoord2f(1.0f, 0.0f)); GLCHK(glVertex3f( 1.0f, -1.0f,  1.0f));	// Bottom Right Of The Texture and Quad
    GLCHK(glTexCoord2f(1.0f, 1.0f)); GLCHK(glVertex3f( 1.0f,  1.0f,  1.0f));	// Top Right Of The Texture and Quad
    GLCHK(glTexCoord2f(0.0f, 1.0f)); GLCHK(glVertex3f(-1.0f,  1.0f,  1.0f));	// Top Left Of The Texture and Quad
    
    // Back Face
    GLCHK(glTexCoord2f(1.0f, 0.0f)); GLCHK(glVertex3f(-1.0f, -1.0f, -1.0f));	// Bottom Right Of The Texture and Quad
    GLCHK(glTexCoord2f(1.0f, 1.0f)); GLCHK(glVertex3f(-1.0f,  1.0f, -1.0f));	// Top Right Of The Texture and Quad
    GLCHK(glTexCoord2f(0.0f, 1.0f)); GLCHK(glVertex3f( 1.0f,  1.0f, -1.0f));	// Top Left Of The Texture and Quad
    GLCHK(glTexCoord2f(0.0f, 0.0f)); GLCHK(glVertex3f( 1.0f, -1.0f, -1.0f));	// Bottom Left Of The Texture and Quad
	
    // Top Face
    GLCHK(glTexCoord2f(0.0f, 1.0f)); GLCHK(glVertex3f(-1.0f,  1.0f, -1.0f));	// Top Left Of The Texture and Quad
    GLCHK(glTexCoord2f(0.0f, 0.0f)); GLCHK(glVertex3f(-1.0f,  1.0f,  1.0f));	// Bottom Left Of The Texture and Quad
    GLCHK(glTexCoord2f(1.0f, 0.0f)); GLCHK(glVertex3f( 1.0f,  1.0f,  1.0f));	// Bottom Right Of The Texture and Quad
    GLCHK(glTexCoord2f(1.0f, 1.0f)); GLCHK(glVertex3f( 1.0f,  1.0f, -1.0f));	// Top Right Of The Texture and Quad
    
    // Bottom Face       
    GLCHK(glTexCoord2f(1.0f, 1.0f)); GLCHK(glVertex3f(-1.0f, -1.0f, -1.0f));	// Top Right Of The Texture and Quad
    GLCHK(glTexCoord2f(0.0f, 1.0f)); GLCHK(glVertex3f( 1.0f, -1.0f, -1.0f));	// Top Left Of The Texture and Quad
    GLCHK(glTexCoord2f(0.0f, 0.0f)); GLCHK(glVertex3f( 1.0f, -1.0f,  1.0f));	// Bottom Left Of The Texture and Quad
    GLCHK(glTexCoord2f(1.0f, 0.0f)); GLCHK(glVertex3f(-1.0f, -1.0f,  1.0f));	// Bottom Right Of The Texture and Quad
    
    // Right face
    GLCHK(glTexCoord2f(1.0f, 0.0f)); GLCHK(glVertex3f( 1.0f, -1.0f, -1.0f));	// Bottom Right Of The Texture and Quad
    GLCHK(glTexCoord2f(1.0f, 1.0f)); GLCHK(glVertex3f( 1.0f,  1.0f, -1.0f));	// Top Right Of The Texture and Quad
    GLCHK(glTexCoord2f(0.0f, 1.0f)); GLCHK(glVertex3f( 1.0f,  1.0f,  1.0f));	// Top Left Of The Texture and Quad
    GLCHK(glTexCoord2f(0.0f, 0.0f)); GLCHK(glVertex3f( 1.0f, -1.0f,  1.0f));	// Bottom Left Of The Texture and Quad
    
    // Left Face
    GLCHK(glTexCoord2f(0.0f, 0.0f)); GLCHK(glVertex3f(-1.0f, -1.0f, -1.0f));	// Bottom Left Of The Texture and Quad
    GLCHK(glTexCoord2f(1.0f, 0.0f)); GLCHK(glVertex3f(-1.0f, -1.0f,  1.0f));	// Bottom Right Of The Texture and Quad
    GLCHK(glTexCoord2f(1.0f, 1.0f)); GLCHK(glVertex3f(-1.0f,  1.0f,  1.0f));	// Top Right Of The Texture and Quad
    GLCHK(glTexCoord2f(0.0f, 1.0f)); GLCHK(glVertex3f(-1.0f,  1.0f, -1.0f));	// Top Left Of The Texture and Quad
    
    GLCHK(glEnd());                                    // 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();
    glutPostRedisplay();
}
static
void keydown (unsigned char key, int x, int y)
{
        switch (key) {
        case 'd':			/* delta, triangle */
                break;
        case 'o':			/* round */
                delta = 0.0f;
                break;
        case 'q':			/* square*/
                break;
        case 'x':			/* cross button */
                exit(0);
                break;
        default:
                ;
        }
}
static
void keyup (unsigned char key, int x, int y)
{
        switch (key) {
        case 'o':
                delta = 1.0f;
                break;
        default:
                ;
        }
}
static
void joystick (unsigned int buttonMask, int x, int y, int z)
{
        GLCHK(glClearColor(x * 1.0f/2000.0f + 0.5f, y * 1.0f/2000.0f + 0.5f, 1.0f, 1.0f));
}
int main(int argc, char* argv[])
{
        glutInit(&argc, argv);
        glutCreateWindow( __FILE__ );
        glutKeyboardFunc(keydown);
        glutKeyboardUpFunc(keyup);
        glutJoystickFunc(joystick, 0);
        glutReshapeFunc(reshape);
        glutDisplayFunc(display);
	InitGL(480, 272);
        glutMainLoop();
        return 0;
}
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.

