Advertisement
Guest User

textureloader

a guest
Nov 3rd, 2012
43
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.32 KB | None | 0 0
  1. #include <windows.h>
  2. #include <GL/gl.h>
  3. #include <GL/glu.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6.  
  7.  
  8.  
  9. // Declarations //
  10.  
  11. GLuint LoadTextureRAW( const char * filename, int wrap, int id );
  12. void FreeTexture( GLuint texture );
  13.  
  14. LRESULT CALLBACK WndProc( HWND hWnd, UINT message,
  15. WPARAM wParam, LPARAM lParam );
  16.  
  17. VOID EnableOpenGL( HWND hWnd, HDC * hDC, HGLRC * hRC );
  18. VOID DisableOpenGL( HWND hWnd, HDC hDC, HGLRC hRC );
  19.  
  20.  
  21.  
  22. // WinMain //
  23. int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
  24. LPSTR lpCmdLine, int iCmdShow )
  25. {
  26. WNDCLASS wc;
  27. HWND hWnd;
  28. HDC hDC;
  29. HGLRC hRC;
  30. MSG msg;
  31. BOOL bQuit = FALSE;
  32.  
  33. GLuint texture;
  34. float theta = 0.0f;
  35.  
  36. // register window class
  37. wc.style = CS_OWNDC;
  38. wc.lpfnWndProc = WndProc;
  39. wc.cbClsExtra = 0;
  40. wc.cbWndExtra = 0;
  41. wc.hInstance = hInstance;
  42. wc.hIcon = LoadIcon( NULL, IDI_APPLICATION );
  43. wc.hCursor = LoadCursor( NULL, IDC_ARROW );
  44. wc.hbrBackground = (HBRUSH)GetStockObject( BLACK_BRUSH );
  45. wc.lpszMenuName = NULL;
  46. wc.lpszClassName = "GLSample";
  47. RegisterClass( &wc );
  48.  
  49. // create main window
  50. hWnd = CreateWindow(
  51. "GLSample", "OpenGL Texture Sample",
  52. WS_CAPTION | WS_POPUPWINDOW | WS_VISIBLE,
  53. 0, 0, 256, 256,
  54. NULL, NULL, hInstance, NULL );
  55.  
  56. // enable OpenGL for the window
  57. EnableOpenGL( hWnd, &hDC, &hRC );
  58.  
  59. //textur laden
  60. texture = LoadTextureRAW( "texture.raw", TRUE , 1 );
  61.  
  62.  
  63. while ( !bQuit ) {
  64.  
  65.  
  66. if ( PeekMessage( &msg, NULL, 0, 0, PM_REMOVE ) ) {
  67.  
  68.  
  69. if ( msg.message == WM_QUIT ) {
  70. bQuit = TRUE;
  71. } else {
  72. TranslateMessage( &msg );
  73. DispatchMessage( &msg );
  74. }
  75.  
  76. } else {
  77.  
  78.  
  79. glClearColor( 0.0f, 0.0f, 0.0f, 0.0f );
  80. glClear( GL_COLOR_BUFFER_BIT );
  81.  
  82.  
  83. glEnable( GL_TEXTURE_2D );
  84. glBindTexture( GL_TEXTURE_2D, texture );
  85.  
  86. glPushMatrix();
  87. glRotatef( theta, 0.0f, 0.0f, 1.0f );
  88. glBegin( GL_QUADS );
  89. glTexCoord2d(0.0,0.0); glVertex2d(-1.0,-1.0);
  90. glTexCoord2d(1.0,0.0); glVertex2d(+1.0,-1.0);
  91. glTexCoord2d(1.0,1.0); glVertex2d(+1.0,+1.0);
  92. glTexCoord2d(0.0,1.0); glVertex2d(-1.0,+1.0);
  93. glEnd();
  94. glPopMatrix();
  95.  
  96. SwapBuffers( hDC );
  97.  
  98. theta += 1.0f;
  99. Sleep(100);
  100. }
  101.  
  102. }
  103.  
  104.  
  105. FreeTexture( texture );
  106. DisableOpenGL( hWnd, hDC, hRC );
  107. DestroyWindow( hWnd );
  108. return msg.wParam;
  109.  
  110. }
  111.  
  112.  
  113.  
  114. // Texture //
  115.  
  116.  
  117. GLuint LoadTextureRAW( const char * filename, int wrap , int id)
  118. {
  119. GLuint texture;
  120. int width, height;
  121. BYTE * data ;
  122. FILE * file;
  123.  
  124. // datei öffnen
  125. file = fopen( filename, "texture" );
  126. if ( file == NULL ) return 0;
  127.  
  128. // buffer
  129. width = 256;
  130. height = 256;
  131. data =static_cast<BYTE*>( malloc( width * height * 3 ) );
  132.  
  133.  
  134. /* Fehlerbehandlung etc..*/
  135.  
  136.  
  137. // datei einlesen
  138. fread( data, width * height * 3, 1, file );
  139. fclose( file );
  140.  
  141. // texturname+id zuweisen
  142. glGenTextures( id, &texture );
  143.  
  144. // aktuelle textur auswählen
  145. glBindTexture( GL_TEXTURE_2D, texture );
  146.  
  147. // textur modi
  148. glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
  149.  
  150. // when texture area is small, bilinear filter the closest MIP map
  151. glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
  152. GL_LINEAR_MIPMAP_NEAREST );
  153.  
  154. //glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
  155.  
  156. // if wrap is true, the texture wraps over at the edges (repeat)
  157. // ... false, the texture ends at the edges (clamp)
  158. glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S,
  159. wrap ? GL_REPEAT : GL_CLAMP );
  160. glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T,
  161. wrap ? GL_REPEAT : GL_CLAMP );
  162.  
  163. // texturmaps
  164. gluBuild2DMipmaps( GL_TEXTURE_2D, 3, width,
  165. height, GL_RGB, GL_UNSIGNED_BYTE, data );
  166.  
  167.  
  168. free( data );
  169.  
  170. return texture;
  171.  
  172. }
  173.  
  174. void FreeTexture( GLuint texture )
  175. {
  176.  
  177. glDeleteTextures( 1, &texture );
  178.  
  179. }
  180.  
  181.  
  182.  
  183.  
  184.  
  185. LRESULT CALLBACK WndProc( HWND hWnd, UINT message,
  186. WPARAM wParam, LPARAM lParam )
  187. {
  188.  
  189. switch ( message ) {
  190.  
  191. case WM_CREATE:
  192. return 0;
  193.  
  194. case WM_CLOSE:
  195. PostQuitMessage( 0 );
  196. return 0;
  197.  
  198. case WM_DESTROY:
  199. return 0;
  200.  
  201. case WM_KEYDOWN:
  202. switch ( wParam ) {
  203.  
  204. case VK_ESCAPE:
  205. PostQuitMessage( 0 );
  206. return 0;
  207.  
  208. }
  209. return 0;
  210.  
  211. default:
  212. return DefWindowProc( hWnd,
  213. message, wParam, lParam );
  214.  
  215. }
  216.  
  217. }
  218.  
  219.  
  220.  
  221.  
  222. VOID EnableOpenGL( HWND hWnd, HDC * hDC, HGLRC * hRC )
  223. {
  224. PIXELFORMATDESCRIPTOR pfd;
  225. int iFormat;
  226.  
  227. // get the device context (DC)
  228. *hDC = GetDC( hWnd );
  229.  
  230. // set the pixel format for the DC
  231. ZeroMemory( &pfd, sizeof( pfd ) );
  232. pfd.nSize = sizeof( pfd );
  233. pfd.nVersion = 1;
  234. pfd.dwFlags = PFD_DRAW_TO_WINDOW |
  235. PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
  236. pfd.iPixelType = PFD_TYPE_RGBA;
  237. pfd.cColorBits = 24;
  238. pfd.cDepthBits = 16;
  239. pfd.iLayerType = PFD_MAIN_PLANE;
  240. iFormat = ChoosePixelFormat( *hDC, &pfd );
  241. SetPixelFormat( *hDC, iFormat, &pfd );
  242.  
  243. // create and enable the render context (RC)
  244. *hRC = wglCreateContext( *hDC );
  245. wglMakeCurrent( *hDC, *hRC );
  246.  
  247. }
  248.  
  249.  
  250.  
  251. VOID DisableOpenGL( HWND hWnd, HDC hDC, HGLRC hRC )
  252. {
  253. wglMakeCurrent( NULL, NULL );
  254. wglDeleteContext( hRC );
  255. ReleaseDC( hWnd, hDC );
  256. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement