//********************************************
// Mesh3d.cpp
//********************************************
// class CMesh3d
//********************************************
// alliez@usc.edu
// Created : 15/01/98
// Modified : 15/01/98
//********************************************
#include "stdafx.h"
#include <math.h>
#include "Base3d.h"
#include "Mesh3d.h"
#include "ColorRamp.h"
#include "AVLInd.h" // Gaspard Breton's stuff
#include "WmfTools.h"
//////////////////////////////////////////////
// CONSTRUCTORS
//////////////////////////////////////////////
//********************************************
// Constructor
//********************************************
CMesh3d::CMesh3d()
{
m_ListDone = 0;
m_Modified = 1;
m_NormalBinding = NORMAL_PER_FACE;
m_ColorBinding = COLOR_PER_VERTEX;
m_Name = _T("Mesh");
// Texture
m_IndexTexture = -1;
m_pTextureCoordinate = NULL;
m_pTextureCoordinateIndex = NULL;
m_Show = 1;
}
//********************************************
// Destructor
//********************************************
CMesh3d::~CMesh3d()
{
Free();
}
//********************************************
// Free
//********************************************
void CMesh3d::Free()
{
//TRACE("Cleanup mesh %x\n",this);
m_ArrayVertex.Free();
m_ArrayFace.Free();
if(m_pTextureCoordinate != NULL)
delete [] m_pTextureCoordinate;
if(m_pTextureCoordinateIndex != NULL)
delete [] m_pTextureCoordinateIndex;
}
//////////////////////////////////////////////
// OPENGL
//////////////////////////////////////////////
//********************************************
// BuildList
//********************************************
int CMesh3d::glBuildList()
{
//TRACE(" Start building list ...\n");
// Check for valid mesh
if(m_ArrayVertex.GetSize() == 0 ||
m_ArrayFace.GetSize() == 0)
{
TRACE("CMesh3d::BuildList : empty mesh\n");
return 0;
}
if(!m_Modified && m_ListDone)
return 0;
// Erase last list
::glDeleteLists(m_ListOpenGL,1);
// Search for a new list
m_ListOpenGL = ::glGenLists(1);
if(m_ListOpenGL == 0)
{
TRACE("CMesh3d::BuildList : unable to build DrawList\n");
return 0;
}
// Start list
::glNewList(m_ListOpenGL,GL_COMPILE_AND_EXECUTE);
glDrawDirect();
::glEndList();
// List is done now
m_ListDone = 1;
m_Modified = 0;
return 1;
}
//*********************************
// glDrawDirect
//*********************************
int CMesh3d::glDrawDirect()
{
unsigned int NbVertex = (unsigned int)m_ArrayVertex.GetSize();
unsigned int NbFace = (unsigned int)m_ArrayFace.GetSize();
if(!NbVertex)
return 0;
if(!NbFace)
return 0;
CFace3d *pFace;
CVector3d *pVector;
CColor *pColorPrevious;
::glPushMatrix();
// Position / translation / scaling
glTranslatef(m_Transform.GetTranslation()->x(),
m_Transform.GetTranslation()->y(),
m_Transform.GetTranslation()->z());
glScalef(m_Transform.GetScale()->x(),
m_Transform.GetScale()->y(),
m_Transform.GetScale()->z());
glRotatef(m_Transform.GetValueRotation(),
m_Transform.GetRotation()->x(),
m_Transform.GetRotation()->y(),
m_Transform.GetRotation()->z());
// Init color
pFace = m_ArrayFace[0];
pColorPrevious = pFace->GetColor();
::glColor3ub(pFace->GetColor()->r(),pFace->GetColor()->g(),pFace->GetColor()->b());
// Triangles
::glBegin(GL_TRIANGLES);
for(unsigned int i=0;i<NbFace;i++)
{
{
pFace = m_ArrayFace[i];
ASSERT(pFace != NULL);
// Normal (per face)
if(m_NormalBinding == NORMAL_PER_FACE)
{
pVector = pFace->GetNormal();
::glNormal3f(pVector->x(),pVector->y(),pVector->z());
}
// Color (per face)
if(m_ColorBinding == COLOR_PER_FACE &&
pColorPrevious != pFace->GetColor())
{
::glColor3ub(pFace->GetColor()->r(),pFace->GetColor()->g(),pFace->GetColor()->b());
pColorPrevious = pFace->GetColor();
}
for(int j=0;j<3;j++)
{
// Normal
if(m_NormalBinding == NORMAL_PER_VERTEX)
{
pVector = pFace->v(j)->GetNormal();
::glNormal3f(pVector->x(),pVector->y(),pVector->z());
}
// Color (per vertex)
if(m_ColorBinding == COLOR_PER_VERTEX &&
pColorPrevious != pFace->v(j)->GetColor())
{
::glColor3ub(pFace->v(j)->GetColor()->r(),pFace->v(j)->GetColor()->g(),pFace->v(j)->GetColor()->b());
pColorPrevious = pFace->v(j)->GetColor();
}
// Texture coordinate (if needed)
if(m_IndexTexture != -1)
{
glTexCoord2f(m_pTextureCoordinate[2*m_pTextureCoordinateIndex[3*i+j]],
m_pTextureCoordinate[2*m_pTextureCoordinateIndex[3*i+j]+1]);
}
// Vertex
::glVertex3f(pFace->v(j)->x(),pFace->v(j)->y(),pFace->v(j)->z());
}
}
}
::glEnd();
::glPopMatrix();
return 1;
}
//********************************************
// Draw
//********************************************
int CMesh3d::glDraw()
{
if(!m_Show)
return 0;
// Build list at first
if(!m_ListDone || m_Modified)
glBuildList();
// Search for a new list
if(::glIsList(m_ListOpenGL)==GL_TRUE)
{
::glCallList(m_ListOpenGL);
return 1;
}
else
{
TRACE(" CMesh3d::Draw : unable to draw list %d\n",m_ListOpenGL);
return 0;
}
}
//////////////////////////////////////////////
// DATAS
//////////////////////////////////////////////
//********************************************
// Copy
//********************************************
void CMesh3d::Copy(CMesh3d *pMesh)
{
// Vertices
int NbVertex = pMesh->NbVertex();
m_ArrayVertex.SetSize(NbVertex);
for(int i=0;i<NbVertex;i++)
m_ArrayVertex.SetAt(i,new CVertex3d(pMesh->GetVertex(i)));
// Faces
int NbFace = pMesh->NbFace();
m_ArrayFace.SetSize(NbFace);
for(i=0;i<NbFace;i++)
{
CFace3d *pFace = pMesh->GetFace(i);
m_ArrayFace.SetAt(i,new CFace3d(
m_ArrayVertex[pMesh->IndexFrom(pFace->v1())],
m_ArrayVertex[pMesh->IndexFrom(pFace->v2())],
m_ArrayVertex[pMesh->IndexFrom(pFace->v3())]));
}
// Transform
m_Transform.Copy(pMesh->GetTransform());
}
//********************************************
// GetType
//********************************************
int CMesh3d::GetType()
{
return TYPE_MESH3D;
}
//********************************************
// IsValid
//********************************************
int CMesh3d::IsValid()
{
int NbFace = m_ArrayFace.GetSize();
for(int i=0;i<NbFace;i++)
if(!m_ArrayFace[i]->IsValid())
return 0;
return 1;
}
//********************************************
// DeleteVertex
//********************************************
int CMesh3d::DeleteVertex(CVertex3d *pVertex)
{
int size = m_ArrayVertex.GetSize();
for(int i=0;i<size;i++)
{
CVertex3d *pV = m_ArrayVertex[i];
if(pV == pVertex)
{
m_ArrayVertex.RemoveAt(i);
delete pVertex;
return 1;
}
}
return 0;
}
//********************************************
// DeleteVertex
//********************************************
int CMesh3d::DeleteVertex(int index)
{
if(index < m_ArrayVertex.GetSize())
{
CVertex3d *pVertex = (CVertex3d *)m_ArrayVertex[index];
m_ArrayVertex.RemoveAt(index);
delete pVertex;
return 1;
}
return 0;
}
//********************************************
// DeleteFace
//********************************************
int CMesh3d::DeleteFaceNbNeighbors(int NbNeighbor)
{
TRACE("Delete faces which has %d neighbors...",NbNeighbor);
int deleted = 0;
for(int i=0;i<m_ArrayFace.GetSize();i++)
{
CFace3d *pFace = m_ArrayFace[i];
if(pFace->NbFaceNeighbor() == NbNeighbor)
{
m_ArrayFace.RemoveAt(i);
delete pFace;
i--;
deleted++;
}
}
TRACE("%d face(s) deleted\n",deleted);
return deleted;
}
//////////////////////////////////////////////
// RANGE
///////////////////////////////
没有合适的资源?快使用搜索试试~ 我知道了~
基于OpenGL的实用源代码
共207个文件
gif:34个
h:27个
cpp:23个
需积分: 10 3 下载量 193 浏览量
2008-11-25
10:46:37
上传
评论
收藏 5.07MB RAR 举报
温馨提示
人物头像模拟人物头像模拟人物头像模拟人物头像模拟人物头像模拟
资源推荐
资源详情
资源评论
收起资源包目录
基于OpenGL的实用源代码 (207个子文件)
Mesh.aps 36KB
Toolbar.bmp 2KB
Mesh.bsc 3.51MB
Mesh.clw 5KB
Mesh3d.cpp 60KB
Texture.cpp 27KB
Utils3d.cpp 26KB
ParserVrml.cpp 22KB
Face3d.cpp 19KB
Vertex3d.cpp 19KB
MeshView.cpp 16KB
PsRender.cpp 13KB
SceneGraph3d.cpp 12KB
MeshDoc.cpp 10KB
Vector3d.cpp 9KB
DialogWmf.cpp 7KB
Edge3d.cpp 5KB
Mesh.cpp 4KB
ColorRamp.cpp 3KB
Material.cpp 3KB
MainFrm.cpp 2KB
Transform.cpp 2KB
ChildFrm.cpp 1KB
Color.cpp 1KB
Object3d.cpp 1010B
Array3d.cpp 997B
StdAfx.cpp 202B
CodeProject.css 25KB
ForumClassic.css 7KB
Thumbs.db 88KB
Mesh.dsp 7KB
Mesh.dsw 531B
metafile.emf 158KB
Mesh.exe 92KB
Mesh.exe 92KB
logo225x90.gif 7KB
smiley_redface.gif 2KB
smiley_laugh.gif 1KB
msg_question.gif 1KB
forum_newmsg.gif 1KB
msg_rant.gif 1023B
msg_admin.gif 1020B
smiley_cool.gif 1016B
forum_faq.gif 1000B
forum_search.gif 974B
msg_news.gif 958B
msg_answer.gif 955B
msg_general.gif 923B
minus.gif 846B
smiley_tongue.gif 688B
ajax-loader.gif 673B
msg_joke.gif 659B
thumbs_down.gif 576B
thumbs_up.gif 572B
staff_sm.gif 561B
prize_winner.gif 550B
FR.gif 366B
discuss.gif 286B
mail.gif 270B
fave.gif 267B
smiley_frown.gif 215B
print.gif 186B
report.gif 144B
pollcol.gif 134B
member_sm.gif 133B
usersuss.gif 109B
red.gif 77B
t_002.gif 43B
t.gif 43B
AVLInd.h 21KB
Vertex3d.h 7KB
Mesh3d.h 6KB
PsRender.h 6KB
Array3d.h 5KB
Face3d.h 5KB
Utils3d.h 4KB
MeshView.h 3KB
Texture.h 3KB
MeshDoc.h 3KB
Edge3d.h 2KB
Vector3d.h 2KB
Color.h 2KB
SceneGraph3d.h 2KB
DialogWmf.h 2KB
resource.h 2KB
MainFrm.h 2KB
ChildFrm.h 1KB
Transform.h 1KB
ParserVrml.h 1KB
Mesh.h 1KB
StdAfx.h 1KB
Base3d.h 1KB
Material.h 1KB
ColorRamp.h 962B
Object3d.h 564B
WmfTools.h 501B
glexport.aspx.htm 208KB
ServeThirdParty_003.htm 2KB
ServeThirdParty_004.htm 2KB
t.htm 1KB
共 207 条
- 1
- 2
- 3
资源评论
tyh719
- 粉丝: 2
- 资源: 5
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功