代碼下載
#include "CELLWinApp.hpp"
#include <gl/GLU.h>
#include <assert.h>
#include <math.h>
#pragma comment(lib,"opengl32.lib")
#pragma comment(lib,"glu32.lib")
#pragma comment(lib,"winmm.lib")
/**
* 這個例子介紹如何使用
glEnableClientState,
glVertexPointer.
glColorPointer,
glTexCoordPointer,
glDrawArrays
函數進行繪制
接上一個例子,上一個例子中使用了三個緩沖區,定點緩沖區,紋理緩沖區,顏色緩沖
* 緩沖區多了以後靈活度增加了,但是管理卻很麻煩。
OpenGL也支持一個單獨的緩沖區
*/
struct Vertex
{
float x, y, z;
float u,v;
float r, g, b;
};
Vertex g_cubeVertices[] =
{
{ -1.0f,-1.0f, 1.0f,0.0f, 0.0f,1.0f, 0.0f, 0.0f },
{ 1.0f,-1.0f, 1.0f,1.0f, 0.0f,1.0f, 0.0f, 0.0f },
{ 1.0f, 1.0f, 1.0f,1.0f, 1.0f,1.0f, 0.0f, 0.0f },
{ -1.0f, 1.0f, 1.0f,0.0f, 1.0f,1.0f, 0.0f, 0.0f },
{ -1.0f,-1.0f,-1.0f,1.0f, 0.0f,0.0f, 1.0f, 0.0f },
{ -1.0f, 1.0f,-1.0f,1.0f, 1.0f,0.0f, 1.0f, 0.0f },
{ 1.0f, 1.0f,-1.0f,0.0f, 1.0f,0.0f, 1.0f, 0.0f },
{ 1.0f,-1.0f,-1.0f,0.0f, 0.0f,0.0f, 1.0f, 0.0f },
{ -1.0f, 1.0f,-1.0f,0.0f, 1.0f,0.0f, 0.0f, 1.0f },
{ -1.0f, 1.0f, 1.0f,0.0f, 0.0f,0.0f, 0.0f, 1.0f },
{ 1.0f, 1.0f, 1.0f,1.0f, 0.0f,0.0f, 0.0f, 1.0f },
{ 1.0f, 1.0f,-1.0f,1.0f, 1.0f,0.0f, 0.0f, 1.0f },
{ -1.0f,-1.0f,-1.0f,1.0f, 1.0f,1.0f, 1.0f, 0.0f },
{ 1.0f,-1.0f,-1.0f,0.0f, 1.0f,1.0f, 1.0f, 0.0f },
{ 1.0f,-1.0f, 1.0f,0.0f, 0.0f,1.0f, 1.0f, 0.0f },
{ -1.0f,-1.0f, 1.0f,1.0f, 0.0f,1.0f, 1.0f, 0.0f },
{ 1.0f,-1.0f,-1.0f,1.0f, 0.0f,1.0f, 0.0f, 1.0f },
{ 1.0f, 1.0f,-1.0f,1.0f, 1.0f,1.0f, 0.0f, 1.0f },
{ 1.0f, 1.0f, 1.0f,0.0f, 1.0f,1.0f, 0.0f, 1.0f },
{ 1.0f,-1.0f, 1.0f,0.0f, 0.0f,1.0f, 0.0f, 1.0f },
{ -1.0f,-1.0f,-1.0f,0.0f, 0.0f,0.0f, 1.0f, 1.0f },
{ -1.0f,-1.0f, 1.0f,1.0f, 0.0f,0.0f, 1.0f, 1.0f },
{ -1.0f, 1.0f, 1.0f,1.0f, 1.0f,0.0f, 1.0f, 1.0f },
{ -1.0f, 1.0f,-1.0f,0.0f, 1.0f,0.0f, 1.0f, 1.0f }
};
class Tutorial7 :public CELL::Graphy::CELLWinApp
{
public:
Tutorial7(HINSTANCE hInstance)
:CELL::Graphy::CELLWinApp(hInstance)
{
_lbtnDownFlag = false;
_fSpinY = 0;
_fSpinX = 0;
}
virtual void render()
{
do
{
glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
glTranslatef( 0.0f, 0.0f, -5.0f );
glRotatef( -_fSpinY, 1.0f, 0.0f, 0.0f );
glRotatef( -_fSpinX, 0.0f, 1.0f, 0.0f );
glEnableClientState( GL_VERTEX_ARRAY );
glEnableClientState( GL_COLOR_ARRAY );
glEnableClientState( GL_TEXTURE_COORD_ARRAY );
/**
* 這裡大家可以慢慢體會
*/
float* addrVertex = (float*)g_cubeVertices;
float* uvAddress = (float*)&g_cubeVertices[0].u;
float* colorAddress = (float*)&g_cubeVertices[0].r;
//--------------元素個數---元素類型---元素之間的內存偏移---數據地址
//OpenGL根據元素之間的內存偏移來計算下一個元素的位置。
glVertexPointer( 3, GL_FLOAT, sizeof(Vertex), addrVertex );
glColorPointer( 3, GL_FLOAT, sizeof(Vertex), colorAddress);
glTexCoordPointer( 2, GL_FLOAT, sizeof(Vertex), uvAddress );
glDrawArrays( GL_QUADS, 0, 24 );
glDisableClientState( GL_VERTEX_ARRAY );
glDisableClientState( GL_COLOR_ARRAY );
glDisableClientState( GL_TEXTURE_COORD_ARRAY );
SwapBuffers( _hDC );
} while (false);
}
/**
* 生成投影矩陣
* 後面為了重用性,我們會寫一個專門的matrix類,完成矩陣的一系列擦做
* 這個是很有必須要的,當你對Opengl了解的不斷深入,你會發現,很多都是和數學有關的
*/
void perspective(float fovy,float aspect,float zNear,float zFar,float matrix[4][4])
{
assert(aspect != float(0));
assert(zFar != zNear);
#define PI 3.14159265358979323f
float rad = fovy * (PI / 180);
float halfFovy = tan(rad / float(2));
matrix[0][0] = float(1) / (aspect * halfFovy);
matrix[1][1] = float(1) / (halfFovy);
matrix[2][2] = -(zFar + zNear) / (zFar - zNear);
matrix[2][3] = -float(1);
matrix[3][2] = -(float(2) * zFar * zNear) / (zFar - zNear);
#undef PI
}
virtual void onInit()
{
/**
* 調用父類的函數。
*/
CELL::Graphy::CELLWinApp::onInit();
glMatrixMode( GL_PROJECTION );
GLfloat matrix[4][4] =
{
0,0,0,0,
0,0,0,0,
0,0,0,0,
0,0,0,0
};
perspective(45.0f, (GLfloat)_winWidth / (GLfloat)_winHeight, 0.1f, 100.0f,matrix);
glLoadMatrixf((float*)matrix);
glClearColor(0,0,0,1);
/**
* 增加如下兩句話
* glEnable(GL_DEPTH_TEST); 啟動深度測試,這樣,有遮擋計算,被遮蓋的將覆蓋
* glEnable(GL_TEXTURE_2D); 啟動紋理,支持紋理貼圖,這樣才可以繪制紋理出來
*/
glEnable(GL_DEPTH_TEST);
glEnable(GL_TEXTURE_2D);
/**
* 讀一個bmp圖片
*/
HBITMAP hBmp = (HBITMAP)LoadImageA(0,"1.bmp",IMAGE_BITMAP,0,0,LR_LOADFROMFILE);
/**
* 獲取圖片的大小
*/
BITMAP bmpInf = {0};
GetObject(hBmp,sizeof(bmpInf),&bmpInf);
/**
* 獲取圖片的顏色數據(r,g,b)
*/
int size = bmpInf.bmHeight * bmpInf.bmWidth * 3;
char* data = new char[size];
BITMAPINFO bi;
bi.bmiHeader.biSize = sizeof(bi.bmiHeader);
bi.bmiHeader.biWidth = bmpInf.bmWidth;
bi.bmiHeader.biHeight = bmpInf.bmHeight;
bi.bmiHeader.biPlanes = 1;
bi.bmiHeader.biBitCount = 24;
bi.bmiHeader.biCompression = BI_RGB;
bi.bmiHeader.biSizeImage = size;
bi.bmiHeader.biClrUsed = 0;
bi.bmiHeader.biClrImportant = 0;
/**
* 獲取rgb數據
*/
int idata = GetDIBits(_hDC,hBmp,0,bi.bmiHeader.biHeight,data,&bi,DIB_RGB_COLORS);
/**
* 產生一個紋理Id,可以認為是紋理句柄,後面的操作將書用這個紋理id
*/
glGenTextures( 1, &_textureId );
/**
* 使用這個紋理id,或者叫綁定(關聯)
*/
glBindTexture( GL_TEXTURE_2D, _textureId );
/**
* 指定紋理的放大,縮小濾波,使用線性方式,即當圖片放大的時候插值方式
*/
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
/**
* 將圖片的rgb數據上傳給opengl.
*/
glTexImage2D(
GL_TEXTURE_2D, //! 指定是二維圖片
0, //! 指定為第一級別,紋理可以做mipmap,即lod,離近的就采用級別大的,遠則使用較小的紋理
GL_RGB, //! 紋理的使用的存儲格式
bmpInf.bmWidth, //! 寬度,老一點的顯卡,不支持不規則的紋理,即寬度和高度不是2^n。
bmpInf.bmHeight, //! 寬度,老一點的顯卡,不支持不規則的紋理,即寬度和高度不是2^n。
0, //! 是否的邊
GL_BGR_EXT, //! 數據的格式,bmp中,windows,操作系統中存儲的數據是bgr格式
GL_UNSIGNED_BYTE, //! 數據是8bit數據
data
);
delete []data;
/**
* 刪除圖片
*/
DeleteObject(hBmp);
}
virtual int events(unsigned msg, unsigned wParam, unsigned lParam)
{
switch(msg)
{
case WM_LBUTTONDOWN:
{
_mousePos.x = LOWORD (lParam);
_mousePos.y = HIWORD (lParam);
_lbtnDownFlag = true;
SetCapture(_hWnd);
}
break;
case WM_LBUTTONUP:
{
_lbtnDownFlag = false;
ReleaseCapture();
}
break;
case WM_MOUSEMOVE:
{
int curX = LOWORD (lParam);
int curY = HIWORD (lParam);
if( _lbtnDownFlag )
{
_fSpinX -= (curX - _mousePos.x);
_fSpinY -= (curY - _mousePos.y);
}
_mousePos.x = curX;
_mousePos.y = curY;
}
break;
}
return __super::events(msg,wParam,lParam);
}
protected:
unsigned _primitiveType;
/**
* 保存紋理Id
*/
unsigned _textureId;
float _fSpinX ;
float _fSpinY;
POINT _mousePos;
bool _lbtnDownFlag;
};
int CALLBACK _tWinMain(
HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPTSTR lpCmdLine,
int nShowCmd
)
{
(void*)hInstance;
(void*)hPrevInstance;
(void*)lpCmdLine;
(void*)nShowCmd;
Tutorial7 winApp(hInstance);
winApp.start(640,480);
return 0;
}