代码下载
#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,
//-------------绘制图元的类型,索引的数量, 索引的数据格式, 索引数据内存地址
glDrawElements( GL_QUADS, 24, GL_UNSIGNED_BYTE, g_cubeIndices );
函数进行绘制
接上一个例子,我们可以计算以下上两个例子每一绘制要向显卡传送到内存大小
24个点 * sizeof(Vertex)
实际上有很多点都是重复的,我么可以使用索引的方式向显卡传递更少的数据
提高效率
当采用了索引方式绘制以后,需要8个顶点,24个索引
内存大小为 8 * sizeof(Vertext) + 24;
内存使用只有原来的40%不到。
*/
struct Vertex
{
float r, g, b;
float x, y, z;
};
/**
* 一个立方体有8个顶点
*/
Vertex g_cubeVertices_indexed[] =
{
{ 1.0f,0.0f,0.0f, -1.0f,-1.0f, 1.0f }, // 0
{ 0.0f,1.0f,0.0f, 1.0f,-1.0f, 1.0f }, // 1
{ 0.0f,0.0f,1.0f, 1.0f, 1.0f, 1.0f }, // 2
{ 1.0f,1.0f,0.0f, -1.0f, 1.0f, 1.0f }, // 3
{ 1.0f,0.0f,1.0f, -1.0f,-1.0f,-1.0f }, // 4
{ 0.0f,1.0f,1.0f, -1.0f, 1.0f,-1.0f }, // 5
{ 1.0f,1.0f,1.0f, 1.0f, 1.0f,-1.0f }, // 6
{ 1.0f,0.0f,0.0f, 1.0f,-1.0f,-1.0f }, // 7
};
/**
* 对应的索引数据
*/
GLubyte g_cubeIndices[] =
{
0, 1, 2, 3, // Quad 0
4, 5, 6, 7, // Quad 1
5, 3, 2, 6, // Quad 2
4, 7, 1, 0, // Quad 3
7, 6, 2, 1, // Quad 4
4, 0, 3, 5 // Quad 5
};
class Tutorial7_3 :public CELL::Graphy::CELLWinApp
{
public:
Tutorial7_3(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 );
/**
* 这里大家可以慢慢体会
*/
float* colorAddress = (float*)g_cubeVertices_indexed;
float* vertexAddress = (float*)&g_cubeVertices_indexed[0].x;
//--------------元素个数---元素类型---元素之间的内存偏移---数据地址
//OpenGL根据元素之间的内存偏移来计算下一个元素的位置。
glVertexPointer( 3, GL_FLOAT, sizeof(Vertex),vertexAddress);
glColorPointer( 3, GL_FLOAT, sizeof(Vertex), colorAddress);
//glDrawArrays( GL_QUADS, 0, 24 );
/**
* 使用索引绘制
*/
//-------------绘制图元的类型,索引的数量, 索引的数据格式, 索引数据内存地址
glDrawElements( GL_QUADS, 24, GL_UNSIGNED_BYTE, g_cubeIndices );
glDisableClientState( GL_VERTEX_ARRAY );
glDisableClientState( GL_COLOR_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_DEPTH_TEST);
}
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:
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_3 winApp(hInstance);
winApp.start(640,480);
return 0;
}