Subversion Repositories gelsvn

Rev

Rev 369 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
369 jab 1
#include <string>
2
#include <iostream>
3
 
4
#include <windows.h>
5
#include <shellapi.h>
373 jrf 6
#include <gl/glew.h>
7
#include <time.h>
369 jab 8
 
9
#include "SOIL.h"
10
 
11
LRESULT CALLBACK WindowProc(HWND, UINT, WPARAM, LPARAM);
12
void EnableOpenGL(HWND hwnd, HDC*, HGLRC*);
13
void DisableOpenGL(HWND, HDC, HGLRC);
14
 
15
int WINAPI WinMain(HINSTANCE hInstance,
16
                   HINSTANCE hPrevInstance,
17
                   LPSTR lpCmdLine,
18
                   int nCmdShow)
19
{
20
    WNDCLASSEX wcex;
21
    HWND hwnd;
22
    HDC hDC;
23
    HGLRC hRC;
24
    MSG msg;
25
    BOOL bQuit = FALSE;
26
    float theta = 0.0f;
27
 
28
    // register window class
29
    wcex.cbSize = sizeof(WNDCLASSEX);
30
    wcex.style = CS_OWNDC;
31
    wcex.lpfnWndProc = WindowProc;
32
    wcex.cbClsExtra = 0;
33
    wcex.cbWndExtra = 0;
34
    wcex.hInstance = hInstance;
35
    wcex.hIcon = LoadIcon(NULL, IDI_APPLICATION);
36
    wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
37
    wcex.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
38
    wcex.lpszMenuName = NULL;
39
    wcex.lpszClassName = "GLSample";
40
    wcex.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
41
 
42
 
43
    if (!RegisterClassEx(&wcex))
44
        return 0;
45
 
46
    // create main window
47
    hwnd = CreateWindowEx(0,
48
                          "GLSample",
49
                          "SOIL Sample",
50
                          WS_OVERLAPPEDWINDOW,
51
                          CW_USEDEFAULT,
52
                          CW_USEDEFAULT,
53
                          512,
54
                          512,
55
                          NULL,
56
                          NULL,
57
                          hInstance,
58
                          NULL);
59
 
60
    ShowWindow(hwnd, nCmdShow);
61
 
62
    //	check my error handling
63
    /*
64
    SOIL_load_OGL_texture( "img_test.png", SOIL_LOAD_AUTO, SOIL_CREATE_NEW_ID, 0 );
65
    std::cout << "'" << SOIL_last_result() << "'" << std::endl;
66
    */
67
 
68
 
69
    // enable OpenGL for the window
70
    EnableOpenGL(hwnd, &hDC, &hRC);
71
 
72
    glEnable( GL_BLEND );
73
    //glDisable( GL_BLEND );
74
    //	straight alpha
75
    glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
76
    //	premultiplied alpha (remember to do the same in glColor!!)
77
    //glBlendFunc( GL_ONE, GL_ONE_MINUS_SRC_ALPHA );
78
 
79
    //	do I want alpha thresholding?
80
    glEnable( GL_ALPHA_TEST );
81
    glAlphaFunc( GL_GREATER, 0.5f );
82
 
83
    //	log what the use is asking us to load
84
    std::string load_me = lpCmdLine;
85
    if( load_me.length() > 2 )
86
    {
87
		//load_me = load_me.substr( 1, load_me.length() - 2 );
88
		load_me = load_me.substr( 0, load_me.length() - 0 );
89
    } else
90
    {
91
    	//load_me = "img_test_uncompressed.dds";
92
    	//load_me = "img_test_indexed.tga";
93
    	//load_me = "img_test.dds";
94
    	load_me = "img_test.png";
95
    	//load_me = "odd_size.jpg";
96
    	//load_me = "img_cheryl.jpg";
97
    	//load_me = "oak_odd.png";
98
    	//load_me = "field_128_cube.dds";
99
    	//load_me = "field_128_cube_nomip.dds";
100
    	//load_me = "field_128_cube_uc.dds";
101
    	//load_me = "field_128_cube_uc_nomip.dds";
102
    	//load_me = "Goblin.dds";
103
    	//load_me = "parquet.dds";
104
    	//load_me = "stpeters_probe.hdr";
105
    	//load_me = "VeraMoBI_sdf.png";
106
 
107
    	//	for testing the texture rectangle code
108
    	//load_me = "test_rect.png";
109
    }
110
	std::cout << "'" << load_me << "'" << std::endl;
111
 
112
	//	1st try to load it as a single-image-cubemap
113
	//	(note, need DDS ordered faces: "EWUDNS")
114
	GLuint tex_ID;
115
    int time_me;
116
 
117
    std::cout << "Attempting to load as a cubemap" << std::endl;
118
    time_me = clock();
119
	tex_ID = SOIL_load_OGL_single_cubemap(
120
			load_me.c_str(),
121
			SOIL_DDS_CUBEMAP_FACE_ORDER,
122
			SOIL_LOAD_AUTO,
123
			SOIL_CREATE_NEW_ID,
124
			SOIL_FLAG_POWER_OF_TWO
125
			| SOIL_FLAG_MIPMAPS
126
			//| SOIL_FLAG_COMPRESS_TO_DXT
127
			//| SOIL_FLAG_TEXTURE_REPEATS
128
			//| SOIL_FLAG_INVERT_Y
129
			| SOIL_FLAG_DDS_LOAD_DIRECT
130
			);
131
	time_me = clock() - time_me;
132
	std::cout << "the load time was " << 0.001f * time_me << " seconds (warning: low resolution timer)" << std::endl;
133
    if( tex_ID > 0 )
134
    {
135
    	glEnable( GL_TEXTURE_CUBE_MAP );
136
		glEnable( GL_TEXTURE_GEN_S );
137
		glEnable( GL_TEXTURE_GEN_T );
138
		glEnable( GL_TEXTURE_GEN_R );
139
		glTexGeni( GL_S, GL_TEXTURE_GEN_MODE, GL_REFLECTION_MAP );
140
		glTexGeni( GL_T, GL_TEXTURE_GEN_MODE, GL_REFLECTION_MAP );
141
		glTexGeni( GL_R, GL_TEXTURE_GEN_MODE, GL_REFLECTION_MAP );
142
		glBindTexture( GL_TEXTURE_CUBE_MAP, tex_ID );
143
		//	report
144
		std::cout << "the loaded single cube map ID was " << tex_ID << std::endl;
145
		//std::cout << "the load time was " << 0.001f * time_me << " seconds (warning: low resolution timer)" << std::endl;
146
    } else
147
    {
148
    	std::cout << "Attempting to load as a HDR texture" << std::endl;
149
		time_me = clock();
150
		tex_ID = SOIL_load_OGL_HDR_texture(
151
				load_me.c_str(),
152
				//SOIL_HDR_RGBE,
153
				//SOIL_HDR_RGBdivA,
154
				SOIL_HDR_RGBdivA2,
155
				0,
156
				SOIL_CREATE_NEW_ID,
157
				SOIL_FLAG_POWER_OF_TWO
158
				| SOIL_FLAG_MIPMAPS
159
				//| SOIL_FLAG_COMPRESS_TO_DXT
160
				);
161
		time_me = clock() - time_me;
162
		std::cout << "the load time was " << 0.001f * time_me << " seconds (warning: low resolution timer)" << std::endl;
163
 
164
		//	did I fail?
165
		if( tex_ID < 1 )
166
		{
167
			//	loading of the single-image-cubemap failed, try it as a simple texture
168
			std::cout << "Attempting to load as a simple 2D texture" << std::endl;
169
			//	load the texture, if specified
170
			time_me = clock();
171
			tex_ID = SOIL_load_OGL_texture(
172
					load_me.c_str(),
173
					SOIL_LOAD_AUTO,
174
					SOIL_CREATE_NEW_ID,
175
					SOIL_FLAG_POWER_OF_TWO
176
					| SOIL_FLAG_MIPMAPS
177
					//| SOIL_FLAG_MULTIPLY_ALPHA
178
					//| SOIL_FLAG_COMPRESS_TO_DXT
179
					| SOIL_FLAG_DDS_LOAD_DIRECT
180
					//| SOIL_FLAG_NTSC_SAFE_RGB
181
					//| SOIL_FLAG_CoCg_Y
182
					//| SOIL_FLAG_TEXTURE_RECTANGLE
183
					);
184
			time_me = clock() - time_me;
185
			std::cout << "the load time was " << 0.001f * time_me << " seconds (warning: low resolution timer)" << std::endl;
186
		}
187
 
188
		if( tex_ID > 0 )
189
		{
190
			//	enable texturing
191
			glEnable( GL_TEXTURE_2D );
192
			//glEnable( 0x84F5 );// enables texture rectangle
193
			//  bind an OpenGL texture ID
194
			glBindTexture( GL_TEXTURE_2D, tex_ID );
195
			//	report
196
			std::cout << "the loaded texture ID was " << tex_ID << std::endl;
197
			//std::cout << "the load time was " << 0.001f * time_me << " seconds (warning: low resolution timer)" << std::endl;
198
		} else
199
		{
200
			//	loading of the texture failed...why?
201
			glDisable( GL_TEXTURE_2D );
202
			std::cout << "Texture loading failed: '" << SOIL_last_result() << "'" << std::endl;
203
		}
204
    }
205
 
206
    // program main loop
207
    const float ref_mag = 0.1f;
208
    while (!bQuit)
209
    {
210
        // check for messages
211
        if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
212
        {
213
            // handle or dispatch messages
214
            if (msg.message == WM_QUIT)
215
            {
216
                bQuit = TRUE;
217
            }
218
            else
219
            {
220
                TranslateMessage(&msg);
221
                DispatchMessage(&msg);
222
            }
223
        }
224
        else
225
        {
226
            // OpenGL animation code goes here
373 jrf 227
            theta = clock() * 0.1f;
369 jab 228
 
229
            float tex_u_max = 1.0f;//0.2f;
230
            float tex_v_max = 1.0f;//0.2f;
231
 
232
            glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
233
            glClear(GL_COLOR_BUFFER_BIT);
234
 
235
            glPushMatrix();
236
            glScalef( 0.8f, 0.8f, 0.8f );
237
            //glRotatef(-0.314159f*theta, 0.0f, 0.0f, 1.0f);
238
			glColor4f( 1.0f, 1.0f, 1.0f, 1.0f );
239
			glNormal3f( 0.0f, 0.0f, 1.0f );
240
            glBegin(GL_QUADS);
241
				glNormal3f( -ref_mag, -ref_mag, 1.0f );
242
                glTexCoord2f( 0.0f, tex_v_max );
243
                glVertex3f( -1.0f, -1.0f, -0.1f );
244
 
245
                glNormal3f( ref_mag, -ref_mag, 1.0f );
246
                glTexCoord2f( tex_u_max, tex_v_max );
247
                glVertex3f( 1.0f, -1.0f, -0.1f );
248
 
249
                glNormal3f( ref_mag, ref_mag, 1.0f );
250
                glTexCoord2f( tex_u_max, 0.0f );
251
                glVertex3f( 1.0f, 1.0f, -0.1f );
252
 
253
                glNormal3f( -ref_mag, ref_mag, 1.0f );
254
                glTexCoord2f( 0.0f, 0.0f );
255
                glVertex3f( -1.0f, 1.0f, -0.1f );
256
            glEnd();
257
            glPopMatrix();
258
 
259
			tex_u_max = 1.0f;
260
            tex_v_max = 1.0f;
261
            glPushMatrix();
262
            glScalef( 0.8f, 0.8f, 0.8f );
263
            glRotatef(theta, 0.0f, 0.0f, 1.0f);
264
			glColor4f( 1.0f, 1.0f, 1.0f, 1.0f );
265
			glNormal3f( 0.0f, 0.0f, 1.0f );
266
            glBegin(GL_QUADS);
267
                glTexCoord2f( 0.0f, tex_v_max );		glVertex3f( 0.0f, 0.0f, 0.1f );
268
                glTexCoord2f( tex_u_max, tex_v_max );		glVertex3f( 1.0f, 0.0f, 0.1f );
269
                glTexCoord2f( tex_u_max, 0.0f );		glVertex3f( 1.0f, 1.0f, 0.1f );
270
                glTexCoord2f( 0.0f, 0.0f );		glVertex3f( 0.0f, 1.0f, 0.1f );
271
            glEnd();
272
            glPopMatrix();
273
 
274
            {
275
				/*	check for errors	*/
276
				GLenum err_code = glGetError();
277
				while( GL_NO_ERROR != err_code )
278
				{
279
					printf( "OpenGL Error @ %s: %i", "drawing loop", err_code );
280
					err_code = glGetError();
281
				}
282
			}
283
 
284
            SwapBuffers(hDC);
285
 
286
            Sleep (1);
287
        }
288
    }
289
 
290
    //	and show off the screenshot capability
291
    /*
292
    load_me += "-screenshot.tga";
293
    SOIL_save_screenshot( load_me.c_str(), SOIL_SAVE_TYPE_TGA, 0, 0, 512, 512 );
294
    //*/
295
    //*
296
    load_me += "-screenshot.bmp";
297
    SOIL_save_screenshot( load_me.c_str(), SOIL_SAVE_TYPE_BMP, 0, 0, 512, 512 );
298
    //*/
299
    /*
300
    load_me += "-screenshot.dds";
301
    SOIL_save_screenshot( load_me.c_str(), SOIL_SAVE_TYPE_DDS, 0, 0, 512, 512 );
302
    //*/
303
 
304
    // shutdown OpenGL
305
    DisableOpenGL(hwnd, hDC, hRC);
306
 
307
    // destroy the window explicitly
308
    DestroyWindow(hwnd);
309
 
310
    return msg.wParam;
311
}
312
 
313
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
314
{
315
    switch (uMsg)
316
    {
317
        case WM_CLOSE:
318
            PostQuitMessage(0);
319
        break;
320
 
321
        case WM_DESTROY:
322
            return 0;
323
 
324
        case WM_KEYDOWN:
325
        {
326
            switch (wParam)
327
            {
328
                case VK_ESCAPE:
329
                    PostQuitMessage(0);
330
                break;
331
            }
332
        }
333
        break;
334
 
335
        default:
336
            return DefWindowProc(hwnd, uMsg, wParam, lParam);
337
    }
338
 
339
    return 0;
340
}
341
 
342
void EnableOpenGL(HWND hwnd, HDC* hDC, HGLRC* hRC)
343
{
344
    PIXELFORMATDESCRIPTOR pfd;
345
 
346
    int iFormat;
347
 
348
    /* get the device context (DC) */
349
    *hDC = GetDC(hwnd);
350
 
351
    /* set the pixel format for the DC */
352
    ZeroMemory(&pfd, sizeof(pfd));
353
 
354
    pfd.nSize = sizeof(pfd);
355
    pfd.nVersion = 1;
356
    pfd.dwFlags = PFD_DRAW_TO_WINDOW |
357
                  PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
358
    pfd.iPixelType = PFD_TYPE_RGBA;
359
    pfd.cColorBits = 24;
360
    pfd.cDepthBits = 16;
361
    pfd.iLayerType = PFD_MAIN_PLANE;
362
 
363
    iFormat = ChoosePixelFormat(*hDC, &pfd);
364
 
365
    SetPixelFormat(*hDC, iFormat, &pfd);
366
 
367
    /* create and enable the render context (RC) */
368
    *hRC = wglCreateContext(*hDC);
369
 
370
    wglMakeCurrent(*hDC, *hRC);
371
}
372
 
373
void DisableOpenGL (HWND hwnd, HDC hDC, HGLRC hRC)
374
{
375
    wglMakeCurrent(NULL, NULL);
376
    wglDeleteContext(hRC);
377
    ReleaseDC(hwnd, hDC);
378
}
379