visibility/passing of variables

Discussion in 'C++' started by Michael Sgier, Sep 22, 2005.

  1. Hello
    i don't understand the visibility/passing of variables. I've in
    texture.h:

    class CTexture
    {
    public:
    unsigned int texID;
    GLuint texture[5];

    int LoadTex(char* filename);
    }


    and in terrain.h:
    class CTerrain
    {
    public:
    CTexture ebene;
    }


    the texture loading function in texture.cpp:
    int CTexture::LoadTex(char* filename)
    {
    static int i = 0;

    SDL_Surface* img;
    img = SDL_LoadBMP(filename);
    if (!img) {
    printf("Error: %s\n", SDL_GetError());
    exit(1);
    }
    glGenTextures(1, &texture);
    glBindTexture(GL_TEXTURE_2D, texture);

    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, FILTER);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, FILTER);

    glTexImage2D(GL_TEXTURE_2D, 0, 3, img->w, img->h, 0, GL_RGB,
    GL_UNSIGNED_BYTE, img->pixels);

    printf("loaded file '%s' as texture %i\n", filename, i);
    i++;
    return 0;
    }


    and finally the function gets called in terrain.cpp

    #include "terrain.h"
    ebene.LoadTex("pics/ground.bmp");

    also another function here uses the variable:
    void CTerrain::OnDraw()
    {
    glEnable(GL_TEXTURE_2D);
    glBindTexture(GL_TEXTURE_2D, ebene.texID);
    ....
    }

    It compiles but the texture doesn't get drawed. I dont understand how to
    pass texture[] and how to
    draw it then. Also where texture[] is visible.
    THANKS and regards
    Michael
    Michael Sgier, Sep 22, 2005
    #1
    1. Advertising

  2. Michael Sgier wrote:
    > Hello
    > i don't understand the visibility/passing of variables. I've in
    > texture.h:
    >
    > class CTexture
    > {
    > public:
    > unsigned int texID;
    > GLuint texture[5];
    >
    > int LoadTex(char* filename);
    > }
    >
    >
    > and in terrain.h:
    > class CTerrain
    > {
    > public:
    > CTexture ebene;
    > }
    >
    >
    > the texture loading function in texture.cpp:
    > int CTexture::LoadTex(char* filename)
    > {
    > static int i = 0;
    >
    > SDL_Surface* img;
    > img = SDL_LoadBMP(filename);
    > if (!img) {
    > printf("Error: %s\n", SDL_GetError());
    > exit(1);
    > }
    > glGenTextures(1, &texture);
    > glBindTexture(GL_TEXTURE_2D, texture);
    >
    > glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, FILTER);
    > glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, FILTER);
    >
    > glTexImage2D(GL_TEXTURE_2D, 0, 3, img->w, img->h, 0, GL_RGB,
    > GL_UNSIGNED_BYTE, img->pixels);
    >
    > printf("loaded file '%s' as texture %i\n", filename, i);
    > i++;
    > return 0;
    > }
    >
    >
    > and finally the function gets called in terrain.cpp
    >
    > #include "terrain.h"
    > ebene.LoadTex("pics/ground.bmp");
    >
    > also another function here uses the variable:
    > void CTerrain::OnDraw()
    > {
    > glEnable(GL_TEXTURE_2D);
    > glBindTexture(GL_TEXTURE_2D, ebene.texID);
    > ...
    > }
    >
    > It compiles but the texture doesn't get drawed. I dont understand how to
    > pass texture[] and how to
    > draw it then. Also where texture[] is visible.
    > THANKS and regards
    > Michael


    You're storing the texture ID in CTexture::texture[0..4]. Then you bind
    that ID to some texture data using glTexImage2D. Then the function ends,
    and you lose your pointer to the image, possibly creating a memory leak.
    (I'm not sure what the SDL policy is.)
    Then later on you try to use the texture by calling glBindTexture with
    CTexture::texID, but that variable doesn't have anything meaningful in
    it. The texture IDs are in CTexture::texture[...].

    Here's a common "texture" object:

    struct texture_data{
    GLuint id;
    vector<unsigned char> data;
    GLuint width, height;
    };

    You could probably replace vector<unsigned char> with a pointer to an
    SDL_Surface. The point is that you usually have a 1-to-1 relationship
    between texture IDs and the actual pixels; not 1:5 as your class implies.

    If your real question is "how do I write a class that loads 5 textures"
    then here's some (completely untested) food for thought:

    struct texture{
    GLuint id;
    SDL_Surface *image;
    texture(): id(0), image(NULL){}
    ~texture(){ delete image; }
    };

    class five_textures{
    vector<texture> textures;
    public:
    void load(const string& filename);

    GLuint getTexture(int n) const{
    if (n < textures.size()){
    return textures[n].id;
    }
    return 0;
    }

    vector<texture>::size_type size() const{ return textures.size(); }
    };

    void five_textures::load(const string& filename){
    texture tx;
    tx.image = SDL_LoadBMP(filename.c_str());
    glGenTextures(1, &tx.id);
    glBindTexture(GL_TEXTURE_2D, tx.id);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, FILTER);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, FILTER);
    glTexImage2D(GL_TEXTURE_2D, 0, 3, tx.image->w,
    tx.image->h, 0, GL_RGB, GL_UNSIGNED_BYTE, tx.image->pixels);
    textures.push_back(tx);
    }
    Jacques Labuschagne, Sep 22, 2005
    #2
    1. Advertising

  3. Hello
    well my question was how to return the texture[] for further use. Like
    this it doesn't work. What is my mistake?
    THANKS and regards
    Michael

    GLuint * CTexture::LoadTex(char* filename)
    {
    ...
    return texture;
    }
    Michael Sgier, Sep 22, 2005
    #3
  4. Michael Sgier

    mlimber Guest

    Michael Sgier wrote:
    > Hello
    > well my question was how to return the texture[] for further use. Like
    > this it doesn't work. What is my mistake?
    > THANKS and regards
    > Michael
    >
    > GLuint * CTexture::LoadTex(char* filename)
    > {
    > ...
    > return texture;
    > }


    Your first problem is that the code in your original post is buggy, as
    Jacques pointed out. Pay more attention to his post. As for the
    function you've written here, it should work with the previous code if
    you change the return type of CTexture::LoadTex() to match this one.

    It sounds to me like you probably need to read a good C++ book,
    however. Try Stroustrup's _The C++ Programming Language_ (3rd Ed.),
    Lippman et al.'s _C++ Primer_ (4th ed.), or Koenig and Moo's
    _Accelerated C++_.

    Cheers! --M
    mlimber, Sep 22, 2005
    #4
  5. Michael Sgier wrote:
    > Hello
    > well my question was how to return the texture[] for further use. Like
    > this it doesn't work. What is my mistake?
    > THANKS and regards
    > Michael
    >
    > GLuint * CTexture::LoadTex(char* filename)
    > {
    > ...
    > return texture;
    > }


    You don't need to return texture to anywhere... It's a member of
    CTexture. When CTerrain::OnDraw() wants to use a texture it should refer
    to ebene.texture[n] rather than ebene.texId.

    Ideally texture[] would be private in CTexture, and you'd add a member
    function to get the texture ID. For example:

    class CTexture{
    private:
    GLuint texture[5];

    public:
    GLuint getTextureId(int n){
    if (n < 0 or n > 4){
    return 0;
    }
    return texture[n];
    }
    // ...
    };

    void CTerrain::OnDraw(){
    // ...
    glBindTexture(GL_TEXTURE_2D, ebene.getTextureId(0));
    // ...
    }
    Jacques Labuschagne, Sep 23, 2005
    #5
  6. Michael Sgier

    mlimber Guest

    mlimber, Sep 23, 2005
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    15
    Views:
    1,292
    Jerry Coffin
    Feb 1, 2005
  2. Balu

    Data Grid Column Visibility

    Balu, Jan 3, 2006, in forum: ASP .Net
    Replies:
    0
    Views:
    505
  3. G Iveco
    Replies:
    6
    Views:
    485
    Mike Treseler
    Aug 7, 2007
  4. Replies:
    9
    Views:
    925
  5. ash
    Replies:
    0
    Views:
    318
Loading...

Share This Page