16,472
社区成员
发帖
与我相关
我的任务
分享
protected:
HGLRC hRC;
HDC hDC;
GLuint texture;
unsigned int m_nFPS=60;
public:
BOOL InitPixelFormat();//绑定DC
void ChangeDrawWinSize();
void DrawScene();
BOOL CMainDlg::InitPixelFormat()
{
PIXELFORMATDESCRIPTOR pixelDesc;
pixelDesc.nSize = sizeof(PIXELFORMATDESCRIPTOR);
pixelDesc.nVersion = 1;
pixelDesc.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER | PFD_TYPE_RGBA;
pixelDesc.iPixelType = PFD_TYPE_RGBA;
pixelDesc.cColorBits = 24;
pixelDesc.cRedBits = 0;
pixelDesc.cRedShift = 0;
pixelDesc.cGreenBits = 0;
pixelDesc.cGreenShift = 0;
pixelDesc.cBlueBits = 0;
pixelDesc.cBlueShift = 0;
pixelDesc.cAlphaBits = 0;
pixelDesc.cAlphaShift = 0;
pixelDesc.cAccumBits = 0;
pixelDesc.cAccumRedBits = 0;
pixelDesc.cAccumGreenBits = 0;
pixelDesc.cAccumBlueBits = 0;
pixelDesc.cAccumAlphaBits = 0;
pixelDesc.cDepthBits = 0;
pixelDesc.cStencilBits = 1;
pixelDesc.cAuxBuffers = 0;
pixelDesc.iLayerType = PFD_MAIN_PLANE;
pixelDesc.bReserved = 0;
pixelDesc.dwLayerMask = 0;
pixelDesc.dwVisibleMask = 0;
pixelDesc.dwDamageMask = 0;
int pixelformat;
hDC = ::GetDC(this->GetSafeHwnd());
if (!(pixelformat = ChoosePixelFormat(hDC, &pixelDesc)))
{
MessageBox(_T("ChoosePixelFormat failed!"));
return FALSE;
}
if (!SetPixelFormat(hDC, pixelformat, &pixelDesc))
{
MessageBox(_T("SetPixelFormat failed!"));
return FALSE;
}
if (!(hRC = wglCreateContext(hDC)))
{
MessageBox(_T("CreateContext failed!"));
return FALSE;
}
if (!wglMakeCurrent(hDC, hRC))
{
MessageBox(_T("MakeCurrent failed!"));
return FALSE;
}
return TRUE;
}
void CMainDlg::ChangeDrawWinSize()
{
CRect rect;
GetClientRect(rect);
glViewport(0, 0, rect.Width(), rect.Height());
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0f, rect.Width() / rect.Height(), 0.1f, 100.0f);// 计算窗口的外观比例
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glClearColor(0.0f, 0.0f, 0.0f, 1.0f); // 黑色背景
glClearDepth(1.0f); // 设置深度缓存
glEnable(GL_DEPTH_TEST); // 启用深度测试
glDepthFunc(GL_LEQUAL); // 所作深度测试的类型
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); // 真正精细的透视修正
}
void CMainDlg::DrawScene()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glTranslatef(-1.0f, 0.0f, -3.0f);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, texture);//没有纹理.....不会!!!
glBegin(GL_QUADS);
glTexCoord2f(0, 0); glVertex3f(0, 0, 0);
glTexCoord2f(1, 0); glVertex3f(1, 0, 0);
glTexCoord2f(1, 1); glVertex3f(1, 1, 0);
glTexCoord2f(0, 1); glVertex3f(0, 1, 0);
glEnd();
SwapBuffers(hDC);
}
WCHAR *pFilePath = L"v:\\1.png";
BYTE *pDat = GetFileData(pFilePath);
Bitmap *pImage;
Gdiplus::Rect RctLock;
Gdiplus::BitmapData BitmapDat;
GdiplusStartupInput StartupIn;
ULONG_PTR s_uGdiplusToken;
BYTE *pFullDat;
GdiplusStartup(&s_uGdiplusToken, &StartupIn, NULL);
pImage = pImage->FromFile(pFilePath);
RctLock.X = 0;
RctLock.Y = 0;
RctLock.Width = pImage->GetWidth();
RctLock.Height = pImage->GetHeight();
pImage->LockBits(&RctLock, Gdiplus::ImageLockModeRead, PixelFormat24bppRGB, &BitmapDat);
pFullDat = (BYTE *)BitmapDat.Scan0;
glEnable(GL_TEXTURE_2D);
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, pImage->GetWidth() ,pImage->GetHeight(), 0, GL_BGR_EXT, GL_UNSIGNED_BYTE, pFullDat);
delete pImage;
#include "glos.h"
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glaux.h>
void myinit(void);
void CALLBACK myReshape(GLsizei w, GLsizei h);
void CALLBACK display(void);
GLubyte rasters[12] = { 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xfc, 0xfc, 0xc0, 0xc0, 0xc0, 0xff, 0xff};
void myinit(void)
{
glPixelStorei (GL_UNPACK_ALIGNMENT, 1);
glClearColor (0.0, 0.0, 0.0, 0.0);
glClear(GL_COLOR_BUFFER_BIT);
}
void CALLBACK display(void)
{
glColor3f (1.0, 0.0, 1.0);
glRasterPos2i (100, 200);
glBitmap (8, 12, 0.0, 0.0, 20.0, 20.0, rasters);
lBitmap (8, 12, 0.0, 0.0, 0.0, 0.0, rasters);
glColor3f (1.0, 1.0, 0.0); glRasterPos2i (150, 200);
glBitmap (8, 12, 0.0, 0.0, 0.0, 0.0, rasters);
glFlush();
}
void CALLBACK myReshape(GLsizei w, GLsizei h)
{
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho (0, w, 0, h, -1.0, 1.0);
glMatrixMode(GL_MODELVIEW);
}
void main(void)
{
auxInitDisplayMode (AUX_SINGLE | AUX_RGBA);
auxInitPosition (0, 0, 500, 500);
auxInitWindow ("Bitmap");
myinit();
auxReshapeFunc (myReshape);
auxMainLoop(display);
}
参考下 gluPerspective(45.0f, rect.Width() / rect.Height(), 0.1f, 100.0f);// 计算窗口的外观比例
rect.Width()*1.0f / rect.Height()