gl.cc (4560B)
1 #include <GL/glew.h> // glewInit, glewIsSupported 2 #include <GLFW/glfw3.h> 3 #define GLAPI extern 4 //#include <GL/osmesa.h> // OSMesa{Context,CreateContext,MakeCurrent} 5 6 #include <err.h> 7 #include <stdio.h> // fclose, fopen, fputc 8 #include <stdlib.h> 9 #include <string.h> 10 11 #include "gl_main.h" // citrun::gl_main 12 13 14 void 15 keyboard_func(GLFWwindow *window, int key, int scancode, int action, int mods) 16 { 17 //static_vu->keyboard_func(window, key, scancode, action, mods); 18 } 19 20 static void 21 error_callback(int error, const char *desc) 22 { 23 fprintf(stderr, "Error: %s\n", desc); 24 } 25 26 // 27 // osdemo.c originally from Brian Paul, public domain. 28 // 29 static void 30 write_targa(const char *filename, const GLubyte *buffer, int width, int height) 31 { 32 FILE *f = fopen( filename, "w" ); 33 if (f) { 34 int i, x, y; 35 const GLubyte *ptr = buffer; 36 printf ("osdemo, writing tga file \n"); 37 fputc (0x00, f); /* ID Length, 0 => No ID */ 38 fputc (0x00, f); /* Color Map Type, 0 => No color map included */ 39 fputc (0x02, f); /* Image Type, 2 => Uncompressed, True-color Image */ 40 fputc (0x00, f); /* Next five bytes are about the color map entries */ 41 fputc (0x00, f); /* 2 bytes Index, 2 bytes length, 1 byte size */ 42 fputc (0x00, f); 43 fputc (0x00, f); 44 fputc (0x00, f); 45 fputc (0x00, f); /* X-origin of Image */ 46 fputc (0x00, f); 47 fputc (0x00, f); /* Y-origin of Image */ 48 fputc (0x00, f); 49 fputc (width & 0xff, f); /* Image Width */ 50 fputc ((width>>8) & 0xff, f); 51 fputc (height & 0xff, f); /* Image Height */ 52 fputc ((height>>8) & 0xff, f); 53 fputc (0x18, f); /* Pixel Depth, 0x18 => 24 Bits */ 54 fputc (0x20, f); /* Image Descriptor */ 55 fclose(f); 56 f = fopen( filename, "ab" ); /* reopen in binary append mode */ 57 for (y=height-1; y>=0; y--) { 58 for (x=0; x<width; x++) { 59 i = (y*width + x) * 4; 60 fputc(ptr[i+2], f); /* write blue */ 61 fputc(ptr[i+1], f); /* write green */ 62 fputc(ptr[i], f); /* write red */ 63 } 64 } 65 } 66 } 67 68 #if 0 69 int 70 altmain(int argc, char *argv[]) 71 { 72 OSMesaContext ctx; 73 void *buffer; 74 char *filename = NULL; 75 int width = 400; 76 int height = 400; 77 78 filename = argv[1]; 79 if (filename == NULL) 80 errx(0,"Specify a filename if you want to make an image file"); 81 82 if (argc == 4) { 83 width = atoi(argv[2]); 84 height = atoi(argv[3]); 85 } 86 87 // Create an RGBA-mode context 88 #if OSMESA_MAJOR_VERSION * 100 + OSMESA_MINOR_VERSION >= 305 89 // specify Z, stencil, accum sizes 90 ctx = OSMesaCreateContextExt(OSMESA_RGBA, 16, 0, 0, NULL); 91 #else 92 ctx = OSMesaCreateContext(OSMESA_RGBA, NULL); 93 #endif 94 if (!ctx) 95 errx(1, "OSMesaCreateContext failed!"); 96 97 // Allocate the image buffer 98 if ((buffer = malloc(width * height * 4 * sizeof(GLubyte))) == NULL) 99 errx(1, "Alloc image buffer failed!"); 100 101 // Bind the buffer to the context and make it current 102 if (!OSMesaMakeCurrent(ctx, buffer, GL_UNSIGNED_BYTE, width, height)) 103 errx(1, "OSMesaMakeCurrent failed!"); 104 105 int z, s, a; 106 glGetIntegerv(GL_DEPTH_BITS, &z); 107 glGetIntegerv(GL_STENCIL_BITS, &s); 108 glGetIntegerv(GL_ACCUM_RED_BITS, &a); 109 printf("Depth=%d Stencil=%d Accum=%d\n", z, s, a); 110 111 GLenum glew_status = glewInit(); 112 if (GLEW_OK != glew_status) 113 errx(1, "glewInit %s", glewGetErrorString(glew_status)); 114 if (!glewIsSupported("GL_VERSION_2_0")) 115 errx(1, "No support for OpenGL 2.0 found"); 116 117 citrun::gl_main main; 118 main.tick(); 119 120 write_targa(filename, static_cast<const GLubyte *>(buffer), width, height); 121 printf("all done\n"); 122 123 free(buffer); 124 OSMesaDestroyContext( ctx ); 125 126 return 0; 127 } 128 #endif 129 130 int 131 main(int argc, char *argv[]) 132 { 133 /* 134 if (argc > 1) 135 return altmain(argc, argv); 136 */ 137 138 GLFWwindow *window; 139 140 glfwSetErrorCallback(error_callback); 141 142 if (!glfwInit()) 143 return 1; 144 145 glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 2); 146 glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0); 147 glfwWindowHint(GLFW_SRGB_CAPABLE, 1); 148 149 window = glfwCreateWindow(1600, 1200, "C It Run", NULL, NULL); 150 if (window == NULL) { 151 glfwTerminate(); 152 return 1; 153 } 154 155 glfwSetKeyCallback(window, keyboard_func); 156 157 glfwMakeContextCurrent(window); 158 glfwSwapInterval(1); 159 160 GLenum glew_status = glewInit(); 161 if (GLEW_OK != glew_status) 162 errx(1, "%s", glewGetErrorString(glew_status)); 163 if (!glewIsSupported("GL_VERSION_2_0")) 164 errx(1, "No support for OpenGL 2.0 found"); 165 166 citrun::gl_main main; 167 168 while (!glfwWindowShouldClose(window)) { 169 170 main.tick(); 171 172 glfwSwapBuffers(window); 173 glfwPollEvents(); 174 } 175 176 glfwDestroyWindow(window); 177 glfwTerminate(); 178 179 return 0; 180 }