Opengl- 텍스처가 부적절하게로드됩니다
문제
업데이트:
여기 에이 코드가 문제가되지 않기 때문에 아래에 렌더러 코드를 게시했습니다.
한 번에 하나씩 OpenGL에 여러 텍스처를 업로드하려고 할 때 렌더러가 단일 텍스처를 사용하여 끝나는 경우 다소 장관이 실패하는 일부 코드에 문제가 있습니다. 이 기능에 오류를 추적하기 위해 약간의 디버깅을 수행했지만 기능의 일부가 결함이있는 부분을 파악하는 데 문제가 있습니다. 내가 단순히 보지 못하는 것 또는 내 코드에 더 미묘한 결함이 있습니까?
다음은 텍스처 정보를 저장하는 데 사용하고 일반적으로 모든 포인터를 추적하는 데 사용하는 스트러크입니다.
typedef struct {
float Width;
float Height;
} texInfo;
typedef struct {
dshlib::utfstr ResourceName;
texInfo * TextureInfo;
GLuint TextureNum;
SDL_Surface * Image;
} texCacheItem;
현재 WIP 그래픽 로더가 있습니다. 기본적으로 Prewritten 라이브러리를 사용하여 .zip 아카이브에서 이름 .png 파일을로드합니다 (우연히이 프로그램에서 테스트 중). 그런 다음 libpng로로드 된 다음 텍스처로로드하고 캐싱이 적재 속도를 높이고 단일 텍스처를 두 번 이상로드하지 않도록합니다. #include 진술이 단지 cruft이기 때문에 생략했습니다.
texCacheItem * loadGraphics(dshlib::utfstr FileName) {
for(int i = 0; i < NumTexCached; i++) { //First see if this texture has already been loaded
if(TextureCache[i]->ResourceName == FileName)
return TextureCache[i];
}
dshlib::utfstr FullFileName = "Data/Graphics/"; //If not, create the full file path in the archive
FullFileName += FileName;
dshlib::FilePtr file = resourceCtr.OpenFile(FullFileName); //And open the file
if (!file->IsOk()) { //If the file failed to load...
EngineState = ENGINESTATE_ERR;
return NULL;
}
SDL_Surface * T = loadPNG(file);
texCacheItem * Texture = new texCacheItem;
Texture->TextureInfo = new texInfo;
glGenTextures(1, &Texture->TextureNum); //Allocate one more texture and save the name to the texCacheItem
glBindTexture(GL_TEXTURE_2D, Texture->TextureNum); //Then create it
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, T->w, T->h, 0, GL_RGBA, GL_UNSIGNED_BYTE, T->pixels);
Texture->TextureInfo->Width = (float)T->w; //Write the useful data
Texture->TextureInfo->Height = (float)T->h;
Texture->ResourceName = FileName; //And the caching info needed
Texture->Image = T; //And save the image for if it's needed later and for deleting
if (!TexCacheSize) { //If this is the first load this is 0, so allocate the first 8 Cache slots.
TexCacheSize = 8;
TextureCache = new texCacheItem*[8];
}
if(NumTexCached == TexCacheSize) { //If we're out of cache space
if (TexCacheSize == 32768) { //If too many cache items, error out
EngineState = ENGINESTATE_ERR;
return NULL;
}
TexCacheSize <<= 1; //Double cache size
texCacheItem ** NewSet = new texCacheItem*[TexCacheSize];
memcpy(NewSet, TextureCache, NumTexCached * sizeof(texCacheItem*)); //And copy over the old cache
delete TextureCache; //Delete the old cache
TextureCache = NewSet; //And assign the pointer to the new one
}
TextureCache[NumTexCached++] = Texture; //Store the texCacheItem to the Cache
file->Close(); //Close the file
file = NULL; //And NULL the smart pointer. [NTS: Confirm with Disch this is what won't cause a memory leak]
return Texture; //And return the loaded texture in texCacheItem form.
}
SDL_Surface *loadPNG(dshlib::FilePtr File)
{
Uint8 *PNGFile = new Uint8[(long)File->GetSize()];
File->GetAr<Uint8>(PNGFile, (long)File->GetSize());
return IMG_LoadPNG_RW(SDL_RWFromMem(PNGFile, (long)File->GetSize()));
}
렌더러 코드 파일은 다음과 같습니다. 지금은 매우 지저분합니다. 사과드립니다. Level-> activemap은 기본적으로 Tilemap의 "레이어"(0 전면, 3 뒷면)의 렌더러에게 위의 스프라이트를 그리도록 지시합니다.
#include "../MegaJul.h"
void render(void) {
//Render the current tilemap to the screen
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glTranslatef(0.0f, 0.0f, -4.0f);
if (level) {
glBegin(GL_QUADS);
float dT = 32.0f / level->dTex;
float sX, fX, fXa, sY, tX, tY, sYa, sYb, sXa, tXa, tYa;
unsigned long m = level->mapDimensions[0] * level->mapDimensions[1];
float ai; long long t; Sint16 * p;
glBindTexture(GL_TEXTURE_2D, level->tilemap->TextureNum);
for (int i = 3; i >= 0; i--) {
if (level->layers[i]->mapPosition[0] > 0)
level->layers[i]->mapPosition[0] = 0;
if (level->layers[i]->mapPosition[0] < 0 - (signed long)((level->mapDimensions[0] - 21) * 32))
level->layers[i]->mapPosition[0] = 0 - (signed long)((level->mapDimensions[0] - 21) * 32);
if (level->layers[i]->mapPosition[1] < 0)
level->layers[i]->mapPosition[1] = 0;
if (level->layers[i]->mapPosition[1] > (signed long)((level->mapDimensions[1] - 16) * 32))
level->layers[i]->mapPosition[1] = (signed long)((level->mapDimensions[1] - 16) * 32);
if (i == level->activeMap) {
for (int j = 0; j < NumSprites; j++) {
glBindTexture(GL_TEXTURE_2D, Sprites[j]->Graphics->TextureNum);
Sprites[j]->render(level->layers[i]->mapPosition[0], level->layers[i]->mapPosition[1]);
}
for (int j = 0; j < NumBullets; j++) {
glBindTexture(GL_TEXTURE_2D, Bullets[j]->Texture->TextureNum);
Bullets[j]->render(level->layers[i]->mapPosition[0], level->layers[i]->mapPosition[1]);
}
}
glBindTexture(GL_TEXTURE_2D, level->tilemap->TextureNum);
t = 0 - ((level->layers[i]->mapPosition[0] - (level->layers[i]->mapPosition[0] % 32)) /32) + (((level->layers[i]->mapPosition[1] - (level->layers[i]->mapPosition[1] % 32)) /32) * level->mapDimensions[0]);
ai = (float)(3 - i); //Invert Z-Index
sX = (float)((level->layers[i]->mapPosition[0] % 32));
sY = (float)((level->layers[i]->mapPosition[1] % 32));
if (sX > 0)
sX -= 32;
if (sY < 0)
sY += 32;
fX = sX /= 32.0f;
sY /= 32.0f;
fXa = sXa = sX + 1.0f;
sYa = sY + 14.0f;
sYb = sY + 15.0f;
for (int y = 0; y < 16; y++) {
for (int x = 0; x < 21; x++) {
p = level->tiles[level->layers[i]->map[t]]->position;
tX = p[0] / level->dTex;
tY = p[1] / level->dTex;
tXa = tX + dT;
tYa = tY + dT;
glTexCoord2f(tX, tYa); glVertex3f(fX, sYa, ai); // Bottom Left Of The Texture and Quad
glTexCoord2f(tXa,tYa); glVertex3f(fXa, sYa, ai); // Bottom Right Of The Texture and Quad
glTexCoord2f(tXa,tY); glVertex3f(fXa, sYb, ai); // Top Right Of The Texture and Quad
glTexCoord2f(tX, tY); glVertex3f(fX, sYb, ai); // Top Left Of The Texture and Quad
fX += 1.0f;
fXa += 1.0f;
t++;
if (t >= m) break;
}
sYb -= 1.0f; sYa -= 1.0f;
fXa = sXa; fX = sX;
t += level->mapDimensions[0] - 21; //21 is the number of tiles drawn on a line (20 visible + 1 extra for scrolling)
}
}
glEnd();
}
SDL_GL_SwapBuffers();
}
다음은 스프라이트 및 레벨에 대한 Tilemap 데이터를 설정하는 코드 세그먼트입니다.
수준:
void loadLevel(dshlib::utfstr FileName) {
-snip-
texCacheItem * Tex = loadGraphics(FileName);
if (!Tex) { //Load the tile graphics for the level
unloadLevel();
EngineState = ENGINESTATE_ERR;
return;
} else {
level->dTex = Tex->TextureInfo->Width;
level->tilemap = Tex;
}
-snip-
}
스프라이트:
void SpriteBase::created() {
this->Graphics = loadGraphics(DefaultGFX());
-snip-
}
UPDATE 2:
Sid Farkus는 렌더러로 저지른 한 가지 큰 실수를 언급 했으므로 여기에 업데이트 된 렌더러가 있습니다 .cpp :
#include "../MegaJul.h"
void render(void) {
//Render the current tilemap to the screen
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glTranslatef(0.0f, 0.0f, -4.0f);
if (level) {
float dT = 32.0f / level->dTex;
float sX, fX, fXa, sY, tX, tY, sYa, sYb, sXa, tXa, tYa;
unsigned long m = level->mapDimensions[0] * level->mapDimensions[1];
float ai; long long t; Sint16 * p;
for (int i = 3; i >= 0; i--) {
if (level->layers[i]->mapPosition[0] > 0)
level->layers[i]->mapPosition[0] = 0;
if (level->layers[i]->mapPosition[0] < 0 - (signed long)((level->mapDimensions[0] - 21) * 32))
level->layers[i]->mapPosition[0] = 0 - (signed long)((level->mapDimensions[0] - 21) * 32);
if (level->layers[i]->mapPosition[1] < 0)
level->layers[i]->mapPosition[1] = 0;
if (level->layers[i]->mapPosition[1] > (signed long)((level->mapDimensions[1] - 16) * 32))
level->layers[i]->mapPosition[1] = (signed long)((level->mapDimensions[1] - 16) * 32);
if (i == level->activeMap) {
for (int j = 0; j < NumSprites; j++) {
glBindTexture(GL_TEXTURE_2D, Sprites[j]->Graphics->TextureNum);
glBegin(GL_QUADS);
Sprites[j]->render(level->layers[i]->mapPosition[0], level->layers[i]->mapPosition[1]);
glEnd();
}
for (int j = 0; j < NumBullets; j++) {
glBindTexture(GL_TEXTURE_2D, Bullets[j]->Texture->TextureNum);
glBegin(GL_QUADS);
Bullets[j]->render(level->layers[i]->mapPosition[0], level->layers[i]->mapPosition[1]);
glEnd();
}
}
glBindTexture(GL_TEXTURE_2D, level->tilemap->TextureNum);
glBegin(GL_QUADS);
-snipped out renderer since it was bloat
glEnd();
}
SDL_GL_SwapBuffers();
}
해결책
렌더러에서 GlbindTexture를 적절하게 호출하고 있습니까? 렌더러가 마지막으로 업로드 한 텍스처를 사용하는 것처럼 들립니다. 그 이후로 GlbindTexture라고 불렀습니다. Glbindtexture는 OpenGL 텍스처를 다각형에 사용하도록 지시합니다.
다른 팁
렌더링 코드를 사용하면 Glbegin/End 블록에서 BindTexture를 호출하는 것을 볼 수 있습니다. OpenGL 문서에서 :
GLBINDTEXTURE가 GLBEGIN의 실행과 해당 GLEND의 실행 사이에 GLBINDTEXTURE가 실행되면 GL_INVALID_OPERATION이 생성됩니다.
glbegin ()/glend () 블록 밖에서 BindTexture 통화를 이동하면 황금빛이어야합니다. 렌더링 스타일을 수용하기 위해 여러 블록이 있어야 할 것입니다.
편집하다:
업데이트 된 코드를 사용하여 몇 가지를 확인하십시오. 스프라이트 위치는 현재 프로젝션/모델보기 행렬로 화면에서 볼 수 있으며 스프라이트 텍스처 ID는 유효한 텍스처입니다. 기술적으로 나에게 튀어 나오는 것은 아무것도 없지만 값이 꺼져있을 수 있습니다.
나는 당신이 이것을 위해 어떤 종류의 디버깅이나 로깅을 할 수 없다고 가정하고 있습니까? 당신이 할 수 있다면, 나는 이것이 진단하기가 사소할 것으로 기대합니다.
나에게 위험 해 보이는 가장 중요한 것은 loadpng에서 반환 값을 확인하지 않는다는 것입니다. 나는 내가 한 첫 번째 일로 거기에 무언가를 넣었습니다.
나는 이미 캐 형성 질감에 대한 초기 점검을 언급하는 것을 고려할 것입니다. 그 시점에서 일이 작동하기 시작하면 리소스 이름이나 파일 이름 (또는 비교)에 문제가 있다는 것을 알고 있습니다.
제쳐두고, 나는 당신이 클래스와 스마트 포인터를 사용하고 있지만 베어 포인터와 배열로 자신의 std :: 벡터를 굴리는 것에 놀랐습니다. ;)