PDA

View Full Version : i need help in opengl texture loading



kane96
08-30-2016, 01:57 PM
hi i start learning opengl trough 3dbuzz opengl in depth tutorials , i know it's out dated but since i can't find another one so it's not bad for just beginning ,the problem is in texture lesson i followed every single line of code like they did but in the end my texture not showing on the cube , it's TGA image and i tested it with another code and it work,so i don't know what is the wrong with my code. this is my cpp file
#include "texture.h"

vector<Texture *>Texture::textures;

Texture::Texture(string in_filename, string in_name)
{
imageData = NULL;

loadTGA(in_filename);

name = in_name;

textures.push_back(this);
}

Texture::~Texture()
{
for ( vector<Texture *>::iterator it = textures.begin(); it != textures.end(); it++)
{
if ( (*it) == this )
{
textures.erase(it);
}
}

if (imageData)
{
delete imageData;
}
}

bool Texture::loadTGA(string filename)
{
TGA_Header TGAheader;

ifstream file( filename.data(), std::ios_base::binary );

if ( file.is_open() )
{
return false;
}


if (!file.read( (char * )&TGAheader, sizeof(TGAheader)))
{
return false;
}


if ( TGAheader.ImageType != 2)
{
return false;
}


width = TGAheader.imageWidth;
height = TGAheader.imageHeight;
bpp = TGAheader.pixelDepth;

if ( width <=0 || height <=0 || (bpp != 24 && bpp != 32) )
{
return false;
}

GLuint type = GL_RGBA;
if ( bpp == 24 )
{
type = GL_RGB;
}

GLuint bytesPerPixel = bpp / 8;
GLuint imageSize = width * height * bytesPerPixel;

imageData = new GLubyte[imageSize];

if ( imageData == NULL)
{
return false;
}

if (!file.read( ( char*)imageData, imageSize))
{
delete imageData;

return false;
}
//Concerts BGR--> TO RGB;
for ( GLuint i = 0; i < (int)imageSize; i+=bytesPerPixel)
{
GLuint temp = imageData[i];
imageData[i] = imageData[i+2];
imageData[i+2] = temp;
}

createTexture(imageData, width, height, type);

//No problems

return true;

}

bool Texture::createTexture ( unsigned char *imageData, int width, int height, int type)
{
glGenTextures(1, &texID);
glBindTexture(GL_TEXTURE_2D, texID);

glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);


glTexImage2D(GL_TEXTURE_2D,0,type, width, height, 0 , type, GL_UNSIGNED_BYTE, imageData);
return true;
}

my header file


#ifndef TEXTURE_H
#define TEXTURE_H
#include <string>
#include <vector>
#include <fstream>
using std::string;
using namespace std;


#ifdef WIN32
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#endif

#if defined(__APPLE__) && defined(__MACH__)
#include <OpenGL/gl.h>
#include <OpenGL/glu.h>
#else
#include <GL/glu.h>
#include <GL/gl.h>
#endif

struct TGA_Header
{
GLubyte ID_Length;
GLubyte ColorMapType;
GLubyte ImageType;
GLubyte ColorMapSpecification[5];
GLshort xOrigin;
GLshort yOrigin;
GLshort imageWidth;
GLshort imageHeight;
GLubyte pixelDepth;
};

class Texture
{
public:
Texture( string filename, string name = " ");
~Texture();

public:
unsigned char *imageData;
unsigned int bpp; //bits per pixel
unsigned int width;
unsigned int height;
unsigned int texID;

string name;

static vector<Texture *> textures;

private:
bool loadTGA( string filename);
bool createTexture( unsigned char *imageData, int width, int height, int type);
};

#endif

and my main

#include "texture.h"
#include <GL/gl.h>
#include <GL/glu.h>
#include <SDL.h>
#ifdef WIN32
#include <windows.h>
#endif

UINT8 *keys=NULL;
const GLsizei windowWidth=500;
const GLsizei windowHight=500;
GLfloat cuberotatX=45;
GLfloat cuberotatY=45;

Texture *textures=NULL;
GLvoid establishprojectmatrix(GLsizei width,GLsizei height)
{
glViewport(0,0,width,height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0f,(GLfloat)width/(GLfloat)height,0.1f,200.f);
}
GLvoid initgl(GLsizei width,GLsizei height)
{
establishprojectmatrix(width,height);
glShadeModel(GL_SMOOTH);
glClearColor(0.0f,0.0f,0.0f,1.0f) ;
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
glHint(GL_PERSPECTIVE_CORRECTION_HINT,GL_NICEST);
glEnable(GL_PERSPECTIVE_CORRECTION_HINT);
glEnable(GL_TEXTURE_2D);
textures= new Texture("data/myTex.tga","lklk");


}
/*GLvoid displayfps()
{
static long lasttime=SDL_GetTicks();
static long loops=0;
static GLfloat fps=0.0f;
int newtime=SDL_GetTicks();
if(newtime-lasttime>100)
{float newfps =(float)loops/(float)(newtime-lasttime)*100.0f;
fps=(fps+newfps)/2.0f;
char title[80];
sprintf(title,"Opengl demo -%0.2f",fps);
SDL_WM_SetCaption(title,NULL);
lasttime=newtime;
loops++;
}
}*/
GLvoid drawscene()
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(0,0,-5.0f);
glRotatef(cuberotatX,1,0,0);
glRotatef(cuberotatY,0,1,0);
glBindTexture(GL_TEXTURE_2D,textures->texID);
textures= new Texture("data/Uncompressed.tga","test texture");

glBegin(GL_QUADS);
//top face;
//glColor3f(1.0f,0.5f,0.0f);
glTexCoord2f(1.0f,1.0f);
glVertex3f(1.0f,1.0f,-1.0f);
glTexCoord2f(0.0f,1.0f);
glVertex3f(-1.0f,1.0f,-1.0f);
glTexCoord2f(0.0f,0.0f);
glVertex3f(-1.0f,1.0f,1.0f);
glTexCoord2f(1.0f,0.0f);
glVertex3f(1.0f,1.0f,1.0f);
// bottom face
//glColor3f(0.0f,1.0f,0.0f);
glTexCoord2f(1.0f,1.0f); glVertex3f(1.0f,-1.0f,-1.0f);
glTexCoord2f(0.0f,1.0f); glVertex3f(-1.0f,-1.0f,-1.0f);
glTexCoord2f(0.0f,0.0f);glVertex3f(-1.0f,-1.0f,1.0f);
glTexCoord2f(1.0f,0.0f); glVertex3f(1.0f,-1.0f,1.0f);
// front face
// glColor3f(0.0f,0.0f,1.0f);
glTexCoord2f(1.0f,1.0f); glVertex3f(1.0f,1.0f,1.0f);
glTexCoord2f(0.0f,1.0f); glVertex3f(-1.0f,1.0f,1.0f);
glTexCoord2f(0.0f,0.0f);glVertex3f(-1.0f,-1.0f,1.0f);
glTexCoord2f(1.0f,0.0f);glVertex3f(1.0f,-1.0f,1.0f);
//back face
//glColor3f(1.0f,0.0f,0.0f);

glTexCoord2f(1.0f,1.0f); glVertex3f(1.0f,-1.0f,-1.0f);
glTexCoord2f(0.0f,1.0f); glVertex3f(-1.0f,-1.0f,-1.0f);
glTexCoord2f(0.0f,0.0f); glVertex3f(-1.0f,1.0f,-1.0f);
glTexCoord2f(1.0f,0.0f); glVertex3f(1.0f,1.0f,-1.0f);
//right face
//glColor3f(1.0f,0.0f,1.0f);
glTexCoord2f(1.0f,1.0f);glVertex3f(1.0f,1.0f,1.0f) ;
glTexCoord2f(0.0f,1.0f);glVertex3f(1.0f,1.0f,-1.0f);
glTexCoord2f(0.0f,0.0f);glVertex3f(1.0f,-1.0f,-1.0f);
glTexCoord2f(1.0f,0.0f); glVertex3f(1.0f,-1.0f,1.0f);
//left face

//glColor3f(1.0f,0.0f,1.0f);
glTexCoord2f(1.0f,1.0f);glVertex3f(-1.0f,1.0f,1.0f);
glTexCoord2f(0.0f,1.0f);glVertex3f(-1.0f,1.0f,-1.0f);
glTexCoord2f(0.0f,0.0f);glVertex3f(-1.0f,-1.0f,-1.0f);
glTexCoord2f(1.0f,0.0f);glVertex3f(-1.0f,-1.0f,1.0f);
glEnd();


glFlush();
SDL_GL_SwapBuffers();
//displayfps();
}

GLboolean checkkeys()
{
const GLfloat speed=0.02f;
if (keys[SDLK_ESCAPE])
return true;




if(keys[SDLK_LEFT])
cuberotatY-=speed;

if(keys[SDLK_RIGHT])
cuberotatY+=speed;

if(keys[SDLK_UP])
cuberotatX-=speed;

if(keys[SDLK_DOWN])
cuberotatX+=speed;


return false;

}
/*GLvoid timerloop(int value)
{
if(checkkeys())
exit(0);
glutPostRedisplay();

glutTimerFunc(1,timerloop,0);
}*/



int main (int argc, char *argv[])
{
if(SDL_Init(SDL_INIT_VIDEO)<0)
{

}
if(SDL_SetVideoMode(windowWidth,windowHight,0,SDL_ OPENGL)==NULL)
{

}
initgl(windowWidth,windowHight);

int done=0;
while (!done)
{drawscene();
SDL_Event event;
while (SDL_PollEvent(&event))
{
if(event.type==SDL_QUIT)
done=1;
keys=SDL_GetKeyState(NULL);
}
if(checkkeys())
done=1;
}
SDL_Quit();
return 1;
}

i hope someone have an answer , and sorry for my bad english.

Silence
08-30-2016, 11:31 PM
Use an image loader library. I suggest you this one (http://openil.sourceforge.net/).

For a quite recent tutorial, you can visit this page (http://www.opengl-tutorial.org/) or this one (https://open.gl/) or even this one (http://learnopengl.com/). They all were quickly found and look to cover most important aspects.