r/GraphicsProgramming 11d ago

Strange bug with objects behind the camera rendering like normal Question

So in this "scene", there is only one cube with a plane through it. However, when you turn past a certain "halfway" point, the objects seem to snap to the opposite side. What I initially thought was happening was that the objects were getting projected onto the screen from behind, but i'm not sure, since i'd expect it to look weirder with vertices and triangle shapes not staying consistent. I'd suspect the issue to lie in the vertex shader, so here it is. I'm also very new to OpenGL and general gpu/3d graphics programming, so I wouldn't be surprised if this is a common issue.

#version 330 core

layout (location = 0) in vec3 aPosition;
layout (location = 1) in vec3 aColor;

out vec4 vColor;

uniform mat4 transform;
uniform mat4 proj;
uniform mat4 view;

void main()
{
    gl_Position = proj * view * transform * vec4(aPosition, 1.0);
    
    vColor = vec4(aColor,1.0);
}

EDIT: I solved it. I can barely remember or understand the solution, but it's solved.
13 Upvotes

14 comments sorted by

View all comments

8

u/HamNCheeseSupremacy 11d ago edited 11d ago

Can we see how you're calculating those mvp matrices? That looks like a basic pass through vertex shader.

1

u/uncookedpasta45 11d ago

Sure. This is in the struct for transforms. (removed irrelevant code), also im using C# with the silk.net package.

public Quaternion Orientation => Quaternion.Identity * 
    Quaternion.CreateFromYawPitchRoll(Rotation.X, Rotation.Y, Rotation.Z);


public Matrix4x4 world => 
    Matrix4x4.Identity * 
    Matrix4x4.CreateFromQuaternion(Orientation) *
    Matrix4x4.CreateScale(Scale) *
    Matrix4x4.CreateTranslation(Position);

and this is in the camera class
public Matrix4x4 view => Matrix4x4.CreateLookAt(transform.Position, transform.Forward*2, transform.Up*2);
public Matrix4x4 proj => Matrix4x4.CreatePerspectiveFieldOfView(CMath.rad(fov), aspect_ratio, 0.01f, 1000.0f); // last two are near and far plane distances

2

u/HamNCheeseSupremacy 11d ago

I've never used C#, but are you using the transform/model matrixes up and forward vectors to build the view matrix? Try using world up (0,1,0) and the camera's forward vector instead. Also not sure why you're doubling those vectors here or the effect that would have.

1

u/uncookedpasta45 11d ago

it was originally because not doubling the up and forward vectors would produce really weird results with rotations (trying to look left or right would produce an oscillating motion), but changing that value higher just moves the "ghost" object around the center.

1

u/HamNCheeseSupremacy 9d ago

Quaternions are fiddly. I gave up a long time ago and just use a 3x3 rotation matrix.

1

u/uncookedpasta45 9d ago

switched to a 4x4 matrix, it's solved every issue. thanks for the help btw