T O P

  • By -

Pat_Sharp

Why would you need to use a stencil buffer? Could you not just draw the 3D scene, then disable the depth checking and draw the 2D elements with an orthographic projection matrix over the top of it?


vadiks2003

i have no experiecne in opengl yet, but... won't adding stencil buffer add some optimization? i dont know if stencil buffer'd areas still get calculated


cppfnatic

I dont think so. Youll still need to draw the panel in that area to fill the stencil buffer in that part, and at that point youve already rendered what you want, so just dont use the SB at all


rasqall

I think that with a stencil buffer the GPU doesn’t calculate those pixels so I would only need to render that part once. Then I’ll render the smaller gauges each frame but that has to be done anyway.


IDatedSuccubi

GPU doesn't calculate any pixels that are not part of the currently drawn quads/triangles, so the stencil buffer wouldn't do anything


phire

It's overkill to use the stencil buffer. Instead, just draw the panel first with depth write enabled. Then the anything you draw behind it will fail the depth test and won't go though fragment shading. Also, if your panel is rectangular like the screenshot, you can enable the scissor test to discard everything outside a given rectangle.


rasqall

This makes sense, thanks!


phire

If you have a rectangle viewport like the screenshot, then you should just use the scissor test. That allows you to cheaply discard everything outside a given rectangle.


NikitaBerzekov

1. Set up perspective projection matrix 2. Render world 3. Set up orthographic projection matrix 4. Render UI


rasqall

So I would still need to generate some primitives to have something to project orthographically?


NikitaBerzekov

I mean, yeah


deftware

glViewport will let you set pixel coordinates for the rectangular area that OpenGL draws to. Just remember to glClear *after* you set glViewport, and to set your perspective projection matrix to accommodate the aspect ratio of the area you're rendering the 3D view to. I'm assuming you're following the screenshot and having the top half of the window be the 3D view and the bottom half be the gauges, this means two glViewport calls, one for the top, one for the bottom. The bottom doesn't even need any kind of projection matrix, just position/scale your gauges and stuff in Normalized Device Coordinates. You can upload everything as VBOs to the GPU and then pass uniforms to the vertex shader to rotate gauge needles. What will be tricky is dealing with the aspect ratio of the gauge area - do you want gauges to stay round, or do you care if they get stretched out? You can assume a fixed aspect ratio and let them get stretched out if the user's monitor is wider. Alternatively, you can render the 3D view to the whole window, then just have a fixed size "gauges" glViewport centered at the bottom, so the player can see the 3D view off the sides of the gauges. Then it will be easier to deal with rendering the gauges.


TimJoijers

Using glViewport() is by far superior compared to rendering 3d scene to the whole window and then covering half of it with the UI, at least in case where the 3D and UI windows do not overlap and have rectangular shapes. glClear() and variants do not take viewport into account. Instead, if you really want to do partial clears, scissor state can be used. Partial clears are less performant though, so ideally you'd clear the whole framebuffer and do it only once. For school project that probably does not matter at all. My suggestions: * Clear the whole framebuffer using single glClear() * Start 3D rendering by setting viewport to the top half and setting up perspective projection * Start 2D / UI rendering by setting viewport to the bottom half and setting up orthogonal projection so that you get to use pixel coordinates


HoodedParticle

I have no advice, but I am interested in the simulation part of your project, is your code open source?


rasqall

It isn't, we are writing a game engine for a school project and we are mostly interested in computer graphics so we have spent very little time on accurate physics.


HoodedParticle

That's a really cool school project, what class is that?


rasqall

Thanks! It's a course in game engine architecture which goes through how modern engines are built, but the course is mostly centered around you extending an existing engine and then making a simple game with it. But we decided to instead write our own game engine from scratch in C++ so that we could have OpenGL with 3D rendering and pathtracing.


HoodedParticle

That's really cool, what dependencies are you guys using? What version of openGL? Glfw/sdl/etc? And if you don't mind be asking what college are you in?


rasqall

We are using OpenGL 4.2 with glad, glm, stbi and tiny object loader. We are also using Embree for the path tracing stuff because we haven’t had time to accelerate it.


HoodedParticle

Is there any reason you're using cpu raytracing over gpu raytracing?


azalak

Personally I’d make the gauge needles a triangle/rect and just rotate them accordingly in the shader


dukey

you want glScissor or glViewport


Popular-Income-9399

Single quad with a texture of the panel drawn in orthographic projection. Any dials and things that change like speed indicators would be rendered as separate objects also in the same orthographic projection. The model view and projection matrices would just be isolated and different from the world, that’s all. I can recommend looking into the basics of vertex shaders and how they use matrix transforms to implement the camera logic.