Ok, I found this code after searching for days, and to my delight it actually worked. It loads a TGA file into the memory... but I have no idea about how to load it into an OpenGL texture that I can use to texture a cube. I have literally spent days trying to work on this and I have no idea how to get it to work.
Here is the TGA Loader:
Link
Here is my overall code:
/* ************************************************************************************************ */
/* ****************************** INCLUDES *********************************** */
/* ************************************************************************************************ */
#include "SDL.h"
#include "SDL_opengl.h"
#include "stdio.h"
const int triangle = 1;
float currentX = 0.0;
float currentY = 0.0;
float currentZ = 0.0;
/* ************************************************************************************************ */
/* ********************* TGA LOADER *********************************** */
/* ************************************************************************************************ */
struct STGA
{
STGA()
{
data = (unsigned char*)0;
width = 0;
height = 0;
byteCount = 0;
}
~STGA() { delete[] data; data = 0; }
void destroy() { delete[] data; data = 0; }
int width;
int height;
unsigned char byteCount;
unsigned char* data;
};
bool loadTGA(const char *filename, STGA& tgaFile)
{
FILE *file;
unsigned char type[4];
unsigned char info[6];
file = fopen(filename, "rb");
if (!file)
return false;
fread (&type, sizeof (char), 3, file);
fseek (file, 12, SEEK_SET);
fread (&info, sizeof (char), 6, file);
//image type either 2 (color) or 3 (greyscale)
if (type[1] != 0 || (type[2] != 2 && type[2] != 3))
{
fclose(file);
return false;
}
tgaFile.width = info[0] + info[1] * 256;
tgaFile.height = info[2] + info[3] * 256;
tgaFile.byteCount = info[4] / 8;
if (tgaFile.byteCount != 3 && tgaFile.byteCount != 4) {
fclose(file);
return false;
}
long imageSize = tgaFile.width * tgaFile.height * tgaFile.width * tgaFile.byteCount;
//allocate memory for image data
tgaFile.data = new unsigned char[imageSize];
//read in image data
fread(tgaFile.data, sizeof(unsigned char), imageSize, file);
//close file
fclose(file);
return true;
}
/* ************************************************************************************************ */
/* ********************* CUBE DRAWER *********************************** */
/* ************************************************************************************************ */
void drawCube(float xloc, float yloc, float zloc)
{
glPushMatrix();
xloc = xloc*10;
yloc = yloc*10;
zloc = zloc*10;
STGA tgaFile;
if(loadTGA("data/image.tga", tgaFile)){
glBindTexture(GL_TEXTURE_2D, );
glTranslatef(xloc, yloc, zloc);
glBegin(GL_QUADS);
//front face
glColor3f(0.821, 0.525, 0.043);
glVertex3f(10/2, 10/2, 10/2);
glVertex3f(-10/2, 10/2, 10/2);
glVertex3f(-10/2, -10/2, 10/2);
glVertex3f(10/2, -10/2, 10/2);
//left face
glColor3f(0.821, 0.525, 0.043);
glVertex3f(-10/2, 10/2, 10/2);
glVertex3f(-10/2, 10/2, -10/2);
glVertex3f(-10/2, -10/2, -10/2);
glVertex3f(-10/2, -10/2, 10/2);
//back face
glColor3f(0.721, 0.425, 0.043);
glVertex3f(10/2, 10/2, -10/2);
glVertex3f(-10/2, 10/2, -10/2);
glVertex3f(-10/2, -10/2, -10/2);
glVertex3f(10/2, -10/2, -10/2);
//right face
glColor3f(0.821, 0.525, 0.043);
glVertex3f(10/2, 10/2, -10/2);
glVertex3f(10/2, 10/2, 10/2);
glVertex3f(10/2, -10/2, 10/2);
glVertex3f(10/2, -10/2, -10/2);
//top face
glColor3f(0.419, 0.556, 0.137);
glVertex3f(10/2, 10/2, 10/2);
glVertex3f(-10/2, 10/2, 10/2);
glVertex3f(-10/2, 10/2, -10/2);
glVertex3f(10/2, 10/2, -10/2);
//bottom face
glColor3f(1.0, 1.0, 1.0);
glVertex3f(10/2, -10/2, 10/2);
glVertex3f(-10/2, -10/2, 10/2);
glVertex3f(-10/2, -10/2, -10/2);
glVertex3f(10/2, -10/2, -10/2);
glEnd();
glPopMatrix();
}
}
/* ************************************************************************************************ */
/* ********************* INIT *********************************** */
/* ************************************************************************************************ */
void init()
{
glClearColor(0.0, 0.0, 0.0, 1.0); //RGB + Alpha
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45, 640 / 480, 1.0, 500.0); //sets camera params
glMatrixMode(GL_MODELVIEW);
SDL_WM_SetCaption( "OpenGL Game", NULL );
glShadeModel(GL_SMOOTH);
glEnable(GL_DEPTH_TEST);
glEnable(GL_TEXTURE_2D);
}
/* ************************************************************************************************ */
/* ********************* DISPLAY *********************************** */
/* ************************************************************************************************ */
void display(){
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
//glTranslatef(0.0, 0.0, -5.0);
//glRotatef(angle, 1.0, 1.0, 1.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-100.0f, 100.0f, -100.0f, 100.0f, -500.0f, 500.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glRotatef(35.264f, 1.0f, 0.0f, 0.0f);
glRotatef(-45.0f, 0.0f, 1.0f, 0.0f);
glScalef(1.0f, 1.0f, -1.0f);
//current cube position
drawCube(currentX, currentY, currentZ);
}
/* ************************************************************************************************ */
/* ********************* MAIN *********************************** */
/* ************************************************************************************************ */
int main( int agrc, char* args[] )
{
SDL_Init(SDL_INIT_EVERYTHING);
SDL_Surface* screen=SDL_SetVideoMode(600,400,32, SDL_SWSURFACE|SDL_OPENGL );
bool running = true;
Uint32 start;
SDL_Event event;
init();
while(running)
{
start=SDL_GetTicks();
if (SDL_PollEvent(&event))
{
if (event.type == SDL_QUIT){
running = false;
}
if (event.type == SDL_KEYDOWN){
SDLKey keyPressed = event.key.keysym.sym;
switch (keyPressed)
{
case SDLK_ESCAPE:
running = false;
break;
case SDLK_UP:
currentZ++;
break;
case SDLK_DOWN:
currentZ--;
break;
case SDLK_RIGHT:
currentX++;
break;
case SDLK_LEFT:
currentX--;
break;
}
}
}
display();
SDL_GL_SwapBuffers();
if(1000/30>(SDL_GetTicks()-start))
SDL_Delay(1000/30>(SDL_GetTicks()-start));
}
SDL_Quit();
return 0;
}
All I need is to find a way to make a texture ID and make OpenGL load it into that cube in the drawCube() function.
Thanks,
Jungle