Mesh* pModel = NULL;
int showNormals = 0;
int showColor = 0;
float normalLength = 0.001; // 初始法线长度
void HSVtoRGB(float h, float s, float v, float& r, float& g, float& b) {
int i = floor(h * 6);
float f = h * 6 - i;
float p = v * (1 - s);
float q = v * (1 - f * s);
float t = v * (1 - (1 - f) * s);
code
switch (i % 6) {
case 0: r = v, g = t, b = p; break;
case 1: r = q, g = v, b = p; break;
case 2: r = p, g = v, b = t; break;
case 3: r = p, g = q, b = v; break;
case 4: r = t, g = p, b = v; break;
case 5: r = v, g = p, b = q; break;
}
Mesh* pModel = NULL;
int showNormals = 0;
int showColor = 0;
float normalLength = 0.001; // 初始法线长度
void HSVtoRGB(float h, float s, float v, float& r, float& g, float& b) {
int i = floor(h * 6);
float f = h * 6 - i;
float p = v * (1 - s);
float q = v * (1 - f * s);
float t = v * (1 - (1 - f) * s);
code
switch (i % 6) {
case 0: r = v, g = t, b = p; break;
case 1: r = q, g = v, b = p; break;
case 2: r = p, g = v, b = t; break;
case 3: r = p, g = q, b = v; break;
case 4: r = t, g = p, b = v; break;
case 5: r = v, g = p, b = q; break;
}
“继续对这段代码进行优化,只要实现显示[30 分] 代码可以正常编译并运行,可以在主窗口中看到点云; [10 分] 可以通过拖动鼠标左键,对点云做任意方向旋转; [10….” Conversation with gemini-1.5-pro-api-0514, from the LMArena Human Preference 100K dataset. The Chat Archive, 2026. https://thechatarchive.com/c/01m851b5bx467v133d92mcwrsb
float maxx, minx, maxy, miny, maxz, minz;
float cx, cy, cz, w, h, d;
float scale;
/* get the max/mins */
maxx = minx = vertices[0];
maxy = miny = vertices[1];
maxz = minz = vertices[2];
for (int i = 0; i < numVertices; i++)
{
if (maxx < vertices[3 * i + 0])
maxx = vertices[3 * i + 0];
if (minx > vertices[3 * i + 0])
minx = vertices[3 * i + 0];
if (maxy < vertices[3 * i + 1])
maxy = vertices[3 * i + 1];
if (miny > vertices[3 * i + 1])
miny = vertices[3 * i + 1];
if (maxz < vertices[3 * i + 2])
maxz = vertices[3 * i + 2];
if (minz > vertices[3 * i + 2])
minz = vertices[3 * i + 2];
}
/* calculate model width, height, and depth */
w = abs(maxx) + abs(minx);
h = abs(maxy) + abs(miny);
d = abs(maxz) + abs(minz);
/* calculate center of the model */
cx = (maxx + minx) / 2.0;
cy = (maxy + miny) / 2.0;
cz = (maxz + minz) / 2.0;
/* calculate unitizing scale factor */
scale = 2.0 / max(max(w, h), d);
/* translate around center then scale */
for (int i = 0; i <numVertices; i++)
{
vertices[3 * i + 0] -= cx;
vertices[3 * i + 1] -= cy;
vertices[3 * i + 2] -= cz;
vertices[3 * i + 0] *= scale;
vertices[3 * i + 1] *= scale;
vertices[3 * i + 2] *= scale;
}
return;
}
Mesh* readOBJ(char* filename)
{
Mesh *pModel = new Mesh;
FILE *file = fopen(filename, "r");
char line[200];
int numVertices = 0;
int numFaces = 0;
int numTexcoords = 0;
int numNormalVertices = 0;
code
// first pass
while (true)
{
fgets(line, 200, file); // fgets routine reads a line in the file
if (feof(file) != 0) // if come to the end of the file, break
break;
if (line[0] == '#')
continue;
if (line[0] == 'v' && line[1] != 'n') // if the first charactor is 'v', this is the vertex command
numVertices++;
if (line[0] == 'f' ) // if the first charactor is 'f', this is the face command
numFaces++;
if (line[0] == 'v' && line[1] == 'n')
numNormalVertices++;
}
// allocate memory
pModel->numVertices = numVertices;
pModel->numFaces = numFaces;
pModel->numNormalVertices = numNormalVertices;
pModel->numTexcoords = numTexcoords;
pModel->vertices = new float[numVertices * 3]; // allocate memory to store all vertices
pModel->faces = new int[numFaces * 3]; // allocate memory to store all faces
pModel->normalVertices = new float[numNormalVertices * 3];
rewind(file); // reset the file pointer to the head of the file
// the second pass
float fx, fy, fz;
int idx0, idx1, idx2;
float nx, ny, nz;
float u, v;
int vcnt = 0, fcnt = 0, tcnt = 0,nvcnt = 0;
while (true)
{
fgets(line, 200, file); // fgets routine reads a line in the file
if (feof(file) != 0) // if come to the end of the file, break
break;
if (line[0] == '#')
continue;
if (line[0] == 'v' && line[1] != 'n')
{
sscanf(line, "v %f %f %f", &fx, &fy, &fz);
pModel->vertices[vcnt * 3 + 0] = fx;
pModel->vertices[vcnt * 3 + 1] = fy;
pModel->vertices[vcnt * 3 + 2] = fz;
vcnt++;
}
if (line[0] == 'f') // if the first charactor is 'f', this is the face command
{
sscanf(line, "f %d %d %d", &idx0, &idx1, &idx2);
pModel->faces[fcnt * 3 + 0] = idx0 - 1;
pModel->faces[fcnt * 3 + 1] = idx1 - 1;
pModel->faces[fcnt * 3 + 2] = idx2 - 1;
fcnt++;
}
}
if (line[0] == 'v' && line[1] == 'n') // if the first charactor is 'f', this is the face command
{
sscanf(line, "vn %f %f %f", &nx, &ny, &nz);
pModel->normalVertices[nvcnt * 3 + 0] = nx;
pModel->normalVertices[nvcnt * 3 + 1] = ny;
pModel->normalVertices[nvcnt * 3 + 2] = nz;
nvcnt++;
}
fclose(file);
return pModel;
}
void drawMesh(Mesh* pMesh)
{
glPointSize(3.0); // Set point size
glBegin(GL_POINTS);
for (int i = 0; i < pMesh->numVertices; i++)
{
if (pMesh->isColor) {
// Use normal as color
glColor3f(fabs(pMesh->normalVertices[3 * i]),
fabs(pMesh->normalVertices[3 * i + 1]),
fabs(pMesh->normalVertices[3 * i + 2]));
}
else {
glColor3f(1.0, 1.0, 1.0); // White color
}
glVertex3fv(&pMesh->vertices[3 * i]);
}
glEnd();
code
if (pMesh->isGrid) {
glBegin(GL_LINES);
for (int i = 0; i < pMesh->numVertices; i++)
{
glVertex3fv(&pMesh->vertices[3 * i]);
glVertex3f(pMesh->vertices[3 * i] + pMesh->normalVertices[3 * i] * 0.1,
pMesh->vertices[3 * i + 1] + pMesh->normalVertices[3 * i + 1] * 0.1,
pMesh->vertices[3 * i + 2] + pMesh->normalVertices[3 * i + 2] * 0.1);
}
glEnd();
}
}
继续对这段代码修改,要求绘制的法线是顶点法线
主要改动:
在Mesh类中添加 normals 成员:
cpp
float* normals;
这将存储每个顶点的法向量。
修改 compute_normal 函数:
为 normals 数组分配内存。
在计算每个面的法向量后,将该法向量 累加 到该面 每个顶点 的法向量上。
遍历所有顶点,将累加后的法向量 归一化。
修改 drawMesh 函数:
在绘制法线时,使用 normals 数组中存储的 顶点法线,而不是面法线。
现在,代码将计算并绘制每个顶点的法线,而不是每个面的法线。这将使法线更平滑,更 accurately represent the surface's curvature。
// returns the maximum of two floats
static float max(float a, float b)
{
if (b > a)
return b;
return a;
}
// returns the absolute value of a float
static float abs(float f)
{
if (f < 0)
return -f;
return f;
}
void Mesh::unitize()
{
if (vertices == NULL)
return;
code
float maxx, minx, maxy, miny, maxz, minz;
float cx, cy, cz, w, h, d;
float scale;
/* get the max/mins */
maxx = minx = vertices[0];
maxy = miny = vertices[1];
maxz = minz = vertices[2];
for (int i = 0; i < numVertices; i++)
{
if (maxx < vertices[3 * i + 0])
maxx = vertices[3 * i + 0];
if (minx > vertices[3 * i + 0])
minx = vertices[3 * i + 0];
if (maxy < vertices[3 * i + 1])
maxy = vertices[3 * i + 1];
if (miny > vertices[3 * i + 1])
miny = vertices[3 * i + 1];
if (maxz < vertices[3 * i + 2])
maxz = vertices[3 * i + 2];
if (minz > vertices[3 * i + 2])
minz = vertices[3 * i + 2];
}
/* calculate model width, height, and depth */
w = abs(maxx) + abs(minx);
h = abs(maxy) + abs(miny);
d = abs(maxz) + abs(minz);
/* calculate center of the model */
cx = (maxx + minx) / 2.0;
cy = (maxy + miny) / 2.0;
cz = (maxz + minz) / 2.0;
/* calculate unitizing scale factor */
scale = 2.0 / max(max(w, h), d);
/* translate around center then scale */
for (int i = 0; i <numVertices; i++)
{
vertices[3 * i + 0] -= cx;
vertices[3 * i + 1] -= cy;
vertices[3 * i + 2] -= cz;
vertices[3 * i + 0] *= scale;
vertices[3 * i + 1] *= scale;
vertices[3 * i + 2] *= scale;
}
return;
}
Mesh* readOBJ(char* filename)
{
Mesh *pModel = new Mesh;
FILE *file = fopen(filename, "r");
char line[200];
int numVertices = 0;
int numFaces = 0;
int numTexcoords = 0;
int numNormalVertices = 0;
code
// first pass
while (true)
{
fgets(line, 200, file); // fgets routine reads a line in the file
if (feof(file) != 0) // if come to the end of the file, break
break;
if (line[0] == '#')
continue;
if (line[0] == 'v' && line[1] != 'n') // if the first charactor is 'v', this is the vertex command
numVertices++;
if (line[0] == 'f' ) // if the first charactor is 'f', this is the face command
numFaces++;
if (line[0] == 'v' && line[1] == 'n')
numNormalVertices++;
}
// allocate memory
pModel->numVertices = numVertices;
pModel->numFaces = numFaces;
pModel->numNormalVertices = numNormalVertices;
pModel->numTexcoords = numTexcoords;
pModel->vertices = new float[numVertices * 3]; // allocate memory to store all vertices
pModel->faces = new int[numFaces * 3]; // allocate memory to store all faces
pModel->normalVertices = new float[numNormalVertices * 3];
rewind(file); // reset the file pointer to the head of the file
// the second pass
float fx, fy, fz;
int idx0, idx1, idx2;
float nx, ny, nz;
float u, v;
int vcnt = 0, fcnt = 0, tcnt = 0,nvcnt = 0;
while (true)
{
fgets(line, 200, file); // fgets routine reads a line in the file
if (feof(file) != 0) // if come to the end of the file, break
break;
if (line[0] == '#')
continue;
if (line[0] == 'v' && line[1] != 'n')
{
sscanf(line, "v %f %f %f", &fx, &fy, &fz);
pModel->vertices[vcnt * 3 + 0] = fx;
pModel->vertices[vcnt * 3 + 1] = fy;
pModel->vertices[vcnt * 3 + 2] = fz;
vcnt++;
}
if (line[0] == 'f') // if the first charactor is 'f', this is the face command
{
sscanf(line, "f %d %d %d", &idx0, &idx1, &idx2);
pModel->faces[fcnt * 3 + 0] = idx0 - 1;
pModel->faces[fcnt * 3 + 1] = idx1 - 1;
pModel->faces[fcnt * 3 + 2] = idx2 - 1;
fcnt++;
}
}
if (line[0] == 'v' && line[1] == 'n') // if the first charactor is 'f', this is the face command
{
sscanf(line, "vn %f %f %f", &nx, &ny, &nz);
pModel->normalVertices[nvcnt * 3 + 0] = nx;
pModel->normalVertices[nvcnt * 3 + 1] = ny;
pModel->normalVertices[nvcnt * 3 + 2] = nz;
nvcnt++;
}
fclose(file);
return pModel;