-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodel.cpp
More file actions
115 lines (96 loc) · 2.27 KB
/
model.cpp
File metadata and controls
115 lines (96 loc) · 2.27 KB
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
#include "model.h"
#include "gl/glut.h"
#include <string>
#include "vector3.h"
#include <fstream>
#include <iostream>
void CModel::Create( string& mesh, vector3& Pos, float ang)
{
m_Pos = Pos;
m_Loaded = Load(mesh);
angle = ang;
init = false;
}
void CModel::setPosition(vector3 position,float ang)
{
m_Pos = position;
angle = ang;
}
CModel::~CModel(void)
{
// 释放内存
if (m_Loaded)
{
delete[] m_Point;
delete[] m_Index;
}
}
bool CModel::Load(string m_Name)
{
ifstream file(("img\\" + m_Name).c_str(), ios::binary | ios::in);
if (!file)
{
cout << "无法打开 " << m_Name << endl;
return false;
}
file.read((char*)&m_NumPoint, 4); // 读取顶点个数m_NumPoint
file.read((char*)&m_NumFace, 4); // 读取面数m_NumFace
// 分配内存
m_Point = new POINT_TNV[m_NumPoint];
m_Index = new unsigned short[m_NumFace * 3];
// 读取数据
for (unsigned int i = 0; i<m_NumPoint; i++)
{
file.read((char*)m_Point[i].tex.v, sizeof(Coord));
m_Point[i].nor.zero();
file.read((char*)m_Point[i].ver.v, sizeof(vector3));
m_Point[i].ver.exchange();
}
file.read((char*)m_Index, m_NumFace*sizeof(unsigned short) * 3);
file.close();
// 遍历对象的所有面,计算法向量
for (unsigned int i = 0; i<m_NumFace; i++)
{
unsigned int p1, p2, p3;
p1 = m_Index[i * 3];
p2 = m_Index[i * 3 + 1];
p3 = m_Index[i * 3 + 2];
vector3 nor = vector3::GetTriangleNormal(m_Point[p1].ver, m_Point[p2].ver, m_Point[p3].ver);
m_Point[p1].nor += nor;
m_Point[p2].nor += nor;
m_Point[p3].nor += nor;
}
// 遍历对象的所有顶点,单位化向量
for (unsigned int i = 0; i<m_NumPoint; i++)
{
m_Point[i].nor.normalize();
}
return 1;
}
void CModel::Render(bool move)
{
glBindTexture(GL_TEXTURE_2D, m_Texture);
glPushMatrix();
if (!init)
{
init = true;
glTranslatef(m_Pos.x, m_Pos.y, m_Pos.z);
Draw();
glGetFloatv(GL_PROJECTION_MATRIX, mat);
}
else{
glLoadMatrixf(mat);
if (move){
glTranslatef(m_Pos.x, m_Pos.y, m_Pos.z);
glRotatef(angle, 0., 1., 0.);
glGetFloatv(GL_PROJECTION_MATRIX, mat);
}
Draw();
}
glPopMatrix();
}
void CModel::Draw(void)
{
glInterleavedArrays(GL_T2F_N3F_V3F, 0, m_Point);
glDrawElements(GL_TRIANGLES, m_NumFace * 3, GL_UNSIGNED_SHORT, m_Index);
}