简体   繁体   English

带有可见边的C ++ OpenGL空立方体

[英]C++ OpenGL Empty Cube with Visible Edges

I am trying to create a cube. 我正在尝试创建一个立方体。 I want the cube itself to be clear (black since the background is black), but I'd like the 12 lines to be thin and white. 我希望立方体本身是清晰的(黑色,因为背景是黑色的),但我希望12条线条变得薄而白。 Is the only way to do this to create lines and lay them on top of the edges? 是唯一的方法来创建线条并将它们放在边缘的顶部? Or is there a different way to approach it? 或者有不同的方法来接近它吗?

The reason being I have to create balls bouncing around inside the box. 原因是我必须创造出在盒子里面弹跳的球。

Maybe I should just do glBegin(GL_LINES) and not even worry about surfaces to collide against since I can just create that mathematically? 也许我应该做glBegin(GL_LINES),甚至不担心表面会碰撞,因为我可以用数学方法创建它?

I am just creating my sides like this: 我只是这样创造我的方面:

glBegin(GL_POLYGON);
glVertex3f( -0.5, -0.5,  0.5 );
glVertex3f( -0.5,  0.5,  0.5 );
glVertex3f( -0.5,  0.5, -0.5 );
glVertex3f( -0.5, -0.5, -0.5 );
glEnd();

You can just draw the 'wireframe' cube. 您只需绘制“线框”立方体即可。 You will see the edges but no faces. 您将看到边缘但没有面孔。 Set the fill mode to wire and render lines instead of polygons. 将填充模式设置为连线并渲染线而不是多边形。

glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);  // this tells it to only render lines

glBegin(GL_LINES);

// endpoints of 1 line/edge
glVertex3f( ... 
glVertex3f( ...

// endpoints of second line/edge
glVertex3f( 
glVertex3f( 

// on up thru all 12 lines/edges

glEnd();

Now, this isn't the most efficient. 现在,这不是最有效的。 You could use a line strip perhaps, or just draw 6 quads. 您可以使用线条,或者只绘制6个四边形。 But since this is "day one", this might be an easy start. 但由于这是“第一天”,这可能是一个简单的开始。

Eventually you'll want to not used fixed-functionality at all - it's deprecated. 最终你根本不想使用固定功能 - 它已被弃用。 But this will give you an environment in which to get comfortable with matrices and lighting, etc. When you have serious gemoetry to render, you'll put it in buffers and send it off to the GPU in big chunks, letting your GLSL shaders process the data on the graphics card. 但这将为您提供一个适应矩阵和照明等的环境。当您有严格的宝石渲染时,您将把它放入缓冲区并以大块的形式发送到GPU,让您的GLSL着色器处理显卡上的数据。

Welcome to graphics! 欢迎来图文!

Maybe I should just do glBegin(GL_LINES) and not even worry about surfaces to collide against since I can just create that mathematically? 也许我应该做glBegin(GL_LINES),甚至不担心表面会碰撞,因为我可以用数学方法创建它?

Correct. 正确。 You already know the bounds of your cube. 您已经知道了多维数据集的边界。

Do some lines, and bounce your balls. 做一些线,然后反弹你的球。

You could set the polygon mode (glPolygonMode, read here) to GL_LINE to achieve the same thing. 您可以将多边形模式(glPolygonMode,此处读取)设置为GL_LINE以实现相同的功能。

Maybe I should just do glBegin(GL_LINES) and not even worry about surfaces to collide against since I can just create that mathematically? 也许我应该做glBegin(GL_LINES),甚至不担心表面会碰撞,因为我可以用数学方法创建它?

OpenGL isn't going to help you with collisions of any sort. OpenGL不会帮助您解决任何类型的冲突。

As a somewhat off topic note, consider using a more modern approach. 作为一个有点偏离主题的说明,请考虑使用更现代的方法。 Immediate mode drawing is effectively deprecated, even if you aren't using the newer OpenGL versions. 即使您没有使用较新的OpenGL版本,也会立即弃用立即模式绘制。

This is a decent place to start 是一个体面的起点

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM