单个 MFC 对话框上的 OpenGL 两种不同的 3d 渲染图片控件不起作用

本文介绍了单个 MFC 对话框上的 OpenGL 两种不同的 3d 渲染图片控件不起作用的处理方法,对大家解决问题具有一定的参考价值

问题描述

我正在使用 VC++ MFC 制作一个应用程序,其中我有两个不同的 opengl 窗口.我遵循了 在此处输入链接描述

I am making an application using VC++ MFC where i have two different opengl windows. I have followed the tutorial of enter link description here

我能够成功使用一个图片控件,其中 opengl 窗口显示正确的渲染,但如果我创建该控件的两个不同对象,则一个窗口不显示任何内容.

I am able to success using one picture control where opengl window shows correct rendering but if i create two different object of that control then one window does not show anything.

在这里解释更多是代码

m_WinGL 和 m_WinGL2 是 initDialog 内部自定义类的两个不同对象.

m_WinGL and m_WinGL2 are two different object of the customized class inside the initDialog.

// Get size and position of the template textfield we created before in the dialog editor
CRect rect;
GetDlgItem(IDC_STATIC_GL)->GetWindowRect(rect);
ScreenToClient(rect);// Convert screen coordinates to client coordinates

m_WinGL.oglCreate(rect, this);// Create OpenGL Control window
m_WinGL.m_unpTimer = m_WinGL.SetTimer(1, 1, 0);// Setup the OpenGL Window's timer to render


GetDlgItem(IDC_STATIC_GL2)->GetWindowRect(rect);
ScreenToClient(rect);// Convert screen coordinates to client coordinates

m_WinGL2.oglCreate(rect, this,L"X");// Create OpenGL Control window
m_WinGL2.m_unpTimer = m_WinGL2.SetTimer(1, 1, 0);// Setup the OpenGL Window's timer to render

这里是自定义控件的类

头文件

#pragma once
#include "afxwin.h"

#include <gl/gl.h>
#include <gl/glu.h>

class COpenGLControl : public CWnd
{
    public:
        /******************/
        /* Public Members */
        /******************/
        UINT_PTR m_unpTimer;
        // View information variables
        float    m_fLastX;
        float    m_fLastY;
        float    m_fPosX;
        float    m_fPosY;
        float    m_fZoom;
        float    m_fRotX;
        float    m_fRotY;
        bool     m_bIsMaximized;

    private:
        /*******************/
        /* Private Members */
        /*******************/
        // Window information
        CWnd  *hWnd;
        HDC   hdc;          
        HGLRC hrc;          
        int   m_nPixelFormat;
        CRect m_rect;
        CRect m_oldWindow;
        CRect m_originalRect;

    public:
        COpenGLControl(void);
        virtual ~COpenGLControl(void);

        void oglCreate(CRect rect, CWnd *parent,CString strWindowName=L"OpenGl");
        void oglInitialize(void);
        void oglDrawScene(void);

        // Added message classes:
        afx_msg void OnPaint();
        afx_msg void OnSize(UINT nType, int cx, int cy);
        afx_msg void OnDraw(CDC *pDC);
        afx_msg int  OnCreate(LPCREATESTRUCT lpCreateStruct);
        afx_msg void OnTimer(UINT nIDEvent);
        afx_msg void OnMouseMove(UINT nFlags, CPoint point);

        DECLARE_MESSAGE_MAP()
};

cpp 文件

    #include "stdafx.h"
    #include "OpenGLControl.h"

    COpenGLControl::COpenGLControl(void)
    {
        m_fPosX = 0.0f;     // X position of model in camera view
        m_fPosY = 0.0f;     // Y position of model in camera view
        m_fZoom = 10.0f;    // Zoom on model in camera view
        m_fRotX = 0.0f;     // Rotation on model in camera view
        m_fRotY = 0.0f;     // Rotation on model in camera view
        m_bIsMaximized = false;
    }

    COpenGLControl::~COpenGLControl(void)
    {
    }

    BEGIN_MESSAGE_MAP(COpenGLControl, CWnd)
        ON_WM_PAINT()
        ON_WM_SIZE()
        ON_WM_CREATE()
        ON_WM_TIMER()
        ON_WM_MOUSEMOVE()
    END_MESSAGE_MAP()

    void COpenGLControl::OnPaint()
    {
        //CPaintDC dc(this); // device context for painting
        ValidateRect(NULL);
    }

    void COpenGLControl::OnSize(UINT nType, int cx, int cy)
    {
        CWnd::OnSize(nType, cx, cy);

        if (0 >= cx || 0 >= cy || nType == SIZE_MINIMIZED) return;

        // Map the OpenGL coordinates.
        glViewport(0, 0, cx, cy);

        // Projection view
        glMatrixMode(GL_PROJECTION);

        glLoadIdentity();

        // Set our current view perspective
        gluPerspective(35.0f, (float)cx / (float)cy, 0.01f, 2000.0f);

        // Model view
        glMatrixMode(GL_MODELVIEW);

        switch (nType)
        {
            // If window resize token is "maximize"
            case SIZE_MAXIMIZED:
            {
                // Get the current window rect
                GetWindowRect(m_rect);

                // Move the window accordingly
                MoveWindow(6, 6, cx - 14, cy - 14);

                // Get the new window rect
                GetWindowRect(m_rect);

                // Store our old window as the new rect
                m_oldWindow = m_rect;

                break;
            }

            // If window resize token is "restore"
            case SIZE_RESTORED:
            {
                // If the window is currently maximized
                if (m_bIsMaximized)
                {
                    // Get the current window rect
                    GetWindowRect(m_rect);

                    // Move the window accordingly (to our stored old window)
                    MoveWindow(m_oldWindow.left, m_oldWindow.top - 18, m_originalRect.Width() - 4, m_originalRect.Height() - 4);

                    // Get the new window rect
                    GetWindowRect(m_rect);

                    // Store our old window as the new rect
                    m_oldWindow = m_rect;
                }

                break;
            }
        }
    }

    int COpenGLControl::OnCreate(LPCREATESTRUCT lpCreateStruct)
    {
        if (CWnd::OnCreate(lpCreateStruct) == -1) return -1;

        oglInitialize();

        return 0;
    }

    void COpenGLControl::OnDraw(CDC *pDC)
    {
        // If the current view is perspective...
        glLoadIdentity();

        glTranslatef(0.0f, 0.0f, -m_fZoom);
        glTranslatef(m_fPosX, m_fPosY, 0.0f);
        glRotatef(m_fRotX, 1.0f, 0.0f, 0.0f);
        glRotatef(m_fRotY, 0.0f, 1.0f, 0.0f);
    }

    void COpenGLControl::OnTimer(UINT nIDEvent)
    {
        switch (nIDEvent)
        {
            case 1:
            {
                // Clear color and depth buffer bits
                glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

                // Draw OpenGL scene
                oglDrawScene();

                // Swap buffers
                SwapBuffers(hdc);

                break;
            }

            default:
                break;
        }

        CWnd::OnTimer(nIDEvent);
    }

    void COpenGLControl::OnMouseMove(UINT nFlags, CPoint point)
    {
        int diffX = (int)(point.x - m_fLastX);
        int diffY = (int)(point.y - m_fLastY);
        m_fLastX  = (float)point.x;
        m_fLastY  = (float)point.y;

        // Left mouse button
        if (nFlags & MK_LBUTTON)
        {
            m_fRotX += (float)0.5f * diffY;

            if ((m_fRotX > 360.0f) || (m_fRotX < -360.0f))
            {
                m_fRotX = 0.0f;
            }

            m_fRotY += (float)0.5f * diffX;

            if ((m_fRotY > 360.0f) || (m_fRotY < -360.0f))
            {
                m_fRotY = 0.0f;
            }
        }

        // Right mouse button
        else if (nFlags & MK_RBUTTON)
        {
            m_fZoom -= (float)0.1f * diffY;
        }

        // Middle mouse button
        else if (nFlags & MK_MBUTTON)
        {
            m_fPosX += (float)0.05f * diffX;
            m_fPosY -= (float)0.05f * diffY;
        }

        OnDraw(NULL);

        CWnd::OnMouseMove(nFlags, point);
    }

    void COpenGLControl::oglCreate(CRect rect, CWnd *parent,CString strWindowName)
    {
        CString className = AfxRegisterWndClass(CS_HREDRAW | CS_VREDRAW | CS_OWNDC, NULL, (HBRUSH)GetStockObject(BLACK_BRUSH), NULL);

        CreateEx(0, className,strWindowName, WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS | WS_CLIPCHILDREN, rect, parent, 0);

        // Set initial variables' values
        m_oldWindow    = rect;
        m_originalRect = rect;

        hWnd = parent;
    }

    void COpenGLControl::oglInitialize(void)
    {
        // Initial Setup:
        //
        static PIXELFORMATDESCRIPTOR pfd =
        {
            sizeof(PIXELFORMATDESCRIPTOR),
            1,
            PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER,
            PFD_TYPE_RGBA,
            32, // bit depth
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
            16, // z-buffer depth
            0, 0, 0, 0, 0, 0, 0,
        };

        // Get device context only once.
        hdc = GetDC()->m_hDC;

        // Pixel format.
        m_nPixelFormat = ChoosePixelFormat(hdc, &pfd);
        SetPixelFormat(hdc, m_nPixelFormat, &pfd);

        // Create the OpenGL Rendering Context.
        hrc = wglCreateContext(hdc);
        wglMakeCurrent(hdc, hrc);

        // Basic Setup:
        //
        // Set color to use when clearing the background.
        glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
        glClearDepth(1.0f);

        // Turn on backface culling
        glFrontFace(GL_CCW);
        glCullFace(GL_BACK);

        // Turn on depth testing
        glEnable(GL_DEPTH_TEST);
        glDepthFunc(GL_LEQUAL);

        // Send draw request
        OnDraw(NULL);
    }

    void COpenGLControl::oglDrawScene(void)
    {
        // Wireframe Mode
        glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);

        glBegin(GL_QUADS);
                // Front Side
                glVertex3f( 1.0f,  1.0f, 1.0f);
                glVertex3f(-1.0f,  1.0f, 1.0f);
                glVertex3f(-1.0f, -1.0f, 1.0f);
                glVertex3f( 1.0f, -1.0f, 1.0f);

                // Back Side
                glVertex3f(-1.0f, -1.0f, -1.0f);
                glVertex3f(-1.0f,  1.0f, -1.0f);
                glVertex3f( 1.0f,  1.0f, -1.0f);
                glVertex3f( 1.0f, -1.0f, -1.0f);

                // Top Side
                glVertex3f( 1.0f, 1.0f,  1.0f);
                glVertex3f( 1.0f, 1.0f, -1.0f);
                glVertex3f(-1.0f, 1.0f, -1.0f);
                glVertex3f(-1.0f, 1.0f,  1.0f);

                // Bottom Side
                glVertex3f(-1.0f, -1.0f, -1.0f);
                glVertex3f( 1.0f, -1.0f, -1.0f);
                glVertex3f( 1.0f, -1.0f,  1.0f);
                glVertex3f(-1.0f, -1.0f,  1.0f);

                // Right Side
                glVertex3f( 1.0f,  1.0f,  1.0f);
                glVertex3f( 1.0f, -1.0f,  1.0f);
                glVertex3f( 1.0f, -1.0f, -1.0f);
                glVertex3f( 1.0f,  1.0f, -1.0f);

                // Left Side
                glVertex3f(-1.0f, -1.0f, -1.0f);
                glVertex3f(-1.0f, -1.0f,  1.0f);
                glVertex3f(-1.0f,  1.0f,  1.0f);
                glVertex3f(-1.0f,  1.0f, -1.0f);
        glEnd();
    }

推荐答案

OpenGL 是一个线程全局上下文状态机.这意味着,您必须将当前线程的 OpenGL 上下文切换到正确的窗口,以便绘制命令最终到达您想要的位置.

OpenGL is a thread global context state machine. That means, that you must switch the OpenGL context of the current thread to the right window so that drawing commands end up where you want them.

在您进行 OpenGL 调用的每个成员函数中

In every member function in which you make OpenGL calls put at the beginning

wglMakeCurrent(hdc, hrc);

在你离开函数之前(即在最后,或者在返回之前)

and before you leave the function (i.e. at the end, or before a return)

wglMakeCurrent(NULL, NULL);

顺便说一句:您可以对任意数量的窗口使用单个 OpenGL 上下文,只要它们共享相同的视觉属性,即,如果您为每个您想要的窗口使用相同的 PFD 调用 SetPixelFormatDescriptor使用的上下文.

BTW: You can use a single OpenGL context for any number of windows, as long as they share the same visual properties, i.e. if you called SetPixelFormatDescriptor with the same PFD for each window you want the context to use with.

最后但并非最不重要的一点是,OnSize 中的所有代码和 onInitialize 中的所有基本设置"都应放入绘图代码中.你的 OnDraw 也不是很有用.

Last but not least, all of the code found in OnSize and all of the "Basic Setup" found in onInitialize should be put into the drawing code. Your OnDraw is not very useful either.

这篇关于单个 MFC 对话框上的 OpenGL 两种不同的 3d 渲染图片控件不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,WP2

admin_action_{$_REQUEST[‘action’]}

do_action( "admin_action_{$_REQUEST[‘action’]}" )动作钩子::在发送“Action”请求变量时激发。Action Hook: Fires when an ‘action’ request variable is sent.目录锚点:#说明#源码说明(Description)钩子名称的动态部分$_REQUEST['action']引用从GET或POST请求派生的操作。源码(Source)更新版本源码位置使用被使用2.6.0 wp-admin/admin.php:...

日期:2020-09-02 17:44:16 浏览:1169

admin_footer-{$GLOBALS[‘hook_suffix’]}

do_action( "admin_footer-{$GLOBALS[‘hook_suffix’]}", string $hook_suffix )操作挂钩:在默认页脚脚本之后打印脚本或数据。Action Hook: Print scripts or data after the default footer scripts.目录锚点:#说明#参数#源码说明(Description)钩子名的动态部分,$GLOBALS['hook_suffix']引用当前页的全局钩子后缀。参数(Parameters)参数类...

日期:2020-09-02 17:44:20 浏览:1070

customize_save_{$this->id_data[‘base’]}

do_action( "customize_save_{$this-&gt;id_data[‘base’]}", WP_Customize_Setting $this )动作钩子::在调用WP_Customize_Setting::save()方法时激发。Action Hook: Fires when the WP_Customize_Setting::save() method is called.目录锚点:#说明#参数#源码说明(Description)钩子名称的动态部分,$this->id_data...

日期:2020-08-15 15:47:24 浏览:806

customize_value_{$this->id_data[‘base’]}

apply_filters( "customize_value_{$this-&gt;id_data[‘base’]}", mixed $default )过滤器::过滤未作为主题模式或选项处理的自定义设置值。Filter Hook: Filter a Customize setting value not handled as a theme_mod or option.目录锚点:#说明#参数#源码说明(Description)钩子名称的动态部分,$this->id_date['base'],指的是设置...

日期:2020-08-15 15:47:24 浏览:898

get_comment_author_url

过滤钩子:过滤评论作者的URL。Filter Hook: Filters the comment author’s URL.目录锚点:#源码源码(Source)更新版本源码位置使用被使用 wp-includes/comment-template.php:32610...

日期:2020-08-10 23:06:14 浏览:930

network_admin_edit_{$_GET[‘action’]}

do_action( "network_admin_edit_{$_GET[‘action’]}" )操作挂钩:启动请求的处理程序操作。Action Hook: Fires the requested handler action.目录锚点:#说明#源码说明(Description)钩子名称的动态部分$u GET['action']引用请求的操作的名称。源码(Source)更新版本源码位置使用被使用3.1.0 wp-admin/network/edit.php:3600...

日期:2020-08-02 09:56:09 浏览:877

network_sites_updated_message_{$_GET[‘updated’]}

apply_filters( "network_sites_updated_message_{$_GET[‘updated’]}", string $msg )筛选器挂钩:在网络管理中筛选特定的非默认站点更新消息。Filter Hook: Filters a specific, non-default site-updated message in the Network admin.目录锚点:#说明#参数#源码说明(Description)钩子名称的动态部分$_GET['updated']引用了非默认的...

日期:2020-08-02 09:56:03 浏览:864

pre_wp_is_site_initialized

过滤器::过滤在访问数据库之前是否初始化站点的检查。Filter Hook: Filters the check for whether a site is initialized before the database is accessed.目录锚点:#源码源码(Source)更新版本源码位置使用被使用 wp-includes/ms-site.php:93910...

日期:2020-07-29 10:15:38 浏览:833

WordPress 的SEO 教学:如何在网站中加入关键字(Meta Keywords)与Meta 描述(Meta Description)?

你想在WordPress 中添加关键字和meta 描述吗?关键字和meta 描述使你能够提高网站的SEO。在本文中,我们将向你展示如何在WordPress 中正确添加关键字和meta 描述。为什么要在WordPress 中添加关键字和Meta 描述?关键字和说明让搜寻引擎更了解您的帖子和页面的内容。关键词是人们寻找您发布的内容时,可能会搜索的重要词语或片语。而Meta Description则是对你的页面和文章的简要描述。如果你想要了解更多关于中继标签的资讯,可以参考Google的说明。Meta 关键字和描...

日期:2020-10-03 21:18:25 浏览:1726

谷歌的SEO是什么

SEO (Search Engine Optimization)中文是搜寻引擎最佳化,意思近于「关键字自然排序」、「网站排名优化」。简言之,SEO是以搜索引擎(如Google、Bing)为曝光媒体的行销手法。例如搜寻「wordpress教学」,会看到本站的「WordPress教学:12个课程…」排行Google第一:关键字:wordpress教学、wordpress课程…若搜寻「网站架设」,则会看到另一个网页排名第1:关键字:网站架设、架站…以上两个网页,每月从搜寻引擎导入自然流量,达2万4千:每月「有机搜...

日期:2020-10-30 17:23:57 浏览:1308