-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmesh.cpp
161 lines (146 loc) · 4.47 KB
/
mesh.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
147
148
149
150
151
152
153
154
155
156
157
#include "Texture2D.hpp"
#include "mesh.hpp"
#include "pmxLoader.hpp"
#include "utility.hpp"
namespace kazakami
{
Mesh::Mesh()
{
pmx = make_unique<PMXLoader>();
}
int Mesh::LoadPMX(const std::string & directoryName, const std::string & filename)
{
const int ret = pmx->readPMX(filename);
if (ret != 0)
return ret;
const int texNum = pmx->textureNames.size();
for (int i = 0; i < texNum; i++)
{
std::shared_ptr<Texture2D> tex(new Texture2D());
const int returnValue = tex->Load(directoryName + kazakami::directorySeparator + pmx->textureNames[i]);
if (returnValue == -1)
std::cerr << "eee" << std::endl;
textures.push_back(tex);
}
return 0;
}
void Mesh::Draw()
{
/*
for (int i = 0; i < pmx->mtrNum; i++)
{
const auto & mat = pmx->materials[i];
const int textureNumber = mat.normalTexture;
//setPMXMaterial(mat);
//setMaterial(red);
if (textureNumber == -1) //テクスチャを使用するか
{
glBindTexture(GL_TEXTURE_2D, 0);
glDisable(GL_TEXTURE_2D);
}
else
{
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, textures[textureNumber]->GetTexName());
}
glBegin(GL_TRIANGLES);
{
glTexCoord2f(0, 1); glNormal3f(0, 0, -1); glVertex3f(0, 10+i, i);
glTexCoord2f(0, 0); glNormal3f(0, 0, -1); glVertex3f(0, 0, i);
glTexCoord2f(1, 0); glNormal3f(0, 0, -1); glVertex3f(10+i, 0, i);
glTexCoord2f(1, 0); glNormal3f(0, 0, -1); glVertex3f(10+i, 0, i);
glTexCoord2f(1, 1); glNormal3f(0, 0, -1); glVertex3f(10+i, 10+i, i);
glTexCoord2f(0, 1); glNormal3f(0, 0, -1); glVertex3f(0, 10+i, i);
}
glEnd();
}
//*/
/*
int i = 0;
for (const auto & tex : textures)
{
glBindTexture(GL_TEXTURE_2D, tex->GetTexName());
glEnable(GL_TEXTURE_2D);
glBegin(GL_TRIANGLES);
{
glTexCoord2f(0, 1); glNormal3f(0, 0, -1); glVertex3f(0, 10+i, i);
glTexCoord2f(0, 0); glNormal3f(0, 0, -1); glVertex3f(0, 0, i);
glTexCoord2f(1, 0); glNormal3f(0, 0, -1); glVertex3f(10+i, 0, i);
glTexCoord2f(1, 0); glNormal3f(0, 0, -1); glVertex3f(10+i, 0, i);
glTexCoord2f(1, 1); glNormal3f(0, 0, -1); glVertex3f(10+i, 10+i, i);
glTexCoord2f(0, 1); glNormal3f(0, 0, -1); glVertex3f(0, 10+i, i);
}
glEnd();
i++;
}
//*/
/*
glColor3d(0.0, 0.0, 0.0);
glBegin(GL_TRIANGLES);
for (int i = 0; i < pmx->faceNum/3; i++)
{
glVertex3fv(pmx->vertexes[pmx->faceIndex[i*3]].pos);
glVertex3fv(pmx->vertexes[pmx->faceIndex[i*3+1]].pos);
glVertex3fv(pmx->vertexes[pmx->faceIndex[i*3+2]].pos);
}
glEnd();
//*/
//*
static bool compiled = false;
static GLuint dispList;
if (compiled)
{
glCallList(dispList);
return;
}
compiled = true;
dispList = glGenLists(1);
glNewList(dispList , GL_COMPILE);
int drawnVertexNum = 0;
for (int mtr_i = 0; mtr_i < pmx->mtrNum; mtr_i++)
{
const auto & mat = pmx->materials[mtr_i];
const int textureNumber = mat.normalTexture;
setPMXMaterial(mat);
//setMaterial(red);
if (textureNumber == -1) //テクスチャを使用するか
{
glBindTexture(GL_TEXTURE_2D, 0);
glDisable(GL_TEXTURE_2D);
}
else
{
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, textures[textureNumber]->GetTexName());
}
if (mat.drawFlag & 0x01) //両面描画か
{
glDisable(GL_CULL_FACE);
}
else
{
glEnable(GL_CULL_FACE);
}
const int loopCount = mat.vertexNum/3;
glBegin(GL_TRIANGLES);
for (int face_i = 0; face_i < loopCount; face_i++)
{
//float * f = pmx->vertexes[pmx->faceIndex[drawnVertexNum+face_i*3]].UV;
//std::cerr << "(" << f[0] << ", " << f[1] << ")" << std::endl;
glTexCoord2fv(pmx->vertexes[pmx->faceIndex[drawnVertexNum+face_i*3]].UV);
glNormal3fv(pmx->vertexes[pmx->faceIndex[drawnVertexNum+face_i*3]].normal);
glVertex3fv(pmx->vertexes[pmx->faceIndex[drawnVertexNum+face_i*3]].pos);
glTexCoord2fv(pmx->vertexes[pmx->faceIndex[drawnVertexNum+face_i*3+1]].UV);
glNormal3fv(pmx->vertexes[pmx->faceIndex[drawnVertexNum+face_i*3+1]].normal);
glVertex3fv(pmx->vertexes[pmx->faceIndex[drawnVertexNum+face_i*3+1]].pos);
glTexCoord2fv(pmx->vertexes[pmx->faceIndex[drawnVertexNum+face_i*3+2]].UV);
glNormal3fv(pmx->vertexes[pmx->faceIndex[drawnVertexNum+face_i*3+2]].normal);
glVertex3fv(pmx->vertexes[pmx->faceIndex[drawnVertexNum+face_i*3+2]].pos);
}
glEnd();
drawnVertexNum += mat.vertexNum;
}
glEndList();
//*/
}
}