//-----------------------------------------------------------------------------
// Name: ogl_vertex_array_range.cpp
// Author: Kevin Harris (kevin@codesampler.com)
// Last Modified: 02/01/05
// Description: This sample demonstrates how to use the OpenGL extension
// NV_vertex_array_range to speed up the rendering of geometry
// by loading its vertex data and/or indices data directly into
// AGP or Video memory. Of course, the downside to this is that
// you become responsible for managing AGP and Video Memory
// which can seriously complicate your code.
//
// Control Keys: F1 - Toggles between using Vertex Array Range buffers and
// regular Vertex Arrays.
//
// F2 - Toggles the test geometry between a complicated sphere
// and a very simple quad. The sphere is heavily
// tessellated and is useful in benchmarking while the
// simple quad demonstrates how to use indices with the
// extension.
//
// F3 - Toggles wire-frame mode.
//-----------------------------------------------------------------------------
#define STRICT
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <math.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/timeb.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glaux.h>
#include "bitmap_fonts.h"
#include "resource.h"
//-----------------------------------------------------------------------------
// FUNCTION POINTERS FOR OPENGL EXTENSIONS
//-----------------------------------------------------------------------------
// For convenience, this project ships with its own "glext.h" extension header
// file. If you have trouble running this sample, it may be that this "glext.h"
// file is defining something that your hardware doesn�t actually support.
// Try recompiling the sample using your own local, vendor-specific "glext.h"
// header file. The same applies for "wglext.h".
#include "glext.h" // Sample's header file
//#include <GL/glext.h> // Your local header file
#include "wglext.h" // Sample's header file
//#include <GL/wglext.h> // Your local header file
// WGL_ARB_extensions_string
//PFNWGLGETEXTENSIONSSTRINGARBPROC wglGetExtensionsStringARB = NULL;
// WGL_NV_allocate_memory
PFNWGLALLOCATEMEMORYNVPROC wglAllocateMemoryNV = NULL;
PFNWGLFREEMEMORYNVPROC wglFreeMemoryNV = NULL;
// GL_NV_vertex_array_range
PFNGLFLUSHVERTEXARRAYRANGENVPROC glFlushVertexArrayRangeNV = NULL;
PFNGLVERTEXARRAYRANGENVPROC glVertexArrayRangeNV = NULL;
//-----------------------------------------------------------------------------
// GLOBALS
//-----------------------------------------------------------------------------
HWND g_hWnd = NULL;
HDC g_hDC = NULL;
HGLRC g_hRC = NULL;
int g_nWindowWidth = 640;
int g_nWindowHeight = 480;
GLuint g_textureID = -1;
bool g_bFirstRendering = true;
timeb g_lastTime;
float g_fTimeSinceLastReport = 0.0;
int g_nFrameCount = 0;
bool g_bRenderInWireFrame = false;
bool g_bUseVertexArrayRange = true;
bool g_bRenderQuad = false;
int g_nTotalBufferSize = 0;
GLfloat *g_pAGPMemoryBuffer = NULL;
int g_nMemoryOffsetForQuad = 0;
// GL_T2F_N3F_V3F
struct Vertex
{
GLfloat tu, tv;
GLfloat nx, ny, nz;
GLfloat vx, vy, vz;
};
// Textured sphere
GLuint g_nPrecision = 500;
GLuint g_nNumSphereVertices;
Vertex *g_pSphereVertices = NULL;
// Textured quad
int g_nNumQuadTriangles = 2;
int g_nNumQuadVertices = 4;
int g_nNumQuadIndices = 6;
GLubyte g_pQuadIndices[] =
{
0, 1, 2, // Tri 1
1, 3, 2 // Tri 2
};
Vertex g_pQuadVertices[] =
{
{ 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, -1.0f, -1.0f, 0.0f },
{ 1.0f, 0.0f, 0.0f, 0.0f, 1.0f, 1.0f, -1.0f, 0.0f },
{ 0.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, 0.0f }
};
//-----------------------------------------------------------------------------
// PROTOTYPES
//-----------------------------------------------------------------------------
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow);
LRESULT CALLBACK WindowProc(HWND g_hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
void loadTexture(void);
void init(void);
void render(void);
void shutDown(void);
GLvoid createSphereGeometry( float cx, float cy, float cz, float r, int n);
GLvoid setVertData(int index,float tu, float tv, float nx, float ny, float nz,
float vx, float vy, float vz);
//-----------------------------------------------------------------------------
// Name: WinMain()
// Desc: The application's entry point
//-----------------------------------------------------------------------------
int WINAPI WinMain( HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow )
{
WNDCLASSEX winClass;
MSG uMsg;
memset(&uMsg,0,sizeof(uMsg));
winClass.lpszClassName = "MY_WINDOWS_CLASS";
winClass.cbSize = sizeof(WNDCLASSEX);
winClass.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
winClass.lpfnWndProc = WindowProc;
winClass.hInstance = hInstance;
winClass.hIcon = LoadIcon(hInstance, (LPCTSTR)IDI_OPENGL_ICON);
winClass.hIconSm = LoadIcon(hInstance, (LPCTSTR)IDI_OPENGL_ICON);
winClass.hCursor = LoadCursor(NULL, IDC_ARROW);
winClass.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
winClass.lpszMenuName = NULL;
winClass.cbClsExtra = 0;
winClass.cbWndExtra = 0;
if( !RegisterClassEx(&winClass) )
return E_FAIL;
g_hWnd = CreateWindowEx( NULL, "MY_WINDOWS_CLASS",
"OpenGL - Vertex Array Range (NV_vertex_array_range - nVIDIA only) ",
WS_OVERLAPPEDWINDOW | WS_VISIBLE,
0, 0, 640, 480, NULL, NULL, hInstance, NULL );
if( g_hWnd == NULL )
return E_FAIL;
ShowWindow( g_hWnd, nCmdShow );
UpdateWindow( g_hWnd );
init();
while( uMsg.message != WM_QUIT )
{
if( PeekMessage( &uMsg, NULL, 0, 0, PM_REMOVE ) )
{
TranslateMessage( &uMsg );
DispatchMessage( &uMsg );
}
else
render();
}
shutDown();
UnregisterClass( "MY_WINDOWS_CLASS", winClass.hInstance );
return uMsg.wParam;
}
//-----------------------------------------------------------------------------
// Name: WindowProc()
// Desc: The window's message handler
//-----------------------------------------------------------------------------
LRESULT CALLBACK WindowProc( HWND g_hWnd,
UINT msg,
WPARAM wParam,
LPARAM lParam )
{
switch( msg )
{
case WM_KEYDOWN:
{
switch( wParam )
{
case VK_ESCAPE:
PostQuitMessage(0);
break;
case VK_F1:
g_bUseVertexArrayRange = !g_bUseVertexArrayRange;
break;
case VK_F2:
g_bRenderQuad = !g_bRenderQuad;
break;
case VK_F3:
g_bRenderInWireFrame = !g_bRenderInWireFrame;
if( g_bRenderInWireFrame == true )
glPolygonMode( GL_FRONT_AND_BACK, GL_LINE );
else
glPolygonMode( GL_FRONT_AND_BACK, GL_FILL );
break;
}
}
break;
评论0