Clone of mesa.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

vp-tris.c 6.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. /* Test glGenProgramsNV(), glIsProgramNV(), glLoadProgramNV() */
  2. #include <assert.h>
  3. #include <string.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <math.h>
  7. #ifndef WIN32
  8. #include <unistd.h>
  9. #include <signal.h>
  10. #endif
  11. #include <GL/glew.h>
  12. #include <GL/glut.h>
  13. static const char *filename = NULL;
  14. static GLuint nr_steps = 4;
  15. static GLuint prim = GL_TRIANGLES;
  16. static GLfloat psz = 1.0;
  17. static GLboolean pointsmooth = 0;
  18. static GLboolean program_point_size = 0;
  19. static void usage( char *name )
  20. {
  21. fprintf( stderr, "usage: %s [ options ] shader_filename\n", name );
  22. fprintf( stderr, "\n" );
  23. fprintf( stderr, "options:\n" );
  24. fprintf( stderr, " -f flat shaded\n" );
  25. fprintf( stderr, " -nNr subdivision steps\n" );
  26. fprintf( stderr, " -fps show frames per second\n" );
  27. }
  28. unsigned show_fps = 0;
  29. unsigned int frame_cnt = 0;
  30. #ifndef WIN32
  31. void alarmhandler(int);
  32. void alarmhandler (int sig)
  33. {
  34. if (sig == SIGALRM) {
  35. printf("%d frames in 5.0 seconds = %.3f FPS\n", frame_cnt,
  36. frame_cnt / 5.0);
  37. frame_cnt = 0;
  38. }
  39. signal(SIGALRM, alarmhandler);
  40. alarm(5);
  41. }
  42. #endif
  43. static void args(int argc, char *argv[])
  44. {
  45. GLint i;
  46. for (i = 1; i < argc; i++) {
  47. if (strncmp(argv[i], "-n", 2) == 0) {
  48. nr_steps = atoi((argv[i]) + 2);
  49. }
  50. else if (strcmp(argv[i], "-f") == 0) {
  51. glShadeModel(GL_FLAT);
  52. }
  53. else if (strcmp(argv[i], "-fps") == 0) {
  54. show_fps = 1;
  55. }
  56. else if (i == argc - 1) {
  57. filename = argv[i];
  58. }
  59. else {
  60. usage(argv[0]);
  61. exit(1);
  62. }
  63. }
  64. if (!filename) {
  65. usage(argv[0]);
  66. exit(1);
  67. }
  68. }
  69. static void Init( void )
  70. {
  71. GLint errno;
  72. GLuint prognum;
  73. char buf[4096];
  74. GLuint sz;
  75. FILE *f;
  76. if ((f = fopen(filename, "r")) == NULL) {
  77. fprintf(stderr, "couldn't open %s\n", filename);
  78. exit(1);
  79. }
  80. sz = (GLuint) fread(buf, 1, sizeof(buf), f);
  81. if (!feof(f)) {
  82. fprintf(stderr, "file too long\n");
  83. exit(1);
  84. }
  85. fprintf(stderr, "%.*s\n", sz, buf);
  86. if (strncmp( buf, "!!VP", 4 ) == 0) {
  87. glEnable( GL_VERTEX_PROGRAM_NV );
  88. glGenProgramsNV( 1, &prognum );
  89. glBindProgramNV( GL_VERTEX_PROGRAM_NV, prognum );
  90. glLoadProgramNV( GL_VERTEX_PROGRAM_NV, prognum, sz, (const GLubyte *) buf );
  91. assert( glIsProgramNV( prognum ) );
  92. }
  93. else {
  94. glEnable(GL_VERTEX_PROGRAM_ARB);
  95. glGenProgramsARB(1, &prognum);
  96. glBindProgramARB(GL_VERTEX_PROGRAM_ARB, prognum);
  97. glProgramStringARB(GL_VERTEX_PROGRAM_ARB, GL_PROGRAM_FORMAT_ASCII_ARB,
  98. sz, (const GLubyte *) buf);
  99. assert(glIsProgramARB(prognum));
  100. }
  101. errno = glGetError();
  102. printf("glGetError = %d\n", errno);
  103. if (errno != GL_NO_ERROR)
  104. {
  105. GLint errorpos;
  106. glGetIntegerv(GL_PROGRAM_ERROR_POSITION_ARB, &errorpos);
  107. printf("errorpos: %d\n", errorpos);
  108. printf("%s\n", (char *)glGetString(GL_PROGRAM_ERROR_STRING_ARB));
  109. }
  110. {
  111. const float Ambient[4] = { 0.0, 1.0, 0.0, 0.0 };
  112. const float Diffuse[4] = { 1.0, 0.0, 0.0, 0.0 };
  113. const float Specular[4] = { 0.0, 0.0, 1.0, 0.0 };
  114. const float Emission[4] = { 0.0, 0.0, 0.0, 1.0 };
  115. glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, Ambient);
  116. glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, Diffuse);
  117. glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, Specular);
  118. glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, Emission);
  119. }
  120. }
  121. union vert {
  122. struct {
  123. GLfloat color[3];
  124. GLfloat pos[3];
  125. } v;
  126. GLfloat f[6];
  127. };
  128. static void make_midpoint( union vert *out,
  129. const union vert *v0,
  130. const union vert *v1)
  131. {
  132. int i;
  133. for (i = 0; i < 6; i++)
  134. out->f[i] = v0->f[i] + .5 * (v1->f[i] - v0->f[i]);
  135. }
  136. static void subdiv( union vert *v0,
  137. union vert *v1,
  138. union vert *v2,
  139. GLuint depth )
  140. {
  141. if (depth == 0) {
  142. glColor3fv(v0->v.color);
  143. glVertex3fv(v0->v.pos);
  144. glColor3fv(v1->v.color);
  145. glVertex3fv(v1->v.pos);
  146. glColor3fv(v2->v.color);
  147. glVertex3fv(v2->v.pos);
  148. }
  149. else {
  150. union vert m[3];
  151. make_midpoint(&m[0], v0, v1);
  152. make_midpoint(&m[1], v1, v2);
  153. make_midpoint(&m[2], v2, v0);
  154. subdiv(&m[0], &m[2], v0, depth-1);
  155. subdiv(&m[1], &m[0], v1, depth-1);
  156. subdiv(&m[2], &m[1], v2, depth-1);
  157. subdiv(&m[0], &m[1], &m[2], depth-1);
  158. }
  159. }
  160. static void enable( GLenum value, GLboolean flag )
  161. {
  162. if (flag)
  163. glEnable(value);
  164. else
  165. glDisable(value);
  166. }
  167. /** Assignment */
  168. #define ASSIGN_3V( V, V0, V1, V2 ) \
  169. do { \
  170. V[0] = V0; \
  171. V[1] = V1; \
  172. V[2] = V2; \
  173. } while(0)
  174. static void Display( void )
  175. {
  176. glClearColor(0.3, 0.3, 0.3, 1);
  177. glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
  178. glPointSize(psz);
  179. enable( GL_POINT_SMOOTH, pointsmooth );
  180. enable( GL_VERTEX_PROGRAM_POINT_SIZE_ARB, program_point_size );
  181. glBegin(prim);
  182. {
  183. union vert v[3];
  184. ASSIGN_3V(v[0].v.color, 0,0,1);
  185. ASSIGN_3V(v[0].v.pos, 0.9, -0.9, 0.0);
  186. ASSIGN_3V(v[1].v.color, 1,0,0);
  187. ASSIGN_3V(v[1].v.pos, 0.9, 0.9, 0.0);
  188. ASSIGN_3V(v[2].v.color, 0,1,0);
  189. ASSIGN_3V(v[2].v.pos, -0.9, 0, 0.0);
  190. subdiv(&v[0], &v[1], &v[2], nr_steps);
  191. }
  192. glEnd();
  193. glFlush();
  194. if (show_fps) {
  195. ++frame_cnt;
  196. glutPostRedisplay();
  197. }
  198. }
  199. static void Reshape( int width, int height )
  200. {
  201. glViewport( 0, 0, width, height );
  202. glMatrixMode( GL_PROJECTION );
  203. glLoadIdentity();
  204. glOrtho(-1.0, 1.0, -1.0, 1.0, -0.5, 1000.0);
  205. glMatrixMode( GL_MODELVIEW );
  206. glLoadIdentity();
  207. /*glTranslatef( 0.0, 0.0, -15.0 );*/
  208. }
  209. static void Key( unsigned char key, int x, int y )
  210. {
  211. (void) x;
  212. (void) y;
  213. switch (key) {
  214. case 'p':
  215. prim = GL_POINTS;
  216. break;
  217. case 't':
  218. prim = GL_TRIANGLES;
  219. break;
  220. case 's':
  221. psz += .5;
  222. break;
  223. case 'S':
  224. if (psz > .5)
  225. psz -= .5;
  226. break;
  227. case 'm':
  228. pointsmooth = !pointsmooth;
  229. break;
  230. case 'z':
  231. program_point_size = !program_point_size;
  232. break;
  233. case '+':
  234. nr_steps++;
  235. break;
  236. case '-':
  237. if (nr_steps)
  238. nr_steps--;
  239. break;
  240. case ' ':
  241. psz = 1.0;
  242. prim = GL_TRIANGLES;
  243. nr_steps = 4;
  244. break;
  245. case 27:
  246. exit(0);
  247. break;
  248. }
  249. glutPostRedisplay();
  250. }
  251. int main( int argc, char *argv[] )
  252. {
  253. glutInit( &argc, argv );
  254. glutInitWindowPosition( 0, 0 );
  255. glutInitWindowSize( 250, 250 );
  256. glutInitDisplayMode( GLUT_RGB | GLUT_SINGLE | GLUT_DEPTH );
  257. glutCreateWindow(argv[argc-1]);
  258. glewInit();
  259. glutReshapeFunc( Reshape );
  260. glutKeyboardFunc( Key );
  261. glutDisplayFunc( Display );
  262. args( argc, argv );
  263. Init();
  264. #ifndef WIN32
  265. if (show_fps) {
  266. signal(SIGALRM, alarmhandler);
  267. alarm(5);
  268. }
  269. #endif
  270. glutMainLoop();
  271. return 0;
  272. }