-
Notifications
You must be signed in to change notification settings - Fork 90
/
Copy pathmodel.cpp
146 lines (137 loc) · 3.84 KB
/
model.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#include "model.h"
GLuint LoadGLTexture( char *filename ) // Load Bitmaps And Convert To Textures
{
QString filePath = QApplication::applicationDirPath() + "/" + QString(filename);
QImage image(filePath);// // Loads The Bitmap Specified By filename
image = image.convertToFormat(QImage::Format_RGB888);
image = image.mirrored();
GLuint texture = 0;//Texture ID
if(!image.isNull())//Load The Bitmap, Check For Errors, If Bitmap's Not Found Quit
{
glGenTextures(1, &texture);
// Typical Texture Generation Using Data From The Bitmap
glBindTexture(GL_TEXTURE_2D, texture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, image.width(), image.height(),
0, GL_RGB, GL_UNSIGNED_BYTE, image.bits());
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
}
return texture;// Return The Status
}
Model::Model()
{
m_numMeshes = 0;
m_pMeshes = NULL;
m_numMaterials = 0;
m_pMaterials = NULL;
m_numTriangles = 0;
m_pTriangles = NULL;
m_numVertices = 0;
m_pVertices = NULL;
}
Model::~Model()
{
for (int i = 0; i < m_numMeshes; i++ )
{
delete[] m_pMeshes[i].m_pTriangleIndices;
}
for (int i = 0; i < m_numMaterials; i++ )
{
delete[] m_pMaterials[i].m_pTextureFilename;
}
m_numMeshes = 0;
if ( m_pMeshes != NULL )
{
delete[] m_pMeshes;
m_pMeshes = NULL;
}
m_numMaterials = 0;
if ( m_pMaterials != NULL )
{
delete[] m_pMaterials;
m_pMaterials = NULL;
}
m_numTriangles = 0;
if ( m_pTriangles != NULL )
{
delete[] m_pTriangles;
m_pTriangles = NULL;
}
m_numVertices = 0;
if ( m_pVertices != NULL )
{
delete[] m_pVertices;
m_pVertices = NULL;
}
}
//有了数据,就可以写出绘制函数了,下面的函数根据模型的信息,按网格分组,分别绘制每一组的数据。
void Model::draw()
{
GLboolean texEnabled = glIsEnabled( GL_TEXTURE_2D );
// 按网格分组绘制
for ( int i = 0; i < m_numMeshes; i++ )
{
int materialIndex = m_pMeshes[i].m_materialIndex;
if ( materialIndex >= 0 )
{
glMaterialfv( GL_FRONT, GL_AMBIENT, m_pMaterials[materialIndex].m_ambient );
glMaterialfv( GL_FRONT, GL_DIFFUSE, m_pMaterials[materialIndex].m_diffuse );
glMaterialfv( GL_FRONT, GL_SPECULAR, m_pMaterials[materialIndex].m_specular );
glMaterialfv( GL_FRONT, GL_EMISSION, m_pMaterials[materialIndex].m_emissive );
glMaterialf( GL_FRONT, GL_SHININESS, m_pMaterials[materialIndex].m_shininess );
if ( m_pMaterials[materialIndex].m_texture > 0 )
{
glBindTexture( GL_TEXTURE_2D, m_pMaterials[materialIndex].m_texture );
glEnable( GL_TEXTURE_2D );
}
else
{
glDisable( GL_TEXTURE_2D );
}
}
else
{
// Material properties?
glDisable( GL_TEXTURE_2D );
}
glBegin( GL_TRIANGLES );
{
for ( int j = 0; j < m_pMeshes[i].m_numTriangles; j++ )
{
int triangleIndex = m_pMeshes[i].m_pTriangleIndices[j];
const Triangle* pTri = &m_pTriangles[triangleIndex];
for ( int k = 0; k < 3; k++ )
{
int index = pTri->m_vertexIndices[k];
glNormal3fv( pTri->m_vertexNormals[k] );
glTexCoord2f( pTri->m_s[k], pTri->m_t[k] );
glVertex3fv( m_pVertices[index].m_location );
}
}
}
glEnd();
}
if ( texEnabled )
{
glEnable( GL_TEXTURE_2D );
}
else
{
glDisable( GL_TEXTURE_2D );
}
}
void Model::reloadTextures()
{
//设置好了一切参数,但纹理还没有载入内存,下面的代码完成这个功能
for ( int i = 0; i < m_numMaterials; i++ )
{
if ( strlen( m_pMaterials[i].m_pTextureFilename ) > 0 )
{
m_pMaterials[i].m_texture = LoadGLTexture( m_pMaterials[i].m_pTextureFilename );
}
else
{
m_pMaterials[i].m_texture = 0;
}
}
}