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

2024-11-05 01:53:57
推荐回答(3个)
回答(1):

#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
PointA[] = { 0.5f, -sqrt(6.0f)/12, -sqrt(3.0f)/6},
PointB[] = {-0.5f, -sqrt(6.0f)/12, -sqrt(3.0f)/6},
PointC[] = { 0.0f, -sqrt(6.0f)/12, sqrt(3.0f)/3},
PointD[] = { 0.0f, sqrt(6.0f)/4, 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;
}

回答完毕。

回答(2):

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

回答(3):

我也不是很懂,你验收一下(有两个参考资料)
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();
}
参考资料: