Advertisement
FoguinhoS

Untitled

Nov 9th, 2014
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.35 KB | None | 0 0
  1. /*
  2. * GLUT Shapes Demo
  3. *
  4. * Written by Nigel Stewart November 2003
  5. *
  6. * This program is test harness for the sphere, cone
  7. * and torus shapes in GLUT.
  8. *
  9. * Spinning wireframe and smooth shaded shapes are
  10. * displayed until the ESC or q key is pressed. The
  11. * number of geometry stacks and slices can be adjusted
  12. * using the + and - keys.
  13. */
  14.  
  15. #include <windows.h>
  16. #ifdef __APPLE__
  17. #include <GLUT/glut.h>
  18. #else
  19. #include <GL/glut.h>
  20. #endif
  21.  
  22. #include <stdio.h>
  23. #include <stdlib.h>
  24. static void resize(int width, int height);
  25.  
  26.  
  27.  
  28. GLuint raw_texture_load(const char *filename, int width, int height)
  29. {
  30. GLuint texture;
  31. unsigned char *data;
  32. FILE *file;
  33.  
  34. // open texture data
  35. file = fopen(filename, "rb");
  36. if (file == NULL) return 0;
  37.  
  38. // allocate buffer
  39. data = (unsigned char*) malloc(width * height * 4);
  40.  
  41. // read texture data
  42. fread(data, width * height * 4, 1, file);
  43. fclose(file);
  44.  
  45. // allocate a texture name
  46. glGenTextures(1, &texture);
  47.  
  48. // select our current texture
  49. glBindTexture(GL_TEXTURE_2D, texture);
  50.  
  51. // select modulate to mix texture with color for shading
  52. glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
  53.  
  54. // when texture area is small, bilinear filter the closest mipmap
  55. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  56. // when texture area is large, bilinear filter the first mipmap
  57. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  58.  
  59. // texture should tile
  60. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
  61. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
  62.  
  63.  
  64. // build our texture mipmaps
  65. gluBuild2DMipmaps(GL_TEXTURE_2D, 4, width, height, GL_RGBA, GL_UNSIGNED_BYTE, data);
  66.  
  67. // free buffer
  68. free(data);
  69.  
  70. return texture;
  71. }
  72.  
  73. static void resize(int width, int height)
  74. {
  75. glViewport(0, 0, width, height);
  76. glMatrixMode(GL_PROJECTION);
  77. glLoadIdentity();
  78. glOrtho(0.0f, width, height, 0.0f, 0.0f, 1.0f);
  79.  
  80. glMatrixMode(GL_MODELVIEW);
  81. glLoadIdentity() ;
  82. }
  83.  
  84.  
  85. void draw_texture(const char * nome, int x,int y,int w,int h)
  86. {
  87. // OpenGL animation code goes here
  88.  
  89.  
  90. // load our texture
  91. GLint texture = raw_texture_load( nome, x , y );
  92. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  93. float theta = 0.0f;
  94.  
  95. // setup texture mapping
  96.  
  97. glEnable( GL_TEXTURE_2D );
  98. glBindTexture( GL_TEXTURE_2D, texture );
  99.  
  100. glPushMatrix();
  101.  
  102. glRotatef( theta, 0.0f, 0.0f, 1.0f );
  103. glBegin( GL_QUADS );
  104.  
  105.  
  106. glTexCoord2d(0.0,0.0); glVertex2d(0, 0);
  107. glTexCoord2d(32,0.0); glVertex2d(w, 0);
  108. glTexCoord2d(32,32); glVertex2d(w, h);
  109. glTexCoord2d(0.0,32); glVertex2d(0,h);
  110.  
  111. glEnd();
  112. glPopMatrix();
  113.  
  114.  
  115. glutSwapBuffers();
  116.  
  117. theta += 1.0f;
  118. }
  119. static void display(void)
  120. {
  121. draw_texture("image.raw", 32, 32, 320, 240);
  122. }
  123.  
  124.  
  125.  
  126.  
  127.  
  128.  
  129. /* Program entry point */
  130.  
  131.  
  132.  
  133. // OpenGL ////////////////////////////////////////////////////////////
  134.  
  135. // Enable OpenGL
  136.  
  137. VOID EnableOpenGL( HWND hWnd, HDC * hDC, HGLRC * hRC )
  138. {
  139. PIXELFORMATDESCRIPTOR pfd;
  140. int iFormat;
  141.  
  142. // get the device context (DC)
  143. *hDC = GetDC( hWnd );
  144.  
  145. // set the pixel format for the DC
  146. ZeroMemory( &pfd, sizeof( pfd ) );
  147. pfd.nSize = sizeof( pfd );
  148. pfd.nVersion = 1;
  149. pfd.dwFlags = PFD_DRAW_TO_WINDOW |
  150. PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
  151. pfd.iPixelType = PFD_TYPE_RGBA;
  152. pfd.cColorBits = 24;
  153. pfd.cDepthBits = 16;
  154. pfd.iLayerType = PFD_MAIN_PLANE;
  155. iFormat = ChoosePixelFormat( *hDC, &pfd );
  156. SetPixelFormat( *hDC, iFormat, &pfd );
  157.  
  158. // create and enable the render context (RC)
  159. *hRC = wglCreateContext( *hDC );
  160. wglMakeCurrent( *hDC, *hRC );
  161.  
  162. }
  163.  
  164. // Disable OpenGL
  165.  
  166. VOID DisableOpenGL( HWND hWnd, HDC hDC, HGLRC hRC )
  167. {
  168. wglMakeCurrent( NULL, NULL );
  169. wglDeleteContext( hRC );
  170. ReleaseDC( hWnd, hDC );
  171. }
  172.  
  173. int main(int argc, char *argv[])
  174. {
  175. glutInit(&argc, argv);
  176. glutInitWindowSize(640,480);
  177. glutInitWindowPosition(10,10);
  178. glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
  179.  
  180. glutCreateWindow("GLUT Shapes");
  181. glutReshapeFunc(resize);
  182. glutDisplayFunc(display);
  183.  
  184. glutMainLoop();
  185.  
  186. return EXIT_SUCCESS;
  187. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement