📄 blp.cpp
字号:
/********************************************************************************
Warcraft 3 Viewer - Utility to view models and textures from Warcraft 3
Copyright (C) 2002 David GRIMBICHLER (theprophet@wanadoo.Fr)
http://www.xeberon.net
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
********************************************************************************/
#include <windows.h>
#include <stdio.h>
#include <conio.h>
#include <math.h>
#include <time.h>
#include <io.h>
#include <gl/gl.h>
#include <gl/glu.h>
#include "math.h"
#include "mdx.h"
#include "blp.h"
#include "SFmpq_static.h"
#include "utility.h"
extern void *malloc_func(const size_t size);
extern void free_func(void* ptr);
extern DWORD ConvertBLP(char *srcBuf, char *destBuf, DWORD *width, DWORD *height, DWORD *type, DWORD *subtype);
extern char gLoadedExternFile[1024], gLoadedExternFilePath[1024];
extern char mpqpath[1024];
extern MaChaine gLoadedTextures[128];
extern BOOL bShowingMDX;
extern BOOL bLoadingAnim;
extern char rIDTable[50][256];
extern HWND dlg;
extern char szAppName[];
void AddTextureToList(char *s)
{
if(bLoadingAnim == TRUE)
return;
long l=0;
while(gLoadedTextures[l][0]!=0) ++l;
lstrcpy(gLoadedTextures[l], s);
}
int LoadBLP(char*sFichier, DWORD& width, DWORD& height, DWORD& type, DWORD& subtype, int rID)
{
DWORD blpsize;
char *blpdata;
DWORD r;
char szFichier[256];
toregistre("Loading texture : %s", sFichier);
toregistre("Passed ReplaceableID : %d", rID);
if((rID !=0)&&(rID!=1)&&(lstrcmpi(rIDTable[rID], "")==0))
{
char tmp[1024];
sprintf(tmp, "Unknown Replaceable ID : %d", rID);
MessageBox(dlg, tmp, szAppName, MB_ICONEXCLAMATION);
}
if(((lstrcmpi(sFichier, "")==0)||(lstrcmpi(sFichier, " ")==0))&&(rID==0))
{
toregistre("Nothing to load");
width=0;
height=0;
type=0;
subtype=0;
return 0;
}
if((rID != 0)&&(lstrcmpi(rIDTable[rID], "")!=0))
lstrcpy(szFichier, rIDTable[rID]);
else
lstrcpy(szFichier, sFichier);
MPQHANDLE hFile;
char ttmp[1024];
ZeroMemory(&ttmp, 1024);
if((lstrcmp(gLoadedExternFilePath, "")!=0)&&(bShowingMDX == TRUE))
utilAddBkSlash(gLoadedExternFilePath, ttmp, 1024);
lstrcat(ttmp, szFichier);
if((bShowingMDX==FALSE)&&(lstrcmp(gLoadedExternFile, "")==0))
{
BOOL b=SFileOpenFile(szFichier, &hFile);
if ((hFile==(HANDLE)0xcccccccc)||(b==FALSE))
{
toregistre("Failed to load %s", szFichier);
width=0;
height=0;
type=0;
subtype=0;
return 0;
}
else
{
toregistre("Found texture : %s", szFichier);
blpsize = SFileGetFileSize(hFile, NULL);
blpdata = (char*)malloc_func(blpsize);
SFileReadFile(hFile, blpdata, blpsize, &r, NULL);
SFileCloseFile(hFile);
//AddTextureToList(szFichier);
goto enfaitcestbon;
}
}
hFile=CreateFile(ttmp, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
if (hFile==INVALID_HANDLE_VALUE)
{
ZeroMemory(&ttmp, 1024);
lstrcpy(ttmp, mpqpath);
lstrcat(ttmp, szFichier);
hFile=CreateFile(ttmp, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
if (hFile==INVALID_HANDLE_VALUE)
{
BOOL b=SFileOpenFile(szFichier, &hFile);
if ((hFile==(HANDLE)0xcccccccc)||(b==FALSE))
{
toregistre("Failed to load %s", szFichier);
width=0;
height=0;
type=0;
subtype=0;
return 0;
}
else
{
toregistre("Found texture : %s", szFichier);
blpsize = SFileGetFileSize(hFile, NULL);
blpdata = (char*)malloc_func(blpsize);
SFileReadFile(hFile, blpdata, blpsize, &r, NULL);
SFileCloseFile(hFile);
//AddTextureToList(szFichier);
goto enfaitcestbon;
}
}
else
{
toregistre("Found texture : %s", ttmp);
blpsize = GetFileSize(hFile, NULL);
blpdata = (char*)malloc_func(blpsize);
ReadFile(hFile, blpdata, blpsize, &r, NULL);
CloseHandle(hFile);
//AddTextureToList(ttmp);
goto enfaitcestbon;
}
}
else
{
toregistre("Found texture : %s", ttmp);
blpsize = GetFileSize(hFile, NULL);
blpdata = (char*)malloc_func(blpsize);
ReadFile(hFile, blpdata, blpsize, &r, NULL);
//AddTextureToList(ttmp);
CloseHandle(hFile);
}
enfaitcestbon:
AddTextureToList(szFichier);
DWORD w,h,t,st,taille = ConvertBLP(blpdata, NULL, &w, &h, &t, &st);
char *timage = (char*)malloc_func(taille);
ConvertBLP(blpdata, timage, &w, &h, &t, &st);
free_func(blpdata);
width = w;
height = h;
type = t;
subtype = st;
toregistre(" Texture info :");
toregistre(" width = %d", width);
toregistre(" height = %d", height);
toregistre(" type = %d", type);
toregistre(" subtype = %d", subtype);
UINT ID;
glEnable(GL_TEXTURE_2D);
glGenTextures(1, &ID);
glBindTexture(GL_TEXTURE_2D, ID);
int bpp=GL_BGRA_EXT;
//int bpp=GL_RGBA;
gluBuild2DMipmaps(GL_TEXTURE_2D, 4, width, height, bpp, GL_UNSIGNED_BYTE, timage);
// glTexImage2D(GL_TEXTURE_2D, 0, 3, width, height, 0, bpp, GL_UNSIGNED_BYTE, timage);
glTexParameteri( GL_TEXTURE_2D,GL_TEXTURE_WRAP_S, GL_REPEAT );
glTexParameteri( GL_TEXTURE_2D,GL_TEXTURE_WRAP_T, GL_REPEAT );
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR_MIPMAP_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_LINEAR);
free_func(timage);
toregistre("Successfully loaded %s", szFichier);
return ID;
}
WarCraft_BLP::WarCraft_BLP(void) :
glbind(0),width(0),height(0)
{
}
WarCraft_BLP::~WarCraft_BLP(void)
{
Free();
}
void WarCraft_BLP::Free(void)
{
if(glbind)
glDeleteTextures(1,&glbind);
glbind = 0;
}
void WarCraft_BLP::Render(int mode)
{
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D,glbind);
if(mode != 0)
glEnable(GL_BLEND);
else
glDisable(GL_BLEND);
if(mode == 1)
glBlendFunc(GL_ONE,GL_ONE);
else
if(mode == 2)
{
glEnable(GL_ALPHA_TEST);
glAlphaFunc(GL_GREATER, 0.75f);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
}
float aspect = (float)width/(float)height;
if(aspect < 1)
{
glTranslatef(0.0f, -0.6f, 0.0f);
glBegin(GL_QUADS);
glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
glTexCoord2f( 1, 1);
glVertex2f (-aspect, 1);
glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
glTexCoord2f( 1, 0);
glVertex2f (-aspect,-1);
glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
glTexCoord2f( 0, 0);
glVertex2f ( aspect,-1);
glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
glTexCoord2f( 0, 1);
glVertex2f ( aspect, 1);
glEnd();
} else
{
float aspect1 = 1/aspect;
glTranslatef(0.0f, -0.6f, 0.0f);
glBegin(GL_QUADS);
glTexCoord2f( 1, 1);
glVertex2f (-1, aspect1);
glTexCoord2f( 1, 0);
glVertex2f (-1,-aspect1);
glTexCoord2f( 0, 0);
glVertex2f ( 1,-aspect1);
glTexCoord2f( 0, 1);
glVertex2f ( 1, aspect1);
glEnd();
}
}
void WarCraft_BLP::Load(char* inName)
{
if(glbind)
glDeleteTextures(1,&glbind);
glbind = 0;
name = inName;
glbind = LoadBLP(inName,width,height,type,subtype,0);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -