您的位置:首页 > 运维架构

如何编写OpenGL程序

2008-04-09 00:40 344 查看
                                                  如何编写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       
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();
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: