zaczynam zabawę z OpneGL i mam następujący problem:
chciałem wszystko z kursu NeHe powoli pozamykać w klasy (później będzie mi wygodniej). Zrobiłem 3 klasy:
Image
#include <windows.h> // Nagłówek dla Windows
#include <stdio.h> // Standardowe wejście/wyjście ( NOWE )
#include <gl\gl.h> // Nagłówek dla OpenGL
#include <gl\glu.h> // Nagłówek dla GLu32
#include <gl\glaux.h> // Nagłówek dla GLaux
#include "Image.h"
Image::Image(char *fileName)
{
this->loadImage(fileName);
}
bool Image::loadImage(char *fileName)
{
FILE *File=NULL; // Uchwyt pliku
if (!fileName) // Upewnij się że podano nazwę pliku
{
this->Success=false;
return false; // Jeżeli nie to zwróć NULL
}
File=fopen(fileName,"r"); // Sprawdź czy plik istnieje
if (File) // Czy plik istnieje
{
fclose(File); // Zamknij plik
this->File=auxDIBImageLoad(fileName);
this->Success=true;
return true; // Załaduj bitmapę i zwróć wskaźnik
}
MessageBox(0,"Podany plik nie istnieje!","Błąd",0);
this->Success=false;
return false;
}
AUX_RGBImageRec *Image::getHandle()
{
return this->File;
}
bool Image::loaded()
{
return this->Success;
}
Texture
#include <gl\glaux.h> // Nagłówek dla GLaux
#include "Texture.h"
#include "Image.h"
Texture::Texture(char * fileName)
{
Image *img = new Image(fileName);
static GLuint x=1;
if (!img->loaded())
{
delete img;
this->Success=false;
return;
}
this->File=img->getHandle();
delete img;
glGenTextures(1, &x); // Stwórz obiekt tekstury
// Powiązanie z obiektem tekstury
glBindTexture(GL_TEXTURE_2D, x);
// Stwórz teksturę
glTexImage2D(GL_TEXTURE_2D, 0, 3, this->File->sizeX, this->File->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, this->File->data);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR); // Filtrowanie liniowe
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR); // Filtrowanie liniowe
this->WhichTexture=x;
x++;
}
Texture::~Texture()
{
//zwalniam zasoby
if (this->File)
{
if (this->File->data)
free(this->File->data);
free(this->File);
}
}
AUX_RGBImageRec *Texture::getHandle()
{
return this->File;
}
bool Texture::loaded()
{
return this->Success;
}
char * Texture::getFileName()
{
return this->FileName;
}
GLuint Texture::getTexture()
{
return this->WhichTexture;
}
i Tank
#include <gl\glaux.h> // Nagłówek dla GLaux
#include "Texture.h"
#include "Tank.h"
#include "header.h"
Tank::Tank(char * fileName, GLfloat x, GLfloat y)
{
static int countTextures=0;
t[countTextures] = new Texture(fileName);
if (!t[countTextures]->loaded())
{
this->Success=false;
delete t;
return;
}
this->Success=true;
this->File=t[countTextures]->getHandle();
this->WhichTexture=t[countTextures]->getTexture();
this->X=x;
this->Y=y;
this->pos=UP; //ustawiam pozycję czołgu (wycelowany do góry)
}
Tank::~Tank()
{
delete t;
}
void Tank::print()
{
glBindTexture(GL_TEXTURE_2D, this->WhichTexture); // Wybierz naszą teksturę
glBegin(GL_QUADS); // Rysujemy kwadrat
glVertex3f( X+0.0f, Y+unit, 0.0f); // górny lewy
glVertex3f( X+unit, Y+unit, 0.0f); // górny prawy
glVertex3f( X+unit, Y+0.0f, 0.0f); // dolny prawy
glVertex3f( X+0.0f, Y+0.0f, 0.0f); // dolny lewy
glEnd(); // Koniec rysowania kwadratu
}
void Tank::moveDown()
{
this->Y-=step;
}
void Tank::moveUp()
{
this->Y+=step;
}
void Tank::moveRight()
{
this->X+=step;
}
void Tank::moveLeft()
{
this->X-=step;
}
bool Tank::loaded()
{
return this->Success;
}
i potem mam funkcję LoadGLTextures
int LoadGLTextures() // Ładuj bitmapę i przekształć ją w teksturę
{
int Status=true; // Status tworzenia tekstury
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR); // Filtrowanie liniowe
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR); // Filtrowanie liniowe
tank= new Tank("tank1.bmp",0,0);
if (!tank->loaded())
{
return false;
}
return Status; // Zwróć status operacji
}
a na samym końcu InitGL
int InitGL(GLvoid)
{
if (!LoadGLTextures()) // Załaduj tekstury ( NOWE )
{
return FALSE; // Jeżeli tekstury nie załadowano zrwóć FALSE ( NOWE )
}
glEnable(GL_TEXTURE_2D); // Włącz nakładanie tekstur ( NOWE )
glShadeModel(GL_SMOOTH);
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glClearDepth(1.0f); // ustawienie bufora głebi
glEnable(GL_DEPTH_TEST); // włączenie testowania głębi
glDepthFunc(GL_LEQUAL); // ustawienie typu testowania
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
return true;
}
nie wczytuje mi tekstury i nie wiem dlaczego :( na ekranie mam biały kwadracik ;P