# How to use maximum resolution (pixel density) with OpenGL in MS Windows

CodeBlocks offers an example program in C, that uses OpenGL. It draws a single multicoloured rotating triangle. I have been building on this example. I am a novice in C and in OpenGL. I believe that I am using an old style of OpenGL coding (possibly called "Immediate Mode ?") and I am trying to keep things as simple as possible. For example, I am not, to my knowledge, using VBOs, nor VAOs, nor GLSL. I am also not, as far as I know, using any helper libraries (if that's a right way to say it), such as GLEW, GLFW, GLut, etc.

I notice that the sides of the triangles are more jagged than they need to be, given the resolution of my laptop screen. Even if no multisampling were used (and I am not yet trying to use it), much smaller "jaggies" could be achieved, if OpenGL's raster array really corresponded to the physical pixel array. I took a "print screen" and opened it in MS Paint, saving it as a .bmp. When I zoomed in, it seemed that multisampling had been used, but to smooth the edges of the large jaggies ! If you see what I mean.

EDIT: Here is a picture:

This shows that the text on the left (which is not from my OpenGL window) is rendered at a higher resolution than my triangley object on the right (which is in my OpenGL window).

My question is, how can I use the maximum resolution of the screen ?

Here are some excerpts from the code:

#include <windows.h>
#include <gl/gl.h>
#include <stdio.h>

int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
WNDCLASSEX wcex;
HWND hwnd;
HDC hDC;
HGLRC hRC;
MSG msg;
BOOL bQuit = FALSE;

const int width = 768;
const int height = 768;

/* register window class */
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_OWNDC;
wcex.lpfnWndProc = WindowProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
wcex.lpszClassName = "GLSample";

if (!RegisterClassEx(&wcex))
return 0;

/* create main window */
hwnd = CreateWindowEx(0,
"GLSample",
"OpenGL Sample",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
width,
height,
NULL,
NULL,
hInstance,
NULL);

ShowWindow(hwnd, nCmdShow);

/* enable OpenGL for the window */
EnableOpenGL(hwnd, &hDC, &hRC);

etc..
}


and the EnableOpenGL function:

void EnableOpenGL(HWND hwnd, HDC* hDC, HGLRC* hRC)
{
PIXELFORMATDESCRIPTOR pfd;

int iFormat;

/* get the device context (DC) */
*hDC = GetDC(hwnd);

/* set the pixel format for the DC */
ZeroMemory(&pfd, sizeof(pfd));

pfd.nSize = sizeof(pfd);
pfd.nVersion = 1;
pfd.dwFlags = PFD_DRAW_TO_WINDOW |
PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
pfd.iPixelType = PFD_TYPE_RGBA;
pfd.cColorBits = 24;
pfd.cDepthBits = 16;
pfd.iLayerType = PFD_MAIN_PLANE;

iFormat = ChoosePixelFormat(*hDC, &pfd);

SetPixelFormat(*hDC, iFormat, &pfd);

/* create and enable the render context (RC) */
*hRC = wglCreateContext(*hDC);

wglMakeCurrent(*hDC, *hRC);
}


You might need to use a setting to tell Windows that your program is DPI-aware, meaning that it can adjust to very high-resolution screens like many current-gen laptops have.

Lots of older programs have trouble with such high resolutions, so without the DPI awareness flag, Windows will tell the program the resolution is lower, and then upscale the whole window. That looks like it might be what's happening to your OpenGL app.

The simplest way to do this is to add a call to SetProcessDPIAware() during your program's startup, before creating the window. You might also want to increase the hard-coded width and height values.

The other way to do it is by adding an XML "manifest" file that gets linked into your app's .exe file, but it might be hard to get that working in Code::Blocks if it doesn't have it built in.