Java OpenGL 画一颗星星

发布于 2024-08-14 14:45:32 字数 511 浏览 6 评论 0原文

我正在开发一个 OpenGL 项目,我想在其中画一颗星星。它不一定是 3D 外观的星星(2D 外观也可以)。我有星星中心的 xyz 坐标,并且我知道我想要的大小。我不做太多 OpenGL/3d 数学编程,所以这里的任何帮助将不胜感激。

编辑:更多细节:我不确定这是否可能,但我可以在 3d 空间中的某个点绘制一个 2D 的星星吗?我尝试了简单的正方形,并尝试了类似的操作:

gl.glTranslated(x, y, z);
gl.glBegin(GL.GL_QUADS);
gl.glVertex2D(x-size,y-size);
gl.glVertex2D(x-size,y+size);
gl.glVertex2D(x+size,y+size);
gl.glVertex2D(x+size,y-size);
gl.glEnd();

即使我转换为 x、y、z,正方形似乎也没有处于正确的深度。它似乎比应有的距离屏幕前面更近。是否可以像这样将 2d 形状转换为 3d 形状?

谢谢,

杰夫

I'm working on an OpenGL project in which I want to draw a star. It doesn't necessarily have to be a 3d looking star (2d looking is fine). I have the xyz coordinates for the center of the star and I know the size I'd like it to be. I don't do much OpenGL/3d math programming, so any help here would be much appreciated.

EDIT: More detail: I'm not sure if this is possible but can I draw a 2D looking star at some point in 3d space? I tried simple with a square and tried something like:

gl.glTranslated(x, y, z);
gl.glBegin(GL.GL_QUADS);
gl.glVertex2D(x-size,y-size);
gl.glVertex2D(x-size,y+size);
gl.glVertex2D(x+size,y+size);
gl.glVertex2D(x+size,y-size);
gl.glEnd();

The square did not seem to be at the correct depth even though I translated to the x,y,z. It appeared to be closer to the front of the screen than it should be. Is it possible to translate a 2d shape in 3d like this?

thanks,

Jeff

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

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

发布评论

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

评论(2

£噩梦荏苒 2024-08-21 14:45:32

您是否设置了投影矩阵?如果不是,它可能设置为恒等,这意味着顶点的 z 值不会影响它们在屏幕空间中的渲染位置。为了获得你想要的深度感,你需要创建一个透视投影矩阵。您可以使用 glFrustum() 创建投影矩阵。下面是一个示例(来自 Wikipedia 的 OpenGL 页面):

glMatrixMode( GL_PROJECTION );      /* Subsequent matrix commands will affect the projection matrix */
glLoadIdentity();                   /* Initialise the projection matrix to identity */
glFrustum( -1, 1, -1, 1, 1, 1000 ); /* Apply a perspective-projection matrix */

网上有大量免费的 OpenGL 教程,所以您应该可以轻松找到更多信息。

Did you set up your projection matrix? If not, it is probably set to identity, which means the z values of your vertices will have no effect on their rendered positions in screen space. To get the sense of depth you want, you need to create a perspective projection matrix. You can use glFrustum() to create your projection matrix. Here's an example (from Wikipedia's OpenGL page):

glMatrixMode( GL_PROJECTION );      /* Subsequent matrix commands will affect the projection matrix */
glLoadIdentity();                   /* Initialise the projection matrix to identity */
glFrustum( -1, 1, -1, 1, 1, 1000 ); /* Apply a perspective-projection matrix */

There are a ton of free OpenGL tutorials online, so you shouldn't have any trouble finding more info.

鸠魁 2024-08-21 14:45:32

我决定使用 3D 绘图,看起来翻译实际上已经为我完成了,我不需要重新翻译。所以事实上我的翻译偏离了主题。感谢大家的帮助。

I decided to go with the 3d drawing and it looks like the translation was actually already done for me and I didn't need to retranslate. So in fact I was translating away from the point. Thanks for everyone's help.

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