# Tag Info

## Hot answers tagged projection-matrix

14

Because if you only divide [x, y, z] by z you get [x/z, y/z, 1] and you lost the actual value of z, which is actually useful if you want to do near/far plane clipping or fill a Z-buffer. The best way to keep some information about z, at least on the GPU, is therefore to use 4 components instead of 3. In practice, what is actually in the last two vector ...

4

user1118321's answer will provide you the correct answer, though it is more general than necessary. Since we're dealing with a right triangle, the easiest solution is to use the definition of the tangent function: tan(α) = A / B Substituting half the height of the screen, the z coordinate of the camera, and half the vertical field of view gets us: tan(22....

4

Wow that was fast. It's crazy how sometimes just writing out the question helps you figure out how to approach a solution. Here is my matrixPerspective function: void matrixPerspective(float angle, float near, float far, float aspect, mat4 m) { //float size = near * tanf(angle / 360.0 * M_PI); float size = near * tanf(degreesToRadians(angle) / 2.0)...

4

For performance reasons GPUs use some simplified approach to projection and assume that you want to project your geometry on a plane, so it works only for FOV angles < 180 deg. You could try some tricks in shaders and compute your custom projection there, but then you'll get into another problem - the assumption that polygons edges, being straight lines ...

3

This was caused by an uninitialized variable being fed into the projection function: static int win_width; //is 0 at initialization static int win_height; //is 0 at initialization int renderer_init(int width,int height) { <....> mat4f proj_mat; proj_mat = mat4f_projection(45.0f, (float)win_width / (float)win_height, 0.1f, 100.0f); ...

3

First of all, there's something fishy about your vertices. If we write them out properly: float[] vertices = { 0.0f, 0.0f, 0.5f, // (1) 0.5f, 0.5f, 0.5f, // (2) 0.0f, 0.5f, 0.5f, // (3) 0.0f, 0.0f, 0.5f, // (1) 0.0f, 0.5f, 0.5f, // (3) 0.0f, 0.5f, 0.5f // (3) }; You have only 3 distinct vertices, so if you are seeing a rectangle, ...

3

Generally speaking, using old OpenGL API is not efficient. In ye olden days of OpenGL 1.0 this pattern was necessary. The model view matrix contains the model and the view matrix. You have the view matrix on the camera and the model matrix on the model. You set the view matrix onto the model view matrix and then push/pop the model matrix on top of it for ...

2

When you divide by the post-projection-matrix .w, you don't get a linear depth from -1 to 1, it mostly gets bunched up near 1.0. To get linear depth you could do, instead, something akin to: float linearDepth = PSVSPosition.z / 2000.0; // if +2000 is your far plane PSColor = vec4(vec3(linearDepth), 1.0f);

2

Check out the Law of Cosines. It allows you to calculate any side or angle in a triangle if you have the opposite 2 angles or sides. Or alternately, use the law of sines (described at the bottom of the above link). In your case, you know that vertical field of view is 45 degrees and that the base side you want is the height of the screen. You can think of ...

2

This projection matrix should do the trick: .tg {border-collapse:collapse;border-spacing:0;} .tg td{font-family:Arial, sans-serif;font-size:14px;padding:10px 5px;border-style:solid;border-width:1px;overflow:hidden;word-break:normal;} .tg th{font-family:Arial, sans-serif;font-size:14px;font-weight:normal;padding:10px 5px;border-style:solid;border-...

2

How about doing it a bit differently? Start as usual and render all markers within viewport. If a marker is outside of the viewport - proceed: Get positions of the marker A Get position of the camera B (maybe slightly in front of it) Calculate 3D vector AB between these two Convert and normalize that vector into 2D screen bounds (A will be in view center ...

2

There isn't really a standard - it's going to be dictated by the needs of your gameplay. For Splinter Cell, determining whether the player (or suspicious evidence) is seen by an AI is the heart of the stealth gameplay. We needed a lot of nuance - consideration of peripheral vision versus direct line of sight, illumination, AI alertness level, movement state,...

2

setToOrtho does not set the projection type of the camera. An OrthographicCamera will always have an orthographic frustrum. Looking at the source, it seems to me that setToOrtho is a convenience method to set the viewport size to the screen size and center the camera on the viewport. setToOrtho(false) is showing your character is because your character is ...

2

A few ideas: Any point in front of the camera along its central axis should get mapped to (0, 0) after the perspective division. Any point selected on / outside the camera's view frustum/prism should map, after perspective division, to a value of 1 or greater, or -1 or less on at least one of the x, y, z axes. Beyond that, you could use some linear algebra ...

2

I would recommend re-reading the post as I don't think you completely understand why you're setting up an orthographic camera. The whole point of this is to move away from pixels and move towards your own unit that doesn't depend on screen size. This way, everything can be properly scaled no matter the device. it seems that the view height is now 20 pixels,...

2

The DirectXMath library is all inline so you can look directly at the source. For example, here is the C version of XMMatrixPerspectiveLH: float TwoNearZ = NearZ + NearZ; float fRange = FarZ / (FarZ - NearZ); XMMATRIX M; M.m = TwoNearZ / ViewWidth; M.m = 0.0f; M.m = 0.0f; M.m = 0.0f; M.m = 0.0f; M.m = TwoNearZ / ...

2

In openTK you need to transpose matrices to make it work. Green lines are frustum in world space. public void Update(Matrix4 cameraMatrixInverse, Matrix4 projectionMatrixInverse) { cameraMatrixInverse.Transpose(); projectionMatrixInverse.Transpose(); Matrix4 clipToWorld = cameraMatrixInverse * ...

2

Thanks @DMGregory for the pointer in the comments. I'm constructing a fullscreen quad at (x, y, -1, 1) in NDC. Notice z = -1 is the near plane in NDC (left-handed unlike normal coordinate systems in OpenGL-land). In the vertex shader: attribute vec2 aVertexPosition; uniform mat4 uInvViewMatrix; uniform mat4 uInvProjMatrix; varying vec3 vZNearPos; varying ...

1

You can't make OpenGL render to infinity. The depth buffer usually uses floats (it can use other types, but none of them can represent an infinite number of values), meaning it has a set precision. If you set the zFar value to 100,000 for instance (not even close to infinity), then you'll have problems with z fighting between objects, that are hundreds of ...

1

Matrix multiplication is non-cumulative, aka A * B doesn't always equal to B * A. The order of the multiplications should be projection * view * model * vector Your projection matrix is also messed up, the values should be this way: I would say where the values in your matrix are, but I have zero idea.

1

You could also try Dual Paraboloid mapping. The idea is that you render your scene two times with a paraboloid projection - once for front and once for the back paraboloid. If you render the front and back paraboloid to two separate textures, you can combine them later with the inverse of the paraboloid projection to a 360 degree(-ish) image. This technique ...

1

Ok mystery solved. The issue was because I wasn't normalizing the mouse x and y before applying the calculations. After doing the following everything started to work correctly: var x = (2.0 * mouseX) / screenWidth - 1.0; var y = 1.0 - (2.0 * mouseY) / screenHeight;

1

You can't. This is assuming that you have used the following function void gluLookAt(GLdouble eyeX, GLdouble eyeY, GLdouble eyeZ, GLdouble centerX, GLdouble centerY, GLdouble centerZ, GLdouble upX, GLdouble upY, GLdouble upZ); to get your transformation matrix, and that you want to find what values were used for center parameters. The ...

1

You can get the position of the eye and test dot(eye-v, n)>0. This checks that the angel between the viewing vector (from v to the eye) and the normal is less than 90°.

1

Setting the glClearColor, as Josh Petrie said, is only necessary to do every frame if you do not know if another part of your program is changing it. Although, usually you would be drawing your own background, which would cover the entire background, so the color should not matter. (Of course, you still need to clear the screen every frame to prevent ...

1

Maybe this is not the whole problem, but you need to divide by the w component after multiplying with a projection matrix. So after float4 lightingPosition = mul(input.WorldPos, LightViewProjClose); but before the if you should do this: lightingPosition.xyz /= lightingPosition.w

1

It's purpose is simple, the -1 gets multiplied with the z-value of any vertex you multiply the perspective matrix with. So w = -z Then during the perspective division each component (x, y, z, w) get's divided by w, (x/w, y/w, z/w, w/w). So the bigger the z-value is, the bigger the values x,y,z are divided by and the closer to zero the values become. Zero ...

Only top voted, non community-wiki answers of a minimum length are eligible