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);
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[8][8] =
{
{1,0,1,0,1,0,1,0},
{0,1,0,1,0,1,0,1},
{0,0,1,0,1,0,1,0},
{1,0,0,1,0,1,0,1},
{0,1,0,0,1,0,1,0},
{1,0,1,0,0,1,0,1},
{0,1,0,1,0,0,1,0},
{1,0,1,0,1,0,0,1}
};
// this will determine the height of each tile
static int height[8][8] =
{
{8,8,8,8,8,8,8,8},
{8,2,2,3,4,4,3,6},
{8,2,3,3,4,3,2,4},
{8,3,4,4,3,2,1,2},
{8,3,4,3,2,1,1,2},
{8,4,3,2,1,2,1,4},
{8,3,2,1,1,1,1,6},
{9,8,2,8,4,8,0,8}
};
// these two arrays contain the texture information
// relevant to the demonstration.
GLuint surfaceTex[2];
GLuint edgeTex[1];
// 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()
{
// Load textures from file.
SDL_Surface *t0 = SDL_LoadBMP("000.bmp");
SDL_Surface *t1 = SDL_LoadBMP("001.bmp");
SDL_Surface *h0 = SDL_LoadBMP("100.bmp");
// allocate the texture slots.
glGenTextures(2, surfaceTex);
glGenTextures(1, edgeTex);
// setup the texture parameter
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);
// more texture loading
// Release the surfaces.
SDL_FreeSurface(t0);
SDL_FreeSurface(t1);
SDL_FreeSurface(h0);
}
// This code frees up the allocated textures.
void DeleteTextures()
{
glDeleteTextures(2, surfaceTex);
glDeleteTextures(1, edgeTex);
}
void DrawGridPiece(int x, int y, int h, GLint tileTex, GLint borderTex)
{
glPushMatrix();
glBindTexture(GL_TEXTURE_2D, tileTex);
glTranslatef(x*64, h*16, y*64);
glBegin(GL_QUADS);
glTexCoord2f(0,0);
glVertex3f(0,0,0);
glTexCoord2f(0,1);
glVertex3f(0,0,64);
glTexCoord2f(1,1);
glVertex3f(64,0,64);
glTexCoord2f(1,0);
glVertex3f(64,0,0);
glEnd();
glPopMatrix();
// more drawing code
}
void SetupScene()
{
glViewport(0,0,480,272);
// glEnable(GL_TEXTURE_2D); // uncommenting this causes a crash
glEnable(GL_CULL_FACE);
glCullFace(GL_BACK);
glEnable(GL_DEPTH_TEST);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-100,100,-200,200,-300,300);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glRotatef(35, 1, 0, 0);
}
void Render()
{
glClearColor(0,0,0,0);
glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
glPushMatrix();
glRotatef(rAngle, 0,1,0);
glTranslatef(xCam, 0, yCam);
glTranslatef(0,-30,0);
for(int y = 0; y < 8; y +=1)
{
for(int x = 0; x < 8; x += 1)
{
DrawGridPiece(x,y,height[y][x], surfaceTex[level[y][x]],edgeTex[0]);
//DrawGridPiece(x,y,height[y][x], surfaceTex[0],0);
}
}
glPopMatrix();
SDL_GL_SwapBuffers();
}
PSP_MAIN_THREAD_ATTR(THREAD_ATTR_USER | THREAD_ATTR_VFPU);
extern "C"
int SDL_main(int argc, char *argv[])
{
SDL_Init(SDL_INIT_EVERYTHING);
//SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
SDL_SetVideoMode(480,272,32,SDL_OPENGL);
SetupScene();
BuildTextures();
SDL_Event e;
while(true)
{
SDL_PollEvent(&e);
if(e.type == SDL_QUIT)
break;
Render();
}
DeleteTextures();
SDL_Quit();
return 0;
}