OpenGL - skybox and frustum clipping - opengl

I want to create skybox, which is just textured cube around camera. But actually i don't understand how this can work, because the camera viewing volume is frustum and the skybox is cube. According to this source:
http://www.songho.ca/opengl/gl_projectionmatrix.html
Note that the frustum culling (clipping) is performed in the clip
coordinates, just before dividing by wc. The clip coordinates, xc, yc
and zc are tested by comparing with wc. If any clip coordinate is less
than -wc, or greater than wc, then the vertex will be discarded.
vertexes of skybox faces should be clipped, if they are outside of frustum.
So it looks for me that the cube should be actually a frustum and should match exactly the gl frustum faces, so my whole scene is wrapped inside of that skybox, but i am sure this is bad. Is there any way how to fill whole screen with something, that wraps whole gl frustum?

The formulation from your link is rather bad. It is not actually vertices that get clipped, but rather fragments. Drawing a primitive with vertices completely off-screen does not prevent the fragments that would intersect with the screen from getting drawn. (The picture in the link also actually shows this being the case.)
That having been said, however, it may (or may not, depending on the design of your rendering code) be easier to simply draw a full-screen quad, and use the inverse of the projection matrix to calculate the texture coordinates instead.

Related

OpenGL sheared near clipping plane

I have four arbitrary points (lt,rt,rb,lb) in 3d space and I would like these points to define my near clipping plane (lt stands for left-top, rt for right-top and so on).
Unfortunately, these points are not necessarily a rectangle (in screen space). They are however a rectangle in world coordinates.
The context is that I want to have a mirror surface by computing the mirrored world into a texture. The mirror is an arbitary translated and rotated rectangle in 3d space.
I do not want to change the texture coordinates on the vertices, because that would lead to ugly pixelisation when you e.g. look at the mirror from the side. When I would do that, also culling would not work correctly which would lead to huge performance impacts in my case (small mirror, huge world).
I also cannot work with the stencil buffer, because in some scenarios I have mirrors facing each other which would also lead to a huge performance drop. Furthermore, I would like to keep my rendering pipeline simple.
Can anyone tell me how to compute the according projection matrix?
Edit: Of cause I already have moved my camera accordingly. That is not the problem here.
Instead of tweaking the projection matrix (which I don't think can be done in the general case), you should define an additional clipping plane. You do that by enabling:
glEnable(GL_CLIP_DISTANCE0);
And then set gl_ClipDistance vertex shader output to be the distance of the vertex from the mirror:
gl_ClipDistance[0] = dot(vec4(vertex_position, 1.0), mirror_plane);

Othographic projection causing geometry to get clipped by the far plane

Let's say I have a rectangle that I want to render oriented 45 degrees about the x-axis to the camera. So it looks like this:
(clipped at the bottom only because it's the edge of the window).
As I shift this rectangle along it's local y-direction though (i.e. increase y on it's vertices before we rotate it about the x-axis) then it eventually gets clipped by the far plane:
How do I prevent this? It seems unnatural that the rectangle should be cut off at all when moved away from the viewport but still within actual view.
I do definitely want to render my rectangles orthographically.
I am quite new to OpenGL so I'm thinking I'm missing something here.
The trick it turns out is just to squish the z position of the vertices in the vertex shader. This way more vertices can fit inside the frustrum. This does distort the mesh but in orthographic projection this actually doesn't make a visible difference (at least in my situation).
I.e. inside the vertex shader do this:
gl_Position.z *= 0.5; // or whatever scale you want

Render plane on a glClipPlane

How can I render a plane on the surface of a glClipPlane clipping plane? The plane is rendered by drawing a polygon between a series of points that are located on the plane. Right now, it's producing some very fun stitching.
I assume glPolygonOffset won't help me here?
It is possible for me to just translate the plane a tiny bit over to one side of the plane, but I would prefer a simpler and more elegant solution, if one exists.
I assume you are not using GLSL? If you have GLSL 1.30, you can just set a vertex's gl_ClipDistance [N] >= 0.0 for clip plane N and the point will not be clipped. If you do that for every vertex in the polygon you want to span your plane, the polygon will not be clipped (against that plane anyway).
As for glPolygonOffset (...) that affects the depth computed during rasterization. It happens after clipping, and likewise clipping has nothing to do with polygon depth, so you are correct it will not help. You will have to translate your vertices before/during primitive assembly for this to work, so that either means performing translation in a vertex shader or using the fixed-function modelview matrix.

OpenGL 360 degree perspective

I'm looking to capture a 360 degree - spherical panorama - photo of my scene. How can I do this best? If I have it right, I can't do this the ordinary way of setting the perspective to 360.
If I would need a vertex shader, is there one available?
This is actually a nontrivial thing to do.
In a naive approach a vertex shader that transforms the vertex positions not by matrix multiplication, but by feeding them through trigonometric functions may seem to do the trick. The problem is, that this will not make straight lines "curvy". You could use a tesselation shader to add sufficient geometry to compensate for this.
The most straightforward approach is two-fold. First you render your scene into a cubemap, i.e. render with a 90°×90° FOV into the 6 directions making up a cube. This allows you to use regular affine projections rendering the scene.
In a second step you use the generated cubemap to texture a screen filling grid, where the texture coordinates of each vertex are azimuth and elevation.
Another approach is to use tiled rendering with very small FOV and rotating the "camera", kind of like doing a panoramic picture without using a wide angle lens. As a matter of fact the cubemap based approach is tiled rendering, but its easier to get right than trying to do this directly with changed camera direction and viewport placement.

Multiple view frustum clipping

The function gluPerspective() can be used to set near Z and far Z clipping planes.
I want to draw a scene clipped at a certain far Z plane,
and draw another scene beyond this Z plane.
Is it possible to do this clipping twice per frame?
There's no reason you shouldn't be able to do this.
Simply setup the first perspective, draw the first scene and then setup the second perspective and draw the seconds scene, all within the drawing of the same frame.
This is generally referred to as multi-pass rendering.
You might need to do a draw the farthest scene first and do a glClear(GL_DEPTH_BUFFER_BIT); before you draw the nearest scene.
A possibility is to assign different depth ranges for the scenes. Some pseudo code would be :
glDepthRange(0.5, 1.0)
draw_far_scene
glDepthRange(0.0, 0.5)
draw_near_scene
You have to setup your projection matrices to perform the proper clipping for the near / far scenes.
The depth ranges assignment is needed to prevent the depth buffer to 'merge' both renderings.