求大佬帮忙!

qq_42408023 2018-06-07 11:10:30

小白求助这几个BUG怎么修复,顺便附上源代码。
string grammar = "FA[*+X][-/&X][/%X]B";//文法
void Grammar::Iteration() {
string temprule = grammar;
for (int i = 1; i <= level; i++)
{
int curlen = temprule.length();
int j = 0;
while (j < curlen)
{
if (temprule[j] == 'X')//迭代,替换成文法模型
{
rule += grammar;
j++;
}
else //保留转角
{
rule += temprule[j];
j++;
}
}
temprule = rule;
rule.clear();
}
rule = temprule;//迭代好之后的文法规则
}
vector<TrunkPosition> trunks; // 所有分支
vector<LeafPosition> leaves;//所有叶子结点
void LSystem::generateFractal() // 利用加载过的文法,创建分形树
{
trunks.clear();
leaves.clear();
curState.pos = Node(0, 0, 0);
curState.dir = Node(0, 1, 0);

State stack[3000]; // 状态栈
for (int i = 0; i <3000; i++)
{
stack[i].pos.x = 0.0;
stack[i].pos.y = 0.0;
stack[i].pos.z = 0.0;
stack[i].dir.x = 0.0;
stack[i].dir.y = 0.0;
stack[i].dir.z = 0.0;
}
size_t i = 0;
while (i <grammar.getRule().length()) {
TrunkPosition tmp_trunk;//单个分支
LeafPosition tmp_leaf;
switch (grammar.getRule()[i])
{
case 'F':
tmp_trunk.pos1 = curState.pos;
curState.pos.x += length * curState.dir.x;
curState.pos.y += length * curState.dir.y;
curState.pos.z += length * curState.dir.z;
tmp_trunk.pos2 = curState.pos;
/*glColor3f(1.0, 0.0, 0.0);
glLineWidth(3);
glBegin(GL_LINES);
glVertex3f(tmp_trunk.pos1.x, tmp_trunk.pos1.y, tmp_trunk.pos1.z);
glVertex3f(tmp_trunk.pos2.x, tmp_trunk.pos2.y, tmp_trunk.pos2.z);
glEnd();*/
tmp_trunk.radius = radius;
trunks.push_back(tmp_trunk);
break;
case 'X': //叶子
tmp_leaf.pos1 = curState.pos;
tmp_trunk.pos1 = curState.pos;
curState.pos.x += length * curState.dir.x;
curState.pos.y += length * curState.dir.y;
curState.pos.z += length * curState.dir.z;
tmp_trunk.pos2 = curState.pos;
tmp_leaf.pos2 = curState.pos;
tmp_leaf.radius = leafRadius;
tmp_trunk.radius = radius;
trunks.push_back(tmp_trunk);
leaves.push_back(tmp_leaf);
break;
case 'A':
length = length * lengthFactor;
radius = radius * radiusFactor;
break;
case 'B':
length = length / lengthFactor;
radius = radius / radiusFactor;
break;
case '[':
stack[stackpointer] = curState;
stackpointer++;;
break;
case ']':
curState = stack[stackpointer - 1];
stackpointer--;
break;
case '+': //绕X轴
trans.set(curState.dir);
curState.dir = trans.Rotate('X', dx);
break;
case '-':
trans.set(curState.dir);
curState.dir = trans.Rotate('X', -dx);
break;
case '*': //绕y轴
trans.set(curState.dir);
curState.dir = trans.Rotate('Y', dy);
break;
case '/':
trans.set(curState.dir);
curState.dir = trans.Rotate('Y', -dy);
break;
case '&'://绕z轴
trans.set(curState.dir);
curState.dir = trans.Rotate('Z', dz);
break;
case '%':
trans.set(curState.dir);
curState.dir = trans.Rotate('Z', -dz);
break;
default:
;
}
i++;
}
}
//以yuandian两端点,r为半径
void drawcone(double r,double h) {
glBindTexture(GL_TEXTURE_2D, texin);
for (int i = 0; i <360; i += 50) { //在原点画了一个圆柱
float temp = i * Pi1 / 180;
float temp1 = (i + 50)*Pi1 / 180;
glBegin(GL_QUADS);
glTexCoord2f((temp*r*tree.trunk.radius_shrink)/(2 * Pi1), 0.0f); glVertex3f(r*cos(temp)*tree.trunk.radius_shrink, 0, r*sin(temp)*tree.trunk.radius_shrink);
glTexCoord2f((temp*r)/(2*Pi1), 1.0f); glVertex3f(r*cos(temp), h, r*sin(temp)); //竖着2倍 横着1/2
glTexCoord2f((temp1*r)/(2 * Pi1), 1.0f); glVertex3f(r*cos(temp1), h, r*sin(temp1));
glTexCoord2f((temp1*r*tree.trunk.radius_shrink) / (2 * Pi1), 0.0f); glVertex3f(r*cos(temp1)*tree.trunk.radius_shrink, 0.0f, r*sin(temp1)*tree.trunk.radius_shrink);
glEnd();
}
}
void DrawChannel(Node A, Node B, double r)
{
// 起始线段:以(0,1,0)为起点,它的长度(distance)通过目标线段计算,
// 终点坐标为(0,1-distance,0)
// 目标线段:以(x1,y1,z1)为起点,以(x2,y2,z2)为终点
// 计算目标向量
GLfloat dx = B.x - A.x;
GLfloat dy = B.y - A.y;
GLfloat dz = B.z - A.z;
// 算出目标向量模(即AB长度)
GLfloat distance = sqrt(dx*dx + dy * dy + dz * dz);
// 计算平移量
GLfloat px = A.x;
GLfloat py = A.y - 1;
GLfloat pz = A.z;
// 起始线段的末端点
GLfloat bx = px;
GLfloat by = (1 - distance) + py;
GLfloat bz = pz;
// 计算起始向量
GLfloat sx = bx - A.x;
GLfloat sy = by - A.y;
GLfloat sz = bz - A.z;
// 计算向量(sx,sy,sz)与向量(dx,dy,dz)的法向量(sy*dz - dy*sz,sz*dx - sx*dz,sx*dy - dx*sy)
GLfloat fx = sy * dz - dy * sz;
GLfloat fy = sz * dx - sx * dz;
GLfloat fz = sx * dy - dx * sy;
// 求两向量间的夹角
// 计算第三条边的长度
GLfloat ax = fabs(B.x - bx);
GLfloat ay = fabs(B.y - by);
GLfloat az = fabs(B.z - bz);
GLfloat length = sqrt(ax*ax + ay * ay + az * az);
// 根据余弦定理计算夹角
GLfloat angle = acos((distance*distance * 2 - length * length) / (2 * distance*distance))*180.0f / 3.14159;
// 绘制圆柱
glPushMatrix();
// 变换的顺序与函数书写的顺序相反,
// 即先平移(0,-1,0),再绕法向量旋转,最后再平移
glTranslatef(A.x, A.y, A.z);
glRotatef(angle, fx, fy, fz);
glTranslatef(0, -distance, 0);
drawcone(r, distance);
glPopMatrix();
}
void drawsquare(double r) {
glRotated(180, 0, 0, 1);
glBindTexture(GL_TEXTURE_2D, texout);
glBegin(GL_QUADS);
glTexCoord2f(0.0f, 0.0f); glVertex3f(r / 2, 0.0f, 0.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f(r / 2, r, 0);
glTexCoord2f(1.0f, 1.0f); glVertex3f(-r / 2, r, 0);
glTexCoord2f(1.0f, 0.0f); glVertex3f(-r / 2, 0, 0);

glEnd();
}
void DrawLeaf(Node A, Node B, double r) {
// 起始线段:以(0,1,0)为起点,它的长度(distance)通过目标线段计算,
// 终点坐标为(0,1-distance,0)
// 目标线段:以(x1,y1,z1)为起点,以(x2,y2,z2)为终点
// 计算目标向量
GLfloat dx = B.x - A.x;
GLfloat dy = B.y - A.y;
GLfloat dz = B.z - A.z;
// 算出目标向量模(即AB长度)
GLfloat distance = sqrt(dx*dx + dy * dy + dz * dz);
// 计算平移量
GLfloat px = A.x;
GLfloat py = A.y - 1;
GLfloat pz = A.z;
// 起始线段的末端点
GLfloat bx = px;
GLfloat by = (1 - distance) + py;
GLfloat bz = pz;
// 计算起始向量
GLfloat sx = bx - A.x;
GLfloat sy = by - A.y;
GLfloat sz = bz - A.z;
// 计算向量(sx,sy,sz)与向量(dx,dy,dz)的法向量(sy*dz - dy*sz,sz*dx - sx*dz,sx*dy - dx*sy)
GLfloat fx = sy * dz - dy * sz;
GLfloat fy = sz * dx - sx * dz;
GLfloat fz = sx * dy - dx * sy;
// 求两向量间的夹角
// 计算第三条边的长度
GLfloat ax = fabs(B.x - bx);
GLfloat ay = fabs(B.y - by);
GLfloat az = fabs(B.z - bz);
GLfloat length = sqrt(ax*ax + ay * ay + az * az);
// 根据余弦定理计算夹角
GLfloat angle = acos((distance*distance * 2 - length * length) / (2 * distance*distance))*180.0f / 3.14159;
glPushMatrix();
// 变换的顺序与函数书写的顺序相反,
// 即先平移(0,-1,0),再绕法向量旋转,最后再平移
glTranslatef(A.x, A.y, A.z);
glRotatef(angle, fx, fy, fz);
glTranslatef(0, -distance, 0);
drawsquare(r);
glPopMatrix();
}
void drawsquare(double r) {
glRotated(180, 0, 0, 1);
glBindTexture(GL_TEXTURE_2D, texout);
glBegin(GL_QUADS);
glTexCoord2f(0.0f, 0.0f); glVertex3f(r / 2, 0.0f, 0.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f(r / 2, r, 0);
glTexCoord2f(1.0f, 1.0f); glVertex3f(-r / 2, r, 0);
glTexCoord2f(1.0f, 0.0f); glVertex3f(-r / 2, 0, 0);

glEnd();
}
void DrawLeaf(Node A, Node B, double r) {
// 起始线段:以(0,1,0)为起点,它的长度(distance)通过目标线段计算,
// 终点坐标为(0,1-distance,0)
// 目标线段:以(x1,y1,z1)为起点,以(x2,y2,z2)为终点
// 计算目标向量
GLfloat dx = B.x - A.x;
GLfloat dy = B.y - A.y;
GLfloat dz = B.z - A.z;
// 算出目标向量模(即AB长度)
GLfloat distance = sqrt(dx*dx + dy * dy + dz * dz);
// 计算平移量
GLfloat px = A.x;
GLfloat py = A.y - 1;
GLfloat pz = A.z;
// 起始线段的末端点
GLfloat bx = px;
GLfloat by = (1 - distance) + py;
GLfloat bz = pz;
// 计算起始向量
GLfloat sx = bx - A.x;
GLfloat sy = by - A.y;
GLfloat sz = bz - A.z;
// 计算向量(sx,sy,sz)与向量(dx,dy,dz)的法向量(sy*dz - dy*sz,sz*dx - sx*dz,sx*dy - dx*sy)
GLfloat fx = sy * dz - dy * sz;
GLfloat fy = sz * dx - sx * dz;
GLfloat fz = sx * dy - dx * sy;
// 求两向量间的夹角
// 计算第三条边的长度
GLfloat ax = fabs(B.x - bx);
GLfloat ay = fabs(B.y - by);
GLfloat az = fabs(B.z - bz);
GLfloat length = sqrt(ax*ax + ay * ay + az * az);
// 根据余弦定理计算夹角
GLfloat angle = acos((distance*distance * 2 - length * length) / (2 * distance*distance))*180.0f / 3.14159;
glPushMatrix();
// 变换的顺序与函数书写的顺序相反,
// 即先平移(0,-1,0),再绕法向量旋转,最后再平移
glTranslatef(A.x, A.y, A.z);
glRotatef(angle, fx, fy, fz);
glTranslatef(0, -distance, 0);
drawsquare(r);
glPopMatrix();
}
...全文
969 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
万俟淋曦 2018-06-12
  • 打赏
  • 举报
回复
小白。。只能看懂缺个分号。。。
赵4老师 2018-06-08
  • 打赏
  • 举报
回复
偶遇到类似问题都是用 “每次用/*...*/注释掉不同部分再重新编译,直到定位到具体语法出错的位置。” 的方法解决的。
qq_42408023 2018-06-07
  • 打赏
  • 举报
回复
代码有点长马上大佬给看看

33,311

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 新手乐园
社区管理员
  • 新手乐园社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧