如何在 OpenGL 上绘制一个像圆锥体一样的填充信封(使用 GLUT)?

发布于 2024-07-08 02:46:28 字数 1165 浏览 10 评论 0原文

我正在使用 freeglut 进行 opengl 渲染...

我需要绘制一个看起来像圆锥体(2D)的信封,必须填充一些颜色并应用一些透明度。

freeglut 工具包是否配备了这样的内置功能来绘制填充的几何图形(或一些技巧)? 或者是否有其他一些 api 具有对填充几何图形的内置支持..

Edit1: 只是为了澄清 2D 圆锥体的问题...包络线是飞机在拦截(敌方飞机)期间覆盖区域的图形解释...类似于圆的扇形...我应该提到扇形。和

glutSolidCone 对我没有帮助,因为我想绘制一个填充的圆扇形...我已经完成了...剩下要做的就是用一些颜色填充它... 如何在opengl中用颜色填充几何图形?

编辑2: 发布到这个问题的所有答案都可以在某种程度上解决我的问题。 但我肯定想知道一种如何用某种颜色填充几何图形的方法。 假设我想绘制一个抛物线的信封...在这种情况下,就没有默认的 glut 函数来实际绘制填充的抛物线(或者有吗?).. 所以概括这个问题......如何用某种纯色绘制自定义几何图形?

编辑3: mstrobl 发布的答案适用于 GL_TRIANGLES,但对于这样的代码:

glBegin(GL_LINE_STRIP);

glColor3f(0.0, 0.0, 1.0);
    glVertex3f(0.0, 0.0, 0.0);
glColor3f(0.0, 0.0, 1.0);
    glVertex3f(200.0, 0.0, 0.0);
glColor3f(0.0, 0.0, 1.0);
    glVertex3f(200.0, 200.0, 0.0);
glColor3f(0.0, 0.0, 1.0);
    glVertex3f(0.0, 200.0, 0.0);
glColor3f(0.0, 0.0, 1.0);
    glVertex3f(0.0, 0.0, 0.0);

glEnd();

绘制一个正方形...仅绘制一个有线正方形...我需要用蓝色填充它。

无论如何要做吗?

如果我为闭合曲线放置一些绘图命令..像馅饼一样..并且我需要用颜色填充它有没有办法使它成为可能...

我不知道GL_TRIANGLES如何可能...但是如何对任何闭合曲线执行此操作?

I am using freeglut for opengl rendering...

I need to draw an envelop looking like a cone (2D) that has to be filled with some color and some transparency applied.

Is the freeglut toolkit equipped with such an inbuilt functionality to draw filled geometries(or some trick)?
or is there some other api that has an inbuilt support for filled up geometries..

Edit1:
just to clarify the 2D cone thing... the envelop is the graphical interpretation of the coverage area of an aircraft during interception(of an enemy aircraft)...that resembles a sector of a circle..i should have mentioned sector instead..

and glutSolidCone doesnot help me as i want to draw a filled sector of a circle...which i have already done...what remains to do is to fill it with some color...
how to fill geometries with color in opengl?

Edit2:
All the answers posted to this questions can work for my problem in a way..
But i would definitely would want to know a way how to fill a geometry with some color.
Say if i want to draw an envelop which is a parabola...in that case there would be no default glut function to actually draw a filled parabola(or is there any?)..
So to generalise this question...how to draw a custom geometry in some solid color?

Edit3:
The answer that mstrobl posted works for GL_TRIANGLES but for such a code:

glBegin(GL_LINE_STRIP);

glColor3f(0.0, 0.0, 1.0);
    glVertex3f(0.0, 0.0, 0.0);
glColor3f(0.0, 0.0, 1.0);
    glVertex3f(200.0, 0.0, 0.0);
glColor3f(0.0, 0.0, 1.0);
    glVertex3f(200.0, 200.0, 0.0);
glColor3f(0.0, 0.0, 1.0);
    glVertex3f(0.0, 200.0, 0.0);
glColor3f(0.0, 0.0, 1.0);
    glVertex3f(0.0, 0.0, 0.0);

glEnd();

which draws a square...only a wired square is drawn...i need to fill it with blue color.

anyway to do it?

if i put some drawing commands for a closed curve..like a pie..and i need to fill it with a color is there a way to make it possible...

i dont know how its possible for GL_TRIANGLES... but how to do it for any closed curve?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

甜是你 2024-07-15 02:46:28

关于Edit3:我理解你的问题的方式是你想要OpenGL绘制边框并且它们之间的任何东西都应该填充颜色。

你的想法是对的,但线带就是一条线,它没有任何面积。

但是,您可以将这些线相互连接以定义多边形。 这将在每个顶点的基础上填充多边形的区域。 调整您的代码:

glBegin(GL_POLYGON);

glColor3f(0.0, 0.0, 1.0);
    glVertex3f(0.0, 0.0, 0.0);
glColor3f(0.0, 0.0, 1.0);
    glVertex3f(200.0, 0.0, 0.0);
glColor3f(0.0, 0.0, 1.0);
    glVertex3f(200.0, 200.0, 0.0);
glColor3f(0.0, 0.0, 1.0);
    glVertex3f(0.0, 200.0, 0.0);
glColor3f(0.0, 0.0, 1.0);
    glVertex3f(0.0, 0.0, 0.0);

glEnd();

但是请注意,以这种方式绘制多边形有两个限制:

  • 多边形必须是凸的。
  • 这是一个缓慢的操作。

但我假设你只是想完成工作,这就能做到。 对于未来,您可能会考虑仅对多边形进行三角测量。

On Edit3: The way I understand your question is that you want to have OpenGL draw borders and anything between them should be filled with colors.

The idea you had was right, but a line strip is just that - a strip of lines, and it does not have any area.

You can, however, have the lines connect to each other to define a polygon. That will fill out the area of the polygon on a per-vertex basis. Adapting your code:

glBegin(GL_POLYGON);

glColor3f(0.0, 0.0, 1.0);
    glVertex3f(0.0, 0.0, 0.0);
glColor3f(0.0, 0.0, 1.0);
    glVertex3f(200.0, 0.0, 0.0);
glColor3f(0.0, 0.0, 1.0);
    glVertex3f(200.0, 200.0, 0.0);
glColor3f(0.0, 0.0, 1.0);
    glVertex3f(0.0, 200.0, 0.0);
glColor3f(0.0, 0.0, 1.0);
    glVertex3f(0.0, 0.0, 0.0);

glEnd();

Please note however, that drawing a polygon this way has two limitations:

  • The polygon must be convex.
  • This is a slow operation.

But I assume you just want to get the job done, and this will do it. For the future you might consider just triangulating your polygon.

往事随风而去 2024-07-15 02:46:28

我不确定你所说的“信封”是什么意思,但圆锥体是 glut 所具有的一个基本元素:

glutSolidCone(radius, height, number_of_slices, number_of_stacks)

用颜色填充它的最简单方法就是用颜色绘制它。 由于您希望使其稍微透明,因此还需要一个 alpha 值:

glColor4f(float red, float green, float blue, float alpha)
// rgb and alpha run from 0.0f to 1.0f; in the example here alpha of 1.0 will
// mean no transparency, 0.0 total transparency. Call before drawing.

要半透明渲染,必须启用混合。 并且您必须设置要使用的混合功能。 您想做的事情可能会通过以下方式实现。 如果您想了解更多信息,请给我留言,我会寻找一些好的建议。 但是您的设置是这样的:

glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

在执行任何绘图操作之前(可能在程序初始化时)调用它。 :)

I'm not sure what you mean by "an envelop", but a cone is a primitive that glut has:

glutSolidCone(radius, height, number_of_slices, number_of_stacks)

The easiest way to fill it with color is to draw it with color. Since you want to make it somewhat transparent, you need an alpha value too:

glColor4f(float red, float green, float blue, float alpha)
// rgb and alpha run from 0.0f to 1.0f; in the example here alpha of 1.0 will
// mean no transparency, 0.0 total transparency. Call before drawing.

To render translucently, blending has to be enabled. And you must set the blending function to use. What you want to do will probably be achieved with the following. If you want to learn more, drop me a comment and I will look for some good pointers. But here goes your setup:

glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

Call that before doing any drawing operations, possibly at program initialization. :)

如痴如狂 2024-07-15 02:46:28

既然您重新澄清了要吃馅饼的问题:也有一种使用 opengl 基元来绘制馅饼的简单方法:

您可以使用 gluSolidSphere() 绘制实心球体。 但是,由于您只想绘制其中的一部分,因此只需将不需要的部分剪掉即可:

void glClipPlane(GLenum plane, const GLdouble * equation);

​​平面为 GL_CLIPPLANE0 至 GL_CLIPPLANEn,方程为正规形式的平面方程 (ax + by + c *z + d = 0 意味着方程将保存值 { a, b, c, d } 请注意,这些值是双精度数而不是浮点数。

Since you reclarified your question to ask for a pie: there's an easy way to draw that too using opengl primitives:

You'd draw a solid sphere using gluSolidSphere(). However, since you only want to draw part of it, you just clip the unwanted parts away:

void glClipPlane(GLenum plane, const GLdouble * equation);

With plane being GL_CLIPPLANE0 to GL_CLIPPLANEn and equation being a plane equation in normal form (ax + by + c*z + d = 0 would mean equation would hold the values { a, b, c, d }. Please note that those are doubles and not floats.

浅听莫相离 2024-07-15 02:46:28

我记得有一个子程序。 但自己做起来也不是太难。

但我不明白二维的东西。 二维圆锥体? 不就是一个三角形吗?

无论如何,这里有一个在 opengl 中绘制圆锥体的算法

首先取一个圆,将其均匀细分,以便获得大量边缘。
现在选择圆的中心,从边缘到圆的中心制作三角形。 然后在圆上选择一个点,并从边缘到该点制作三角形。

大小、形状和方向取决于用于生成圆和两个点的值。 每个步骤都相当简单,不会给您带来麻烦。

首先细分一个标量值。 从 [0-2] 范围开始。 取中点 ((start+end)/2) 并用它分割范围。 将值存储为对。 例如,细分一次应该得到: [(0,1), (1,2)] 递归执行几次,然后计算这些点在圆上的位置。 简单的三角函数,只需记住在继续之前将这些值乘以 π 即可。 之后你就有了一定数量的边缘。 2^n 其中 n 是细分数量。 然后你可以简单地将它们变成三角形,方法是多给它们一个顶点。 因此三角形的数量最终为:2^(n+1)。 (如果您使用固定大小的数组来执行此操作,则数量很有用。

编辑:您真正想要的是一个馅饼。(对不起双关语)

它的渲染同样简单。您可以再次使用只需选择标量范围 [-0.25 - 0.25],细分、投影到圆,并生成一组三角形。

标量 - 圆投影很简单: x=cos(v*pi)r, y= sin(vpi)*r 其中 (x,y) 是结果顶点,r 是半径,三角函数作用于辐射度,而不是度数(如果它们作用于度数,请将 pi 替换为 180)。

使用顶点缓冲区或列表自己渲染它:

关于着色问题,如果您希望几何体的某些部分的颜色不同,您可以为顶点缓冲区本身中的每个顶点分配一个颜色。现在不知道执行此操作的所有 API 调用,但 opengl 中的 API 参考是很容易理解的。

I remember there was a subroutine for that. But it's neither too hard to do by yourself.

But I don't understand the 2D -thing. Cone in 2D? Isn't it just a triangle?

Anyway, here's an algorithm to drawing a cone in opengl

First take a circle, subdivision it evenly so that you get a nice amount of edges.
Now pick the center of the circle, make triangles from the edges to the center of the circle. Then select a point over the circle and make triangles from the edges to that point.

The size shape and orientation depends about the values you use to generate the circle and two points. Every step is rather simple and shouldn't cause trouble for you.

First just subdivision a scalar value. Start from [0-2] -range. Take the midpoint ((start+end)/2) and split the range with it. Store the values as pairs. For instance, subdividing once should give you: [(0,1), (1,2)] Do this recursively couple of times, then calculate what those points are on the circle. Simple trigonometry, just remember to multiply the values with π before proceeding. After this you have a certain amount of edges. 2^n where n is the amount of subdivisions. Then you can simply turn them into triangles by giving them one vertex point more. Amount of triangles ends up being therefore: 2^(n+1). (The amounts are useful to know if you are doing it with fixed size arrays.

Edit: What you really want is a pie. (Sorry the pun)

It's equally simple to render. You can again use just triangles. Just select scalar range [-0.25 - 0.25], subdivide, project to circle, and generate one set of triangles.

The scalar - circle projection is simple as: x=cos(v*pi)r, y=sin(vpi)*r where (x,y) is the resulting vertex point, r is a radius, and trigonometric functions work on radiances, not degrees. (if they work with degrees, replace pi with 180)

Use vertex buffers or lists to render it yourself.

Edit: About the coloring question. glColor4f, if you want some parts of the geometry to be different by its color, you can assign a color for each vertex in vertex buffer itself. I don't right now know all the API calls to do it, but API reference in opengl is quite understandable.

一百个冬季 2024-07-15 02:46:28

关于颜色的编辑:

OpenGL实际上是一个状态机。 这意味着绘图时使用当前材质和/或颜色位置。 由于您可能不会使用材料,因此暂时忽略它。 你想要颜色。

glColor3f(float r, float g, float b) // draw with r/g/b color and alpha of 1
glColor4f(float r, float g, float b, float alpha)

在执行 glColorXX 调用之后,这将影响您绘制的任何顶点的颜色、渲染的任何几何体的颜色 - 无论是 glu 的还是您自己的。 如果绘制带有顶点的面并在 glVertex3f/glVertex2f 调用之间更改颜色,则会对颜色进行插值。

试试这个:

glBegin(GL_TRIANGLES);
glColor3f(0.0, 0.0, 1.0);
glVertex3f(-3.0, 0.0, 0.0);
glColor3f(0.0, 1.0, 0.0);
glVertex3f(0.0, 3.0, 0.0);
glColor3f(1.0, 0.0, 0.0);
glVertex3f(3.0, 0.0, 0.0);
glEnd();

但我已经指出了 glColor4f,所以我假设您想在每个顶点的基础上设置颜色。 并且您想使用显示列表进行渲染。

就像您可以显示顶点列表一样,您也可以让它们具有颜色列表:您所需要做的就是启用颜色列表并告诉 opengl 列表所在的位置。 当然,它们需要与顶点列表具有相同的装备(相同的顺序)。

如果你有的话,

glEnableClientState(GL_VERTEX_ARRAY);   
glVertexPointer(3, GL_FLOAT, 0, vertices_);
glDisableClientState(GL_VERTEX_ARRAY);

你应该这样添加颜色。 它们不必是浮动的; 事实上,你告诉它它应该是什么格式。 对于每个通道 1 个字节和 4 个通道(R、G、B 和 A)的颜色列表,请使用以下命令:

glEnableClientState(GL_VERTEX_ARRAY);   
glEnableClientState(GL_COLOR_ARRAY);
glVertexPointer(3, GL_FLOAT, 0, vertices_);
glColorPointer(4, GL_UNSIGNED_BYTE, 0, colors_);
glDisableClientState(GL_COLOR_ARRAY);
glDisableClientState(GL_VERTEX_ARRAY);

编辑:忘记添加,然后您必须通过调用 glDrawElements

On the edit on colors:

OpenGL is actually a state machine. This means that the current material and/or color position is used when drawing. Since you probably won't be using materials, ignore that for now. You want colors.

glColor3f(float r, float g, float b) // draw with r/g/b color and alpha of 1
glColor4f(float r, float g, float b, float alpha)

This will affect the colors of any vertices you draw, of any geometry you render - be it glu's or your own - after the glColorXX call has been executed. If you draw a face with vertices and change the color inbetween the glVertex3f/glVertex2f calls, the colors are interpolated.

Try this:

glBegin(GL_TRIANGLES);
glColor3f(0.0, 0.0, 1.0);
glVertex3f(-3.0, 0.0, 0.0);
glColor3f(0.0, 1.0, 0.0);
glVertex3f(0.0, 3.0, 0.0);
glColor3f(1.0, 0.0, 0.0);
glVertex3f(3.0, 0.0, 0.0);
glEnd();

But I pointed at glColor4f already, so I assume you want to set the colors on a per-vertex basis. And you want to render using display lists.

Just like you can display lists of vertices, you can also make them have a list of colors: all you need to do is enable the color lists and tell opengl where the list resides. Of course, they need to have the same outfit as the vertex list (same order).

If you had

glEnableClientState(GL_VERTEX_ARRAY);   
glVertexPointer(3, GL_FLOAT, 0, vertices_);
glDisableClientState(GL_VERTEX_ARRAY);

you should add colors this way. They need not be float; in fact, you tell it what format it should be. For a color list with 1 byte per channel and 4 channels (R, G, B and A) use this:

glEnableClientState(GL_VERTEX_ARRAY);   
glEnableClientState(GL_COLOR_ARRAY);
glVertexPointer(3, GL_FLOAT, 0, vertices_);
glColorPointer(4, GL_UNSIGNED_BYTE, 0, colors_);
glDisableClientState(GL_COLOR_ARRAY);
glDisableClientState(GL_VERTEX_ARRAY);

EDIT: Forgot to add that you then have to tell OpenGL which elements to draw by calling glDrawElements.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文