Jump to content
  • Advertisement
Sign in to follow this  
JoshuaWaring

OpenGL OpenGL TGA Textures

This topic is 2606 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I've been looking at NeHe for the texture loader... but all that renders is a White screen

Header

#include <gl\glut.h>
#include <gl\GLU.h>
#include <gl\GL.h>
#include <cstdlib>
#include <iostream>
#include <stdio.h>
#include <Windows.h>

int short DrawCube(0);
int short DrawPlain(0);

#define TextureNum 20

bool keys[256];
bool active=true;
bool fullscreen=false;

GLuint TextureFace(0);
/*
Display list ID calls Texture ID
0: Cube 0: Happy Face
1: Plain
*/





typedef struct{
GLubyte *imageData;
GLuint bpp;
GLuint width;
GLuint height;
GLuint texID;
}TextureImage;

TextureImage Texture[TextureNum];


bool LoadTGA(TextureImage *texture, char *filename) // Loads A TGA File Into Memory
{
GLubyte TGAheader[12]={0,0,2,0,0,0,0,0,0,0,0,0}; // Uncompressed TGA Header
GLubyte TGAcompare[12]; // Used To Compare TGA Header
GLubyte header[6]; // First 6 Useful Bytes From The Header
GLuint bytesPerPixel; // Holds Number Of Bytes Per Pixel Used In The TGA File
GLuint imageSize; // Used To Store The Image Size When Setting Aside Ram
GLuint temp; // Temporary Variable
GLuint type=GL_RGBA; // Set The Default GL Mode To RBGA (32 BPP)

FILE *file = fopen(filename, "rb"); // Open The TGA File

if( file==NULL || // Does File Even Exist?
fread(TGAcompare,1,sizeof(TGAcompare),file)!=sizeof(TGAcompare) || // Are There 12 Bytes To Read?
memcmp(TGAheader,TGAcompare,sizeof(TGAheader))!=0 || // Does The Header Match What We Want?
fread(header,1,sizeof(header),file)!=sizeof(header)) // If So Read Next 6 Header Bytes
{
if (file == NULL) // Does The File Even Exist? *Added Jim Strong*
return FALSE; // Return False
else // Otherwise
{
fclose(file); // If Anything Failed, Close The File
return FALSE; // Return False
}
}

texture->width = header[1] * 256 + header[0]; // Determine The TGA Width (highbyte*256+lowbyte)
texture->height = header[3] * 256 + header[2]; // Determine The TGA Height (highbyte*256+lowbyte)

if( texture->width <=0 || // Is The Width Less Than Or Equal To Zero
texture->height <=0 || // Is The Height Less Than Or Equal To Zero
(header[4]!=24 && header[4]!=32)) // Is The TGA 24 or 32 Bit?
{
fclose(file); // If Anything Failed, Close The File
return FALSE; // Return False
}

texture->bpp = header[4]; // Grab The TGA's Bits Per Pixel (24 or 32)
bytesPerPixel = texture->bpp/8; // Divide By 8 To Get The Bytes Per Pixel
imageSize = texture->width*texture->height*bytesPerPixel; // Calculate The Memory Required For The TGA Data

texture->imageData=(GLubyte *)malloc(imageSize); // Reserve Memory To Hold The TGA Data

if( texture->imageData==NULL || // Does The Storage Memory Exist?
fread(texture->imageData, 1, imageSize, file)!=imageSize) // Does The Image Size Match The Memory Reserved?
{
if(texture->imageData!=NULL) // Was Image Data Loaded
free(texture->imageData); // If So, Release The Image Data

fclose(file); // Close The File
return FALSE; // Return False
}
for(GLuint i=0; i<int(imageSize); i+=bytesPerPixel) // Loop Through The Image Data
{ // Swaps The 1st And 3rd Bytes ('R'ed and 'B'lue)
temp=texture->imageData; // Temporarily Store The Value At Image Data 'i'
texture->imageData = texture->imageData[i + 2]; // Set The 1st Byte To The Value Of The 3rd Byte
texture->imageData[i + 2] = temp; // Set The 3rd Byte To The Value In 'temp' (1st Byte Value)
}
fclose (file); // Close The File

// Build A Texture From The Data
glGenTextures(1, &texture[0].texID); // Generate OpenGL texture IDs

glBindTexture(GL_TEXTURE_2D, texture[0].texID); // Bind Our Texture
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); // Linear Filtered
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); // Linear Filtered

if (texture[0].bpp==24) // Was The TGA 24 Bits
{
type=GL_RGB; // If So Set The 'type' To GL_RGB
}

glTexImage2D(GL_TEXTURE_2D, 0, type, texture[0].width, texture[0].height, 0, type, GL_UNSIGNED_BYTE, texture[0].imageData);

return true; // Texture Building Went Ok, Return True
}

int DisplayModel(int short ListID)
{
switch (ListID)
{
case(0):
{

DrawCube = glGenLists(1);

glNewList(DrawCube, GL_COMPILE);
glBegin(GL_POINTS);
glVertex3f(10, -10, -10);
glVertex3f(-10,-10, -10);
glVertex3f(-10, 10, -10);
glVertex3f(10, 10, -10);
glVertex3f(10, -10, 10);
glVertex3f(-10, -10, 10);
glVertex3f(-10, 10, 10);
glVertex3f(10, 10, 10);
glEnd();
glEndList();

return(DrawCube);
break;
}
case(1):
{
DrawPlain = glGenLists(1);
glNewList(DrawPlain, GL_COMPILE);
glBegin(GL_POINTS);
/* Points for a Plain*/
glEnd();
glEndList();

return(DrawPlain);
break;
}
default:
break;
}

return 0;
}

void DisplayTexture(int short ListID)
{
switch(ListID){
case(0):
// Texture Smily face
LoadTGA(&Texture[0] ,"Bitmap.tga");
break;
default:
break;
}
}



Main


#include <iostream>
#include <gl\glut.h>
#include "DisplayList.h"
//#include "Keyboard.h"

void Cleanup(){
glClear(GL_COLOR_BUFFER_BIT);

}

void Rendercontrol (){
Cleanup();
glOrtho(100,100,100,100,100,100);


glBindTexture(GL_TEXTURE_2D, Texture[0].texID);
glPushMatrix();
glColor3f(1, 1, 1);
glCallList(TextureFace);
glCallList(DrawCube);
glPopMatrix();
glLoadIdentity();
glFlush();
glutSwapBuffers();


}

int main(int argv, char **argc){
glutInit(&argv, argc); // Initilization of the program
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA); // Double Buffer and RGB W/ Alpha
glutInitWindowSize(800, 600); // 800 by 600 Px
glutCreateWindow("Test"); // Name and creation of window
glClearColor(1,1,1,0); // Screen clearing Color

DisplayModel(0);
DisplayTexture(0);
LoadTGA(&Texture[0] ,"Bitmap.tga");

glEnable(GL_DEPTH_TEST); // Depth correction
glEnable(GL_TEXTURE_2D);


glutDisplayFunc(Rendercontrol); // Function to deal with rendering when an action has occured
glutIdleFunc(Rendercontrol); // Idle rendering
//glutMouseFunc(); // Function callout to deal with mouse actions
//glutKeyboardFunc(Keyboard); // Function callout to deal with Keyboard actions

glutMainLoop();
}

Share this post


Link to post
Share on other sites
Advertisement
glEnable(GL_TEXTURE_2D) - checked.

glTexCoord (before each glVertex call) - nowhere. <<

-------------------------------------------------------------------

glEnable(GL_DEPTH_TEST) - you enabled depth testing but you never cleaning it (depth buffer).

------------------------------------------------------------------

glOrtho(100,100,100,100,100,100) - bad usage of matrix setup (are you trying to render a single point?)

your primitive never would be projected into screen with this matrix.




As the results, you seeing white screen because you set clear color to white color (and nothing other would be drawn).




Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!