NeHe Tutorials OpenGL
 X1. OpenGL

, Quake Unreal. , DirectInput OpenGL Windows. 10. .

 

 

, , X Y .

 

typedef struct tagSECTOR

{

int numtriangles;

TRIANGLE* triangle;

} SECTOR;

 

SECTOR sector1; //

 

POINT mpos; // ()

 

LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); // WndProc

 

, , mpos. POINT x y, , . , CreateGLWindow() , .

 

ShowCursor(FALSE); // ()

if (fullscreen) // ?

{

dwExStyle=WS_EX_APPWINDOW;

dwStyle=WS_POPUP;

}

 

ShowCursor(FALSE) , , . , WinMain() :

 

SwapBuffers(hDC); // ( )

GetCursorPos(&mpos); // ()

SetCursorPos(320,240); // ()

heading += (float)(320 - mpos.x)/100 * 5;// ()

yrot = heading; // Y ()

lookupdown -= (float)(240 - mpos.y)/100 * 5;// X ()

 

GetCursorPos(POINT p). , . SetCursorPos(int X, int Y).

 

: 0,0! , , 0,0 . 320, 240 640480.

 

, .

 

float = (P - CX) / U * S;

 

P ,

CX

U

S ( 12).

 

heading lookupdown.

 

. .

 

(DirectX 7)

 

. , . , .

 

, DirectX7. . , Visual C++, .

 

DirectX SDK, , , MicroSoft, .

 

, VisualStudio Project->Settings. Link Object/libraty modules dinput.lib dxguid.lib winmm.lib. DirectInput, DirectX GUID Windows Multimedia , . , Tools->Options Directories (Include files Library files) DirectX SDK .

 

DirectInput , !

 

DirectInput, . DirectInput DirectInput . :

 

#include <stdio.h> // /

#include <gl\gl.h> // OpenGL32

#include <gl\glu.h> // GLu32

#include <gl\glaux.h> // Glaux

#include <dinput.h> // DirectInput ()

 

LPDIRECTINPUT7 g_DI; // DirectInput ()

LPDIRECTINPUTDEVICE7 g_KDIDev; // ()

 

 

DirectInput (g_DI) (g_KDIDev), . DirectInput Windows.

 

Windows DirectInput

VK_LEFT DIK_LEFT

VK_RIGHT DIK_RIGHT

...

 

VK DIK. . DIK dinput.h.

 

DirectInput . CreateGLWindow() :

 

 

// DirectInput ()

int DI_Init()

{

// DirectInput

if ( DirectInputCreateEx( hInstance, // Instance

DIRECTINPUT_VERSION, // DirectInput

IID_IDirectInput7,

(void**)&g_DI, // DirectInput

NULL ) ) // NULL

{

return(false); // DirectInput

}

 

//

if ( g_DI->CreateDeviceEx( GUID_SysKeyboard,

// (, )

IID_IDirectInputDevice7,

(void**)&g_KDIDev, //

NULL ) ) // NULL

{

return(false); //

}

 

//

if ( g_KDIDev->SetDataFormat(&c_dfDIKeyboard) )

{

return(false); //

// DirectInput

}

 

//

if ( g_KDIDev->SetCooperativeLevel(hWnd, DISCL_FOREGROUND | DISCL_EXCLUSIVE) )

{

return(false); //

// DirectInput

}

 

if (g_KDIDev) // ? ( )

g_KDIDev->Acquire(); //

else //

return(false); // false

 

return(true); //

}

 

// DirectInput

void DX_End()

{

if (g_DI)

{

if (g_KDIDev)

{

g_KDIDev->Unacquire();

g_KDIDev->Release();

g_KDIDev = NULL;

}

 

g_DI->Release();

g_DI = NULL;

}

}

 

. DirectInput , . DirectInput , Windows .

 

, DirectInput. , .

 

WndProc():

 

case WM_KEYDOWN: // ?

{

keys[wParam] = TRUE; //

return 0; //

}

 

case WM_KEYUP: // ?

{

keys[wParam] = FALSE; //

return 0; //

}

 

:

 

BYTE buffer[256]; // Keys[] ()

bool active=TRUE; //

bool fullscreen=TRUE; //

bool blend; // ON/OFF

bool bp; //

bool fp; // F1 ()

 

...

 

GLfloat lookupdown = 0.0f;

GLfloat z=0.0f; //

GLuint filter; // ()

 

GLuint texture[5]; // ()

 

WinMain()

 

// OpenGL

if (!CreateGLWindow("Justin Eslinger's & NeHe's Advanced DirectInput Tutorial",640,480,16,fullscreen)) // ()

{

return 0; // ,

}

 

if (!DI_Init()) // DirectInput ()

{

return 0;

}

 

...

 

// , Esc

if ((active && !DrawGLScene())) // ()

 

...

// ()

HRESULT hr = g_KDIDev->GetDeviceState(sizeof(buffer), &buffer);

if ( buffer[DIK_ESCAPE] & 0x80 ) // Escape ()

{

done=TRUE;

}

 

if ( buffer[DIK_B] & 0x80) // B? ()

{

if (!bp)

{

bp = true; // ()

blend=!blend;

if (!blend)

{

glDisable(GL_BLEND);

glEnable(GL_DEPTH_TEST);

}

else

{

glEnable(GL_BLEND);

glDisable(GL_DEPTH_TEST);

}

}

}

else

{

bp = false;

}

 

if ( buffer[DIK_PRIOR] & 0x80 ) // Page Up? ()

{

z-=0.02f;

}

 

if ( buffer[DIK_NEXT] & 0x80 ) // Page Down? ()

{

z+=0.02f;

}

 

if ( buffer[DIK_UP] & 0x80 ) // ? ()

{

xpos -= (float)sin(heading*piover180) * 0.05f;

zpos -= (float)cos(heading*piover180) * 0.05f;

if (walkbiasangle >= 359.0f)

{

walkbiasangle = 0.0f;

}

else

{

walkbiasangle+= 10;

}

 

walkbias = (float)sin(walkbiasangle * piover180)/20.0f;

}

 

if ( buffer[DIK_DOWN] & 0x80 ) // ? ()

{

xpos += (float)sin(heading*piover180) * 0.05f;

zpos += (float)cos(heading*piover180) * 0.05f;

if (walkbiasangle <= 1.0f)

{

walkbiasangle = 359.0f;

}

else

{

walkbiasangle-= 10;

}

 

walkbias = (float)sin(walkbiasangle * piover180)/20.0f;

}

 

if ( buffer[DIK_LEFT] & 0x80 ) // ? ()

{

xpos += (float)sin((heading - 90)*piover180) * 0.05f; ( Modified )

zpos += (float)cos((heading - 90)*piover180) * 0.05f; ( Modified )

if (walkbiasangle <= 1.0f)

{

walkbiasangle = 359.0f;

}

else

{

walkbiasangle-= 10;

}

walkbias = (float)sin(walkbiasangle * piover180)/20.0f;

}

 

if ( buffer[DIK_RIGHT] & 0x80 ) // ? ()

{

xpos += (float)sin((heading + 90)*piover180) * 0.05f; ( Modified )

zpos += (float)cos((heading + 90)*piover180) * 0.05f; ( Modified )

if (walkbiasangle <= 1.0f)

{

walkbiasangle = 359.0f;

}

else

{

walkbiasangle-= 10;

}

walkbias = (float)sin(walkbiasangle * piover180)/20.0f;

}

 

if ( buffer[DIK_F1] & 0x80) // F1 ? ()

{

if (!fp) // ()

{

fp = true; // F1 ()

KillGLWindow(); // ()

fullscreen=!fullscreen; // ()

 

//

if (!CreateGLWindow("Justin Eslinger's & NeHe's Advanced Direct Input Tutorial",640,480,16,fullscreen)) ()

{

return 0; // , ()

}

 

if (!DI_Init()) // DirectInput ()

{

return 0; // ,

}

}

}

else

{

fp = false; // F1

}

 

// Shutdown

//

DX_End(); // DirectInput ()

KillGLWindow(); //

return (msg.wParam); //

}

 

DrawGLScene() .

 

glTranslatef(xtrans, ytrans, ztrans);

numtriangles = sector1.numtriangles;

 

//

for (int loop_m = 0; loop_m < numtriangles; loop_m++)

{

glBindTexture(GL_TEXTURE_2D, texture[sector1.triangle[loop_m].texture]); // ()

 

glBegin(GL_TRIANGLES);

 

 

, . , Windows DirectInput. .

 

DirectInput, ? , . , - .

 

.

 

POINT mpos;

int adjust = 5; // ()

 

// ()

struct

{

__int64 frequency; //

float resolution; //

unsigned long mm_timer_start; //

unsigned long mm_timer_elapsed; //

bool performance_timer; //

__int64 performance_timer_start; //

__int64 performance_timer_elapsed; //

} timer;

 

LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);

 

21. .

 

// ()

void TimerInit(void)

{

memset(&timer, 0, sizeof(timer)); //

//

if (!QueryPerformanceFrequency((LARGE_INTEGER *) &timer.frequency))

{

//

timer.performance_timer = FALSE;

timer.mm_timer_start = timeGetTime(); // timeGetTime()

timer.resolution = 1.0f/1000.0f; // .001f

timer.frequency = 1000; // 1000

//

timer.mm_timer_elapsed = timer.mm_timer_start;

}

else

{

//

QueryPerformanceCounter((LARGE_INTEGER *) &timer.performance_timer_start);

timer.performance_timer = TRUE;

 

//

timer.resolution = (float) (((double)1.0f)/((double)timer.frequency));

//

timer.performance_timer_elapsed = timer.performance_timer_start;

}

}

 

// ()

float TimerGetTime()

{

__int64 time; // 64-

if (timer.performance_timer) //

{

//

QueryPerformanceCounter((LARGE_INTEGER *) &time);

//

return ( (float) ( time - timer.performance_timer_start) * timer.resolution)*1000.0f;

}

else

{

//

return( (float) ( timeGetTime() - timer.mm_timer_start) * timer.resolution)*1000.0f;

}

}

 

, 21- . , winmm.lib.

 

- WinMain().

 

if (!DI_Init()) // DirectInput ()

{

return 0;

}

 

TimerInit(); // ()

 

...

 

float start=TimerGetTime();

// ()

 

// . Esc .

// ()

if ((active && !DrawGLScene()))

{

done=TRUE; // ESC DrawGLScene

}

else //

{

// ()

while(TimerGetTime()<start+float(adjust*2.0f)) {}

 

. , 10.

 

, , . Data.

 

tagTriangle.

 

typedef struct tagTRIANGLE

{

int texture;// ()

VERTEX vertex[3];

} TRIANGLE;

 

SetupWorld

 

for (int loop = 0; loop < numtriangles; loop++)

{

readstr(filein,oneline);// ()

sscanf(oneline, "%i\n", &sector1.triangle[loop].texture); // ()

for (int vert = 0; vert < 3; vert++)

{

 

DrawGLScene

 

//

for (int loop_m = 0; loop_m < numtriangles; loop_m++)

{

// ()

glBindTexture(GL_TEXTURE_2D, texture[sector1.triangle[loop_m].texture]);

 

glBegin(GL_TRIANGLES);

 

LoadGLTextures

 

int LoadGLTextures() //

{

int Status=FALSE; //

AUX_RGBImageRec *TextureImage[5]; //

memset(TextureImage,0,sizeof(void *)*2); // NULL

if( (TextureImage[0]=LoadBMP("Data/floor1.bmp")) && //

(TextureImage[1]=LoadBMP("Data/light1.bmp"))&& //

(TextureImage[2]=LoadBMP("Data/rustyblue.bmp"))&& //

(TextureImage[3]=LoadBMP("Data/crate.bmp")) && //

(TextureImage[4]=LoadBMP("Data/weirdbrick.bmp"))) //

{

Status=TRUE;

glGenTextures(5, &texture[0]); //

for (int loop1=0; loop1<5; loop1++)

{

glBindTexture(GL_TEXTURE_2D, texture[loop1]);

glTexImage2D(GL_TEXTURE_2D, 0, 3, TextureImage[loop1]->sizeX,
TextureImage[loop1]->sizeY, 0,

GL_RGB, GL_UNSIGNED_BYTE, TextureImage[loop1]->data);

glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);

glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);

}

for (loop1=0; loop1<5; loop1++)

{

if (TextureImage[loop1]->data)

{

free(TextureImage[loop1]->data);

}

free(TextureImage[loop1]);

}

}

return Status; //

}

 

DirectInput. , . , !

 

:

 

nehex1.rar (305 Kb)

Justin Eslinger (BlackScar)
blackscar@ticz.com
http://members.xoom.com/Blackscar/
 

PMG  25 2002 (c)  Cupper