如何编写OpenGL程序

news/2024/6/2 14:48:22

                                                  如何编写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();





http://www.niftyadmin.cn/n/3654714.html

相关文章

启动异常:java.lang.NoClassDefFoundError: org/apache/curator/framework/recipes/cache/TreeCacheListener 解决

Springboot&#xff1a;Zookeeper整合Dubbo报错: java.lang.NoClassDefFoundError: org/apache/curator/framework/recipes/cache/TreeCacheListener 解决方法&#xff1a; 添加以下依赖 <dependency><groupId>org.apache.curator</groupId><artifact…

气死老师的试卷答案(超级爆笑)

1、 地理教过&#xff0c;中国产煤最多的地方是辽宁省抚顺&#xff0c;产铁最多是辽宁省鞍山&#xff0c; 所以抚顺被称为中国的“煤都”&#xff0c;鞍山称为“铁都”。某次考试&#xff0c;试卷上&#xff1a;中国的煤都是(黑的)&#xff0c;中国的铁都是(硬的)。考完还说&am…

在YC++中编写多线程代码

在YC中编写多线程代码在运行程序时&#xff0c;很多费时的操作或计算(如通过socket读网络文件, 解一个微分方程等等)&#xff0c;都有可能使界面失去反应&#xff0c;这种现象叫做阻塞。如何解决阻塞问题呢&#xff1f;最好的办法就是使用多线程。 在YC编程中&#xff0c;…

在YC++中如何实现Ajax功能

在YC中如何实现Ajax功能在Web页面中&#xff0c;有时当用户点击一个按钮后&#xff0c;只有其中的一小块内容将要发生变化。在使用Ajax以前&#xff0c;一般必须刷新整个页面&#xff0c;无论页面中变化的部分还是没有变化的部分&#xff0c;都要从服务器中重新读取。使用Ajax后…

股票技术指标详解--移动平均线(MA)

移动平均线(MA) 移动平均线是应用非常广泛的一种技术指标。它构造简单&#xff0c;客观公正&#xff0c;不易人为操作骗线&#xff0c;受到很多股票投资者的青睐。 一、指数平均数的计算 所谓"移动平均线"是指一段时间内的算术平均线&#xff0c;通常以收盘价…

股票技术指标详解--指数平滑移动平均线(MACD)

指数平滑移动平均线(MACD) 一、指数平滑移动平均线 运用两条移动平均线相互背离&#xff0c;相互应证的交易法则&#xff0c;就可以得出指数平滑移动平均线(MACD)。它是运用快速和慢速移动平均线交叉换位、合并分离的特性加以双重平滑运算&#xff0c;来判断买卖时机。该指…

欢迎使用WINX!

WINX关注一个目标&#xff1a;简单而高效。它是一个C界面开发库&#xff0c;容易使用&#xff0c;兼容WTL。WTL是高效的&#xff0c;然而&#xff0c;它并不容易上手。官方博客&#xff1a;http://winxcn.blogspot.com/您可以在sourceforge获得WINX的最新版本&#xff1a;http:…

OWC学习笔记-Spreadsheet插入行/列

在owc提供的Spreadsheet api 中&#xff0c;没有直接添加行列的方法&#xff0c;可以使用执行命令的方式实现 添加新行在第3行&#xff0c;代码如下&#xff1a; varssConstants Spreadsheet1.Constants; Spreadsheet1.ActiveSheet.Row(3).Select(); Spreadsheet1.Command…