Exemple #1
0
bool loadTextures() {
  SDL_Surface *bitmap = loadBitmap("data/nehe.bmp");
  if (!bitmap) return false;

  if (!isPowerOf2(bitmap->w)) {
    fprintf(stderr, "image width is not power of 2");
    return false;
  }
  if (!isPowerOf2(bitmap->h)) {
    fprintf(stderr, "image height is not power of 2");
    return false;
  }

  int numColours = bitmap->format->BytesPerPixel;
  GLenum texture_format;
  if (numColours == 4) {
    // Contains alpha channel.
    if (bitmap->format->Rmask == 0x000000ff) {
      texture_format = GL_RGBA;
    } else {
      texture_format = GL_BGRA;
    }
  } else if (numColours == 3) {
    // No alpha channel.
    if (bitmap->format->Rmask == 0x000000ff) {
      texture_format = GL_RGB;
    } else {
      texture_format = GL_BGR;
    }
  } else {
    fprintf(stderr, "Image is not true colour");
    return false;
  }
  printf("texture format is '%s'\n", textureFormatToString(texture_format));

  // Create the texture.
  glGenTextures(1, &texture[1]);

  // Typical texture generation using data from the bitmap.
  glBindTexture(GL_TEXTURE_2D, texture[0]);

  // Linear Filtering.
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  
  bool withMipMap = false;
  if (withMipMap) {
    gluBuild2DMipmaps(GL_TEXTURE_2D, 
                      3, bitmap->w, bitmap->h, 
                      GL_BGR, GL_UNSIGNED_BYTE, 
                      bitmap->pixels);
  } else {
    RGB bmp[bitmap->w * bitmap->h];
    flipPixels(bmp, bitmap);
    bool flip = false;
    glTexImage2D(GL_TEXTURE_2D, 0,
                 numColours, bitmap->w, bitmap->h,
                 0, texture_format,
                 GL_UNSIGNED_BYTE, flip? bmp : bitmap->pixels);
  }

  SDL_FreeSurface(bitmap);

  printf("texture \"name\" is %d\n", texture[0]);
  return true;
}
void GLRenderTexture::Generate(ZFlags zflags)
{
    ASSERT_MAIN_THREAD();

    ASSERT(m_size.x >= 1 && m_size.y >= 1);

    GLsizei width = m_size.x;
    GLsizei height = m_size.y;
    
#if OPENGL_ES
    // textures must be a power of 2 on ios
    width = roundUpPower2(width);
    height = roundUpPower2(height);
#endif
    
    if (s_defaultFramebuffer < 0)
    {
        glGetIntegerv(GL_FRAMEBUFFER_BINDING, &s_defaultFramebuffer);
    }

    m_texsize = float2(width, height);
    DPRINT(SHADER, ("Generating render texture, %dx%d %s %s", width, height,
                    textureFormatToString(m_format), (zflags&HASZ) ? "Z16" : "No_Z"));

    glReportError();
    glGenFramebuffers(1, &m_fbname);
    glReportError();

    glGenTextures(1, &m_texname);
    glActiveTexture(GL_TEXTURE0);
    glBindTexture(GL_TEXTURE_2D, m_texname);
    glReportError();
 
#if OPENGL_ES
    if (m_format == GL_RGBA16F_ARB)
    {
        glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_HALF_FLOAT_OES, 0);
    }
    else
#endif
    {
        glTexImage2D(GL_TEXTURE_2D, 0, m_format, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
    }
    glReportError();
    gpuMemoryUsed += width * height * textureFormatBytesPerPixel(m_format);

    glBindFramebuffer(GL_FRAMEBUFFER, m_fbname);
    glReportError();

    // The depth buffer
    if (zflags&HASZ)
    {
        glGenRenderbuffers(1, &m_zrbname);
        glReportError();
        glBindRenderbuffer(GL_RENDERBUFFER, m_zrbname);
        glReportError();
        
        glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT16, width, height);
        glReportError();
        glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, m_zrbname);
        glReportError();

        gpuMemoryUsed += width * height * 2;
    }
    else
    {
        m_zrbname = 0;
    }

    // Set "renderedTexture" as our colour attachement #0
    glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, m_texname, 0);
    glReportError();

    glActiveTexture(GL_TEXTURE0);
    glBindTexture(GL_TEXTURE_2D, m_texname);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);

#if 0 
    // Set the list of draw buffers.
    GLenum DrawBuffers[2] = {GL_COLOR_ATTACHMENT0};
    glDrawBuffers(1, DrawBuffers); // "1" is the size of DrawBuffers
#endif

    // Always check that our framebuffer is ok
    glReportError();
    glReportFramebufferError();
}