Texture* SolidColorTextureCache::CreateFor(const Color &color, Texture *existing) { LOG_INFO(LOGCAT_ASSETS, "SolidColorTextureCache: creating texture for color 0x%8x.\n", color.ToInt()); Image *img = new Image(); bool imageCreateSuccess = img->Create(8, 8, IMAGE_FORMAT_RGBA); ASSERT(imageCreateSuccess == true); Texture *texture; if (existing != NULL) { texture = existing; texture->Release(); } else texture = new Texture(); img->Clear(color); bool success = texture->Create(m_graphicsDevice, img); SAFE_DELETE(img); if (!success) { // if we allocated the texture ourselves, we should free it if (existing != NULL) { SAFE_DELETE(texture); } } return texture; }
bool PackedTexture::InsertImage(const Image & pImg, int & pOffsetU, int & pOffsetV, unsigned int & pTextureIndex) { // Validate image size if(pImg.GetWidth() > mTexSize || pImg.GetHeight() > mTexSize) { return false; } // Copy to one of the existing image TextureTree::Node * node = NULL; unsigned int nodeTree = 0; for(unsigned int i = 0; i < mTexTrees.size(); i++) { node = mTexTrees[i]->InsertImage( pImg ); nodeTree = i; } // No space found, create a new tree if(!node) { mTexTrees.push_back(new TextureTree(mTexSize)); Image* newPage = new Image(); newPage->Create(mTexSize, mTexSize, mTexFormat); newPage->Clear(); mImages.push_back(newPage); node = mTexTrees[mTexTrees.size() - 1]->InsertImage(pImg); nodeTree = mTexTrees.size() - 1; } // A node must have been found. arx_assert(node); // Copy texture there if(node) { mImages[nodeTree]->Copy( pImg, node->mRect.left, node->mRect.top ); // Copy values back into info structure. pOffsetU = node->mRect.left; pOffsetV = node->mRect.top; pTextureIndex = nodeTree; } return node != NULL; }
/// Initialize the texture. void GLViewer::InitTexture(const char * aFileName) { Image image; // Load texture from a file if(aFileName) { // Check if file exists, and determine actual file name (relative or absolute) bool fileExists = false; string name = string(aFileName); FILE * inFile = fopen(name.c_str(), "rb"); if(inFile) fileExists = true; else if(mFilePath.size() > 0) { // Try the same path as the mesh file name = mFilePath + string(aFileName); inFile = fopen(name.c_str(), "rb"); if(inFile) fileExists = true; } if(inFile) fclose(inFile); if(fileExists) { cout << "Loading texture (" << aFileName << ")..." << endl; try { image.LoadFromFile(name.c_str()); } catch(exception &e) { cout << "Error loading texture: " << e.what() << endl; image.Clear(); } } } // If no texture was loaded if(image.IsEmpty()) { cout << "Loading texture (dummy)..." << endl; // Create a default, synthetic texture image.SetSize(256, 256, 1); for(int y = 0; y < image.mHeight; ++ y) { for(int x = 0; x < image.mWidth; ++ x) { if(((x & 0x000f) == 0) || ((y & 0x000f) == 0)) image.mData[y * image.mWidth + x] = 192; else image.mData[y * image.mWidth + x] = 255; } } } // Upload the texture to OpenGL if(!image.IsEmpty()) glGenTextures(1, &mTexHandle); else mTexHandle = 0; if(mTexHandle) { // Determine the color format GLuint format; if(image.mComponents == 3) format = GL_RGB; else if(image.mComponents == 4) format = GL_RGBA; else format = GL_LUMINANCE; glBindTexture(GL_TEXTURE_2D, mTexHandle); if(GLEW_VERSION_1_4) { // Generate mipmaps automatically and use them glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_TRUE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); } else { 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, image.mComponents, image.mWidth, image.mHeight, 0, format, GL_UNSIGNED_BYTE, (GLvoid *) &image.mData[0]); } }
bool TextureContainer::CreateHalo() { Image srcImage; if(!srcImage.LoadFromFile(m_pTexture->getFileName())) { return false; } // Allocate and add the texture to the linked list of textures; res::path haloName = m_texName.string(); haloName.append("_halo"); TextureHalo = new TextureContainer(haloName, NoMipmap | NoColorKey); if(!TextureHalo) { return false; } TextureHalo->m_pTexture = GRenderer->CreateTexture2D(); if(!TextureHalo->m_pTexture) { return true; } Image im; int width = m_size.x + HALO_RADIUS * 2; int height = m_size.y + HALO_RADIUS * 2; im.Create(width, height, srcImage.GetFormat()); // Center the image, offset by radius to contain the edges of the blur im.Clear(); im.Copy(srcImage, HALO_RADIUS, HALO_RADIUS); // Keep a copy of the image at this stage, in order to apply proper alpha masking later Image copy = im; // Convert image to grayscale, and turn it to black & white im.ToGrayscale(Image::Format_L8A8); im.ApplyThreshold(0, ~0); // Blur the image im.Blur(HALO_RADIUS); // Increase the gamma of the blur outline im.QuakeGamma(10.0f); // Set alpha to inverse of original image alpha copy.ApplyColorKeyToAlpha(); im.SetAlpha(copy, true); TextureHalo->m_pTexture->Init(im, 0); TextureHalo->m_size.x = TextureHalo->m_pTexture->getSize().x; TextureHalo->m_size.y = TextureHalo->m_pTexture->getSize().y; Vec2i storedSize = TextureHalo->m_pTexture->getStoredSize(); TextureHalo->uv = Vec2f( float(TextureHalo->m_size.x) / storedSize.x, float(TextureHalo->m_size.y) / storedSize.y ); TextureHalo->hd = Vec2f(.5f / storedSize.x, .5f / storedSize.y); return true; }
void Render(Image &dest) const { dest.Clear(); for (PolyList::const_iterator it = poly_.begin(); it != poly_.end(); ++it) it->Render(dest); }
int main(int argc, char **argv) { Atom atomWMDeleteWindow; int screenNumber; Screen *screen; Window window; XWindowAttributes windowAttributes; Colormap colormap; PaletteInfo paletteInfo; Image image; XImage *xImage; int x, y; int captureFrame; XEvent event; bool sizeChanged; // ProgramExit initialization display = NULL; v4l = -1; captureBuf = NULL; on_exit(ProgramExit, NULL); // Get command line options magnification = 1; if (argc > 1) { magnification = atoi(argv[1]); } // end if magnification = max(1, magnification); printf("Magnification is %i\n", magnification); // Open display if ((display = XOpenDisplay(NULL)) == NULL) { // NULL for DISPLAY printf("Error: XOpenDisplay() failed\n"); exit(1); } // end if screenNumber = DefaultScreen(display); screen = XScreenOfDisplay(display, screenNumber); // Obtain WM protocols atom for ClientMessage exit event if ((atomWMDeleteWindow = XInternAtom(display, AtomWMDeleteWindowName, True)) == None) { printf("Error: %s atom does not exist\n", AtomWMDeleteWindowName); exit(1); } // end if // Create window, inheriting depth and visual from root window window = XCreateSimpleWindow( display, RootWindowOfScreen(screen), 0, // x 0, // y 640, // width 480, // height 0, // border width BlackPixelOfScreen(screen), // border BlackPixelOfScreen(screen) // background ); XStoreName(display, window, "V4L RGB Test"); XGetWindowAttributes(display, window, &windowAttributes); if (((windowAttributes.depth == 8) && (windowAttributes.visual->c_class != PseudoColor)) || ((windowAttributes.depth > 8) && (windowAttributes.visual->c_class != TrueColor))) { printf("Error: Visual not supported\n"); exit(1); } // end if // Create PseudoColor HI240 colormap, if needed if (windowAttributes.depth == 8) { colormap = XCreateColormap(display, window, windowAttributes.visual, AllocAll); paletteInfo.display = display; paletteInfo.colormap = colormap; Hi240BuildPalette((ulong) 0x10000, (Hi240StorePaletteEntry *) StoreColormapEntry, &paletteInfo); XSetWindowColormap(display, window, colormap); } // end if // Create image if (image.Create( display, window, // Defines visual, depth MaxImageWidth, MaxImageHeight, True // MITSHM ) < 0) { printf("Error: image.Create() failed\n"); exit(1); } // end if image.Clear(); #if (1) printf("\nDisplay:\n"); printf("Image byte order = %s\n", ByteOrderName(ImageByteOrder(display))); printf("Bitmap unit = %i\n", BitmapUnit(display)); printf("Bitmap bit order = %s\n", ByteOrderName(BitmapBitOrder(display))); printf("Bitmap pad = %i\n", BitmapPad(display)); printf("\nWindow:\n"); printf("Depth = %i\n", windowAttributes.depth); printf("Visual ID = 0x%02x\n", windowAttributes.visual->visualid); printf("Visual class = %s\n", VisualClassName(windowAttributes.visual->c_class)); printf("Red mask = 0x%08lx\n", windowAttributes.visual->red_mask); printf("Green mask = 0x%08lx\n", windowAttributes.visual->green_mask); printf("Blue mask = 0x%08lx\n", windowAttributes.visual->blue_mask); printf("Bits per R/G/B = %i\n", windowAttributes.visual->bits_per_rgb); // log2 # colors xImage = image.X(); printf("\nImage:\n"); printf("Image byte order = %s\n", ByteOrderName(xImage->byte_order)); printf("Bitmap unit = %i\n", xImage->bitmap_unit); printf("Bitmap bit order = %s\n", ByteOrderName(xImage->bitmap_bit_order)); printf("Bitmap pad = %i\n", xImage->bitmap_pad); printf("Depth = %i\n", xImage->depth); printf("Red mask = 0x%08lx\n", xImage->red_mask); printf("Green mask = 0x%08lx\n", xImage->green_mask); printf("Blue mask = 0x%08lx\n", xImage->blue_mask); printf("Bits per pixel = %i\n", xImage->bits_per_pixel); // ZPixmap printf("Bytes per line = %i\n", xImage->bytes_per_line); printf("IsShared = %s\n", image.IsShared() ? "True" : "False"); printf("HasSharedPixmap = %s\n", image.HasSharedPixmap() ? "True" : "False"); #endif // V4L stuff if ((v4l = open(BigPictureDevice, O_RDWR)) < 0) { printf("Error: Can't open %s: %s\n", BigPictureDevice, strerror(errno)); exit(1); } // end if if (V4LMGetMMInfo(v4l, &v4lMMInfo) < 0) { printf("Error: V4LMGetMMInfo: %s\n", strerror(errno)); exit(1); } // end if #if (0) printf("Capture buffer size = %i\n", v4lMMInfo.size); printf("Capture buffer frames = %i\n", v4lMMInfo.frames); #endif if (v4lMMInfo.frames < 2) { printf("Error: V4LMGetMMInfo: frames < 2\n"); exit(1); } // end if if ((captureBuf = (bits8 *) mmap(0, v4lMMInfo.size, PROT_READ | PROT_WRITE, MAP_SHARED, v4l, 0)) == MAP_FAILED) { printf("Error: mmap(): %s\n", strerror(errno)); exit(1); } // end if if (V4LSetSource(v4l, BigPictureCompositeSource, VIDEO_MODE_NTSC) < 0) { printf("Error: V4LSetSource: %s\n", strerror(errno)); exit(1); } // end if if (V4LGetCaps(v4l, &v4lCaps) < 0) { printf("Error: V4LGetCaps: %s\n", strerror(errno)); exit(1); } // end if // Select V4L RGB capture format to exactly match image/visual (no LUTs!) if ((captureFormat = XImageCaptureFormat(image.X())) < 0) { printf("Error: No match for visual/image\n"); exit(1); } // end if // Initialize capture size based on window size windowWidth = windowAttributes.width; windowHeight = windowAttributes.height;; WindowResize(v4l, windowWidth, windowHeight, magnification); // Does V4LMSetFormat(). // Initialize picture attributes to mid-range V4LSetBrightness(v4l, 65535 / 2); V4LSetContrast(v4l, 65535 / 2); V4LSetSaturation(v4l, 65535 / 2); V4LSetHue(v4l, 65535 / 2); // Ready to start: Display window, select events, and initiate capture sequence XMapRaised(display, window); XSetWMProtocols(display, window, &atomWMDeleteWindow, 1); XSelectInput(display, window, StructureNotifyMask | ExposureMask); captureFrame = 0; if (V4LMCapture(v4l, captureFrame) < 0) { printf("Error: V4LMCapture: %s\n", strerror(errno)); exit(1); } // end if while (1) { if (XPending(display) > 0) { XNextEvent(display, &event); switch (event.type) { case ClientMessage: // From WM if (event.xclient.data.l[0] == atomWMDeleteWindow) { exit(0); } // end if break; case ConfigureNotify: sizeChanged = false; if (event.xconfigure.width != windowWidth) { sizeChanged = true; windowWidth = event.xconfigure.width; } // end if if (event.xconfigure.height != windowHeight) { sizeChanged = true; windowHeight = event.xconfigure.height; } // end if if (sizeChanged) { image.Clear(); XClearWindow(display, window); WindowResize(v4l, windowWidth, windowHeight, magnification); } // end if break; case Expose: if (event.xexpose.count == 0) { Put(window, image); } // end if break; } // end switch } else { // Wait for this frame if (V4LMSync(v4l, captureFrame) < 0) { printf("Error: V4LMSync: %s\n", strerror(errno)); exit(1); } // end if // Start capture for next frame if (V4LMCapture(v4l, 1 - captureFrame) < 0) { printf("Error: V4LMCapture: %s\n", strerror(errno)); exit(1); } // end if Draw(image, captureBuf + v4lMMInfo.offsets[captureFrame], magnification); Put(window, image); captureFrame = 1 - captureFrame; // 0<->1 } // endif } // end while printf("Error: Fell out of event loop!\n"); exit(1); } // end main