0001:
0002:
0003:
0004:
0005:
0006: #pragma once
0007: #include <windows.h>
0008: #include "GLApp.h"
0009: #include "main.h"
0010:
0011:
0012:
0013:
0014: CMyD3DApplication* g_pApp = NULL;
0015: HINSTANCE g_hInst = NULL;
0016:
0017:
0018:
0019:
0020:
0021:
0022:
0023: INT WINAPI WinMain( HINSTANCE hInst, HINSTANCE, LPSTR, INT nCmdShow)
0024: {
0025: CMyD3DApplication d3dApp;
0026:
0027: g_pApp = &d3dApp;
0028: g_hInst = hInst;
0029:
0030: if( FAILED( d3dApp.Create( hInst, nCmdShow ) ) )
0031: return 0;
0032:
0033: return d3dApp.Run();
0034: }
0035:
0036:
0037:
0038:
0039:
0040:
0041:
0042:
0043: CMyD3DApplication::CMyD3DApplication()
0044: {
0045: m_hProgramObject = 0;
0046:
0047: m_dwCreationWidth = 512;
0048: m_dwCreationHeight = 512;
0049: m_strWindowTitle = TEXT( "main" );
0050:
0051: m_bLoadingApp = TRUE;
0052: }
0053:
0054:
0055:
0056:
0057:
0058:
0059:
0060:
0061: CMyD3DApplication::~CMyD3DApplication()
0062: {
0063: }
0064:
0065:
0066:
0067:
0068:
0069:
0070:
0071:
0072:
0073:
0074: HRESULT CMyD3DApplication::OneTimeSceneInit()
0075: {
0076:
0077: SendMessage( m_hWnd, WM_PAINT, 0, 0 );
0078:
0079: m_bLoadingApp = FALSE;
0080:
0081: return S_OK;
0082: }
0083:
0084:
0085:
0086:
0087:
0088:
0089:
0090:
0091: HRESULT CMyD3DApplication::ConfirmDevice( void* pCaps,
0092: DWORD dwBehavior, void* Format )
0093: {
0094: return S_OK;
0095: }
0096:
0097:
0098:
0099:
0100:
0101:
0102:
0103:
0104:
0105:
0106:
0107:
0108: HRESULT CMyD3DApplication::InitDeviceObjects()
0109: {
0110: HRESULT hr;
0111:
0112:
0113: hr = GLSL::Init();
0114: if (FAILED(hr)) {
0115: MessageBox( NULL, "Faild to initialize the shader system.", "ERROR", MB_OK);
0116: return FALSE;
0117: }
0118:
0119:
0120: hr = GLSL::CreateShaderFromFile("GLSL", &m_hProgramObject);
0121: if (FAILED(hr)) {
0122: MessageBox( NULL, "Cannot install shader", "ERROR", MB_OK);
0123: return FALSE;
0124: }
0125:
0126:
0127: m_locLightPos = glGetUniformLocationARB(m_hProgramObject, "LightPos");
0128:
0129:
0130: return S_OK;
0131: }
0132:
0133:
0134:
0135:
0136:
0137:
0138:
0139:
0140: HRESULT CMyD3DApplication::DeleteDeviceObjects()
0141: {
0142: if (m_hProgramObject){glDeleteObjectARB(m_hProgramObject);m_hProgramObject=NULL;}
0143:
0144: return S_OK;
0145: }
0146:
0147:
0148:
0149:
0150:
0151:
0152:
0153:
0154:
0155: HRESULT CMyD3DApplication::RestoreDeviceObjects()
0156: {
0157:
0158: GLdouble aspect = ((FLOAT)m_mysdBackBuffer.Width) / m_mysdBackBuffer.Height;
0159: glMatrixMode( GL_PROJECTION );
0160: glLoadIdentity();
0161: gluPerspective( 60.0, aspect, 1.0, 10.0 );
0162:
0163:
0164: glMatrixMode( GL_MODELVIEW );
0165:
0166: return S_OK;
0167: }
0168:
0169:
0170:
0171:
0172:
0173:
0174:
0175:
0176: HRESULT CMyD3DApplication::InvalidateDeviceObjects()
0177: {
0178: return S_OK;
0179: }
0180:
0181:
0182:
0183:
0184:
0185:
0186:
0187:
0188: HRESULT CMyD3DApplication::FinalCleanup()
0189: {
0190: return S_OK;
0191: }
0192:
0193:
0194:
0195:
0196:
0197: float rot = 0;
0198:
0199:
0200:
0201:
0202:
0203: HRESULT CMyD3DApplication::FrameMove()
0204: {
0205: rot = this->m_fTime;
0206:
0207: return S_OK;
0208: }
0209:
0210:
0211:
0212:
0213: GLfloat CubeNormals[6][3] =
0214: {
0215: {+1.0, 0.0, 0.0}, {0.0, +1.0, 0.0}, {-1.0, 0.0, 0.0},
0216: { 0.0, -1.0, 0.0}, {0.0, 0.0,+1.0}, {0.0, 0.0, -1.0}
0217: };
0218:
0219: GLint CubeFaces[6][4] =
0220: {
0221: {0, 1, 2, 3}, {3, 2, 6, 7}, {7, 6, 5, 4},
0222: {4, 5, 1, 0}, {5, 6, 2, 1}, {7, 4, 0, 3}
0223: };
0224:
0225: GLfloat CubeVertices[8][3];
0226:
0227: static void InitializeCube(GLfloat v[8][3])
0228: {
0229: v[0][0] = v[1][0] = v[2][0] = v[3][0] = -1;
0230: v[4][0] = v[5][0] = v[6][0] = v[7][0] = 1;
0231: v[0][1] = v[1][1] = v[4][1] = v[5][1] = -1;
0232: v[2][1] = v[3][1] = v[6][1] = v[7][1] = 1;
0233: v[0][2] = v[3][2] = v[4][2] = v[7][2] = 1;
0234: v[1][2] = v[2][2] = v[5][2] = v[6][2] = -1;
0235: }
0236:
0237:
0238:
0239:
0240:
0241: HRESULT CMyD3DApplication::Render(void)
0242: {
0243: static int init=0;
0244: if(!init){init=1;
0245:
0246: InitializeCube(CubeVertices);
0247: }
0248:
0249:
0250: glClearColor(0,0,0,0);
0251: glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
0252:
0253:
0254: glLoadIdentity();
0255:
0256: gluLookAt( 0.0, 3.0, 5.0,
0257: 0.0, 0.0, 0.0,
0258: 0.0, 1.0, 0.0);
0259:
0260:
0261: glUseProgramObjectARB(m_hProgramObject);
0262:
0263:
0264: float lightPosition[3] = {2.0f, 8.0f, -10.0f };
0265: glUniform3fvARB(m_locLightPos, 1, &lightPosition[0]);
0266:
0267:
0268: glPushMatrix();
0269: glRotated(100*rot, 0.0, 1.0, 0.0);
0270:
0271: for(int i = 0; i < 6; i++) {
0272: glBegin(GL_QUADS);
0273:
0274: glNormal3fv(&CubeNormals[i][0]);
0275: glColor3d(CubeVertices[CubeFaces[i][0]][0]*0.5+0.5
0276: , CubeVertices[CubeFaces[i][0]][1]*0.5+0.5
0277: , CubeVertices[CubeFaces[i][0]][2]*0.5+0.5);
0278: glVertex3fv(&CubeVertices[CubeFaces[i][0]][0]);
0279: glColor3d(CubeVertices[CubeFaces[i][1]][0]*0.5+0.5
0280: , CubeVertices[CubeFaces[i][1]][1]*0.5+0.5
0281: , CubeVertices[CubeFaces[i][1]][2]*0.5+0.5);
0282: glVertex3fv(&CubeVertices[CubeFaces[i][1]][0]);
0283: glColor3d(CubeVertices[CubeFaces[i][2]][0]*0.5+0.5
0284: , CubeVertices[CubeFaces[i][2]][1]*0.5+0.5
0285: , CubeVertices[CubeFaces[i][2]][2]*0.5+0.5);
0286: glVertex3fv(&CubeVertices[CubeFaces[i][2]][0]);
0287: glColor3d(CubeVertices[CubeFaces[i][3]][0]*0.5+0.5
0288: , CubeVertices[CubeFaces[i][3]][1]*0.5+0.5
0289: , CubeVertices[CubeFaces[i][3]][2]*0.5+0.5);
0290: glVertex3fv(&CubeVertices[CubeFaces[i][3]][0]);
0291:
0292: glEnd();
0293: }
0294:
0295: glPopMatrix();
0296:
0297: return S_OK;
0298: }
0299:
0300:
0301:
0302:
0303: LRESULT CMyD3DApplication::MsgProc( HWND hWnd, UINT msg,
0304: WPARAM wParam, LPARAM lParam )
0305: {
0306: switch( msg )
0307: {
0308: case WM_PAINT:
0309: {
0310: if( m_bLoadingApp )
0311: {
0312:
0313: HDC hDC = GetDC( hWnd );
0314: TCHAR strMsg[MAX_PATH];
0315: wsprintf(strMsg, TEXT("Loading... Please wait"));
0316: RECT rct;
0317: GetClientRect( hWnd, &rct );
0318: DrawText( hDC, strMsg, -1, &rct
0319: , DT_CENTER|DT_VCENTER|DT_SINGLELINE );
0320: ReleaseDC( hWnd, hDC );
0321: }
0322: break;
0323: }
0324:
0325: }
0326: return CD3DApplication::MsgProc( hWnd, msg, wParam, lParam );
0327: }
0328:
0329:
0330: