简体   繁体   中英

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. 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?

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. 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.

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?

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.

Maybe I should just do glBegin(GL_LINES) and not even worry about surfaces to collide against since I can just create that mathematically?

OpenGL isn't going to help you with collisions of any sort.

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.

This is a decent place to start

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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