Справочник функций

Ваш аккаунт

Войти через: 
Забыли пароль?
Регистрация
Информацию о новых материалах можно получать и без регистрации:

Почтовая рассылка

Подписчиков: -1
Последний выпуск: 19.06.2015

OpenGL под MFC без использования Wizzard'а.

279
20 ноября 2004 года
bave
456 / / 07.03.2004
Подскажите ,пжалста, как OpenGL использовать под MFC не используя AppWizzard, т. е. как
дополнить проэкт минимального приложения, чтобы в окшко выводить изображение,
сам я пытался въехать, но безуспешно. Вот выкладываю мой не работающий вариант, подскажите где чё не так:

Это вообщем хедр:

#include <afxwin.h>

#include <gl/gl.h>
#include <gl/glu.h>
#include <gl/glaux.h>

class CBaveView: public CView {
public:
void display();
virtual void OnDraw(CDC *pCD);
virtual BOOL OnPreparePrinting(CPrintInfo* pInfo);
virtual void OnBeginPrinting(CDC* pDC, CPrintInfo* pInfo);
virtual void OnEndPrinting(CDC* pDC, CPrintInfo* pInfo);
virtual ~CBaveView();
CBaveView();
afx_msg void OnDestroy();
afx_msg void OnSize(UINT nType, int cx, int cy);
afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
DECLARE_MESSAGE_MAP()
private:
CClientDC *pdc;
HGLRC hGLRC;
int SetWindowPixelFormat(HDC);
};
class CBaveWnd: public CFrameWnd {
public:
CBaveWnd();
~CBaveWnd();
private:
DECLARE_MESSAGE_MAP()
};
class CBaveApp: public CWinApp {
public:
CBaveApp() {}
BOOL InitInstance();
};

А вот и сам glgraph.cpp:

#include <afxwin.h>
//-------------------------------------->
#include "StdAfx.h"
//-------------------------------------->
#include "Graph.hpp"


BOOL CBaveApp:: InitInstance()
{
m_pMainWnd = new CBaveWnd;
ASSERT(m_pMainWnd);
m_pMainWnd->ShowWindow(SW_RESTORE);
m_pMainWnd->UpdateWindow();
return TRUE;
};
CBaveWnd:: CBaveWnd()
{
RECT BaveWndRect;
BaveWndRect.left = BaveWndRect.top = 10;
BaveWndRect.right = 450; BaveWndRect.bottom = 450;
this->Create(NULL, "OpenGL", WS_OVERLAPPEDWINDOW, BaveWndRect, NULL, NULL);
}
CBaveWnd:: ~CBaveWnd() {}
CBaveView:: CBaveView() {}
CBaveView:: ~CBaveView() {}
void CBaveView:: display()
{
glClear(GL_COLOR_BUFFER_BIT| GL_DEPTH_BUFFER_BIT);
glTranslated(0.01, 0, 0);
glColor3d(1,0,0);
auxSolidSphere(1);
glFinish();
SwapBuffers(wglGetCurrentDC());
}
void CBaveView:: OnDraw(CDC *pDC)
{
display();
}

BOOL CBaveView:: OnPreparePrinting(CPrintInfo *pInfo)
{
return DoPreparePrinting(pInfo);
}

void CBaveView:: OnBeginPrinting(CDC*, CPrintInfo*) {}
void CBaveView:: OnEndPrinting(CDC*, CPrintInfo*) {}
int CBaveView:: SetWindowPixelFormat(HDC hDC)
{
int m_GLPixelIndex;
PIXELFORMATDESCRIPTOR pfd;
pfd.nSize = sizeof(PIXELFORMATDESCRIPTOR);
pfd.nVersion = 1;
pfd.dwFlags = PFD_DRAW_TO_WINDOW|PFD_SUPPORT_OPENGL|PFD_DOUBLEBUFFER;
pfd.iPixelType = PFD_TYPE_RGBA;
pfd.cColorBits = 32;
pfd.cRedBits = 8;
pfd.cRedShift = 16;
pfd.cGreenBits = 8;
pfd.cGreenShift = 8;
pfd.cBlueBits = 8;
pfd.cBlueShift = 0;
pfd.cAlphaBits = 0;
pfd.cAlphaShift = 0;
pfd.cAccumBits = 64;
pfd.cAccumRedBits = 16;
pfd.cAccumGreenBits = 16;
pfd.cAccumBlueBits = 16;
pfd.cAccumAlphaBits = 0;
pfd.cDepthBits = 32;
pfd.cStencilBits = 8;
pfd.cAuxBuffers = 0;
pfd.iLayerType = PFD_MAIN_PLANE;
pfd.bReserved = 0;
pfd.dwLayerMask = 0;
pfd.dwVisibleMask = 0;
pfd.dwDamageMask = 0;

m_GLPixelIndex = ChoosePixelFormat( hDC, &pfd);
if(m_GLPixelIndex == 0)
{
m_GLPixelIndex = 1;
if(DescribePixelFormat(hDC,m_GLPixelIndex,sizeof(PIXELFORMATDESCRIPTOR),&pfd)==0) return 0;
}


if (SetPixelFormat( hDC, m_GLPixelIndex, &pfd) == FALSE) return 0;

return 1;
}
int CBaveView:: OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if(CBaveView:: OnCreate(lpCreateStruct)==-1) return -1;
pdc = new CClientDC(this);
if(SetWindowPixelFormat(pdc->m_hDC) == FALSE) return -1;
hGLRC = wglCreateContext(pdc->m_hDC);
if(hGLRC == NULL) return -1;
if(wglMakeCurrent(pdc->m_hDC, hGLRC)==FALSE) return -1;

glEnable(GL_ALPHA_TEST);
glEnable(GL_DEPTH_TEST);
glEnable(GL_COLOR_MATERIAL);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

float pos[4] = {3,3,3,1};
float dir[3] = {-1,-1,-1};
glLightfv(GL_LIGHT0, GL_POSITION, pos);
glLightfv(GL_LIGHT0, GL_SPOT_DIRECTION, dir);

return 0;
}
void CBaveView:: OnDestroy()
{
if(wglGetCurrentContext() != NULL) wglMakeCurrent(NULL, NULL);
if(hGLRC != NULL)
{
wglDeleteContext(hGLRC);
hGLRC = NULL;
}
delete pdc;
CView:: OnDestroy();
}
void CBaveView:: OnSize(UINT nType, int cx, int cy)
{
CView::OnSize(nType, cx, cy);

glViewport(0,0,cx,cy);
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
glOrtho(-5,5, -5,5, 2,12);
gluLookAt( 0,0,5, 0,0,0, 0,1,0 );
glMatrixMode( GL_MODELVIEW );
}

BEGIN_MESSAGE_MAP(CBaveWnd, CFrameWnd)
END_MESSAGE_MAP()

BEGIN_MESSAGE_MAP(CBaveView, CView)
ON_WM_DESTROY()
ON_WM_SIZE()
END_MESSAGE_MAP()

CBaveApp theBaveApp;

Результат такой:

Graph.obj : error LNK2001: unresolved external symbol _gluLookAt@72
Glaux.lib(shapes.obj) : error LNK2001: unresolved external symbol _gluSphere@20
Glaux.lib(shapes.obj) : error LNK2001: unresolved external symbol _gluQuadricDrawStyle@8
Glaux.lib(shapes.obj) : error LNK2001: unresolved external symbol _gluNewQuadric@0
Glaux.lib(shapes.obj) : error LNK2001: unresolved external symbol _gluQuadricNormals@8
Glaux.lib(shapes.obj) : error LNK2001: unresolved external symbol _gluCylinder@36
Debug/GraphStep.exe : fatal error LNK1120: 6 unresolved externals
527
20 ноября 2004 года
pavor
275 / / 28.09.2003
/*
* 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;
}

Library: Use Opengl32.lib, Glu32.lib
279
20 ноября 2004 года
bave
456 / / 07.03.2004
А походу этот, пример под API, спасибо конечно,
но мне бы на MFC как-то, чтобы объявлялись нужные
классы вних нужные методы и потом они реализовывались.
Реклама на сайте | Обмен ссылками | Ссылки | Экспорт (RSS) | Контакты
Добавить статью | Добавить исходник | Добавить хостинг-провайдера | Добавить сайт в каталог