Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for vr rendering not taking render target size into account. fixes #2420 #2424

Merged
merged 1 commit into from
Apr 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions examples/core/core_vr_simulator.c
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,11 @@ int main(void)

// Initialize framebuffer for stereo rendering
// NOTE: Screen size should match HMD aspect ratio
RenderTexture2D target = LoadRenderTexture(GetScreenWidth(), GetScreenHeight());
RenderTexture2D target = LoadRenderTexture(device.hResolution, device.vResolution);

// The target's height is flipped (in the source Rectangle), due to OpenGL reasons
Rectangle sourceRec = { 0.0f, 0.0f, (float)target.texture.width, -(float)target.texture.height };
Rectangle destRec = { 0.0f, 0.0f, (float)GetScreenWidth(), (float)GetScreenHeight() };

// Define the camera to look into our 3d world
Camera camera = { 0 };
Expand Down Expand Up @@ -121,8 +125,7 @@ int main(void)
BeginDrawing();
ClearBackground(RAYWHITE);
BeginShaderMode(distortion);
DrawTextureRec(target.texture, (Rectangle){ 0, 0, (float)target.texture.width,
(float)-target.texture.height }, (Vector2){ 0.0f, 0.0f }, WHITE);
DrawTexturePro(target.texture, sourceRec, destRec, (Vector2){ 0.0f, 0.0f }, 0.0f, WHITE);
EndShaderMode();
DrawFPS(10, 10);
EndDrawing();
Expand Down
4 changes: 2 additions & 2 deletions src/rcore.c
Original file line number Diff line number Diff line change
Expand Up @@ -2347,8 +2347,8 @@ VrStereoConfig LoadVrStereoConfig(VrDeviceInfo device)

// Fovy is normally computed with: 2*atan2f(device.vScreenSize, 2*device.eyeToScreenDistance)
// ...but with lens distortion it is increased (see Oculus SDK Documentation)
//float fovy = 2.0f*atan2f(device.vScreenSize*0.5f*distortionScale, device.eyeToScreenDistance); // Really need distortionScale?
float fovy = 2.0f*(float)atan2f(device.vScreenSize*0.5f, device.eyeToScreenDistance);
float fovy = 2.0f*atan2f(device.vScreenSize*0.5f*distortionScale, device.eyeToScreenDistance); // Really need distortionScale?
// float fovy = 2.0f*(float)atan2f(device.vScreenSize*0.5f, device.eyeToScreenDistance);

// Compute camera projection matrices
float projOffset = 4.0f*lensShift; // Scaled to projection space coordinates [-1..1]
Expand Down
19 changes: 18 additions & 1 deletion src/rlgl.h
Original file line number Diff line number Diff line change
Expand Up @@ -929,6 +929,11 @@ typedef struct rlglData {

int framebufferWidth; // Default framebuffer width
int framebufferHeight; // Default framebuffer height

int viewportX; // Current opengl viewport offset x
raysan5 marked this conversation as resolved.
Show resolved Hide resolved
int viewportY; // Current opengl viewport offset y
int viewportWidth; // Current opengl viewport width
int viewportHeight; // Current opengl viewport height

} State; // Renderer state
struct {
Expand Down Expand Up @@ -1232,6 +1237,11 @@ void rlOrtho(double left, double right, double bottom, double top, double znear,
// Set the viewport area (transformation from normalized device coordinates to window coordinates)
void rlViewport(int x, int y, int width, int height)
{
RLGL.State.viewportX = x;
RLGL.State.viewportY = y;
RLGL.State.viewportWidth = width;
RLGL.State.viewportHeight = height;

glViewport(x, y, width, height);
}

Expand Down Expand Up @@ -2494,6 +2504,11 @@ void rlDrawRenderBatch(rlRenderBatch *batch)
Matrix matProjection = RLGL.State.projection;
Matrix matModelView = RLGL.State.modelview;

int originalViewportX = RLGL.State.viewportX;
int originalViewportY = RLGL.State.viewportY;
int originalViewportWidth = RLGL.State.viewportWidth;
int originalViewportHeight = RLGL.State.viewportHeight;

int eyeCount = 1;
if (RLGL.State.stereoRender) eyeCount = 2;

Expand All @@ -2502,7 +2517,7 @@ void rlDrawRenderBatch(rlRenderBatch *batch)
if (eyeCount == 2)
{
// Setup current eye viewport (half screen width)
rlViewport(eye*RLGL.State.framebufferWidth/2, 0, RLGL.State.framebufferWidth/2, RLGL.State.framebufferHeight);
rlViewport(originalViewportX + eye * originalViewportWidth / 2, originalViewportY, originalViewportWidth / 2, originalViewportHeight);

// Set current eye view offset to modelview matrix
rlSetMatrixModelview(rlMatrixMultiply(matModelView, RLGL.State.viewOffsetStereo[eye]));
Expand Down Expand Up @@ -2601,6 +2616,8 @@ void rlDrawRenderBatch(rlRenderBatch *batch)

glUseProgram(0); // Unbind shader program
}

if (eyeCount == 2) rlViewport(originalViewportX, originalViewportY, originalViewportWidth, originalViewportHeight);
//------------------------------------------------------------------------------------------------------------

// Reset batch buffers
Expand Down