Merge pull request #1 from SubstituteR/opengldpi

Add basic OpenGL DPI Scaling
This commit is contained in:
Riley S 2021-02-23 20:57:27 -05:00 committed by GitHub
commit a8e005e080
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 29 additions and 5 deletions

View File

@ -216,8 +216,10 @@ cameraSync(ObjectWithFrame *obj)
inv.at.x = -inv.at.x; inv.at.x = -inv.at.x;
inv.pos.x = -inv.pos.x; inv.pos.x = -inv.pos.x;
float32 xscl = 1.0f/(2.0f*cam->viewWindow.x); V2d dpiScale = engine->device.dpiScale(cam->frameBuffer->width, cam->frameBuffer->height);
float32 yscl = 1.0f/(2.0f*cam->viewWindow.y);
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.flags = 0;
proj.right.x = xscl; proj.right.x = xscl;

View File

@ -559,6 +559,7 @@ Device renderdevice = {
null::im3DRenderPrimitive, null::im3DRenderPrimitive,
null::im3DRenderIndexedPrimitive, null::im3DRenderIndexedPrimitive,
null::im3DEnd, null::im3DEnd,
null::dpiScale,
null::deviceSystem null::deviceSystem
}; };

View File

@ -1253,8 +1253,9 @@ beginUpdate(Camera *cam)
setViewMatrix(view); setViewMatrix(view);
// Projection Matrix // Projection Matrix
float32 invwx = 1.0f/cam->viewWindow.x; V2d dpiScale = engine->device.dpiScale(cam->frameBuffer->width, cam->frameBuffer->height);
float32 invwy = 1.0f/cam->viewWindow.y; 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); float32 invz = 1.0f/(cam->farPlane-cam->nearPlane);
proj[0] = invwx; proj[0] = invwx;
@ -1381,6 +1382,16 @@ showRaster(Raster *raster, uint32 flags)
#endif #endif
} }
static V2d dpiScale(float x, float y)
{
V2d v;
int w = 0;
int h = 0;
glfwGetFramebufferSize(glGlobals.window, &w, &h);
v.set(w / x, h / y);
return v;
}
static bool32 static bool32
rasterRenderFast(Raster *raster, int32 x, int32 y) rasterRenderFast(Raster *raster, int32 x, int32 y)
{ {
@ -1388,6 +1399,7 @@ rasterRenderFast(Raster *raster, int32 x, int32 y)
Raster *dst = Raster::getCurrentContext(); Raster *dst = Raster::getCurrentContext();
Gl3Raster *natdst = PLUGINOFFSET(Gl3Raster, dst, nativeRasterOffset); Gl3Raster *natdst = PLUGINOFFSET(Gl3Raster, dst, nativeRasterOffset);
Gl3Raster *natsrc = PLUGINOFFSET(Gl3Raster, src, nativeRasterOffset); Gl3Raster *natsrc = PLUGINOFFSET(Gl3Raster, src, nativeRasterOffset);
V2d dpiScale = engine->device.dpiScale(src->width, src->height);
switch(dst->type){ switch(dst->type){
case Raster::NORMAL: case Raster::NORMAL:
@ -1398,7 +1410,7 @@ rasterRenderFast(Raster *raster, int32 x, int32 y)
setActiveTexture(0); setActiveTexture(0);
glBindTexture(GL_TEXTURE_2D, natdst->texid); glBindTexture(GL_TEXTURE_2D, natdst->texid);
glCopyTexSubImage2D(GL_TEXTURE_2D, 0, x, (dst->height-src->height)-y, glCopyTexSubImage2D(GL_TEXTURE_2D, 0, x, (dst->height-src->height)-y,
0, 0, src->width, src->height); src->width/dpiScale.x, src->height/dpiScale.y, src->width, src->height);
glBindTexture(GL_TEXTURE_2D, boundTexture[0]); glBindTexture(GL_TEXTURE_2D, boundTexture[0]);
return 1; return 1;
} }
@ -2032,6 +2044,7 @@ Device renderdevice = {
gl3::im3DRenderPrimitive, gl3::im3DRenderPrimitive,
gl3::im3DRenderIndexedPrimitive, gl3::im3DRenderIndexedPrimitive,
gl3::im3DEnd, gl3::im3DEnd,
gl3::dpiScale,
#ifdef LIBRW_SDL2 #ifdef LIBRW_SDL2
gl3::deviceSystemSDL2 gl3::deviceSystemSDL2
#else #else

View File

@ -66,6 +66,7 @@ struct Device
void (*im3DRenderIndexedPrimitive)(PrimitiveType primType, void *indices, int32 numIndices); void (*im3DRenderIndexedPrimitive)(PrimitiveType primType, void *indices, int32 numIndices);
void (*im3DEnd)(void); void (*im3DEnd)(void);
V2d (*dpiScale)(float x, float y);
DeviceSystem *system; DeviceSystem *system;
}; };
@ -255,6 +256,13 @@ namespace null {
void im3DRenderIndexedPrimitive(PrimitiveType primType, void *indices, int32 numIndices); void im3DRenderIndexedPrimitive(PrimitiveType primType, void *indices, int32 numIndices);
void im3DEnd(void); 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); int deviceSystem(DeviceReq req, void *arg0, int32 n);
extern Device renderdevice; extern Device renderdevice;