<p>I can send WM_PAINT messages with SendMessage or PostMessage, but the window wont be redrawn. To redraw the window I have to call InvalidateRect or InvalidateRgn. You can also use RedrawWindow.</p>
<p>In the end that wasn't the problem though. I was calling glViewport on WM_SIZE, which for some season unknown to me means that WM_PAINT wasn't being processed properly, but the window was drawing correctly during resizing.</p>
<p>I also changed my message loop, to clear the message queue before redrawing the window, which also seems to speed things up a lot.</p>
<p>Edit: Ok, I know why glViewport in WM_SIZE was causing issues now. I wasn't returning 0 after processing that message, and MSDN docs mentions this. After making this change, it all works as expected.</p>

CC BY-SA 4.0
2023-01-17T03:05:52.177Z
2023-01-18T03:27:06.873Z
2023-01-18T03:27:06.873Z
lewis
<p>I have a work thread function which updates the value of a float called scale:</p>
<pre><code>    DWORD WINAPI WorkThreadFn(LPVOID lpParam)
{
    MyWin* pWin = (MyWin*)lpParam;
    while (pWin-&gt;running)
    {
        //pWin-&gt;scale = pWin-&gt;scale - 0.001;
        pWin-&gt;scale = 0.5;
        SendMessage(pWin-&gt;hwnd, WM_PAINT, NULL, NULL);
        Sleep(1);
    }
    return 0;
}
</code></pre>
<p>If I change the value of scale in this function, the WM_PAINT draws my triangle to the correct scale. But if I uncomment the section which increments the scale, then remove the line setting the scale, the scale of the triangle remains the same, despite the fact I can see from the window procedure that the value of scale is decreasing.</p>
<p>Code for Window procedure:</p>
<pre><code>    LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
    PAINTSTRUCT ps;
    HDC hdc;
    MyWin* pThis = NULL;
    if (uMsg == WM_CREATE)
    {
        CREATESTRUCT* pCreate = (CREATESTRUCT*)lParam;
        pThis = (MyWin*)pCreate-&gt;lpCreateParams;
        SetWindowLongPtr(hwnd, GWLP_USERDATA, (LONG_PTR)pThis);
    }
    else
    {
        pThis = (MyWin*)GetWindowLongPtr(hwnd, GWLP_USERDATA);
    }
..
switch(uMsg) {
case WM_PAINT:
        hdc = BeginPaint(hwnd, &amp;ps);
        RECT region = ps.rcPaint;
        glClearColor(0.62, 0.3, 0.5, 1.0);
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

        pThis-&gt;glUniform1f(pThis-&gt;scaleLoc, pThis-&gt;scale);
        
        glBindVertexArray(vaNames[0]);
        glDrawArrays(GL_TRIANGLES, 0, 3);

        glBindVertexArray(0);
        SwapBuffers(hdc);
</code></pre>
<p>And also here is the vertex shader code</p>
<pre><code>in vec3 in_Position;
in vec3 in_Color;
out vec3 ex_Color;

uniform float scale;

void main(void)
{
    gl_Position = vec4(in_Position * scale, 1.0);
    ex_Color = in_Color;
}
</code></pre>
<p>I'm sure I'm doing something stupid here, can anybody help?</p>
<p>Edit (forgot to add the main function):</p>
<pre><code>int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PWSTR pCmdLine, int nCmdShow)
{
    MyWin win(hInstance, nCmdShow);

    
    DWORD ThreadID;
    HANDLE hThread;
    hThread = CreateThread(NULL, 0, WorkThreadFn, &amp;win, 0, &amp;ThreadID);

    win.MsgLoop();

    if (hThread != NULL)
        CloseHandle(hThread);

    return 0;
}
</code></pre>

CC BY-SA 4.0
2023-01-17T02:43:48.537Z
Ok so, I discovered that if I resize the window the expected change in the triangle size takes place. Which I guess, means that windows does not like the way I am sending WM_PAINT messages to the window procedure. Still working on it.
lewis
<opengl><c++>
Why does my uniform only update once?
lewis
# Why does my uniform only update once?

(Asked by: username_0 on 2023-01-17)

I have a work thread function which updates the value of a float called scale:

```
    DWORD WINAPI WorkThreadFn(LPVOID lpParam)
{
    MyWin* pWin = (MyWin*)lpParam;
    while (pWin->running)
    {
        //pWin->scale = pWin->scale - 0.001;
        pWin->scale = 0.5;
        SendMessage(pWin->hwnd, WM_PAINT, NULL, NULL);
        Sleep(1);
    }
    return 0;
}

```

If I change the value of scale in this function, the WM\_PAINT draws my triangle to the correct scale. But if I uncomment the section which increments the scale, then remove the line setting the scale, the scale of the triangle remains the same, despite the fact I can see from the window procedure that the value of scale is decreasing.

Code for Window procedure:

```
    LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
    PAINTSTRUCT ps;
    HDC hdc;
    MyWin* pThis = NULL;
    if (uMsg == WM_CREATE)
    {
        CREATESTRUCT* pCreate = (CREATESTRUCT*)lParam;
        pThis = (MyWin*)pCreate->lpCreateParams;
        SetWindowLongPtr(hwnd, GWLP_USERDATA, (LONG_PTR)pThis);
    }
    else
    {
        pThis = (MyWin*)GetWindowLongPtr(hwnd, GWLP_USERDATA);
    }
..
switch(uMsg) {
case WM_PAINT:
        hdc = BeginPaint(hwnd, &ps);
        RECT region = ps.rcPaint;
        glClearColor(0.62, 0.3, 0.5, 1.0);
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

        pThis->glUniform1f(pThis->scaleLoc, pThis->scale);
        
        glBindVertexArray(vaNames[0]);
        glDrawArrays(GL_TRIANGLES, 0, 3);

        glBindVertexArray(0);
        SwapBuffers(hdc);

```

And also here is the vertex shader code

```
in vec3 in_Position;
in vec3 in_Color;
out vec3 ex_Color;

uniform float scale;

void main(void)
{
    gl_Position = vec4(in_Position * scale, 1.0);
    ex_Color = in_Color;
}

```

I'm sure I'm doing something stupid here, can anybody help?

Edit (forgot to add the main function):

```
int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PWSTR pCmdLine, int nCmdShow)
{
    MyWin win(hInstance, nCmdShow);

    
    DWORD ThreadID;
    HANDLE hThread;
    hThread = CreateThread(NULL, 0, WorkThreadFn, &win, 0, &ThreadID);

    win.MsgLoop();

    if (hThread != NULL)
        CloseHandle(hThread);

    return 0;
}

```

**Tags:** opengl, c++

> **Comments:**
> **username_0**: Ok so, I discovered that if I resize the window the expected change in the triangle size takes place. Which I guess, means that windows does not like the way I am sending WM_PAINT messages to the window procedure. Still working on it.  

---

## 1 answers

### Answer by username_0 on 2023-01-17. Score: 1

I can send WM\_PAINT messages with SendMessage or PostMessage, but the window wont be redrawn. To redraw the window I have to call InvalidateRect or InvalidateRgn. You can also use RedrawWindow.

In the end that wasn't the problem though. I was calling glViewport on WM\_SIZE, which for some season unknown to me means that WM\_PAINT wasn't being processed properly, but the window was drawing correctly during resizing.

I also changed my message loop, to clear the message queue before redrawing the window, which also seems to speed things up a lot.

Edit: Ok, I know why glViewport in WM\_SIZE was causing issues now. I wasn't returning 0 after processing that message, and MSDN docs mentions this. After making this change, it all works as expected.


---

