如何編寫OpenGL程式

來源:互聯網
上載者:User

                                                  如何編寫OpenGL程式

    OpenGL是一個三維圖形庫,在程式中用它即時產生三維造型或三維動畫是很方便的。
    YC++編譯器是可以編譯並連結OpenGL庫的, YC++預設的標頭檔yca.h不包含OpenGL庫,
因此需要在程式開始處使用YC++的新文法:

auto("opengl32.dll")
{
     #include        "gl.h"
};

auto("glu32.dll")
{
     #include        "glu.h"
};

   來指定OpenGL動態庫和包含其相關的標頭檔。

/*****************************************************************************************************************/
下面這個例子的用法是:

將下列代碼存入名字任取的檔案, 如: gl.cpp
在yc++中, 用<檔案/開啟或建立cpp來源程式>調入gl.cpp, 再用 <工具/執行>運行gl.cpp
   或在dos中, 用 ycc gl.cpp 產生 gl.exe, 再運行gl.exe
在vc++中, 用 cl gl.cpp 產生 gl.exe, 再運行gl.exe

/*****************************************************************************************************************/

#ifdef YCC
auto("opengl32.dll")
{
     #include        "gl.h"
};

auto("glu32.dll")
{
     #include        "glu.h"
};
#else
#include        <windows.h>
extern "C"
{
#include        "include/gl.h"
#include        "include/glu.h"
#include        "include/ycapi.h"
}
#pragma         comment(lib, "gdi32.lib")
#pragma         comment(lib, "user32.lib")
#pragma         comment(lib, "opengl32.lib")
#pragma         comment(lib, "glu32.lib")
#pragma         comment(lib, "yxbapi.lib")
#endif

////////////////////////////////////////////////////////////////////////////////////////////////////////////
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nShowCmd)
{
    /////////建立主視窗. 用YC++的API函數建立, 它不需要註冊. 也可以用Win32函數建立主視窗
    int WINAPI MainWndProc(HWND hwnd,UINT iMessage,UINT wParam,LONG lParam,void *pUserData);
    HWND hwnd = YXB_Window(MainWndProc,NULL,0,
      WS_OVERLAPPEDWINDOW | WS_CAPTION | WS_SYSMENU,
      "使用OpenGL",110,50,800,600,NULL,WT_WIN);

    MSG  msg;
    while(GetMessage(&msg,NULL,0,0))
      {
 TranslateMessage(&msg);
 DispatchMessage(&msg);
      }
    return 0;
}

int WINAPI MainWndProc(HWND hwnd,UINT iMessage,UINT wParam,LONG lParam,void *pUserData)
{
    RECT mRect;
    switch(iMessage)
      {
 case WM_SIZE:      InvalidateRect(hwnd,NULL,TRUE);   return FALSE;
 case WM_DESTROY:   PostQuitMessage(0);               return FALSE;

 case WM_PAINT:
      PAINTSTRUCT ps;
      BeginPaint(hwnd, &ps);
      YIMG yimg;
      memset(&yimg,0,sizeof YIMG);
      yimg.hdc = ps.hdc;     //通過這個設定, 可以將視窗客戶區當作一幅圖象來操作
      GetClientRect(hwnd, &mRect);
      YXB_ImageFill(&yimg, 0, 0, mRect.right, mRect.bottom, RGB(130,150,160));

      HGLRC hglrc;
      void OpenGL_Init(HDC hdc,int ww,int hh,HGLRC *hglrc);
      OpenGL_Init(yimg.hdc,mRect.right,mRect.bottom,&hglrc);

      void OpenGL_draw_scene();
      OpenGL_draw_scene();

      void OpenGL_End(HGLRC hglrc);
      OpenGL_End(hglrc);

      EndPaint(hwnd, &ps);
      return FALSE;
      }
    return DefWindowProc(hwnd,iMessage,wParam,lParam);
}

PIXELFORMATDESCRIPTOR pfd = {
       sizeof(PIXELFORMATDESCRIPTOR), 1, PFD_SUPPORT_OPENGL | PFD_SUPPORT_GDI | PFD_DRAW_TO_BITMAP,
       PFD_TYPE_RGBA, 32, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 32, 0, 0, PFD_MAIN_PLANE, 0, 0, 0, 0 };

void OpenGL_Init(HDC hdc,int ww,int hh,HGLRC *hglrc)
{
int    pixelformat;

    if((pixelformat=ChoosePixelFormat(hdc,&pfd)) == 0)
      {
 if((pixelformat=ChoosePixelFormat(hdc,&pfd)) == 0)      return;
      }
    if(SetPixelFormat(hdc,pixelformat,&pfd) == FALSE)   return;

    *hglrc = wglCreateContext(hdc);
    wglMakeCurrent(hdc,*hglrc);

    glViewport(0,0,ww,hh);
    gluPerspective(35,1,3,7);
    glTranslatef(0.0, 0.0, -4.5);
}

void OpenGL_End(HGLRC hglrc)
{
    wglMakeCurrent(NULL,NULL);
    wglDeleteContext(hglrc);
}

void OpenGL_draw_scene()
{
    glDepthMask (GL_FALSE);
    glBlendFunc (GL_SRC_ALPHA, GL_ONE);

    glBegin(GL_TRIANGLE_FAN);
      glColor4f(1.0, 0.0, 0.0, 1.0);
      glVertex3f(0.0, 1.0, 0.0);
      glColor4f(0.0, 1.0, 0.0, 1.0);
      glVertex3f(1.0, 0.0, 0.0);
      glColor4f(0.0, 0.0, 1.0, 1.0);
      glVertex3f(0, -1.0, 0.0);
      glColor4f(0.0, 1.0, 1.0, 1.0);
      glVertex3f(-1.0, 0.0, 0.0);
    glEnd();
    glFinish();
} 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.