平移计算机图形学代码注释,求代码注释:计算机图形学的OpenGL画四面体。高手来吧。争取每句都注释下。谢谢...

求代码注释:计算机图形学的OpenGL画四面体。高手来吧。争取每句都注释下。谢谢

答案:3  信息版本:手机版

解决时间 2018-12-20 05:00

已解决

2018-12-20 01:58

求代码注释:计算机图形学的OpenGL画四面体。高手来吧。争取每句都注释下。谢谢

最佳答案

2018-12-20 03:34

#include #define WIDTH 400

#define HEIGHT 400

#include #define ColoredVertex(c, v) do{ glColor3fv(c); glVertex3fv(v); }while(0) //这段就不用解释了吧……

GLfloat angle = 0.0f; //设定转角

void myDisplay(void) //绘图函数

{

static int list = 0;

if( list == 0 )

{

// 如果显示列表不存在,则创建

GLfloat //GLfloat为OpenGL用到的数据类型,与C的float基本一致

PointA[] = {-0.5, -5*sqrt(5)/48, sqrt(3)/6}, //此处为4个顶点的坐标,因为时3D坐标系下的,所以每个坐标有3个分量,分别对应X,Y,Z轴。至于各轴方向定义……默认下屏幕水平为X,竖直为Y,里外为Z。

PointB[] = { 0.5, -5*sqrt(5)/48, sqrt(3)/6},

PointC[] = { 0, -5*sqrt(5)/48, -sqrt(3)/3},

PointD[] = { 0, 11*sqrt(6)/48, 0};

GLfloat

ColorR[] = {1, 0, 0}, //定义颜色数组,每个数组为一个颜色,也含有3个分量,对应红,绿,蓝,分量范围[0,1],每种颜色都可看做是这3个颜色混合得到。可一自己改变下其中的数值看看具体效果。

ColorG[] = {0, 1, 0},

ColorB[] = {0, 0, 1},

ColorY[] = {1, 1, 0};

list = glGenLists(1);

glNewList(list, GL_COMPILE); //创建一个顶点表,这个表里包含有绘图的顶点信息

glBegin(GL_TRIANGLES); //开始绘图,(GL_TRIANGLES)表示绘制三角形

// 平面ABC

ColoredVertex(ColorR, PointA); //以颜色R绘制点A,以下类推,ColoredVertex()函数在程序开头定义了。

ColoredVertex(ColorG, PointB);

ColoredVertex(ColorB, PointC);

// 平面ACD

ColoredVertex(ColorR, PointA);

ColoredVertex(ColorB, PointC);

ColoredVertex(ColorY, PointD);

// 平面CBD

ColoredVertex(ColorB, PointC);

ColoredVertex(ColorG, PointB);

ColoredVertex(ColorY, PointD);

// 平面BAD

ColoredVertex(ColorG, PointB);

ColoredVertex(ColorR, PointA);

ColoredVertex(ColorY, PointD);

glEnd();

glEndList(); //结束绘图 结束绘图顶点表。

glEnable(GL_DEPTH_TEST); //打开深度测试。打开深度测试的作用是:如果在场景中有多个物体,而它们相对观察者的距离不同(简单理解为远近),那么这个时候,前面的物体则可以挡住后面的物体(没错吧),使场景具有深度感。如果不打开深度测试,那么绘图会按绘制的顺序,后绘制的物体覆盖住现绘制的物体。这里要注意的是,深度仅影响物体重合时谁显示谁不显示,并不影响其3D效果,远处的物体仍然会变“小”,物体在空间中的位置仍为三维的。

}

// 已经创建了显示列表,在每次绘制正四面体时将调用它

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); //清除颜色缓存和深度缓存

glPushMatrix(); //绘图坐标系入栈,可以简单理解为存下了这次画图时画笔的起始位置

glRotatef(angle, 1, 0.5, 0); //绘图坐标系绕(1,0.5,0)轴旋转angle角,可理解为模型绕该轴旋转angle角。

glCallList(list); //调用顶点表,绘制四面体模型

glPopMatrix(); //绘图坐标系出栈,则回到了刚才的绘图起始位置

glutSwapBuffers(); //使用双缓存(第一个在前面显示模型,另一个在后面绘制新的模型,当新模型绘制完毕后送给第一个缓存显示。这样不会出现模型上一些点已经绘制完了,而另一些点还在绘制的情况。)

}

void myIdle(void)

{

++angle;

if( angle >= 360.0f )

angle = 0.0f; //转角超过360度,将其置零。

myDisplay(); //绘制模型}

int main(int argc, char* argv[]) { glutInit(&argc, argv); //创建绘图窗口

glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE); //设置绘图模式

glutInitWindowPosition(200, 200); //设置窗口位置

glutInitWindowSize(WIDTH, HEIGHT); //设置窗口大小

glutCreateWindow("OpenGL 窗口"); //设置窗口标题

glutDisplayFunc(&myDisplay); //重复调用函数

glutIdleFunc(&myIdle);//重复调用函数

glutMainLoop(); //该循环用来对上述两个重复调用函数不断调用

return 0;

}

回答完毕。

全部回答

1楼

2018-12-20 04:40

我也不是很懂,你验收一下(有两个参考资料)

CS 535

WEILER-ATHERTON

PROBLEM

Implement Weiler-Atherton. You should draw each polygon in a different color and fill the clip areas generated with a third color.

NOTES:

The Weiler-Atherton algorithm is the most general of the clipping algorithms. We have two 2D polygons (may be convex or concave and they both may have holes). The polygon to be clipped is called the SUBJECT polygon and the polygon defining the clipping area is called the CLIP polygon. To make the algorithm work easier (in the data structures, etc.) we usually assume that the exterior vertices are given clockwise and the hole vertices are given counterclockwise. In clipping we usually want to find the parts of the subject polygon that are inside the clip polygon. However, this algorithm can be used in modeling to find the "union", "intersection", and "difference" of the polygons.

The data structures are several circular linked lists of vertices which are also linked together and the clipping is done by traversing these. The lists could be doubly linked. This enables the traversal in either direction at any node in the list. Starting at a particular node and traversing in one direction will produce the interior polygon(s) while starting at a different node and traversing can produce the outside. Note that producing the exterior does need the doubly linking and care must be taken in performing the traversal.

STEP 1:

The first phase of the building of the data structures occurs when we input the edges and put them in two linked lists - the SUBJ list and the CLIP list. The vertices are place in order from input (thus clockwise). There are separate lists for the exterior and for each hole. Thus, the initial build is a standard queue insert (input a vertex - insert it at end of list). Note that this creates a list in which a standard list traversal is equivalent to "walking around" the polygon edge visiting the vertices in order.

STEP 2:

The second phase of the list building is computing and inserting the INTERSECTION points. If we have a SUBJECT polygon edge (SVi to SVi+1) that intersects a CLIP polygon edge (CVj to CVj+1) at a point INTER. Note that the edges form straight lines that may intersect, we are assuming that the line segments SVi to SVi+1 intersects the line segment CVj to CVj+1. The intersection point INTER is then inserted on BOTH of the linked lists - SUBJ and CLIP. It is inserted BETWEEN SVi and SVi+1 on the SUBJ list and CVj and CVj+1 on the CLIP list. The idea is still that traversing the list using a standard list traversal we would encounter the points in their geometric order. Note that there may be several intersection points between any given pair of vertices and they must be inserted in the correct order in the lists. This is done by using the t and u values computed in the vector line intersection subprogram. Each intersection point thus has TWO nodes - one on each list (SUBJ and CLIP). We link these two entries together which provides a way of getting from one list to the other.

STEP 2.5:

Any straight line divides the plane into two half-planes. Thus each polygon edge (extended to a line) will divide the plane into two half-planes. Because we listed the vertices clockwise, we consider the half-plane to the right as containing the interior of the polygon. Thus the right half-plane is called the interior half-plane. If we consider ourselves as "walking along" a polygon edge, then we can categorize each of the INTERSECTION points as "entering" or "exiting". This is usually done from the SUBJ polygon's point of view. Thus, as we walk along the SUBJ polygon edge SVi to SVi+1 and we encounter intersection point INTER, we can ask the question - am I "entering" the CLIP polygon or am I "exiting" the CLIP polygon? The second part of computing the intersection point is to classify them as "entering" or "exiting". We create one or two lists - one for entering intersections and one for exiting intersections.

STEP3:

Once the lists are built the basic idea to do the clipping is as follows

Pick an entry from the ENTERING list - it will be an intersection point (and delete it)

Locate that point on the SUBJ list

Traverse the current (SUBJ) list until you find the next intersection point - it should be an exiting or entering point. Output each vertex encountered to some data structure, say POLY

Follow the link from the current (SUBJ) list to the other (CLIP) list and

Continue the traversal until you find the next intersection (Note: delete each entering intersection from the ENTERING list - not the linked lists. By deleting it we will get the distinct intersecting polygons and not duplicate a polygon multiple times).

Terminate the traversal when you get to an intersection that is the SAME as the initial one that you removed from the ENTERING list. At this point POLY will have one of the clip regions and can be output.

REPEAT the construction and output of POLY until the ENTERING list is empty.

Remark: For the exterior, try starting with an EXITING point. Start the traversal on the SUBJ list (same direction as the Interior). At what point do you need to use the double link and to traverse in the opposite direction? (Hint: look at the CLIP polygon list).

IMPLEMENTATION:

In the below data structures we place all of the vertices and intersection points in a 1D array and use the subscript instead of the actual coordinates.

const int MAXVERT = 500;

const int MAXPOLYV = 50;

const int MAXH = 10;

struct Point2D

{float x,y;

};

typedef Point2D Vertices[MAXVERT];

enum VerType = ;

typedef struct ClipListRec * ClipPtr;

struct ClipListRec

{ int Vindex;

ClipPtr next;

VerType Kind;

float t;

ClipPtr otherList;

}

struct Polygon

{ int nVertex;

int vert[MAXPOLYV];

ClipPtr list;

}

struct GenPolygon

{ Polygon exterior;

int numHoles;

Polygon Holes[MAXH];

}

GenPolygon Sub,Clip;

int entering[MAXVERT],exiting[MAXVERT];

Vertices V;

int numVertex = 0; // size of the array of verticies

int clipPoly[MAXVERT]; // a clip polygon

int readPoint();

{ cin >> inX; cin >> inY;

if (numVertex < MAXVERT)

{ V[numVertex].x = inX;

V[numVertex].y = inY;

idNo = numVertex;

numVertex++;

}

else

idNo = -1;

return idNo;

}

void readPolygon (GenPolygon p)

{ cin >> p.exterior.nVertex;

for (i = 0; i < p.exterior.nVertex; i++)

{ newId = readPoint();

if (newId < 0)

error

else

{ insertAtRear (p.exterior.list,newId);

p.exterior.vert[i] = newId;

}

}

// now do the holes basically the same way

. . .

}

// the "main" program loop would then be (pseudocode)

while (!EMPTY(entering))

{ nextInter = delete (entering);

SEARCH (SubjectPolygon,nextInter,ptr1);

AddToOutputList (ptr1->. . .)

StartPoint = ptr1->. . .

ptr1 = prt1->next;

while (ptr1->. . . != StartPoint)

{ AddToOutputList (ptr1->. . .);

if (ptr1-> . . == INTERSECTION)

ptr1 = prt1->otherList->next;

else

ptr1 = ptr1->next;

}

FixListForOutput();

DrawPolygon();

EmptyOutputList();

}

参考资料:

2楼

2018-12-20 03:49

很专业的,有专门论坛吧,用于#3D的一种方法。可是我们不采用,也不相信是有效的。换一个吧。

图杭GIs软件真3D软件。

我要举报

如果感觉以上信息为低俗/不良/侵权的信息,可以点下面链接进行举报,我们会做出相应处理,感谢你的支持!

大家都在看

推荐资讯

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
编写OpenGL程序来绘制三维空间一个四面体的三视图并输出。 首先,我们需要创建一个OpenGL窗口和布,以便我们可以在其绘制三维物体。然后,我们使用OpenGL提供的函数来设置透视投影,调整视口大小和位置等。 接下来,我们需要定义四面体的顶点坐标。由于平移矢量为1,我们可以假设四面体的其一个顶点位于原点,然后通过向各个方向平移1个单位来得到其他顶点的坐标。例如,我们可以将四面体的原点设置为(0, 0, 0),然后其他三个顶点的坐标可以是(1, 0, 0),(0, 1, 0),(0, 0, 1)。 然后,我们可以使用OpenGL提供的绘制函数来绘制四面体的三视图。根据题目要,我们需要绘制正面视图、侧面视图和顶视图。 对于正面视图,我们可以使用OpenGL的三角形绘制函数来连接四面体的顶点,从而形成一个正面视图。 对于侧面视图,我们需要以不同的顺序连接四面体的顶点,以获得正确的侧面投影。同样,我们可以使用OpenGL的绘制函数来完成这个任务。 对于顶视图,我们需要使用不同的顺序连接四面体的顶点,并使用正确的投影坐标系来绘制。同样,我们可以使用OpenGL的绘制函数来实现这个要。 最后,我们可以在屏幕上输出三个视图,以展示四面体的三视图。这样,我们就完成了编写空间四面体的三视图程序并输出的任务。 需要注意的是,上述过程是一个简化的示例,实际的编写过程可能涉及更多的OpenGL函数和计算步骤。同时,我们还可以添加一些额外的功能,如旋转、缩放和交互等来增强程序的可玩性和视觉效果。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值