Для тех кому нехватает возможностей/производительности в каких-то движках например GameMaker...
Для начала вводим в MSDN "OpenGL" или в гугле "OpenGL site:msdn.microsoft.com" (без ковычек). Я буду описывать всё по ссылкам на online версии msdn.
Сначала попадаем на
OpenGL Start PageИ смотрим примеры:
1) Портабельный исходник:
/*
* Example of an X Window System OpenGL program.
* OpenGL code is taken from auxdemo.c in the Platform SDK
*/
#include <GL/glx.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include <X11/keysym.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <stdio.h>
/* X globals, defines, and prototypes */
Display *dpy;
Window glwin;
static int attributes[] = {GLX_DEPTH_SIZE, 16, GLX_DOUBLEBUFFER, None};
#define SWAPBUFFERS glXSwapBuffers(dpy, glwin)
#define BLACK_INDEX 0
#define RED_INDEX 1
#define GREEN_INDEX 2
#define BLUE_INDEX 4
#define WIDTH 300
#define HEIGHT 200
/* OpenGL globals, defines, and prototypes */
GLfloat latitude, longitude, latinc, longinc;
GLdouble radius;
#define GLOBE 1
#define CYLINDER 2
#define CONE 3
GLvoid resize(GLsizei, GLsizei);
GLvoid initializeGL(GLsizei, GLsizei);
GLvoid drawScene(GLvoid);
void polarView( GLdouble, GLdouble, GLdouble, GLdouble);
static Bool WaitForMapNotify(Display *d, XEvent *e, char *arg)
{
if ((e->type == MapNotify) && (e->xmap.window == (Window)arg)) {
return GL_TRUE;
}
return GL_FALSE;
}
void
main(int argc, char **argv)
{
XVisualInfo *vi;
Colormap cmap;
XSetWindowAttributes swa;
GLXContext cx;
XEvent event;
GLboolean needRedraw = GL_FALSE, recalcModelView = GL_TRUE;
int dummy;
dpy = XOpenDisplay(NULL);
if (dpy == NULL){
fprintf(stderr, "could not open display\n");
exit(1);
}
if(!glXQueryExtension(dpy, &dummy, &dummy)){
fprintf(stderr, "could not open display");
exit(1);
}
/* find an OpenGL-capable Color Index visual with depth buffer */
vi = glXChooseVisual(dpy, DefaultScreen(dpy), attributes);
if (vi == NULL) {
fprintf(stderr, "could not get visual\n");
exit(1);
}
/* create an OpenGL rendering context */
cx = glXCreateContext(dpy, vi, None, GL_TRUE);
if (cx == NULL) {
fprintf(stderr, "could not create rendering context\n");
exit(1);
}
/* create an X colormap since probably not using default visual */
cmap = XCreateColormap(dpy, RootWindow(dpy, vi->screen),
vi->visual, AllocNone);
swa.colormap = cmap;
swa.border_pixel = 0;
swa.event_mask = ExposureMask | KeyPressMask | StructureNotifyMask;
glwin = XCreateWindow(dpy, RootWindow(dpy, vi->screen), 0, 0, WIDTH,
HEIGHT, 0, vi->depth, InputOutput, vi->visual,
CWBorderPixel | CWColormap | CWEventMask, &swa);
XSetStandardProperties(dpy, glwin, "xogl", "xogl", None, argv,
argc, NULL);
glXMakeCurrent(dpy, glwin, cx);
XMapWindow(dpy, glwin);
XIfEvent(dpy, &event, WaitForMapNotify, (char *)glwin);
initializeGL(WIDTH, HEIGHT);
resize(WIDTH, HEIGHT);
/* Animation loop */
while (1) {
KeySym key;
while (XPending(dpy)) {
XNextEvent(dpy, &event);
switch (event.type) {
case KeyPress:
XLookupString((XKeyEvent *)&event, NULL, 0, &key, NULL);
switch (key) {
case XK_Left:
longinc += 0.5;
break;
case XK_Right:
longinc -= 0.5;
break;
case XK_Up:
latinc += 0.5;
break;
case XK_Down:
latinc -= 0.5;
break;
}
break;
case ConfigureNotify:
resize(event.xconfigure.width, event.xconfigure.height);
break;
}
}
drawScene();
}
}
/* OpenGL code */
GLvoid resize( GLsizei width, GLsizei height )
{
GLfloat aspect;
glViewport( 0, 0, width, height );
aspect = (GLfloat) width / height;
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
gluPerspective( 45.0, aspect, 3.0, 7.0 );
glMatrixMode( GL_MODELVIEW );
}
GLvoid createObjects()
{
GLUquadricObj *quadObj;
glNewList(GLOBE, GL_COMPILE);
quadObj = gluNewQuadric ();
gluQuadricDrawStyle (quadObj, GLU_LINE);
gluSphere (quadObj, 1.5, 16, 16);
glEndList();
glNewList(CONE, GL_COMPILE);
quadObj = gluNewQuadric ();
gluQuadricDrawStyle (quadObj, GLU_FILL);
gluQuadricNormals (quadObj, GLU_SMOOTH);
gluCylinder(quadObj, 0.3, 0.0, 0.6, 15, 10);
glEndList();
glNewList(CYLINDER, GL_COMPILE);
glPushMatrix ();
glRotatef ((GLfloat)90.0, (GLfloat)1.0, (GLfloat)0.0, (GLfloat)0.0);
glTranslatef ((GLfloat)0.0, (GLfloat)0.0, (GLfloat)-1.0);
quadObj = gluNewQuadric ();
gluQuadricDrawStyle (quadObj, GLU_FILL);
gluQuadricNormals (quadObj, GLU_SMOOTH);
gluCylinder (quadObj, 0.3, 0.3, 0.6, 12, 2);
glPopMatrix ();
glEndList();
}
GLvoid initializeGL(GLsizei width, GLsizei height)
{
GLfloat maxObjectSize, aspect;
GLdouble near_plane, far_plane;
glClearIndex( (GLfloat)BLACK_INDEX);
glClearDepth( 1.0 );
glEnable(GL_DEPTH_TEST);
glMatrixMode( GL_PROJECTION );
aspect = (GLfloat) width / height;
gluPerspective( 45.0, aspect, 3.0, 7.0 );
glMatrixMode( GL_MODELVIEW );
near_plane = 3.0;
far_plane = 7.0;
maxObjectSize = 3.0F;
radius = near_plane + maxObjectSize/2.0;
latitude = 0.0F;
longitude = 0.0F;
latinc = 6.0F;
longinc = 2.5F;
createObjects();
}
void polarView(GLdouble radius, GLdouble twist, GLdouble latitude,
GLdouble longitude)
{
glTranslated(0.0, 0.0, -radius);
glRotated(-twist, 0.0, 0.0, 1.0);
glRotated(-latitude, 1.0, 0.0, 0.0);
glRotated(longitude, 0.0, 0.0, 1.0);
}
GLvoid drawScene(GLvoid)
{
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
glPushMatrix();
latitude += latinc;
longitude += longinc;
polarView( radius, 0, latitude, longitude );
glIndexi(RED_INDEX);
glCallList(CONE);
glIndexi(BLUE_INDEX);
glCallList(GLOBE);
glIndexi(GREEN_INDEX);
glPushMatrix();
glTranslatef(0.8F, -0.65F, 0.0F);
glRotatef(30.0F, 1.0F, 0.5F, 1.0F);
glCallList(CYLINDER);
glPopMatrix();
glPopMatrix();
SWAPBUFFERS;
}
2) Исходник портированный под Win32
/*
* Example of a Win32 OpenGL program.
* The OpenGL code is the same as that used in
* the X Window System sample
*/
#include <windows.h>
#include <GL/gl.h>
#include <GL/glu.h>
/* Windows globals, defines, and prototypes */
CHAR szAppName[]="Win OpenGL";
HWND ghWnd;
HDC ghDC;
HGLRC ghRC;
#define SWAPBUFFERS SwapBuffers(ghDC)
#define BLACK_INDEX 0
#define RED_INDEX 13
#define GREEN_INDEX 14
#define BLUE_INDEX 16
#define WIDTH 300
#define HEIGHT 200
LONG WINAPI MainWndProc (HWND, UINT, WPARAM, LPARAM);
BOOL bSetupPixelFormat(HDC);
/* OpenGL globals, defines, and prototypes */
GLfloat latitude, longitude, latinc, longinc;
GLdouble radius;
#define GLOBE 1
#define CYLINDER 2
#define CONE 3
GLvoid resize(GLsizei, GLsizei);
GLvoid initializeGL(GLsizei, GLsizei);
GLvoid drawScene(GLvoid);
void polarView( GLdouble, GLdouble, GLdouble, GLdouble);
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
MSG msg;
WNDCLASS wndclass;
/* Register the frame class */
wndclass.style = 0;
wndclass.lpfnWndProc = (WNDPROC)MainWndProc;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hInstance = hInstance;
wndclass.hIcon = LoadIcon (hInstance, szAppName);
wndclass.hCursor = LoadCursor (NULL,IDC_ARROW);
wndclass.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wndclass.lpszMenuName = szAppName;
wndclass.lpszClassName = szAppName;
if (!RegisterClass (&wndclass) )
return FALSE;
/* Create the frame */
ghWnd = CreateWindow (szAppName,
"Generic OpenGL Sample",
WS_OVERLAPPEDWINDOW | WS_CLIPSIBLINGS | WS_CLIPCHILDREN,
CW_USEDEFAULT,
CW_USEDEFAULT,
WIDTH,
HEIGHT,
NULL,
NULL,
hInstance,
NULL);
/* make sure window was created */
if (!ghWnd)
return FALSE;
/* show and update main window */
ShowWindow (ghWnd, nCmdShow);
UpdateWindow (ghWnd);
/* animation loop */
while (1) {
/*
* Process all pending messages
*/
while (PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE) == TRUE)
{
if (GetMessage(&msg, NULL, 0, 0) )
{
TranslateMessage(&msg);
DispatchMessage(&msg);
} else {
return TRUE;
}
}
drawScene();
}
}
/* main window procedure */
LONG WINAPI MainWndProc (
HWND hWnd,
UINT uMsg,
WPARAM wParam,
LPARAM lParam)
{
LONG lRet = 1;
PAINTSTRUCT ps;
RECT rect;
switch (uMsg) {
case WM_CREATE:
ghDC = GetDC(hWnd);
if (!bSetupPixelFormat(ghDC))
PostQuitMessage (0);
ghRC = wglCreateContext(ghDC);
wglMakeCurrent(ghDC, ghRC);
GetClientRect(hWnd, &rect);
initializeGL(rect.right, rect.bottom);
break;
case WM_PAINT:
BeginPaint(hWnd, &ps);
EndPaint(hWnd, &ps);
break;
case WM_SIZE:
GetClientRect(hWnd, &rect);
resize(rect.right, rect.bottom);
break;
case WM_CLOSE:
if (ghRC)
wglDeleteContext(ghRC);
if (ghDC)
ReleaseDC(hWnd, ghDC);
ghRC = 0;
ghDC = 0;
DestroyWindow (hWnd);
break;
case WM_DESTROY:
if (ghRC)
wglDeleteContext(ghRC);
if (ghDC)
ReleaseDC(hWnd, ghDC);
PostQuitMessage (0);
break;
case WM_KEYDOWN:
switch (wParam) {
case VK_LEFT:
longinc += 0.5F;
break;
case VK_RIGHT:
longinc -= 0.5F;
break;
case VK_UP:
latinc += 0.5F;
break;
case VK_DOWN:
latinc -= 0.5F;
break;
}
default:
lRet = DefWindowProc (hWnd, uMsg, wParam, lParam);
break;
}
return lRet;
}
BOOL bSetupPixelFormat(HDC hdc)
{
PIXELFORMATDESCRIPTOR pfd, *ppfd;
int pixelformat;
ppfd = &pfd;
ppfd->nSize = sizeof(PIXELFORMATDESCRIPTOR);
ppfd->nVersion = 1;
ppfd->dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL |
PFD_DOUBLEBUFFER;
ppfd->dwLayerMask = PFD_MAIN_PLANE;
ppfd->iPixelType = PFD_TYPE_COLORINDEX;
ppfd->cColorBits = 8;
ppfd->cDepthBits = 16;
ppfd->cAccumBits = 0;
ppfd->cStencilBits = 0;
pixelformat = ChoosePixelFormat(hdc, ppfd);
if ( (pixelformat = ChoosePixelFormat(hdc, ppfd)) == 0 )
{
MessageBox(NULL, "ChoosePixelFormat failed", "Error", MB_OK);
return FALSE;
}
if (SetPixelFormat(hdc, pixelformat, ppfd) == FALSE)
{
MessageBox(NULL, "SetPixelFormat failed", "Error", MB_OK);
return FALSE;
}
return TRUE;
}
/* OpenGL code */
GLvoid resize( GLsizei width, GLsizei height )
{
GLfloat aspect;
glViewport( 0, 0, width, height );
aspect = (GLfloat) width / height;
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
gluPerspective( 45.0, aspect, 3.0, 7.0 );
glMatrixMode( GL_MODELVIEW );
}
GLvoid createObjects()
{
GLUquadricObj *quadObj;
glNewList(GLOBE, GL_COMPILE);
quadObj = gluNewQuadric ();
gluQuadricDrawStyle (quadObj, GLU_LINE);
gluSphere (quadObj, 1.5, 16, 16);
glEndList();
glNewList(CONE, GL_COMPILE);
quadObj = gluNewQuadric ();
gluQuadricDrawStyle (quadObj, GLU_FILL);
gluQuadricNormals (quadObj, GLU_SMOOTH);
gluCylinder(quadObj, 0.3, 0.0, 0.6, 15, 10);
glEndList();
glNewList(CYLINDER, GL_COMPILE);
glPushMatrix ();
glRotatef ((GLfloat)90.0, (GLfloat)1.0, (GLfloat)0.0, (GLfloat)0.0);
glTranslatef ((GLfloat)0.0, (GLfloat)0.0, (GLfloat)-1.0);
quadObj = gluNewQuadric ();
gluQuadricDrawStyle (quadObj, GLU_FILL);
gluQuadricNormals (quadObj, GLU_SMOOTH);
gluCylinder (quadObj, 0.3, 0.3, 0.6, 12, 2);
glPopMatrix ();
glEndList();
}
GLvoid initializeGL(GLsizei width, GLsizei height)
{
GLfloat maxObjectSize, aspect;
GLdouble near_plane, far_plane;
glClearIndex( (GLfloat)BLACK_INDEX);
glClearDepth( 1.0 );
glEnable(GL_DEPTH_TEST);
glMatrixMode( GL_PROJECTION );
aspect = (GLfloat) width / height;
gluPerspective( 45.0, aspect, 3.0, 7.0 );
glMatrixMode( GL_MODELVIEW );
near_plane = 3.0;
far_plane = 7.0;
maxObjectSize = 3.0F;
radius = near_plane + maxObjectSize/2.0;
latitude = 0.0F;
longitude = 0.0F;
latinc = 6.0F;
longinc = 2.5F;
createObjects();
}
void polarView(GLdouble radius, GLdouble twist, GLdouble latitude,
GLdouble longitude)
{
glTranslated(0.0, 0.0, -radius);
glRotated(-twist, 0.0, 0.0, 1.0);
glRotated(-latitude, 1.0, 0.0, 0.0);
glRotated(longitude, 0.0, 0.0, 1.0);
}
GLvoid drawScene(GLvoid)
{
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
glPushMatrix();
latitude += latinc;
longitude += longinc;
polarView( radius, 0, latitude, longitude );
glIndexi(RED_INDEX);
glCallList(CONE);
glIndexi(BLUE_INDEX);
glCallList(GLOBE);
glIndexi(GREEN_INDEX);
glPushMatrix();
glTranslatef(0.8F, -0.65F, 0.0F);
glRotatef(30.0F, 1.0F, 0.5F, 1.0F);
glCallList(CYLINDER);
glPopMatrix();
glPopMatrix();
SWAPBUFFERS;
}
Оба выглядят ужасно (для тех кто видел только консольные приложения на С++, или вообще не видел код на С++). Работать будем со вторым. Почему? Потому что я с первым не работал (зачем я буду обучать тому что не знаю?).
Для начала как заставить пример работать? Microsoft Visual Studio 2005 например или VC6.0
File->New->Project->Visual C++ -> Win32->Win32 Project
Пишем имя (например: OpenGL Test)
Ok, Next, windows application empty project
Finish
Затем в папке Source Files создаём исполняемый файл (LMOUSE_BUTTON->Add->New Item->Visual C++ -> Code -> C++ File (.cpp)
Пишем имя (например: main.cpp)
Вставляем исходник примера (второго)
Сохраняем файл, и пытаемся скомпилировать (Build->Build Solution или просто F7)