Clone of mesa.
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

reflect.c 9.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. /* $Id: reflect.c,v 1.7 2001/04/25 15:51:32 brianp Exp $ */
  2. /*
  3. * Demo of a reflective, texture-mapped surface with OpenGL.
  4. * Brian Paul August 14, 1995 This file is in the public domain.
  5. *
  6. * Hardware texture mapping is highly recommended!
  7. *
  8. * The basic steps are:
  9. * 1. Render the reflective object (a polygon) from the normal viewpoint,
  10. * setting the stencil planes = 1.
  11. * 2. Render the scene from a special viewpoint: the viewpoint which
  12. * is on the opposite side of the reflective plane. Only draw where
  13. * stencil = 1. This draws the objects in the reflective surface.
  14. * 3. Render the scene from the original viewpoint. This draws the
  15. * objects in the normal fashion. Use blending when drawing
  16. * the reflective, textured surface.
  17. *
  18. * This is a very crude demo. It could be much better.
  19. */
  20. /*
  21. * Authors:
  22. * Brian Paul
  23. * Dirk Reiners (reiners@igd.fhg.de) made some modifications to this code.
  24. * Mark Kilgard (April 1997)
  25. * Brian Paul (April 2000 - added keyboard d/s options)
  26. */
  27. #include <math.h>
  28. #include <stdio.h>
  29. #include <stdlib.h>
  30. #include "GL/glut.h"
  31. #include "../util/showbuffer.c"
  32. #include "../util/readtex.c"
  33. #define DEG2RAD (3.14159/180.0)
  34. #define TABLE_TEXTURE "../images/tile.rgb"
  35. static GLint ImgWidth, ImgHeight;
  36. static GLenum ImgFormat;
  37. static GLubyte *Image = NULL;
  38. #define MAX_OBJECTS 2
  39. static GLint table_list;
  40. static GLint objects_list[MAX_OBJECTS];
  41. static GLfloat xrot, yrot;
  42. static GLfloat spin;
  43. static GLint Width = 400, Height = 300;
  44. static GLenum ShowBuffer = GL_NONE;
  45. static GLboolean Anim = GL_TRUE;
  46. /* performance info */
  47. static GLint T0 = 0;
  48. static GLint Frames = 0;
  49. static void make_table( void )
  50. {
  51. static GLfloat table_mat[] = { 1.0, 1.0, 1.0, 0.6 };
  52. static GLfloat gray[] = { 0.4, 0.4, 0.4, 1.0 };
  53. table_list = glGenLists(1);
  54. glNewList( table_list, GL_COMPILE );
  55. /* load table's texture */
  56. glMaterialfv( GL_FRONT, GL_AMBIENT_AND_DIFFUSE, table_mat );
  57. /* glMaterialfv( GL_FRONT, GL_EMISSION, gray );*/
  58. glMaterialfv( GL_FRONT, GL_DIFFUSE, table_mat );
  59. glMaterialfv( GL_FRONT, GL_AMBIENT, gray );
  60. /* draw textured square for the table */
  61. glPushMatrix();
  62. glScalef( 4.0, 4.0, 4.0 );
  63. glBegin( GL_POLYGON );
  64. glNormal3f( 0.0, 1.0, 0.0 );
  65. glTexCoord2f( 0.0, 0.0 ); glVertex3f( -1.0, 0.0, 1.0 );
  66. glTexCoord2f( 1.0, 0.0 ); glVertex3f( 1.0, 0.0, 1.0 );
  67. glTexCoord2f( 1.0, 1.0 ); glVertex3f( 1.0, 0.0, -1.0 );
  68. glTexCoord2f( 0.0, 1.0 ); glVertex3f( -1.0, 0.0, -1.0 );
  69. glEnd();
  70. glPopMatrix();
  71. glDisable( GL_TEXTURE_2D );
  72. glEndList();
  73. }
  74. static void make_objects( void )
  75. {
  76. GLUquadricObj *q;
  77. static GLfloat cyan[] = { 0.0, 1.0, 1.0, 1.0 };
  78. static GLfloat green[] = { 0.2, 1.0, 0.2, 1.0 };
  79. static GLfloat black[] = { 0.0, 0.0, 0.0, 0.0 };
  80. q = gluNewQuadric();
  81. gluQuadricDrawStyle( q, GLU_FILL );
  82. gluQuadricNormals( q, GLU_SMOOTH );
  83. objects_list[0] = glGenLists(1);
  84. glNewList( objects_list[0], GL_COMPILE );
  85. glMaterialfv( GL_FRONT, GL_AMBIENT_AND_DIFFUSE, cyan );
  86. glMaterialfv( GL_FRONT, GL_EMISSION, black );
  87. gluCylinder( q, 0.5, 0.5, 1.0, 15, 1 );
  88. glEndList();
  89. objects_list[1] = glGenLists(1);
  90. glNewList( objects_list[1], GL_COMPILE );
  91. glMaterialfv( GL_FRONT, GL_AMBIENT_AND_DIFFUSE, green );
  92. glMaterialfv( GL_FRONT, GL_EMISSION, black );
  93. gluCylinder( q, 1.5, 0.0, 2.5, 15, 1 );
  94. glEndList();
  95. }
  96. static void init( void )
  97. {
  98. make_table();
  99. make_objects();
  100. Image = LoadRGBImage( TABLE_TEXTURE, &ImgWidth, &ImgHeight, &ImgFormat );
  101. if (!Image) {
  102. printf("Couldn't read %s\n", TABLE_TEXTURE);
  103. exit(0);
  104. }
  105. gluBuild2DMipmaps(GL_TEXTURE_2D, 3, ImgWidth, ImgHeight,
  106. ImgFormat, GL_UNSIGNED_BYTE, Image);
  107. glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );
  108. glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );
  109. glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
  110. glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST );
  111. xrot = 30.0;
  112. yrot = 50.0;
  113. spin = 0.0;
  114. glShadeModel( GL_FLAT );
  115. glEnable( GL_LIGHT0 );
  116. glEnable( GL_LIGHTING );
  117. glClearColor( 0.5, 0.5, 0.9, 0.0 );
  118. glEnable( GL_NORMALIZE );
  119. }
  120. static void reshape(int w, int h)
  121. {
  122. GLfloat yAspect = 2.5;
  123. GLfloat xAspect = yAspect * (float) w / (float) h;
  124. Width = w;
  125. Height = h;
  126. glViewport(0, 0, w, h);
  127. glMatrixMode(GL_PROJECTION);
  128. glLoadIdentity();
  129. glFrustum( -xAspect, xAspect, -yAspect, yAspect, 10.0, 30.0 );
  130. glMatrixMode(GL_MODELVIEW);
  131. glLoadIdentity();
  132. }
  133. static void draw_objects( GLfloat eyex, GLfloat eyey, GLfloat eyez )
  134. {
  135. (void) eyex;
  136. (void) eyey;
  137. (void) eyez;
  138. #ifndef USE_ZBUFFER
  139. if (eyex<0.5) {
  140. #endif
  141. glPushMatrix();
  142. glTranslatef( 1.0, 1.5, 0.0 );
  143. glRotatef( spin, 1.0, 0.5, 0.0 );
  144. glRotatef( 0.5*spin, 0.0, 0.5, 1.0 );
  145. glCallList( objects_list[0] );
  146. glPopMatrix();
  147. glPushMatrix();
  148. glTranslatef( -1.0, 0.85+3.0*fabs( cos(0.01*spin) ), 0.0 );
  149. glRotatef( 0.5*spin, 0.0, 0.5, 1.0 );
  150. glRotatef( spin, 1.0, 0.5, 0.0 );
  151. glScalef( 0.5, 0.5, 0.5 );
  152. glCallList( objects_list[1] );
  153. glPopMatrix();
  154. #ifndef USE_ZBUFFER
  155. }
  156. else {
  157. glPushMatrix();
  158. glTranslatef( -1.0, 0.85+3.0*fabs( cos(0.01*spin) ), 0.0 );
  159. glRotatef( 0.5*spin, 0.0, 0.5, 1.0 );
  160. glRotatef( spin, 1.0, 0.5, 0.0 );
  161. glScalef( 0.5, 0.5, 0.5 );
  162. glCallList( objects_list[1] );
  163. glPopMatrix();
  164. glPushMatrix();
  165. glTranslatef( 1.0, 1.5, 0.0 );
  166. glRotatef( spin, 1.0, 0.5, 0.0 );
  167. glRotatef( 0.5*spin, 0.0, 0.5, 1.0 );
  168. glCallList( objects_list[0] );
  169. glPopMatrix();
  170. }
  171. #endif
  172. }
  173. static void draw_table( void )
  174. {
  175. glCallList( table_list );
  176. }
  177. static void draw_scene( void )
  178. {
  179. static GLfloat light_pos[] = { 0.0, 20.0, 0.0, 1.0 };
  180. GLfloat dist = 20.0;
  181. GLfloat eyex, eyey, eyez;
  182. glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
  183. eyex = dist * cos(yrot*DEG2RAD) * cos(xrot*DEG2RAD);
  184. eyez = dist * sin(yrot*DEG2RAD) * cos(xrot*DEG2RAD);
  185. eyey = dist * sin(xrot*DEG2RAD);
  186. /* view from top */
  187. glPushMatrix();
  188. gluLookAt( eyex, eyey, eyez, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0 );
  189. glLightfv( GL_LIGHT0, GL_POSITION, light_pos );
  190. /* draw table into stencil planes */
  191. glDisable( GL_DEPTH_TEST );
  192. glEnable( GL_STENCIL_TEST );
  193. glStencilFunc( GL_ALWAYS, 1, 0xffffffff );
  194. glStencilOp( GL_REPLACE, GL_REPLACE, GL_REPLACE );
  195. glColorMask( GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE );
  196. draw_table();
  197. glColorMask( GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE );
  198. glEnable( GL_DEPTH_TEST );
  199. /* render view from below (reflected viewport) */
  200. /* only draw where stencil==1 */
  201. if (eyey>0.0) {
  202. glPushMatrix();
  203. glStencilFunc( GL_EQUAL, 1, 0xffffffff ); /* draw if ==1 */
  204. glStencilOp( GL_KEEP, GL_KEEP, GL_KEEP );
  205. glScalef( 1.0, -1.0, 1.0 );
  206. /* Reposition light in reflected space. */
  207. glLightfv(GL_LIGHT0, GL_POSITION, light_pos);
  208. draw_objects(eyex, eyey, eyez);
  209. glPopMatrix();
  210. /* Restore light's original unreflected position. */
  211. glLightfv(GL_LIGHT0, GL_POSITION, light_pos);
  212. }
  213. glDisable( GL_STENCIL_TEST );
  214. glEnable( GL_BLEND );
  215. glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
  216. glEnable( GL_TEXTURE_2D );
  217. draw_table();
  218. glDisable( GL_TEXTURE_2D );
  219. glDisable( GL_BLEND );
  220. /* view from top */
  221. glPushMatrix();
  222. draw_objects(eyex, eyey, eyez);
  223. glPopMatrix();
  224. glPopMatrix();
  225. if (ShowBuffer == GL_DEPTH) {
  226. ShowDepthBuffer(Width, Height, 1.0, 0.0);
  227. }
  228. else if (ShowBuffer == GL_STENCIL) {
  229. ShowStencilBuffer(Width, Height, 255.0, 0.0);
  230. }
  231. else if (ShowBuffer == GL_ALPHA) {
  232. ShowAlphaBuffer(Width, Height);
  233. }
  234. glutSwapBuffers();
  235. {
  236. GLint t = glutGet(GLUT_ELAPSED_TIME);
  237. Frames++;
  238. if (t - T0 >= 5000) {
  239. GLfloat seconds = (t - T0) / 1000.0;
  240. GLfloat fps = Frames / seconds;
  241. printf("%d frames in %g seconds = %g FPS\n", Frames, seconds, fps);
  242. T0 = t;
  243. Frames = 0;
  244. }
  245. }
  246. }
  247. static void idle( void )
  248. {
  249. spin += 2.0;
  250. yrot += 3.0;
  251. glutPostRedisplay();
  252. }
  253. static void Key( unsigned char key, int x, int y )
  254. {
  255. (void) x;
  256. (void) y;
  257. if (key == 'd') {
  258. ShowBuffer = GL_DEPTH;
  259. }
  260. else if (key == 's') {
  261. ShowBuffer = GL_STENCIL;
  262. }
  263. else if (key == 'a') {
  264. ShowBuffer = GL_ALPHA;
  265. }
  266. else if (key == ' ') {
  267. Anim = !Anim;
  268. if (Anim)
  269. glutIdleFunc(idle);
  270. else
  271. glutIdleFunc(NULL);
  272. }
  273. else if (key==27) {
  274. exit(0);
  275. }
  276. else {
  277. ShowBuffer = GL_NONE;
  278. }
  279. glutPostRedisplay();
  280. }
  281. static void SpecialKey( int key, int x, int y )
  282. {
  283. (void) x;
  284. (void) y;
  285. switch (key) {
  286. case GLUT_KEY_UP:
  287. xrot += 3.0;
  288. if ( xrot > 85 )
  289. xrot = 85;
  290. break;
  291. case GLUT_KEY_DOWN:
  292. xrot -= 3.0;
  293. if ( xrot < 5 )
  294. xrot = 5;
  295. break;
  296. case GLUT_KEY_LEFT:
  297. yrot += 3.0;
  298. break;
  299. case GLUT_KEY_RIGHT:
  300. yrot -= 3.0;
  301. break;
  302. }
  303. glutPostRedisplay();
  304. }
  305. int main( int argc, char *argv[] )
  306. {
  307. glutInit(&argc, argv);
  308. glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH | GLUT_STENCIL);
  309. glutInitWindowPosition( 0, 0 );
  310. glutInitWindowSize( Width, Height );
  311. glutCreateWindow(argv[0]);
  312. glutReshapeFunc(reshape);
  313. glutDisplayFunc(draw_scene);
  314. glutKeyboardFunc(Key);
  315. glutSpecialFunc(SpecialKey);
  316. glutIdleFunc(idle);
  317. init();
  318. glutMainLoop();
  319. return 0;
  320. }