如何编写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();
}
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();
}
相关文章推荐
- 如何编写OpenGL程序
- 如何编写OpenGL程序
- 如何编写OpenGL程序
- 如何编写OpenGL程序
- 如何打开eclipse进行编写Java程序
- 如何使用inotify_init,inotify_add_watch,inotify_rm_watch,read编写监控程序
- 如何在windows中编写R程序包(转载)
- 如何使用inotify编写目录监控程序
- 如何使用 C++11 编写 Linux 多线程程序
- mfc编写的程序如何在别人机器上运行?
- 如何用VC++60编写查看二进制文件程序
- PB中如何编写COM和COM+程序.
- Java千百问_02基本使用(011)_如何编写单线程Socket程序
- 如何编写控制面板程序
- 如何编写第一个成功的Android NDK程序
- 如何编写100% cache miss的C程序
- 如何编写100% cache miss的C程序
- 通过学习学生信息管理系统软件,C程序中,如何设计和编写一个应用系统
- 如何组织编写模板程序 (How To Organize Template Source Code)
- 如何编写和调用java的jni程序