[ Main Page ]

SDL_imageをOpenGLのTextureで利用する

OpenGL(GL/GLU/GLUT)自体は、殆んどの画像フォーマットをサポートしていないので、 Textureの読込みは骨が折れます。そこで、 OpenGLとSDLの連携 を参考にして、SDLで様々な画像をTextureとして読み込む例を挙げてみます。

TextureImage.h

#ifndef TEXTUREIMAGE_H
#define TEXTUREIMAGE_H

#include <iostream>
#include <GL/glut.h>
#include <SDL/SDL_image.h>
#include <vector>
#include <map>
#include <string>

typedef struct{
  std::string filename;
  GLuint textureID;
  int width;
  int height;
} TextureInfo;

class TextureImage
{
  private:
  std::map<std::string, TextureInfo> Textures;
  public:
  int loadImage(char * filename);
  TextureInfo * getTexture(char * filename);
};

#endif
      

TextureImage.cpp

#include "TextureImage.h"
#include <string.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>

int TextureImage::loadImage(char *filename)
{
  SDL_Surface *Image = NULL;
  SDL_Surface *tmpImage;
  GLuint texID = 0;
  TextureInfo texture;

  if(Image = IMG_Load(filename))
  {
    tmpImage = SDL_CreateRGBSurface(SDL_SWSURFACE, 0, 0, 32, 
        0x000000ff, 
        0x0000ff00, 
        0x00ff0000, 
        0xff000000);
    Image = SDL_ConvertSurface(Image, tmpImage->format, SDL_SWSURFACE);
    SDL_FreeSurface(tmpImage);

    glGenTextures(1, &texID);
    glBindTexture(GL_TEXTURE_2D, texID);
    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, GL_RGBA8, Image->w, Image->h, 0,
        GL_RGBA, GL_UNSIGNED_BYTE, Image->pixels); */

    gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGBA, Image->w, Image->h,
        GL_RGBA, GL_UNSIGNED_BYTE, Image->pixels);

    SDL_FreeSurface(Image);

    texture.filename = filename;
    texture.textureID = texID;
    texture.width = Image->w;
    texture.height = Image->h;
    Textures.insert(std::pair<std::string, TextureInfo>(filename, texture));
    return 0;
  }
  return -1;
}

TextureInfo * TextureImage::getTexture(char *filename)
{
  std::map<std::string, TextureInfo>::iterator i = Textures.find(filename);
  if(i != Textures.end())
    {
      return &(i->second);
    }
  if(loadImage(filename) == 0)
    {
      return getTexture(filename);
    }
  return NULL;
}
      

glTexImage2Dの代わりにgluBuild2DMipmapsを使えば、画像Pixelサイズに 左右されずに詠みこめる筈です。もっとも、最近のOpenGLでは、 その問題は解消されたようですが。あとは、以下のようにすれば 一応Textureは使える筈です。

#include "TextureImage.h"
...
TextureImage *texture;
...
texture = new TextureImage();
...
void display(void)
{
  TextureInfo * tex;

  glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
  glRotated(1.1, 0.0, 1.0, 0.0);

  glEnable(GL_LINE_SMOOTH);  
  glEnable(GL_DEPTH_TEST);

  glEnable(GL_TEXTURE_2D);
  if((tex = texture->getTexture("sample.gif")) == NULL)
    exit(-1);
  glBindTexture(GL_TEXTURE_2D, tex->textureID);
  glBegin(GL_POLYGON);
  glTexCoord2f(0, 1);
  glVertex2f(0, 0);
  glTexCoord2f(0, 0);
  glVertex2f(0, tex->height/100);
  glTexCoord2f(1, 0);
  glVertex2f(tex->width/100, tex->height/100);
  glTexCoord2f(1, 1);
  glVertex2f(tex->width/100, 0);
  glEnd();
  glDisable(GL_TEXTURE_2D);

  glEnable(GL_TEXTURE_2D);
  // エラーチェックなし
  glBindTexture(GL_TEXTURE_2D, texture->getTexture("tiles.png")->textureID);
  glBegin(GL_POLYGON);
  glTexCoord2f(0 , 0); glVertex3f(-4.9 ,1,3);
  glTexCoord2f(0 , 1); glVertex3f(-4.9 ,1,-3);
  glTexCoord2f(1 , 1); glVertex3f(3.9 , 1,-3);
  glTexCoord2f(1 , 0); glVertex3f(3.9 , 1,3);
  glEnd();
  glDisable(GL_TEXTURE_2D);
  ...
  glutSwapBuffers();
}
...
      

-lSDL -lSDL_imageをリンクすればいい筈です。

       <rindolf>  ljuwaidah: so aren't there more people who use FOSS in
                  Oman?
     <ljuwaidah>  there are, but they're only a few
       <rindolf>  ljuwaidah: ah.
       <rindolf>  Oman has .om as its TLD.
       <rindolf>  .om .nom .nom
       <rindolf>  p.om p.om
       <rindolf>  kabo.om
       <rindolf>  DJB got the cr.yp.to domain.
       <rindolf>  And there's also ali.as which is Adam Kennedy's.
     <ljuwaidah>  looool
     <ljuwaidah>  rand.om :P
     <ljuwaidah>  do.om :P
     <ljuwaidah>  ro.om
     <ljuwaidah>  m.om
 <Black_Phoenix>  your.m.om
     <ljuwaidah>  hahahaha
     <ljuwaidah>  is that supposed to be a milf site? :P
       <rindolf>  yahoo.c.om
     <ljuwaidah>  bro.om
       <rindolf>  http://yourmom.com/
       <rindolf>  Though it's down now.
     <ljuwaidah>  don't keep your hopes up on .om domains
     <ljuwaidah>  our ISP is a greedy money sucker
       <rindolf>  "I'd rather be a .com than a .om"
     <ljuwaidah>  rindolf: me too
       <rindolf>  ljuwaidah: this was a joke.
       <rindolf>  I have a few .org domains.
       <rindolf>  From GoDaddy
     <ljuwaidah>  unless you need it for a domain trick
               *  Black_Phoenix <--- .com bastard here
     <ljuwaidah>  rindolf: where's the joke?
       <rindolf>  ljuwaidah: joke?
       <rindolf>  ljuwaidah: it rhymes.
       <rindolf>  dot-com, dot-om - get it? get it? get it?
       <rindolf>  ;-)
     <ljuwaidah>  no :(
       <rindolf>  ljuwaidah: om rhymes with com.
     <ljuwaidah>  so?
       <rindolf>  ljuwaidah: "I'd rather be a dot-com than a dot-om"
       <rindolf>  ljuwaidah: it's just a silly rhyme.
 <Black_Phoenix>  I'd rather a website
       <rindolf>  Black_Phoenix: I accidentally a web-site.
     <ljuwaidah>  I still don't get the joke
 <Black_Phoenix>  rindolf, I'd still rather a blog
       <rindolf>  Black_Phoenix: rather what a blog?
 <Black_Phoenix>  Website a blog
       <rindolf>  ljuwaidah: never mind.
     <ljuwaidah>  sorry, I don't know why I'm slow today
       <rindolf>  Black_Phoenix: do you mean that you would rather *be* a
                  web-site?
 <Black_Phoenix>  I'd rather website a blog
 <Black_Phoenix>  broken grammar time :D
     <ljuwaidah>  Black_Phoenix: when did website become a verb?
 <Black_Phoenix>  it didn't
 <Black_Phoenix>  I'm using it as a verb to produce nonsense
     <ljuwaidah>  I sense light!
     <ljuwaidah>  {I see}
       <rindolf>  ljuwaidah: http://www.google.com.om/
     <ljuwaidah>  rindolf: I know
       <rindolf>  If Google can do it - so can you.
     <ljuwaidah>  I don't use it
     <ljuwaidah>  'cause some services don't work on it
       <rindolf>  ljuwaidah: ah.
       <rindolf>  ljuwaidah: in any case, seems like one can register a
                  .com.om domain.

    -- The .om domain
    -- ##programming, Freenode

I disagree with your generalization that physicists are smarter than
professors of French Literature.

Try this thought experiment. A dictator takes over the US and sends all the
professors to re-education camps. The physicists are told they have to learn
how to write academic articles about French literature, and the French
literature professors are told they have to learn how to write original
physics papers. If they fail, they'll be shot. Which group is more worried?

We have some evidence here: the famous parody that physicist Alan Sokal got
published in Social Text. How long did it take him to master the art of
writing deep-sounding nonsense well enough to fool the editors? A couple
weeks?

What do you suppose would be the odds of a literary theorist getting a parody
of a physics paper published in a physics journal?

    -- Paul Graham
    -- Re: What You Can't Say ( http://www.paulgraham.com/resay.html )


Powered by UNIX fortune(6)
[ Main Page ]