#include "StdAfx.h"
#include "CLoad3DS.h"
#pragma warning (disable: 4996)
// 读入一个纹理
int CLoad3DS::BuildTexture(char *szPathName, GLuint &texid)
{
HDC hdcTemp; // The DC To Hold Our Bitmap
HBITMAP hbmpTemp; // Holds The Bitmap Temporarily
IPicture *pPicture; // IPicture Interface
OLECHAR wszPath[MAX_PATH+1]; // Full Path To Picture (WCHAR)
char szPath[MAX_PATH+1]; // Full Path To Picture
long lWidth; // Width In Logical Units
long lHeight; // Height In Logical Units
long lWidthPixels; // Width In Pixels
long lHeightPixels; // Height In Pixels
GLint glMaxTexDim ; // Holds Maximum Texture Size
if (strstr(szPathName, "http://")) // If PathName Contains http:// Then...
{
strcpy(szPath, szPathName); // Append The PathName To szPath
}
else // Otherwise... We Are Loading From A File
{
GetCurrentDirectory(MAX_PATH, szPath); // Get Our Working Directory
strcat(szPath, "\\Data\\"); // Append "\" After The Working Directory
strcat(szPath, szPathName); // Append The PathName
}
MultiByteToWideChar(CP_ACP, 0, szPath, -1, wszPath, MAX_PATH); // Convert From ASCII To Unicode
HRESULT hr = OleLoadPicturePath(wszPath, 0, 0, 0, IID_IPicture, (void**)&pPicture);
if(FAILED(hr)) // If Loading Failed
return FALSE; // Return False
hdcTemp = CreateCompatibleDC(GetDC(0)); // Create The Windows Compatible Device Context
if(!hdcTemp) // Did Creation Fail?
{
pPicture->Release(); // Decrements IPicture Reference Count
return FALSE; // Return False (Failure)
}
glGetIntegerv(GL_MAX_TEXTURE_SIZE, &glMaxTexDim); // Get Maximum Texture Size Supported
pPicture->get_Width(&lWidth); // Get IPicture Width (Convert To Pixels)
lWidthPixels = MulDiv(lWidth, GetDeviceCaps(hdcTemp, LOGPIXELSX), 2540);
pPicture->get_Height(&lHeight); // Get IPicture Height (Convert To Pixels)
lHeightPixels = MulDiv(lHeight, GetDeviceCaps(hdcTemp, LOGPIXELSY), 2540);
// Resize Image To Closest Power Of Two
if (lWidthPixels <= glMaxTexDim) // Is Image Width Less Than Or Equal To Cards Limit
lWidthPixels = 1 << (int)floor((log((double)lWidthPixels)/log(2.0f)) + 0.5f);
else // Otherwise Set Width To "Max Power Of Two" That The Card Can Handle
lWidthPixels = glMaxTexDim;
if (lHeightPixels <= glMaxTexDim) // Is Image Height Greater Than Cards Limit
lHeightPixels = 1 << (int)floor((log((double)lHeightPixels)/log(2.0f)) + 0.5f);
else // Otherwise Set Height To "Max Power Of Two" That The Card Can Handle
lHeightPixels = glMaxTexDim;
// Create A Temporary Bitmap
BITMAPINFO bi = {0}; // The Type Of Bitmap We Request
DWORD *pBits = 0; // Pointer To The Bitmap Bits
bi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER); // Set Structure Size
bi.bmiHeader.biBitCount = 32; // 32 Bit
bi.bmiHeader.biWidth = lWidthPixels; // Power Of Two Width
bi.bmiHeader.biHeight = lHeightPixels; // Make Image Top Up (Positive Y-Axis)
bi.bmiHeader.biCompression = BI_RGB; // RGB Encoding
bi.bmiHeader.biPlanes = 1; // 1 Bitplane
// Creating A Bitmap This Way Allows Us To Specify Color Depth And Gives Us Imediate Access To The Bits
hbmpTemp = CreateDIBSection(hdcTemp, &bi, DIB_RGB_COLORS, (void**)&pBits, 0, 0);
if(!hbmpTemp) // Did Creation Fail?
{
DeleteDC(hdcTemp); // Delete The Device Context
pPicture->Release(); // Decrements IPicture Reference Count
return FALSE; // Return False (Failure)
}
SelectObject(hdcTemp, hbmpTemp); // Select Handle To Our Temp DC And Our Temp Bitmap Object
// Render The IPicture On To The Bitmap
pPicture->Render(hdcTemp, 0, 0, lWidthPixels, lHeightPixels, 0, lHeight, lWidth, -lHeight, 0);
// Convert From BGR To RGB Format And Add An Alpha Value Of 255
for(long i = 0; i < lWidthPixels * lHeightPixels; i++) // Loop Through All Of The Pixels
{
BYTE* pPixel = (BYTE*)(&pBits[i]); // Grab The Current Pixel
BYTE temp = pPixel[0]; // Store 1st Color In Temp Variable (Blue)
pPixel[0] = pPixel[2]; // Move Red Value To Correct Position (1st)
pPixel[2] = temp; // Move Temp Value To Correct Blue Position (3rd)
// This Will Make Any Black Pixels, Completely Transparent (You Can Hardcode The Value If You Wish)
if ((pPixel[0]==0) && (pPixel[1]==0) && (pPixel[2]==0)) // Is Pixel Completely Black
pPixel[3] = 0; // Set The Alpha Value To 0
else // Otherwise
pPixel[3] = 255; // Set The Alpha Value To 255
}
glGenTextures(1, &texid); // Create The Texture
// Typical Texture Generation Using Data From The Bitmap
glBindTexture(GL_TEXTURE_2D, texid); // Bind To The Texture ID
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR); // (Modify This For The Type Of Filtering You Want)
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR); // (Modify This For The Type Of Filtering You Want)
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, lWidthPixels, lHeightPixels, 0, GL_RGBA, GL_UNSIGNED_BYTE, pBits); // (Modify This If You Want Mipmaps)
DeleteObject(hbmpTemp); // Delete The Object
DeleteDC(hdcTemp); // Delete The Device Context
pPicture->Release(); // Decrements IPicture Reference Count
return TRUE; // Return True (All Good)
}
// 构造函数的功能是初始化tChunk数据
CLoad3DS::CLoad3DS()
{
m_CurrentChunk = new tChunk; // 初始化并为当前的块分配空间
m_TempChunk = new tChunk; // 初始化一个临时块并分配空间
}
// 打开一个3ds文件,读出其中的内容,并释放内存
bool CLoad3DS::Import3DS(t3DModel *pModel, char *strFileName)
{
char strMessage[255] = {0};
// 打开一个3ds文件
m_FilePointer = fopen(strFileName, "rb");
// 确保所获得的文件指针合法
if(!m_FilePointer)
{
sprintf(strMessage, "Unable to find the file: %s!", strFileName);
MessageBox(NULL, strMessage, "Error", MB_OK);
return false;
}
// 当文件打开之后,首先应该将文件最开始的数据块读出以判断是否是一个3ds文件
// 如果是3ds文件的话,第一个块ID应该是PRIMARY
// 将文件的第一块读出并判断是否是3ds文件
ReadChunk(m_CurrentChunk);
// 确保是3ds文件
if (m_CurrentChunk->ID != PRIMARY)
{
sprintf(strMessage, "Unable to load PRIMARY chuck from file: %s!", strFileName);
MessageBox(NULL, strMessage, "Error", MB_OK);
return false;
}
// 现在开始读入数据,ProcessNextChunk()是一个递归函数
// 通过调用下面的递归函数,将对象读出
ProcessNextChunk(pModel, m_CurrentChunk);
// 在读完整个3ds文件之后,计算顶点的法线
ComputeNormals(pModel);
// 释放内存空间
CleanUp();
return true;
}
// 下面的函数释放所有的内存空间,并关闭文件
void CLoad3DS::CleanUp()
{
fclose(m_FilePointer); // 关闭当前的文件指针
delete m_CurrentChunk; // 释放当前块
delete m_TempChunk; // 释放临时块
}
// 下面的函数读出3ds文件的主要部分
void CLoad3DS::ProcessNextChunk(t3DModel *pModel, tChunk *pPreviousChunk)
{
t3DObject newObject = {0};
没有合适的资源?快使用搜索试试~ 我知道了~
OpenGL深度值+AABB包围盒
共50个文件
bmp:13个
h:11个
cpp:10个
4星 · 超过85%的资源 需积分: 50 82 下载量 54 浏览量
2013-11-11
21:41:02
上传
评论 1
收藏 843KB RAR 举报
温馨提示
读OpenGL深度值并结合AABB包围盒做的拣选,但总没成功,请帮忙看看
资源推荐
资源详情
资源评论
收起资源包目录
OpenGL深度值+AABB包围盒.rar (50个子文件)
OpenGL深度值+AABB包围盒
OpenGL.h 1KB
BoundingBox.cpp 8KB
OpenGLDoc.h 1KB
Matrix4x3.cpp 18KB
res
OpenGL.ico 1KB
OpenGL.rc2 395B
Toolbar.bmp 1KB
OpenGLDoc.ico 1KB
new.ico 1KB
OpenGL.rc 12KB
del.bat 198B
Vector.cpp 2KB
CLoad3DS.cpp 23KB
OpenGL.clw 3KB
OpenGLView.h 3KB
CLoad3DS.h 1KB
Vector.h 6KB
FormCommand.h 2KB
BoundingBox.h 1KB
OpenGLView.cpp 17KB
OpenGLDoc.cpp 2KB
OpenGL.cpp 4KB
OpenGL.dsp 5KB
StdAfx.cpp 205B
MainFrm.cpp 3KB
resource.h 684B
Matrix4x3.h 4KB
MainFrm.h 2KB
StdAfx.h 1KB
OpenGL.dsw 537B
FormCommand.cpp 5KB
Data
printer.3DS 176KB
zhuozi.3DS 85KB
bijiben2.bmp 453KB
display1.bmp 107KB
printer1.bmp 107KB
jixiang2.bmp 107KB
peixianj.bmp 768KB
pingbipeixianjia.3DS 633KB
display3.bmp 615KB
bijiben.3DS 182KB
jixiang1.bmp 107KB
bijiben1.bmp 43KB
zhuozi.bmp 442KB
jixiang.3DS 557KB
bijiben3.bmp 453KB
printer2.bmp 107KB
display2.bmp 107KB
display.3DS 159KB
OpenGL.vcproj 9KB
共 50 条
- 1
资源评论
- v纵横四海2016-09-07这个资源好
- lzp18222019-01-11OpenGL深度值并结合AABB包围盒做的拣选
- laugh苍生2019-08-26非常有用的源码,感谢分享
liuhailong520
- 粉丝: 4
- 资源: 6
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功