zoukankan      html  css  js  c++  java
  • MFC初始化OpenGL编程环境

    //库文件

    opengl32.lib

    glu32.lib

    // odemoView.h : CodemoView 类的接口

    #include <windows.h>
    #include <gl\gl.h>
    #include <gl\glu.h>
     

    public:
    afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
    afx_msg void OnDestroy();
    afx_msg void OnSize(UINT nType, int cx, int cy);


    protected:
    BOOL SetWindowPixelFormat(HDC hDC);//设置像素格式
    BOOL CreateViewGLContext(HDC hDC);//创建绘制环境(RC)并使之成为当前绘制环境
    BOOL InitGL(GLvoid);//初始化openGL
    int DrawGLScene(GLvoid);//绘图代码区
    int m_GLPixelIndex;//像素格式索引
    HGLRC m_hGLContext;//绘制环境

    // odemoView.cpp : CodemoView 类的实现

    CodemoView::CodemoView()
    {
    // TODO: 在此处添加构造代码
    this->m_GLPixelIndex = 0;
    this->m_hGLContext = NULL;
    }

    BOOL CodemoView::PreCreateWindow(CREATESTRUCT& cs)
    {
    // TODO: 在此处通过修改
    // CREATESTRUCT cs 来修改窗口类或样式
    cs.style |= (WS_CLIPCHILDREN | WS_CLIPSIBLINGS);//openGL必需的
    return CView::PreCreateWindow(cs);
    }

    BOOL CodemoView::SetWindowPixelFormat(HDC hDC)
    {//定义窗口的像素格式
    PIXELFORMATDESCRIPTOR pixelDesc=
    {
    sizeof(PIXELFORMATDESCRIPTOR),
    1,
    PFD_DRAW_TO_WINDOW|PFD_SUPPORT_OPENGL|
    PFD_DOUBLEBUFFER|PFD_SUPPORT_GDI,
    PFD_TYPE_RGBA,
    24,
    0,0,0,0,0,0,
    0,
    0,
    0,
    0,0,0,0,
    32,
    0,
    0,
    PFD_MAIN_PLANE,
    0,
    0,0,0
    };
    this->m_GLPixelIndex = ChoosePixelFormat(hDC,&pixelDesc);//选择最相近的像素格式
    if(this->m_GLPixelIndex==0)
    {//选择失败
    this->m_GLPixelIndex = 1;//默认的像素格式
    if(DescribePixelFormat(hDC,this->m_GLPixelIndex,sizeof(PIXELFORMATDESCRIPTOR),&pixelDesc)==0)
    {//用默认的像素格式进行设置
    return FALSE;
    }
    }
    if(SetPixelFormat(hDC,this->m_GLPixelIndex,&pixelDesc)==FALSE)
    {
    return FALSE;
    }
    return TRUE;
    }

    BOOL CodemoView::InitGL(GLvoid) // All Setup For OpenGL Goes Here
    {
    glShadeModel(GL_SMOOTH); // Enable Smooth Shading
    glClearColor(0.0,0.0,0.0,0.0); // Black Background
    glClearDepth(1.0f); // Depth Buffer Setup
    //glEnable(GL_DEPTH_TEST); // Enables Depth Testing
    //glDepthFunc(GL_LEQUAL); // The Type Of Depth Testing To Do
    glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);// Really Nice Perspective Calculations
    return TRUE; // Initialization Went OK
    }

    BOOL CodemoView::CreateViewGLContext(HDC hDC)
    {
    this->m_hGLContext = wglCreateContext(hDC);//创建RC
    if(this->m_hGLContext==NULL)
    {//创建失败
    return FALSE;
    }
    if(wglMakeCurrent(hDC,this->m_hGLContext)==FALSE)
    {//选为当前RC失败
    return FALSE;
    }
    return TRUE;
    }

    int CodemoView::OnCreate(LPCREATESTRUCT lpCreateStruct)
    {
    if (CView::OnCreate(lpCreateStruct) == -1)
    return -1;
    // TODO: 在此添加您专用的创建代码
    HWND hWnd = this->GetSafeHwnd();
    HDC hDC = ::GetDC(hWnd);
    if(this->SetWindowPixelFormat(hDC)==FALSE)
    {//设置像素格式
    return 0;
    }
    if(this->CreateViewGLContext(hDC)==FALSE)
    {//创建RC并选为所用
    return 0;
    }
    if(!this->InitGL())
    {//初始化openGL
    return 0;
    }
    return 0;
    }

    void CodemoView::OnDestroy()
    {
    CView::OnDestroy();
    // TODO: 在此处添加消息处理程序代码
    if(wglGetCurrentContext()!=NULL)
    {
    wglMakeCurrent(NULL,NULL);
    }
    if(this->m_hGLContext!=NULL)
    {
    wglDeleteContext(this->m_hGLContext);
    this->m_hGLContext = NULL;
    }
    }

    void CodemoView::OnSize(UINT nType, int cx, int cy)
    {
    CView::OnSize(nType, cx, cy);
    // TODO: 在此处添加消息处理程序代码
    GLsizei width,height;
    width = cx;
    height = cy;
    if (height==0) // Prevent A Divide By Zero By
    {
    height=1; // Making Height Equal One
    }
    glViewport(0,0,width,height); // Reset The Current Viewport
    glMatrixMode(GL_PROJECTION); // Select The Projection Matrix
    glLoadIdentity(); // Reset The Projection Matrix
    // Calculate The Aspect Ratio Of The Window
    gluPerspective(45.0f,(GLfloat)width/(GLfloat)height,0.1f,100.0f);//透视投影
    glMatrixMode(GL_MODELVIEW); // Select The Modelview Matrix
    glLoadIdentity(); // Reset The Modelview Matrix
    }

    int CodemoView::DrawGLScene(GLvoid)
    {//在这里写绘图代码

    }

    <完>

  • 相关阅读:
    PartialView
    常用取默认值
    迁移环境或升级正常功能出bug常见原因
    模拟登陆语法
    fsg报表相关
    常用数组定义方式
    to_char函数 官方文档详解(数字格式转换和日期转换)
    个人工作window10常用快捷键
    ebs常见概念解释
    常用ASCII码和字符的转换
  • 原文地址:https://www.cnblogs.com/afarmer/p/1634989.html
Copyright © 2011-2022 走看看