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.

osdemo.c 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /* $Id: osdemo.c,v 1.1 1999/08/19 00:55:40 jtg Exp $ */
  2. /*
  3. * Demo of off-screen Mesa rendering
  4. *
  5. * See Mesa/include/GL/osmesa.h for documentation of the OSMesa functions.
  6. *
  7. * If you want to render BIG images you'll probably have to increase
  8. * MAX_WIDTH and MAX_HEIGHT in src/config.h.
  9. *
  10. * This program is in the public domain.
  11. *
  12. * Brian Paul
  13. *
  14. * PPM output provided by Joerg Schmalzl.
  15. * ASCII PPM output added by Brian Paul.
  16. */
  17. /*
  18. * $Log: osdemo.c,v $
  19. * Revision 1.1 1999/08/19 00:55:40 jtg
  20. * Initial revision
  21. *
  22. * Revision 3.0 1998/02/14 18:42:29 brianp
  23. * initial rev
  24. *
  25. */
  26. #include <stdio.h>
  27. #include <stdlib.h>
  28. #include "GL/osmesa.h"
  29. #include "GL/glut.h"
  30. #define WIDTH 400
  31. #define HEIGHT 400
  32. static void render_image( void )
  33. {
  34. GLfloat light_ambient[] = { 0.0, 0.0, 0.0, 1.0 };
  35. GLfloat light_diffuse[] = { 1.0, 1.0, 1.0, 1.0 };
  36. GLfloat light_specular[] = { 1.0, 1.0, 1.0, 1.0 };
  37. GLfloat light_position[] = { 1.0, 1.0, 1.0, 0.0 };
  38. GLfloat red_mat[] = { 1.0, 0.2, 0.2, 1.0 };
  39. GLfloat green_mat[] = { 0.2, 1.0, 0.2, 1.0 };
  40. GLfloat blue_mat[] = { 0.2, 0.2, 1.0, 1.0 };
  41. glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
  42. glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
  43. glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
  44. glLightfv(GL_LIGHT0, GL_POSITION, light_position);
  45. glEnable(GL_LIGHTING);
  46. glEnable(GL_LIGHT0);
  47. glEnable(GL_DEPTH_TEST);
  48. glMatrixMode(GL_PROJECTION);
  49. glLoadIdentity();
  50. glOrtho(-2.5, 2.5, -2.5, 2.5, -10.0, 10.0);
  51. glMatrixMode(GL_MODELVIEW);
  52. glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
  53. glPushMatrix();
  54. glRotatef(20.0, 1.0, 0.0, 0.0);
  55. glPushMatrix();
  56. glTranslatef(-0.75, 0.5, 0.0);
  57. glRotatef(90.0, 1.0, 0.0, 0.0);
  58. glMaterialfv( GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, red_mat );
  59. glutSolidTorus(0.275, 0.85, 20, 20);
  60. glPopMatrix();
  61. glPushMatrix();
  62. glTranslatef(-0.75, -0.5, 0.0);
  63. glRotatef(270.0, 1.0, 0.0, 0.0);
  64. glMaterialfv( GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, green_mat );
  65. glutSolidCone(1.0, 2.0, 16, 1);
  66. glPopMatrix();
  67. glPushMatrix();
  68. glTranslatef(0.75, 0.0, -1.0);
  69. glMaterialfv( GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, blue_mat );
  70. glutSolidSphere(1.0, 20, 20);
  71. glPopMatrix();
  72. glPopMatrix();
  73. }
  74. int main( int argc, char *argv[] )
  75. {
  76. OSMesaContext ctx;
  77. void *buffer;
  78. /* Create an RGBA-mode context */
  79. ctx = OSMesaCreateContext( GL_RGBA, NULL );
  80. /* Allocate the image buffer */
  81. buffer = malloc( WIDTH * HEIGHT * 4 );
  82. /* Bind the buffer to the context and make it current */
  83. OSMesaMakeCurrent( ctx, buffer, GL_UNSIGNED_BYTE, WIDTH, HEIGHT );
  84. render_image();
  85. if (argc>1) {
  86. /* write PPM file */
  87. FILE *f = fopen( argv[1], "w" );
  88. if (f) {
  89. int i, x, y;
  90. GLubyte *ptr = (GLubyte *) buffer;
  91. #define BINARY 0
  92. #if BINARY
  93. fprintf(f,"P6\n");
  94. fprintf(f,"# ppm-file created by %s\n", argv[0]);
  95. fprintf(f,"%i %i\n", WIDTH,HEIGHT);
  96. fprintf(f,"255\n");
  97. fclose(f);
  98. f = fopen( argv[1], "ab" ); /* reopen in binary append mode */
  99. for (y=HEIGHT-1; y>=0; y--) {
  100. for (x=0; x<WIDTH; x++) {
  101. i = (y*WIDTH + x) * 4;
  102. fputc(ptr[i], f); /* write red */
  103. fputc(ptr[i+1], f); /* write green */
  104. fputc(ptr[i+2], f); /* write blue */
  105. }
  106. }
  107. #else /*ASCII*/
  108. int counter = 0;
  109. fprintf(f,"P3\n");
  110. fprintf(f,"# ascii ppm file created by %s\n", argv[0]);
  111. fprintf(f,"%i %i\n", WIDTH, HEIGHT);
  112. fprintf(f,"255\n");
  113. for (y=HEIGHT-1; y>=0; y--) {
  114. for (x=0; x<WIDTH; x++) {
  115. i = (y*WIDTH + x) * 4;
  116. fprintf(f, " %3d %3d %3d", ptr[i], ptr[i+1], ptr[i+2]);
  117. counter++;
  118. if (counter % 5 == 0)
  119. fprintf(f, "\n");
  120. }
  121. }
  122. #endif
  123. fclose(f);
  124. }
  125. }
  126. else {
  127. printf("Specify a filename if you want to make a ppm file\n");
  128. }
  129. printf("all done\n");
  130. /* free the image buffer */
  131. free( buffer );
  132. /* destroy the context */
  133. OSMesaDestroyContext( ctx );
  134. return 0;
  135. }