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 high-dpi resolution when using OpenGL #64

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
6 changes: 4 additions & 2 deletions src/camera.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -216,8 +216,10 @@ cameraSync(ObjectWithFrame *obj)
inv.at.x = -inv.at.x;
inv.pos.x = -inv.pos.x;

float32 xscl = 1.0f/(2.0f*cam->viewWindow.x);
float32 yscl = 1.0f/(2.0f*cam->viewWindow.y);
V2d dpiScale = engine->device.dpiScale(cam->frameBuffer->width, cam->frameBuffer->height);

float32 xscl = 1.0f/(2.0f*cam->viewWindow.x*dpiScale.x);
float32 yscl = 1.0f/(2.0f*cam->viewWindow.y*dpiScale.y);

proj.flags = 0;
proj.right.x = xscl;
Expand Down
1 change: 1 addition & 0 deletions src/d3d/d3ddevice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2003,6 +2003,7 @@ Device renderdevice = {
d3d::im3DRenderPrimitive,
d3d::im3DRenderIndexedPrimitive,
d3d::im3DEnd,
null::dpiScale,
d3d::deviceSystem,
};

Expand Down
1 change: 1 addition & 0 deletions src/engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -559,6 +559,7 @@ Device renderdevice = {
null::im3DRenderPrimitive,
null::im3DRenderIndexedPrimitive,
null::im3DEnd,
null::dpiScale,
null::deviceSystem
};

Expand Down
32 changes: 29 additions & 3 deletions src/gl/gl3device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1253,8 +1253,9 @@ beginUpdate(Camera *cam)
setViewMatrix(view);

// Projection Matrix
float32 invwx = 1.0f/cam->viewWindow.x;
float32 invwy = 1.0f/cam->viewWindow.y;
V2d dpiScale = engine->device.dpiScale(cam->frameBuffer->width, cam->frameBuffer->height);
float32 invwx = 1.0f/cam->viewWindow.x/dpiScale.x;
float32 invwy = 1.0f/cam->viewWindow.y/dpiScale.y;
float32 invz = 1.0f/(cam->farPlane-cam->nearPlane);

proj[0] = invwx;
Expand Down Expand Up @@ -1381,13 +1382,34 @@ showRaster(Raster *raster, uint32 flags)
#endif
}

static V2d dpiScale(float x, float y)
{
//TODO SDL2 version of this
#ifdef LIBRW_SDL2
V2d v;
v.set(1.f, 1.f);
return v;
#else
V2d v;
int w = 0;
int h = 0;
glfwGetFramebufferSize(glGlobals.window, &w, &h);
if (w && h)
v.set(w / x, h / y);
else
v.set(1,1);
return v;
#endif
}

static bool32
rasterRenderFast(Raster *raster, int32 x, int32 y)
{
Raster *src = raster;
Raster *dst = Raster::getCurrentContext();
Gl3Raster *natdst = PLUGINOFFSET(Gl3Raster, dst, nativeRasterOffset);
Gl3Raster *natsrc = PLUGINOFFSET(Gl3Raster, src, nativeRasterOffset);
V2d dpiScale = engine->device.dpiScale(src->width, src->height);

switch(dst->type){
case Raster::NORMAL:
Expand All @@ -1397,8 +1419,11 @@ rasterRenderFast(Raster *raster, int32 x, int32 y)
case Raster::CAMERA:
setActiveTexture(0);
glBindTexture(GL_TEXTURE_2D, natdst->texid);

glCopyTexSubImage2D(GL_TEXTURE_2D, 0, x, (dst->height-src->height)-y,
0, 0, src->width, src->height);
src->width - src->width/dpiScale.x, src->height - src->height/dpiScale.y,
src->width, src->height);

glBindTexture(GL_TEXTURE_2D, boundTexture[0]);
return 1;
}
Expand Down Expand Up @@ -2032,6 +2057,7 @@ Device renderdevice = {
gl3::im3DRenderPrimitive,
gl3::im3DRenderIndexedPrimitive,
gl3::im3DEnd,
gl3::dpiScale,
#ifdef LIBRW_SDL2
gl3::deviceSystemSDL2
#else
Expand Down
1 change: 1 addition & 0 deletions src/ps2/ps2device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ Device renderdevice = {
null::im3DRenderPrimitive,
null::im3DRenderIndexedPrimitive,
null::im3DEnd,
null::dpiScale,
null::deviceSystem
};

Expand Down
8 changes: 8 additions & 0 deletions src/rwengine.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ struct Device
void (*im3DRenderIndexedPrimitive)(PrimitiveType primType, void *indices, int32 numIndices);
void (*im3DEnd)(void);

V2d (*dpiScale)(float x, float y);
DeviceSystem *system;
};

Expand Down Expand Up @@ -255,6 +256,13 @@ namespace null {
void im3DRenderIndexedPrimitive(PrimitiveType primType, void *indices, int32 numIndices);
void im3DEnd(void);

inline V2d dpiScale(float,float)
{
V2d s;
s.set(1.f, 1.f);
return s;
}

int deviceSystem(DeviceReq req, void *arg0, int32 n);

extern Device renderdevice;
Expand Down