void myinit(void) { int width, height, comp; glClearColor (0.0, 0.0, 0.0, 0.0); glEnable(GL_DEPTH_TEST); glGenTextures(50,texName); glBindTexture(GL_TEXTURE_2D, texName[0]); image = (GLubyte *) read_texture("anna.sgi", &width, &height, &comp); gluBuild2DMipmaps(GL_TEXTURE_2D, 4, width, height, GL_RGBA, GL_UNSIGNED_BYTE, image); free(image); glBindTexture(GL_TEXTURE_2D, texName[1]); image = (GLubyte *) read_texture("wall1.sgi", &width, &height, &comp); gluBuild2DMipmaps(GL_TEXTURE_2D, 4, width, height, GL_RGBA, GL_UNSIGNED_BYTE, image); free(image); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_NEAREST); glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); glEnable(GL_TEXTURE_2D); glShadeModel(GL_FLAT); }
void init_images(void) { int comp; a_data = read_texture("a.rgb", &a_width, &a_height, &comp); printf("%dx%dx%d\n", a_width, a_height, comp); b_data = read_texture("b.rgb", &b_width, &b_height, &comp); printf("%dx%dx%d\n", b_width, b_height, comp); }
MultiTex::MultiTex(std::string filename, float alpha, bool repeat) : m_alpha(alpha) { static unsigned *image; static int width, height, components; image = read_texture(filename.c_str(), &width, &height, &components); GLuint texId; glGenTextures(1, &texId); m_texID = texId; glBindTexture(GL_TEXTURE_2D, m_texID); gluBuild2DMipmaps( GL_TEXTURE_2D, components, width, height, GL_RGBA, GL_UNSIGNED_BYTE, image ); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); if (repeat) { glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); } else { glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP); } }
Image* ImageSerializer_rgb::serialize_read(std::istream& in) { int width = 0 ; int height = 0 ; int bpp = 0 ; unsigned* data = read_texture(in, &width, &height, &bpp) ; // Note: it seems that data is always encoded as a RGBA image, // bpp seems to only return the original color encoding... bpp = 4 ; Image::ColorEncoding color_encoding ; switch(bpp) { case 1: color_encoding = Image::GRAY ; break ; case 3: color_encoding = Image::RGB ; break ; case 4: color_encoding = Image::RGBA ; break ; default: ogf_assert(false) ; break ; } Image* result = new Image(color_encoding, width, height) ; memcpy(result->base_mem(), data, width * height * bpp) ; free(data) ; return result ; }
void load_img(const char *fname) { img = read_texture(fname, &w, &h, &comp); if (!img) { fprintf(stderr, "Could not open %s\n", fname); exit(1); } }
void init(int argc, char *argv[]) { unsigned *image; int i, width, height, components, flame=0; char fname[sizeof("../../data//flame/f00") + 1]; glEnable(GL_TEXTURE_2D); glPixelStorei(GL_UNPACK_ALIGNMENT, 1); /* Use the flame files if there are no command-line parameters */ if (argc < 1) { flame = 1; argc = 32; } for(i = 0; i < argc; i++) { if (flame) { (void)sprintf(fname, "../../data/flame/f%.2d",i); image = read_texture(fname, &width, &height, &components); } else image = read_texture(argv[i], &width, &height, &components); if (image == NULL) { fprintf(stderr, "Error: Can't load image file \"%s\".\n", argv[i]); exit(EXIT_FAILURE); } else { printf("%d x %d image loaded\n", width, height); } glBindTexture(GL_TEXTURE_2D, i+1); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP); glTexImage2D(GL_TEXTURE_2D, 0, components, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, image); texture_count++; } glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); glEnable(GL_TEXTURE_2D); glClearColor(.25f, .25f, .25f, .25f); glAlphaFunc(GL_GREATER, 0.f); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); }
int create_texture(const char *fname, GLsizei *w, GLsizei *h, GLsizei *padW, GLsizei *padH, int *comps) { unsigned int *img, *padImg = NULL; int y, iw, ih; iw = (int)(*w); ih = (int)(*h); img = read_texture(fname, &iw, &ih, comps); if (!img) return -1; *w = iw; *h = ih; /* if width & height are not powers of two, pad image with black */ if (*w & (*w - 1)) { *padW = roundup(*w); } else { *padW = *w; } if (*h & (*h - 1)) { *padH = roundup(*h); } else { *padH = *h; } if (*padW != *w || *padH != *h) { printf("rounding %s up...\n", fname); padImg = (unsigned int *)malloc(*padW * *padH * sizeof(GLuint)); if (!padImg) { fprintf(stderr, "Malloc of %d bytes failed.\n", (int)(*padW * *padH * sizeof(GLuint))); exit(1); } memset(padImg, 0, *padW * *padH * sizeof(GLuint)); for (y = 0; y < *h; y++) { memcpy(&padImg[y * *padW], &img[y * *w], *w * sizeof(GLuint)); } } /* you should use texture objects here if your system supports them... */ glTexImage2D(GL_TEXTURE_2D, 0, 4, *padW, *padH, 0, GL_RGBA, GL_UNSIGNED_BYTE, img); free(img); if (padImg) free(padImg); return 0; }
void init(void) { int width, height, components; GLubyte *image; float density, fog_density, far_cull; glClearColor (0.0, 0.0, 0.0, 0.0); glEnable(GL_DEPTH_TEST); glShadeModel(GL_SMOOTH); if (!(image = (GLubyte *)read_texture("data/sea.rgb", &width, &height, &components))) { perror("sea.rgb"); exit(EXIT_FAILURE); } glPixelStorei(GL_UNPACK_ALIGNMENT, 1); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexImage2D(GL_TEXTURE_2D, 0, 4, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, image); glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR); glTexGenfv(GL_S, GL_EYE_PLANE, s_plane); glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR); glTexGenfv(GL_T, GL_EYE_PLANE, t_plane); glEnable(GL_TEXTURE_GEN_S); glEnable(GL_TEXTURE_GEN_T); glEnable(GL_TEXTURE_2D); glEnable(GL_CULL_FACE); glEnable(GL_LIGHTING); glEnable(GL_LIGHT0); glEnable(GL_AUTO_NORMAL); glEnable(GL_NORMALIZE); glFrontFace(GL_CW); glCullFace(GL_BACK); glMaterialf (GL_FRONT, GL_SHININESS, 64.0); { int e; if (e = glGetError()) printf("error %x\n", e); } density = 1.- expf(-5.5 * fog_density * fog_density * far_cull * far_cull); #define MAX(a,b) ((a) > (b) ? (a) : (b)) #define MIN(a,b) ((a) < (b) ? (a) : (b)) density = MAX(MIN(density, 1.), 0.); glClearColor(.09f,.18f,.18f,1.f); glFogi(GL_FOG_MODE, GL_EXP); glFogf(GL_FOG_DENSITY, fog_params[0]); glFogfv(GL_FOG_COLOR, fog_params+1); glEnable(GL_FOG); { GLfloat pos[] = {0.,150.,1.,1.}; glLightfv(GL_LIGHT0, GL_POSITION, pos); } }
GLuint *load_img(char *fname, GLsizei *imgW, GLsizei *imgH) { GLuint *img; img = read_texture(fname, imgW, imgH, &comp); if (!img) { fprintf(stderr, "Could not open %s\n", fname); exit(1); } return img; }
void init(char *filename) { double l = 0; int i; func = brighten; if (filename) { image = read_texture(filename, &width, &height, &components); if (image == NULL) { fprintf(stderr, "Error: Can't load image file \"%s\".\n", filename); exit(1); } else { printf("%d x %d image loaded\n", width, height); } if (components < 3 || components > 4) { printf("must be RGB or RGBA image\n"); exit(1); } } else { int i, j; components = 4; width = height = 512; image = (unsigned *) malloc(width * height * sizeof(unsigned)); for (j = 0; j < height; j++) for (i = 0; i < width; i++) { if (i & 1) image[i + j * width] = 0xff; else image[i + j * width] = 0xff00; if (j & 1) image[i + j * width] |= 0xff0000; } } null = (unsigned *) malloc(width * height * sizeof *image); glPixelStorei(GL_UNPACK_ALIGNMENT, 1); glClearColor(.25, .25, .25, .25); /* compute luminance */ for (i = 0; i < width * height; i++) { GLubyte *p = (GLubyte *) (image + i); double r = p[0] / 255.; double g = p[1] / 255.; double b = p[2] / 255.; l += r * .3086 + g * .0820 + b * .114; } luma = l / (width * height); printf("average luminance = %f\n", luma); }
void init(char *fname) { int width, height, components; GLubyte *image; glClearColor(0.0, 0.0, 0.0, 0.0); glEnable(GL_DEPTH_TEST); glShadeModel(GL_SMOOTH); if (!(image = (GLubyte *)read_texture(fname, &width, &height, &components))) { perror(fname); exit(EXIT_FAILURE); } glPixelStorei(GL_UNPACK_ALIGNMENT, 1); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexImage2D(GL_TEXTURE_2D, 0, 4, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, image); glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP); glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP); glEnable(GL_TEXTURE_GEN_S); glEnable(GL_TEXTURE_GEN_T); glEnable(GL_TEXTURE_2D); glEnable(GL_CULL_FACE); glEnable(GL_LIGHTING); glEnable(GL_LIGHT0); glEnable(GL_NORMALIZE); glFrontFace(GL_CW); glCullFace(GL_BACK); glColorMaterial(GL_FRONT_AND_BACK, GL_DIFFUSE); glEnable(GL_COLOR_MATERIAL); #if 1 glMaterialf (GL_FRONT, GL_SHININESS, 64.0); glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, 1); { GLfloat pos[4] = { 0, 0, 1, 1}; glLightfv(GL_LIGHT0, GL_POSITION, pos); } #endif { int e; if (e = glGetError()) printf("error %x\n", e); } #if 0 glClearColor(.2,.2f,.58f,1.f); #endif glBlendFunc(GL_SRC_ALPHA, GL_ONE); glEnable(GL_BLEND); }
void init(char *filename) { if (filename) { image = read_texture(filename, &width, &height, &components); if (image == NULL) { fprintf(stderr, "Error: Can't load image file \"%s\".\n", filename); exit(1); } else { printf("%d x %d image loaded\n", width, height); } if (components < 3 || components > 4) { printf("must be RGB or RGBA image\n"); exit(1); } } else { int i, j; components = 4; width = height = 512; image = (unsigned *) malloc(width * height * sizeof(unsigned)); for (j = 0; j < height; j++) for (i = 0; i < width; i++) { if (i & 64) image[i + j * width] = 0xff; else image[i + j * width] = 0xff00; if (j & 64) image[i + j * width] |= 0xff0000; } } glPixelStorei(GL_UNPACK_ALIGNMENT, 1); glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexImage2D(GL_TEXTURE_2D, 0, components, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, image); glEnable(GL_TEXTURE_2D); glMatrixMode(GL_TEXTURE); glLoadIdentity(); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(90., 1., .1, 10.); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glTranslatef(0., 0., -1.5); glClearColor(.25, .25, .25, 0.); }
int main(int argc, char *argv[]) { glutInit(&argc, argv); glutInitWindowSize(20, 20); glutInitDisplayMode(GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE | GLUT_ACCUM); (void) glutCreateWindow("Voronoi art"); glutKeyboardFunc(key); glutDisplayFunc(redraw); glutMouseFunc(mouse); glutMotionFunc(motion); glutIdleFunc(idle); /* * Load the image and sample it */ img = (GLuint *)read_texture((argc > 1) ? argv[1] : mandrill, &w, &h, &comp); if (!img) { fprintf(stderr, "Could not open image.\n"); exit(1); } glutReshapeWindow(w, h); init(); /* * Menu */ sampleMenu = glutCreateMenu(menu); glutAddMenuEntry("More samples ('+')", '+'); glutAddMenuEntry("Fewer samples ('-')", '-'); glutAddMenuEntry("Resample points ('r')", 'r'); glutAddMenuEntry("View source image ('i')", 'i'); if (canDrawEdges) glutAddMenuEntry("Toggle drawing region edges ('e')", 'e'); glutAddMenuEntry("", ' '); glutAddMenuEntry("Paint with mouse strokes ('s')", 's'); glutAddMenuEntry("exit", '\033'); glutAttachMenu(GLUT_RIGHT_BUTTON); strokeMenu = glutCreateMenu(menu); glutAddMenuEntry("View source image ('i')", 'i'); glutAddMenuEntry("", ' '); glutAddMenuEntry("Paint with random samples ('s')", 's'); glutAddMenuEntry("exit", '\033'); glutMainLoop(); return 0; }
static void imgLoad(char *filename_in, int *w_out, int *h_out, GLubyte **img_out) { int comp; *img_out = (GLubyte *)read_texture(filename_in, w_out, h_out, &comp); if (img_out == NULL) { fprintf(stderr, "unable to read %s\n", filename_in); exit(EXIT_FAILURE); } if (comp != 3 && comp != 4) { fprintf(stderr, "%s: image is not RGB or RGBA\n", filename_in); exit(EXIT_FAILURE); } }
void init_textures(char *filename) { unsigned *buf; int width, height, components; if (filename) { buf = read_texture(filename, &width, &height, &components); if (buf == NULL) { fprintf(stderr, "Error: Can't load image file \"%s\".\n", filename); exit(1); } else { printf("%d x %d texture loaded\n", width, height); } } else { int i, j; GLubyte *p; components = 4; width = height = 512; buf = (unsigned *) malloc(width * height * sizeof(unsigned)); p = (GLubyte *) buf; for (j = 0; j < height; j++) { for (i = 0; i < width; i++) { if (i & 1) p[4 * (i + j * width) + 0] = 0xff; else p[4 * (i + j * width) + 1] = 0xff; if (j & 1) p[4 * (i + j * width) + 2] = 0xff; } } } glPixelStorei(GL_UNPACK_ALIGNMENT, 1); glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexImage2D(GL_TEXTURE_2D, 0, components, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, buf); free(buf); }
hwgm_material_t* read_material(context_t* context, const reader_material_t* material) { hwgm_material_t* out = NULL; hwgm_texture_t* texture = NULL; hwu32 offset = 0; hwu32 i; out = (hwgm_material_t*)(context->materials + context->material_pos); hwgm_material_initialize(out); offset = context->material_pos + sizeof(hwgm_material_t); textures = (hwgm_texture_t*)((hwu8*)context->materials + offset); for(i = 0; i < material->diffuse_texture_cont; ++i) { texture = read_texture(context, material->diffuse_textures + i); out->textures[i] = texture; } return out; }
void imgLoad(const char *filenameIn, int borderIn, GLfloat borderColorIn[4], int *wOut, int *hOut, GLubyte ** imgOut) { int border = borderIn; int width, height; int w, h; GLubyte *image, *img, *p; int i, j, components; image = (GLubyte *) read_texture(filenameIn, &width, &height, &components); w = width + 2 * border; h = height + 2 * border; img = (GLubyte *) calloc(w * h, 4 * sizeof(unsigned char)); p = img; for (j = -border; j < height + border; ++j) { for (i = -border; i < width + border; ++i) { if (0 <= j && j <= height - 1 && 0 <= i && i <= width - 1) { p[0] = image[4 * (j * width + i) + 0]; p[1] = image[4 * (j * width + i) + 1]; p[2] = image[4 * (j * width + i) + 2]; p[3] = 0xff; } else { p[0] = (unsigned char)(borderColorIn[0]) * 0xff; p[1] = (unsigned char)(borderColorIn[1]) * 0xff; p[2] = (unsigned char)(borderColorIn[2]) * 0xff; p[3] = (unsigned char)(borderColorIn[3]) * 0xff; } p += 4; } } free(image); *wOut = w; *hOut = h; *imgOut = img; }
void init(const char *fname) { static GLfloat lightpos[] = {.5, .75, 1.5, 1}; GLuint *img; int w, h; int comps; glEnable(GL_DEPTH_TEST); #if 0 glEnable(GL_LIGHTING); glEnable(GL_LIGHT0); #endif glEnable(GL_CULL_FACE); glLightfv(GL_LIGHT0, GL_POSITION, lightpos); cone = gluNewQuadric(); base = gluNewQuadric(); sphere = gluNewQuadric(); img = (GLuint *)read_texture(fname, &w, &h, &comps); if (!img) { fprintf(stderr, "Could not open %s\n", fname); exit(1); } floorList = glGenLists(1); glNewList(floorList, GL_COMPILE); glTexImage2D(GL_TEXTURE_2D, 0, 4, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, img); glEndList(); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); free(img); glClearColor(.25, .25, .5, 1.0); realloc_textures(); }
void load_img(const char *fname) { int i; GLubyte *src, *dst; GLfloat pix, avg; img = read_texture(fname, &w, &h, &comp); if (!img) { fprintf(stderr, "Could not open %s\n", fname); exit(1); } black = alloc_image(); memset(black, 0, w * h * sizeof(GLuint)); lum = alloc_image(); src = (GLubyte *)img; dst = (GLubyte *)lum; avg = 0.; /* compute average luminance at same time that we set luminance image. * note that little care is taken to avoid mathematical error when * computing overall average... */ for (i = 0; i < w * h; i++) { pix = (float)src[0]*RW + (float)src[1]*GW + (float)src[2]*BW; if (pix > 255) pix = 255; dst[0] = dst[1] = dst[2] = pix; avg += pix / 255.; src += 4; dst += 4; } avgLum = alloc_image(); pix = avg * 255. / (float)(w*h); dst = (GLubyte *)avgLum; for (i = 0; i < w * h; i++) { dst[0] = dst[1] = dst[2] = pix; dst += 4; } }
/* Parse arguments, and set up interface between OpenGL and window system */ int main(int argc, char *argv[]) { int comp; glutInit(&argc, argv); glutInitWindowSize(512, 512); glutInitDisplayMode(GLUT_RGBA|GLUT_ACCUM); (void)glutCreateWindow("dither"); glutReshapeFunc(reshape); glutKeyboardFunc(key); /* create_menu(); */ img = read_texture((argc > 1) ? argv[1] : "../../data/mandrill_small.rgb", &image_w, &image_h, &comp); if (!img) { fprintf(stderr, "Could not open image.\n"); exit(1); } glutDisplayFunc(redraw); CHECK_ERROR("main"); glutMainLoop(); return 0; }
static void loadtiles(void) { int lx, rx, ty, by; /* image bounding box */ static int ox = TILES*TILESIZE/2, oy = TILES*TILESIZE/2; /* image origin */ static int ot = 0, os = 0; int dx = 0, dy = 0, nx = -1, ny = -1; float trx, try; #define S_TSIZE (TSIZE-TILESIZE) /* visible portion of texture = TSIZE less one tile for slop */ /* calculate tile #'s at corners of visible region */ lx = x - S_TSIZE/2; rx = lx + S_TSIZE; by = y - S_TSIZE/2; ty = by + S_TSIZE; lx /= TILESIZE; rx /= TILESIZE; by /= TILESIZE; ty /= TILESIZE; dx = ((x - S_TSIZE/2)/TILESIZE) - ((ox - S_TSIZE/2)/TILESIZE); nx = lx; ny = by; if (dx < 0) { /* add on left */ os -= TILESIZE; if (os < 0) os += TSIZE; nx = lx; } else if (dx > 0) { nx = rx; } dy = ((y - S_TSIZE/2) / TILESIZE) - ((oy - S_TSIZE/2) / TILESIZE); if (dy > 0) { /* add on bottom */ ny = ty; } else if (dy < 0) { /* add on top */ ot -= TILESIZE; if (ot < 0) ot += TSIZE; ny = by; } if (dx || dy) printf("dx %d dy %d lx %d rx %d by %d ty %d nx %d ny %d os %d ot %d\n", dx, dy, lx, rx, by, ty, nx, ny, os, ot); if (dx) { int t; for(t = 0; t < TSIZE; t += TILESIZE) { glTexSubImage2D(GL_TEXTURE_2D, 0, os, (t+ot) % TSIZE, TILESIZE, TILESIZE, GL_RGBA, GL_UNSIGNED_BYTE, tiles[ny+t/TILESIZE][nx].data); printf("load %d %d %d %d\n", nx, ny+t/TILESIZE, os, (t+ot) % TSIZE); } } if (dy) { int s; for(s = 0; s < TSIZE; s += TILESIZE) { glTexSubImage2D(GL_TEXTURE_2D, 0, (s+os) % TSIZE, ot, TILESIZE, TILESIZE, GL_RGBA, GL_UNSIGNED_BYTE, tiles[ny][nx+s/TILESIZE].data); printf("load %d %d %d %d\n", nx+s/TILESIZE, ny, (s+os) % TSIZE, ot); } } if (dx > 0) { os += TILESIZE; if (os >= TSIZE) os -= TSIZE; } if (dy > 0) { ot += TILESIZE; if (ot >= TSIZE) ot -= TSIZE; } ox = x; oy = y; glMatrixMode(GL_TEXTURE); glLoadIdentity(); trx = (float)((x-TILES*TILESIZE/2) % TSIZE)/TSIZE; try = (float)((y-TILES*TILESIZE/2) % TSIZE)/TSIZE; glTranslatef(trx, try, 0.f); glMatrixMode(GL_MODELVIEW); } static void init(char *filename) { int i; mesh0(-1.f,1.f,-1.f,1.f,0.f,1.f,0.f,1.f,0.f,64,64); if (filename) { image = read_texture(filename, &width, &height, &components); if (image == NULL) { fprintf(stderr, "Error: Can't load image file \"%s\".\n", filename); exit(EXIT_FAILURE); } else { printf("%d x %d image loaded\n", width, height); } if (components < 3 || components > 4) { printf("must be RGB or RGBA image\n"); exit(EXIT_FAILURE); } } else { int i, j; components = 4; width = height = TSIZE; image = (unsigned *) malloc(width*height*sizeof(unsigned)); for (j = 0; j < height; j++) for (i = 0; i < width; i++) { if (i & 1) image[i+j*width] = 0xff; else image[i+j*width] = 0xff00; if (j&1) image[i+j*width] |= 0xff0000; } } if (width % TILESIZE || height % TILESIZE) { #define TXSIZE 192 unsigned *newimage = malloc(TXSIZE*TXSIZE*sizeof *newimage); gluScaleImage(GL_RGBA, width, height, GL_UNSIGNED_BYTE, image, TXSIZE, TXSIZE, GL_UNSIGNED_BYTE, newimage); free(image); image = newimage; width = height = TXSIZE; components = 4; } tile_image(image); glPixelStorei(GL_UNPACK_ALIGNMENT, 1); glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_REPEAT); glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_REPEAT); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, TSIZE, TSIZE, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL); for(i = 0; i < TILES; i++) { int j; for(j = 0; j < TILES; j++) { glTexSubImage2D(GL_TEXTURE_2D, 0, i*TILESIZE, j*TILESIZE, TILESIZE, TILESIZE, GL_RGBA, GL_UNSIGNED_BYTE, tiles[(TILES-TSIZE/TILESIZE)/2+j][(TILES-TSIZE/TILESIZE)/2+i].data); } } glEnable(GL_TEXTURE_2D); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(90.,1.,.1,10.); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glTranslatef(0.,0.,-1.0); glLineWidth(3.0); glClearColor(.25, .25, .25, .25); /* start at center of image */ x = TILES*TILESIZE/2; y = TILES*TILESIZE/2; }
/* Load BMF file format, function return bitmap font descriptor */ int glfLoadBMFFont(char *FName) { FILE *f; char Header[4]; char FontName[97]; int i, flag; int LEndian; float tx, ty, tw, th; unsigned char temp, *tp; unsigned *texture; /* Texture image */ unsigned *mask; /* Mask texture */ int twidth, theight, tcomp; /* Image parameters */ float *temp_width; LEndian = LittleEndian(); fopen_s(&f, FName, "rb"); if (f == NULL) return GLF_ERROR; /* Error opening file */ /* Get header */ fread(Header, 1, 3, f); Header[3] = 0; if (strcmp(Header, "BMF")) return GLF_ERROR; /* Not BMF format */ /* Get font name */ fread(FontName, 1, 96, f); FontName[96] = 0; /* Allocate space for temp widths */ temp_width = (float *)malloc(sizeof(float)*256); /* Read all 256 symbols information */ for (i=0; i<256; i++) { fread(&tx, 4, 1, f); fread(&ty, 4, 1, f); fread(&tw, 4, 1, f); fread(&th, 4, 1, f); if (!LEndian) { tp = (unsigned char *)&tx; temp = tp[0]; tp[0] = tp[3]; tp[3] = temp; temp = tp[1]; tp[1] = tp[2]; tp[2] = temp; tp = (unsigned char *)&ty; temp = tp[0]; tp[0] = tp[3]; tp[3] = temp; temp = tp[1]; tp[1] = tp[2]; tp[2] = temp; tp = (unsigned char *)&tw; temp = tp[0]; tp[0] = tp[3]; tp[3] = temp; temp = tp[1]; tp[1] = tp[2]; tp[2] = temp; tp = (unsigned char *)&th; temp = tp[0]; tp[0] = tp[3]; tp[3] = temp; temp = tp[1]; tp[1] = tp[2]; tp[2] = temp; } Symbols[i].x = tx; Symbols[i].y = ty; Symbols[i].width = tw; Symbols[i].height = th; temp_width[i] = tw; } /* Read texture image from file and build texture */ texture = read_texture(f, &twidth, &theight, &tcomp); /* Generate mask texture */ mask = texture_to_mask(texture, twidth, theight); /* Find unused font descriptor */ flag = 0; for (i=0; i<MAX_FONTS; i++) if (bmf_in_use[i] == 0) { /* Initialize this font */ bmf_in_use[i] = 1; bmf_curfont = i; flag = 1; break; } if (!flag) /* Not enought space for new texture */ { fclose(f); free(texture); free(mask); free(temp_width); return -1; } m_widths[bmf_curfont].width = temp_width; /* Generating textures for font and mask */ glGenTextures(1, &bmf_texture[bmf_curfont]); glGenTextures(1, &bmf_mask[bmf_curfont]); glPixelStorei(GL_UNPACK_ALIGNMENT, 1); /* Build font texture */ glBindTexture(GL_TEXTURE_2D, bmf_texture[bmf_curfont]); glTexImage2D(GL_TEXTURE_2D, 0, 3, twidth, theight, 0, GL_RGBA, GL_UNSIGNED_BYTE, texture); /* Linear filtering for better quality */ glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); /* Build mask texture */ glBindTexture(GL_TEXTURE_2D, bmf_mask[bmf_curfont]); glTexImage2D(GL_TEXTURE_2D, 0, 3, twidth, theight, 0, GL_RGBA, GL_UNSIGNED_BYTE, mask); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); free(texture); free(mask); fclose(f); /* Now build list for each symbol */ list_base[bmf_curfont] = glGenLists(256); for (i=0; i<256; i++) { glNewList(list_base[bmf_curfont]+i, GL_COMPILE); glBegin(GL_QUADS); glTexCoord2f(Symbols[i].x, Symbols[i].y); glVertex2f(0, 0); glTexCoord2f(Symbols[i].x+Symbols[i].width, Symbols[i].y); glVertex2f(Symbols[i].width, 0); glTexCoord2f(Symbols[i].x+Symbols[i].width, Symbols[i].y+Symbols[i].height); glVertex2f(Symbols[i].width, Symbols[i].height); glTexCoord2f(Symbols[i].x, Symbols[i].y+Symbols[i].height); glVertex2f(0, Symbols[i].height); glEnd(); glTranslatef(Symbols[i].width+sym_space, 0, 0); glEndList(); if (Symbols[i].height > m_max_height[bmf_curfont]) m_max_height[bmf_curfont] = Symbols[i].height; } return bmf_curfont; }
texfnt *readtexfont(char *name) { texfnt *tfnt; unsigned char *image; unsigned char *cptr; unsigned short *sbuf, *sptr; short advancecell, xadvance; short llx, lly, urx, ury, ox, oy; int i, y, extralines; texchardesc *cd; int xsize, ysize, components; tfnt = (texfnt *)malloc(sizeof(texfnt)); image = (unsigned char*)read_texture(name, &xsize, &ysize, &components); if(!image) { fprintf(stderr,"textmap: can't open font image %s\n",name); return 0; } extralines = ysize-xsize; if(extralines<1) { fprintf(stderr,"textmap: bad input font!!\n"); return 0; } fb = (unsigned char *)malloc(xsize*extralines); sbuf = (unsigned short *)malloc(xsize*sizeof(short)); cptr = fb; for(y=xsize; y<ysize; y++) { int x; for(x = 0; x < xsize; x++) cptr[x] = image[4*(y*xsize+x)]; cptr += xsize; } initget(); tfnt->rasxsize = xsize; tfnt->rasysize = xsize; getbytes(&tfnt->charmin,sizeof(short)); /*printf("charmin 0x%x\n", tfnt->charmin);*/ getbytes(&tfnt->charmax,sizeof(short)); getbytes(&tfnt->pixhigh,sizeof(float)); getbytes(&advancecell,sizeof(short)); tfnt->nchars = tfnt->charmax-tfnt->charmin+1; tfnt->chars = (texchardesc *)malloc(tfnt->nchars*sizeof(texchardesc)); tfnt->rasdata = (unsigned short *)malloc(tfnt->rasxsize*tfnt->rasysize*sizeof(long)); sptr = tfnt->rasdata; for(y=0; y<tfnt->rasysize; y++) { int x; for(x = 0; x < xsize; x++) { unsigned t = image[4*(y*xsize+x)]; sptr[x] = t | t << 8; } fixrow(sptr,tfnt->rasxsize); sptr += tfnt->rasxsize; } cd = tfnt->chars; for(i=0; i<tfnt->nchars; i++) { /*int c = tfnt->charmin+i;*/ getbytes(&xadvance,sizeof(short)); getbytes(&llx,sizeof(short)); getbytes(&lly,sizeof(short)); getbytes(&urx,sizeof(short)); getbytes(&ury,sizeof(short)); getbytes(&ox,sizeof(short)); getbytes(&oy,sizeof(short)); cd->movex = xadvance/(float)advancecell; if(llx>=0) { cd->haveimage = 1; cd->llx = (llx-ox)/tfnt->pixhigh; cd->lly = (lly-oy)/tfnt->pixhigh; cd->urx = (urx-ox+1)/tfnt->pixhigh; cd->ury = (ury-oy+1)/tfnt->pixhigh; cd->tllx = llx/(float)tfnt->rasxsize; cd->tlly = lly/(float)tfnt->rasysize; cd->turx = (urx+1)/(float)tfnt->rasxsize; cd->tury = (ury+1)/(float)tfnt->rasysize; cd->data[0] = cd->tllx; cd->data[1] = cd->tlly; cd->data[2] = cd->llx; cd->data[3] = cd->lly; cd->data[4] = cd->turx; cd->data[5] = cd->tlly; cd->data[6] = cd->urx; cd->data[7] = cd->lly; cd->data[8] = cd->turx; cd->data[9] = cd->tury; cd->data[10] = cd->urx; cd->data[11] = cd->ury; cd->data[12] = cd->tllx; cd->data[13] = cd->tury; cd->data[14] = cd->llx; cd->data[15] = cd->ury; cd->data[16] = cd->llx; cd->data[17] = cd->lly; cd->data[18] = cd->urx; cd->data[19] = cd->lly; cd->data[20] = cd->urx; cd->data[21] = cd->ury; cd->data[22] = cd->llx; cd->data[23] = cd->ury; } else { cd->haveimage = 0; } cd++; } free(fb); free(sbuf); free(image); return tfnt; }
void init(int argc, char *argv[]) { unsigned *image; int i, width, height, components; GLfloat pos[] = { 0.f, 1.f, 1.f, 0.f}; glEnable(GL_TEXTURE_2D); glPixelStorei(GL_UNPACK_ALIGNMENT, 1); if (argc < 1) { printf("usage: fire [textures]\n"); printf(" e.g., fire flame/*\n\n"); /* */ exit(0); } for(i = 0; i < argc; i++) { image = read_texture(argv[i], &width, &height, &components); if (image == NULL) { fprintf(stderr, "Error: Can't load image file \"%s\".\n", argv[i]); exit(EXIT_FAILURE); } else { printf("%d x %d image loaded\n", width, height); } glBindTexture(GL_TEXTURE_2D, i+1); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP); glTexImage2D(GL_TEXTURE_2D, 0, components, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, image); texture_count++; } glBindTexture(GL_TEXTURE_2D, 1+texture_count); image = read_texture("data/smoke.la", &width, &height, &components); if (image == NULL) { fprintf(stderr, "Error: Can't load image file \"%s\".\n", "smoke.la"); exit(EXIT_FAILURE); } glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP); glTexImage2D(GL_TEXTURE_2D, 0, components, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, image); smoke = new_smoke(0.f, 0.f, 0.f, .0f, 2.5f, 0.f, 25, .4f, 1+texture_count); glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); glEnable(GL_TEXTURE_2D); glClearColor(.25f, .25f, .25f, .25f); glLightfv(GL_LIGHT0, GL_POSITION, pos); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(50.,1.,.1,20.); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glTranslatef(0.,0.,-5.5); glClearColor(.25f, .25f, .75f, .25f); glAlphaFunc(GL_GREATER, 0.016); glEnable(GL_ALPHA_TEST); glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glEnable(GL_DEPTH_TEST); glEnable(GL_LIGHT0); glEnable(GL_NORMALIZE); }
void init(char *filename) { static GLfloat plane_mat[] = { 1.f, 1.f, .2f, 1.f }; static unsigned *image; static int width, height, components, i; if (filename) { image = read_texture(filename, &width, &height, &components); if (image == NULL) { fprintf(stderr, "Error: Can't load image file \"%s\".\n", filename); exit(EXIT_FAILURE); } else { printf("%d x %d image loaded\n", width, height); } if (components != 2 && components != 4) { printf("must be an rgba or la image\n"); exit(EXIT_FAILURE); } for(i = 0; i < width*height; i++) image[i] = image[i] | 0xffffff00; } else { int i, j; unsigned char *img; components = 4; width = height = 512; image = (unsigned *) malloc(width*height*sizeof(unsigned)); img = (unsigned char *)image; for (j = 0; j < height; j++) for (i = 0; i < width; i++) { int w2 = width/2, h2 = height/2; if (i & 32) img[4*(i+j*width)+0] = 0xff; else img[4*(i+j*width)+1] = 0xff; if (j&32) img[4*(i+j*width)+2] = 0xff; if ((i-w2)*(i-w2) + (j-h2)*(j-h2) > 64*64 && (i-w2)*(i-w2) + (j-h2)*(j-h2) < 300*300) img[4*(i+j*width)+3] = 0xff; } } glPixelStorei(GL_UNPACK_ALIGNMENT, 1); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); glTexImage2D(GL_TEXTURE_2D, 0, components, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, image); glEnable(GL_TEXTURE_2D); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(50.,1.,.1,10.); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glTranslatef(0.,0.,-5.5); glEnable(GL_LIGHT0); glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, plane_mat); glClearColor(0.1, 0.1, 0.6, 1.0); glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glEnable(GL_ALPHA_TEST); glAlphaFunc(GL_GREATER, 0./255.); glEnable(GL_DEPTH_TEST); }
int main(int argc, char *argv[]) { unsigned *cloud; int texcomps, texwid, texht; GLUquadricObj *sphere; /* start and end of particles */ static GLfloat begin[] = {0.f, -25.f, 0.f}; static GLfloat end[] = {0.f,-100.f, 0.f}; static GLfloat fogcolor[] = {.4f, .4f, .4f, 1.f}; glutInitWindowSize(winwid, winht); glutInit(&argc, argv); if(argc > 1) { char *args = argv[1]; int done = GL_FALSE; while(!done) { switch(*args) { case 's': /* single buffer */ printf("Single Buffered\n"); dblbuf = GL_FALSE; break; case '-': /* do nothing */ break; case 0: done = GL_TRUE; break; } args++; } } if(dblbuf) glutInitDisplayMode(GLUT_RGBA|GLUT_DEPTH|GLUT_STENCIL|GLUT_DOUBLE); else glutInitDisplayMode(GLUT_RGBA|GLUT_DEPTH|GLUT_STENCIL); (void)glutCreateWindow("snow demo"); glutDisplayFunc(redraw); glutReshapeFunc(reshape); glutMouseFunc(mouse); glutMotionFunc(motion); glutKeyboardFunc(key); glutIdleFunc(idle); glutCreateMenu(menu); glutAddMenuEntry("Toggle Overcast (o, O)", OVERCAST); glutAddMenuEntry("Toggle Fog (f, F)", FOG); glutAddMenuEntry("Toggle Snow (s, S)", SNOW); glutAddMenuEntry("Bigger Flakes (+)", BIGGER); glutAddMenuEntry("Smaller Flakes (-)", SMALLER); glutAddMenuEntry("Reset Flake Size to One (r, R)", RESETSIZE); glutAddMenuEntry("Toggle Point Antialiasing (a, A)", ANTIALIAS); glutAddMenuEntry("Snow Blending (b, B)", BLEND); glutAddMenuEntry("Exit Program", EXIT); glutAttachMenu(GLUT_RIGHT_BUTTON); printf("OpenGL Version %s\n", glGetString(GL_VERSION)); /* draw a perspective scene */ glMatrixMode(GL_PROJECTION); glFrustum(-5., 5., -5., 5., 10., 1000.); glMatrixMode(GL_MODELVIEW); updateMV(); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); /* turn on features */ glEnable(GL_DEPTH_TEST); glEnable(GL_LIGHTING); glEnable(GL_LIGHT0); /* place light 0 in the right place */ glLightfv(GL_LIGHT0, GL_POSITION, lightpos); glClearColor(0.f, 0.f, 1.f, 1.f); glFogfv(GL_FOG_COLOR, fogcolor); glFogi(GL_FOG_MODE, GL_LINEAR); glFogf(GL_FOG_START, -200.f); glFogf(GL_FOG_END, 200.f); glHint(GL_FOG_HINT, GL_NICEST); glEnable(GL_COLOR_MATERIAL); glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE); /* makes texturing faster, and looks better than GL_LINEAR */ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); glNewList(LIGHT, GL_COMPILE); glDisable(GL_LIGHTING); sphere = gluNewQuadric(); glColor3f(.7f, .2f, .7f); gluSphere(sphere, 5.f, 10, 10); gluDeleteQuadric(sphere); glEnable(GL_LIGHTING); glEndList(); /* 10 X 20; vary size with transforms */ /* one corner of house on origin; bottom on xz plane */ glNewList(HOUSE, GL_COMPILE); glBegin(GL_QUADS); /* walls of house */ glColor3f(1.f, 1.f, 0.f); /* front */ glNormal3f( 0.f, 0.f, 1.f); glVertex3i( 0, 0, 0); glVertex3i(10, 0, 0); glVertex3i(10,10, 0); glVertex3i( 0,10, 0); /* back */ glNormal3f( 0.f, 0.f, -1.f); glVertex3i( 0, 0, -20); glVertex3i( 0,10, -20); glVertex3i(10,10, -20); glVertex3i(10, 0, -20); /* left */ glNormal3f(-1, 0.f, 0.f); glVertex3i( 0, 0, 0); glVertex3i( 0, 10, 0); glVertex3i( 0, 10, -20); glVertex3i( 0, 0, -20); /* right */ glNormal3f( 1.f, 0.f, 0.f); glVertex3i(10, 0, 0); glVertex3i(10, 0, -20); glVertex3i(10, 10, -20); glVertex3i(10, 10, 0); /* roof of house */ glColor3f(.8f, .1f, .1f); /* left top */ glNormal3f(-.707f, .707f, 0.f); glVertex3i( 0, 10, 0); glVertex3i( 5, 15, 0); glVertex3i( 5, 15, -20); glVertex3i( 0, 10, -20); /* right top */ glNormal3f( .707f, .707f, 0.f); glVertex3i(10, 10, 0); glVertex3i(10, 10, -20); glVertex3i( 5, 15, -20); glVertex3i( 5, 15, 0); glEnd(); glBegin(GL_TRIANGLES); /* front */ glNormal3f( 0.f, 0.f, 1.f); glVertex3i( 0, 10, 0); glVertex3i(10, 10, 0); glVertex3i( 5, 15, 0); /* back */ glNormal3f( 0.f, 0.f, -1.f); glVertex3i( 0, 10, -20); glVertex3i( 5, 15, -20); glVertex3i(10, 10, -20); glEnd(); glEndList(); glEnable(GL_CULL_FACE); /* load pattern for current 2d texture */ cloud = read_texture("../../data/clouds.bw", &texwid, &texht, &texcomps); gluBuild2DMipmaps(GL_TEXTURE_2D, GL_LUMINANCE, texwid, texht, GL_RGBA, GL_UNSIGNED_BYTE, cloud); glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_BLEND); free(cloud); initpart(&psys, begin, end, 200, 6000); updateptr = updatepart0; CHECK_ERROR("main()"); glutMainLoop(); return 0; }
void init(char *filename) { GLfloat cloud_color[4] = { 1., 1., 1., 0., }; GLfloat fog_color[4], fog_density = 0.05, density, far_cull; unsigned *image; int width, height, components; if (filename) { image = read_texture(filename, &width, &height, &components); if (image == NULL) { fprintf(stderr, "Error: Can't load image file \"%s\".\n", filename); exit(EXIT_FAILURE); } else { printf("%d x %d image loaded\n", width, height); } if (components != 1) { printf("warning: texture should be a bw image (single component)\n"); } } else { int i, j; unsigned char *img; components = 4; width = height = 512; image = (unsigned *) malloc(width*height*sizeof(unsigned)); img = (unsigned char *)image; for (j = 0; j < height; j++) for (i = 0; i < width; i++) { int w2 = width/2, h2 = height/2; if (i & 32) img[4*(i+j*width)+0] = 0xff; else img[4*(i+j*width)+1] = 0xff; if (j&32) img[4*(i+j*width)+2] = 0xff; if ((i-w2)*(i-w2) + (j-h2)*(j-h2) > 64*64 && (i-w2)*(i-w2) + (j-h2)*(j-h2) < 300*300) img[4*(i+j*width)+3] = 0xff; } } glPixelStorei(GL_UNPACK_ALIGNMENT, 1); glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_BLEND); glTexEnvfv(GL_TEXTURE_ENV, GL_TEXTURE_ENV_COLOR, cloud_color); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); glTexImage2D(GL_TEXTURE_2D, 0, components, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, image); glEnable(GL_TEXTURE_2D); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(50.,1.,.1,far_cull = 10.); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glTranslatef(0.,0.,-5.5); density = 1.- expf(-5.5 * fog_density * fog_density * far_cull * far_cull); #define MAX(a,b) ((a) > (b) ? (a) : (b)) #define MIN(a,b) ((a) < (b) ? (a) : (b)) density = MAX(MIN(density, 1.), 0.); fog_color[0] = .23 + density *.57; fog_color[1] = .35 + density *.45; fog_color[2] = .78 + density *.22; glClearColor(fog_color[0], fog_color[1], fog_color[2], 1.f); glFogi(GL_FOG_MODE, GL_EXP2); glFogf(GL_FOG_DENSITY, fog_density); glFogfv(GL_FOG_COLOR, fog_color); if (fog_density > 0) glEnable(GL_FOG); }
void init(char *filename) { GLfloat fog_color[4], fog_density = 0.05, density, far_cull; unsigned *image; int width, height, components; if (filename) { image = read_texture(filename, &width, &height, &components); if (image == NULL) { fprintf(stderr, "Error: Can't load image file \"%s\".\n", filename); exit(EXIT_FAILURE); } else { printf("%d x %d image loaded\n", width, height); } if (components != 1 && components != 2) { printf("must be a l or la image\n"); exit(EXIT_FAILURE); } if (components == 1) { /* hack for RE */ int i; GLubyte *p = (GLubyte *)image; for(i = 0; i < width*height; i++) { p[i*4+3] = p[i*4+0]; } components = 2; } } else { int i, j; unsigned char *img; components = 4; width = height = 512; image = (unsigned *) malloc(width*height*sizeof(unsigned)); img = (unsigned char *)image; for (j = 0; j < height; j++) for (i = 0; i < width; i++) { int w2 = width/2, h2 = height/2; if (i & 32) img[4*(i+j*width)+0] = 0xff; else img[4*(i+j*width)+1] = 0xff; if (j&32) img[4*(i+j*width)+2] = 0xff; if ((i-w2)*(i-w2) + (j-h2)*(j-h2) > 64*64 && (i-w2)*(i-w2) + (j-h2)*(j-h2) < 300*300) img[4*(i+j*width)+3] = 0xff; } } glPixelStorei(GL_UNPACK_ALIGNMENT, 1); glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); glTexImage2D(GL_TEXTURE_2D, 0, components, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, image); /*glEnable(GL_TEXTURE_2D);*/ glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(50.,1.,.1,far_cull = 10.); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glTranslatef(0.,0.,-5.5); density = 1.- expf(-5.5 * fog_density * fog_density * far_cull * far_cull); #define MAX(a,b) ((a) > (b) ? (a) : (b)) #define MIN(a,b) ((a) < (b) ? (a) : (b)) density = MAX(MIN(density, 1.), 0.); fog_color[0] = .23*.2 + density *.57*.2; fog_color[1] = .35*.2 + density *.45*.2; fog_color[2] = .78*.5 + density *.22*.2; glClearColor(fog_color[0], fog_color[1], fog_color[2], 1.f); glFogi(GL_FOG_MODE, GL_EXP2); glFogf(GL_FOG_DENSITY, fog_density); glFogfv(GL_FOG_COLOR, fog_color); if (fog_density > 0) glEnable(GL_FOG); glLineWidth(2.0f); glEnable(GL_LINE_SMOOTH); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glPointSize(10.f); glEnable(GL_POINT_SMOOTH); glHint(GL_POINT_SMOOTH_HINT, GL_NICEST); }
GLubyte * loadtex3d(int *texwid, int *texht, int *texdepth, int *texcomps) { char *filename; GLubyte *tex3ddata; GLuint *texslice; /* 2D slice of 3D texture */ GLint max3dtexdims; /* maximum allowed 3d texture dimension */ GLint newval; int i; /* load 3D texture data */ filename = (char*)malloc(sizeof(char) * strlen("../data/skull/skullXX.la")); tex3ddata = (GLubyte *)malloc(Texwid * Texht * Texdepth * 4 * sizeof(GLubyte)); for(i = 0; i < Texdepth; i++) { glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); sprintf(filename, "../data/skull/skull%d.la", i); /* read_texture reads as RGBA */ texslice = read_texture(filename, texwid, texht, texcomps); if(!texslice) { fprintf(stderr, "Couldn't read texture file skull%d.1a\n", i); exit -1; } memcpy(&tex3ddata[i * Texwid * Texht * 4], /* copy in a slice */ texslice, Texwid * Texht * 4 * sizeof(GLubyte)); free(texslice); } free(filename); *texdepth = Texdepth; #ifdef GL_EXT_texture3D glGetIntegerv(GL_MAX_3D_TEXTURE_SIZE_EXT, &max3dtexdims); fprintf(stderr, "Maximum 3d texture size: %d\n", max3dtexdims); #endif /* adjust width */ newval = *texwid; if(*texwid > max3dtexdims) newval = max3dtexdims; if(NOTPOW2(*texwid)) newval = makepow2(*texwid); fprintf(stderr, "Texture width: %d Adjusted texture width: %d\n", *texwid, newval); if(newval != *texwid) { glPixelStorei(GL_UNPACK_ROW_LENGTH, *texwid); glPixelStorei(GL_UNPACK_SKIP_PIXELS, (*texwid - newval)/2); *texwid = newval; } /* adjust height */ newval = *texht; if(*texht > max3dtexdims) newval = max3dtexdims; if(NOTPOW2(*texht)) newval = makepow2(*texht); fprintf(stderr, "Texture height: %d Adjusted texture height: %d\n", *texht, newval); if(*texht > newval) { #ifdef GL_EXT_texture3D glPixelStorei(GL_UNPACK_IMAGE_HEIGHT_EXT, *texht); #endif glPixelStorei(GL_UNPACK_SKIP_ROWS, (*texht - newval)/2); *texht = newval; } /* adjust depth */ newval = *texdepth; if(*texdepth > max3dtexdims) newval = max3dtexdims; if(NOTPOW2(*texdepth)) newval = makepow2(*texdepth); fprintf(stderr, "Texture depth: %d Adjusted texture depth: %d\n", *texdepth, newval); if(*texdepth > newval) { *texdepth = newval; } return tex3ddata; }
void init(char *filename) { static unsigned *image; static int width, height, components; if (filename) { image = read_texture(filename, &width, &height, &components); if (image == NULL) { fprintf(stderr, "Error: Can't load image file \"%s\".\n", filename); exit(EXIT_FAILURE); } else { printf("%d x %d image loaded\n", width, height); } if (components != 2 && components != 4) { printf("must be an RGBA or LA image\n"); exit(EXIT_FAILURE); } } else { int i, j; unsigned char *img; components = 4; width = height = 512; image = (unsigned *) malloc(width*height*sizeof(unsigned)); img = (unsigned char *)image; for (j = 0; j < height; j++) for (i = 0; i < width; i++) { int w2 = width/2, h2 = height/2; if (i & 32) img[4*(i+j*width)+0] = 0xff; else img[4*(i+j*width)+1] = 0xff; if (j&32) img[4*(i+j*width)+2] = 0xff; if ((i-w2)*(i-w2) + (j-h2)*(j-h2) > 64*64 && (i-w2)*(i-w2) + (j-h2)*(j-h2) < 300*300) img[4*(i+j*width)+3] = 0xff; } } glPixelStorei(GL_UNPACK_ALIGNMENT, 1); glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexImage2D(GL_TEXTURE_2D, 0, components, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, image); glEnable(GL_TEXTURE_2D); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(50.,1.,.1,20.); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glTranslatef(0.,0.,-5.5); glClearColor(.25f, .25f, .75f, .25f); glAlphaFunc(GL_GREATER, 0.016); glEnable(GL_ALPHA_TEST); glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glEnable(GL_DEPTH_TEST); glEnable(GL_LIGHT0); glEnable(GL_COLOR_MATERIAL); glEnable(GL_NORMALIZE); }