VC .NETOpenGL编程快速入门(5)

2008-04-09 04:09:13来源:互联网 阅读 ()

新老客户大回馈,云服务器低至5折


}
  下面我们就可以计算法向量了。

for ( int i = 0; i < 12; i )
{
// 跟据一个三角形的三个顶点计算出该三角形的法向量
CalcNormal( &nTempBox[ i * 9 ], &fNormal[ i * 9 ] );
// 并把这个法向量赋给这个三角形的三个顶点
MoveMemory( &fNormal[ i * 9 3 ], &fNormal[ i * 9 ], sizeof(fNormal[0]) * 3 );
MoveMemory( &fNormal[ i * 9 6 ], &fNormal[ i * 9 ], sizeof(fNormal[0]) * 3 );
}
glEnableClientState( GL_VERTEX_ARRAY );
glVertexPointer( 3, GL_SHORT, 0, nTempBox );
glEnableClientState( GL_NORMAL_ARRAY ); // 启用法向量
glNormalPointer( GL_FLOAT, 0, fNormal ); // 填写法向量数组地址
  因为用不着索引数组,下面的绘制代码将更加简单:

glDrawArrays( GL_TRIANGLES, 0, 36 );
  好了,现在整篇代码应该是这个样子了:

// GlTest.cpp : 定义应用程序的入口点。
//

#include "stdafx.h"
#include "GlTest.h"
#define MAX_LOADSTRING 100


// 全局变量:
HINSTANCE hInst;    // 当前实例
HWND g_hWnd;
HDC g_hDC;
HGLRC g_glRes;

short nSrcBox[ 3 * 8 ] = {
 5, 5, 0,  5, 5, 10,
 5, -5, 0,  5, -5, 10,
-5, -5, 0, -5, -5, 10,
-5, 5, 0, -5, 5, 10,
};

BYTE byIndex[36] ={
0, 4, 6, 0, 2, 4,
0, 6, 7, 0, 7, 1,
0, 3, 2, 0, 1, 3,
5, 2, 3, 5, 4, 2,
5, 6, 4, 5, 7, 6,
5, 1, 7, 5, 3, 1,
};

short nTempBox[ 36 * 3 ];
float fNormal[ 36 * 3 ];

TCHAR szTitle[MAX_LOADSTRING];   // 标题栏文本
TCHAR szWindowClass[MAX_LOADSTRING];  // 主窗口类名


template <typename Type>
HRESULT ReduceToUnit( Type *pVector )
{
double dLength = sqrt( (double)pVector[0] * (double)pVector[0]
 (double)pVector[1] * (double)pVector[1]
 (double)pVector[2] * (double)pVector[2] );

if ( FLOATEQUAL( dLength, 0.0, 1e-8 ) )
{
 dLength = 1.0;
}
pVector[0] /= (Type)dLength;
pVector[1] /= (Type)dLength;
pVector[2] /= (Type)dLength;
return S_OK;
}

template <typename T1, typename T2>
HRESULT CalcNormal( const T1 *pVertical, T2 *pNormal )
{
T1 d1[3], d2[3];
d1[0] = pVertical[0] - pVertical[3];
d1[1] = pVertical[1] - pVertical[4];
d1[2] = pVertical[2] - pVertical[5];
d2[0] = pVertical[3] - pVertical[6];
d2[1] = pVertical[4] - pVertical[7];
d2[2] = pVertical[5] - pVertical[8];
pNormal[0] = (T2)( d1[1] * d2[2] - d1[2] * d2[1] );
pNormal[1] = (T2)( d1[2] * d2[0] - d1[0] * d2[2] );
pNormal[2] = (T2)( d1[0] * d2[1] - d1[1] * d2[0] );
return ReduceToUnit( pNormal );
}

// 此代码模块中包含的函数的前向声明:
void OnCreate( HWND hWnd );
void OnCreated( void );
void OnDestroy( void );
void OnDraw( void );
void SetProjMatrix( WORD wWidth, WORD wHeight );
void SetModalMatrix( void );
void OnIdle( void );

ATOM  MyRegisterClass(HINSTANCE hInstance);
BOOL  InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK About(HWND, UINT, WPARAM, LPARAM);

int APIENTRY _tWinMain(HINSTANCE hInstance,
           HINSTANCE hPrevInstance,
           LPTSTR  lpCmdLine,
           int    nCmdShow)
{
 // TODO: 在此放置代码。
MSG msg;
HACCEL hAccelTable;

// 初始化全局字符串
LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
LoadString(hInstance, IDC_GLTEST, szWindowClass, MAX_LOADSTRING);
MyRegisterClass(hInstance);

// 执行应用程序初始化:
if (!InitInstance (hInstance, nCmdShow))
{
 return FALSE;
}

hAccelTable = LoadAccelerators(hInstance, (LPCTSTR)IDC_GLTEST);

// 主消息循环:
while ( true )
{
 if ( PeekMessage( &msg, NULL, 0, 0, PM_REMOVE ) )
 {
  if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
  {
  TranslateMessage(&msg);
  DispatchMessage(&msg);
  }
  continue;
 }
 if ( WM_QUIT == msg.message )
 {
  break;
 }
 OnIdle();
}

return (int) msg.wParam;
}


ATOM MyRegisterClass(HINSTANCE hInstance)
{
WNDCLASSEX wcex;

wcex.cbSize = sizeof(WNDCLASSEX);

wcex.style  = 0;
wcex.lpfnWndProc = (WNDPROC)WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon  = LoadIcon(hInstance, (LPCTSTR)IDI_GLTEST);
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW 1);
wcex.lpszMenuName = (LPCTSTR)IDC_GLTEST;
wcex.lpszClassName = szWindowClass;
wcex.hIconSm = LoadIcon(wcex.hInstance, (LPCTSTR)IDI_SMALL);

return RegisterClassEx(&wcex);
}

//
//  函数:InitInstance(HANDLE, int)
//
//  目的:保存实例句柄并创建主窗口
//
//  注释:
//
//    在此函数中,我们在全局变量中保存实例句柄并

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:VC下用Win32API实现串行通信

下一篇:VisualC 编程封装ADO类