使用着色器在 OpenGL 中显示多个立方体

发布于 2024-12-09 20:01:10 字数 326 浏览 6 评论 0原文

我是 OpenGL 和着色器的新手。我有一个项目涉及使用着色器来显示立方体。

所以基本上我应该使用距原点 (+-10,+-10,+-10) 处的透视投影来显示八个立方体,每个立方体都以不同的颜色。换句话说,会有一个以 (10, 10, 10) 为中心的立方体,另一个以 (10, 10, -10) 为中心的立方体,依此类推。 (+-10, +-10, +-10) 有 8 种组合。然后我应该提供一个键盘命令“c”,每次按下该键时都会更改所有立方体的颜色。

到目前为止,我能够在原点制作一个立方体。我知道我应该使用这个立方体并将其转换为创建八个立方体,但我不确定如何做到这一点。有谁知道我会如何处理这个问题?

I'm new to OpenGL and shaders. I have a project that involves using shaders to display cubes.

So basically I'm supposed to display eight cubes using a perspective projection at (+-10,+-10,+-10) from the origin each in a different color. In other words, there would be a cube centered at (10, 10, 10), another centered at (10, 10, -10) and so on. There are 8 combinations in (+-10, +-10, +-10). And then I'm supposed to provide a key command 'c' that changes the color of all the cubes each time the key is pressed.

So far I was able to make one cube at the origin. I know I should use this cube and translate it to create the eight cubes but I'm not sure how I would do that. Does anyone know how I would go about with this?

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

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

发布评论

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

评论(1

只为一人 2024-12-16 20:01:10

正如前面提到的,这个问题太宽泛了。但你说你成功地画了一个立方体,所以我可以假设你可以设置相机和窗口。这让我们知道如何渲染 8 个立方体。有很多方法可以做到这一点,但我将提到两种截然不同的方法。

经典:
您创建的函数需要 2 个参数 - 立方体中心和大小。通过这两个,您可以像现在一样构建立方体,但您将使用这些变量而不是固定值。例如,前面是:

glBegin(GL_TRIANGLE_STRIP);
glVertex3f(center.x-size/2, center.y-size/2, center.z+size/2);
glVertex3f(center.x+size/2, center.y-size/2, center.z+size/2);
glVertex3f(center.x-size/2, center.y+size/2, center.z+size/2);
glVertex3f(center.x+size/2, center.y+size/2, center.z+size/2);
glEnd();

这只是为了展示如何从变量中制作它,您可以按照现在的方式进行操作。

现在,您提到您想使用着色器。 Shader 主题非常广泛,就像 openGL 本身一样,但我可以告诉你这个想法。在 openGL 3.2 中添加了称为几何的特殊着色器。它们的目的是处理整个几何体 - 相反,顶点着色器一次仅在 1 个顶点上工作,或者片段着色器一次仅在一个片段上工作 - 几何着色器一次仅在一个几何块上工作。如果您正在渲染三角形,您将获得有关刚刚通过着色器的单个三角形的所有信息。这不会有什么严重的问题,但这些着色器不仅可以修改这些几何图形,还可以创建新的几何图形!因此,我正在我的着色器程序之一中进行操作,在其中渲染点,但是当它们通过几何着色器时,这些点将转换为圆形。类似地,您可以仅渲染点,但在几何着色器中您可以渲染整个立方体。点位置将作为这些立方体的中心,并且您应该以统一的方式传递立方体的大小。如果立方体的大小可能不同,您还需要制作顶点着色器,将大小从属性传递到变量,可以在几何着色器中读取。

至于颜色问题,如果你没有实现片段着色器,你只需在渲染立方体之前调用glColor3f即可。它需要 3 个参数 - 红色、绿色和蓝色值。请注意,这些值的范围不是从 0 到 255,而是从 0 到 1。如果您使用白色背景,并且认为当您将颜色设置为 200、10、10 时,您可能会感到困惑,因为您应该将立方体渲染为 200,10,10看到红色立方体,但你什么也看不到。那是因为实际上您渲染的是白色立方体。为了避免此类错误,我建议将背景设置为灰色白色glClearColor

That question is, as mentioned, too broad. But you said that you managed to draw one cube so I can assume that you can set up camera and your window. That leaves us whit how to render 8 cubes. There are many ways to do this, but I'll mention 2 very different ones.

Classic:
You make function that takes 2 parameters - center of cube, and size. Whit these 2 you can build up cube the same way you're doing it now, but instead of fixed values you will use those variables. For example, front face would be:

glBegin(GL_TRIANGLE_STRIP);
glVertex3f(center.x-size/2, center.y-size/2, center.z+size/2);
glVertex3f(center.x+size/2, center.y-size/2, center.z+size/2);
glVertex3f(center.x-size/2, center.y+size/2, center.z+size/2);
glVertex3f(center.x+size/2, center.y+size/2, center.z+size/2);
glEnd();

This is just for showcase how to make it from variables, you can do it the same way you're doing it now.

Now, you mentioned you want to use shaders. Shader topic is very broad, just like openGL itself, but I can tell you the idea. In openGL 3.2 special shaders called geometry were added. Their purpose is to work with geometry as whole - on contrary that vertex shaders works whit just 1 vertex at time or that fragment shaders work just whit one fragment at time - geometry shaders work whit one geometry piece at time. If you're rendering triangles, you get all info about single triangle that is just passing through shaders. This wouldn't be anything serious, but these shaders doesn't only modify these geometries, they can create new ones! So I'm doing in one of my shader programs, where I render points, but when they pass through geometry shader, these points are converted to circles. Similarly you can render just points, but inside geometry shader you can render whole cubes. The point position would work as center for these cubes and you should pass size of cubes in uniform. If size of cubes may vary, you need to make vertex shader also that will pass the size from attribute to variable, which can be read in geometry shader.

As for color problem, if you don't implement fragment shaders, only thing you need to do is call glColor3f before rendering cubes. It takes 3 parameters - red, green and blue values. Note that these values doesn't range from 0 to 255, but from 0 to 1. You can get confused that you cubes aren't rendered if you use white background and think that when you set colors to 200,10,10 you should see red cubes but you don't see anything. That's because in fact you render white cubes. To avoid such errors, I recommend to set background to something like grey whit glClearColor.

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