Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: textures in opengl  (Read 7421 times)

0 Members and 1 Guest are viewing this topic.

genzm

  • Newbie
  • *
  • Posts: 26
    • View Profile
textures in opengl
« on: January 02, 2013, 12:18:08 pm »
Hy,
I've been using textures in my opengl application for a while. Now I wanted to change it using the sf::Texture class. I get no errors, but I don't see any textures. All objects just have a plain white color.

I'm just wondering if the following code is enough to use textures:
sf::Texture* Gr_Object::load_text_image(string file) {
        if (file == "") {
                return NULL;
        }

        sf::Texture* texture = new sf::Texture();
        if (!texture->loadFromFile(file)) {
                cout << "file was not found or could not be read" << endl;
                delete texture;
                texture = NULL;
        } else {
                texture->setRepeated(true);
        }

        return texture;
}

When using the textures:
image->bind();

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: textures in opengl
« Reply #1 on: January 02, 2013, 12:25:41 pm »
Your code looks ok, so you should show a complete and minimal code that reproduces the problem.
Laurent Gomila - SFML developer

genzm

  • Newbie
  • *
  • Posts: 26
    • View Profile
Re: textures in opengl
« Reply #2 on: January 02, 2013, 01:46:35 pm »
Ok, here is the main loop:

sf::Window window(sf::VideoMode(1024, 576), "First Person Shooter");
        Game game(&window);
        game.init();

        while (game.isRunning())
        {
                game.tick();
                window.display();
        }

        window.close();

the game.init:
/* ... */
/* loading materials from exernal file */
for (unsigned int i = 0; i < materialSize; ++i) {
                Material m;
                /* reading data like ambient, diffuse, specular,... */
                /*   ... */

                /* reading texture name */
                unsigned char length;
                file->read( reinterpret_cast<char*>(&length), sizeof(unsigned char) );
                if (length != 0) {
                        char texname[30];
                        file->read(texname, length);
                        texname[length] = 0;
                        m.image = load_text_image(texname);
                } else {
                        m.image = NULL;
                }
                addMaterial(m);
        }

        return true;

/* ... */

the load_text_image() is this:
sf::Texture* Gr_Object::load_text_image(string file) {
        if (file == "") {
                return NULL;
        }

        sf::Texture* texture = new sf::Texture();
        if (!texture->loadFromFile(file)) {
                cout << "file was not found or could not be read" << endl;
                delete texture;
                texture = NULL;
        } else {
                texture->setRepeated(true);
        }

        return texture;
}

The drawing (in game.tick()):
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);

for (unsigned int i = 0; i < objectList.size(); ++i) {
        Gr_Object faces= objectList[i].getFaces();
        for (int j = 0; j < faces.size(); ++j) {
                faces[i].draw();
        }
}

return 1;

And then the drawing of a face:
void Face3::draw() {
        glBegin(GL_TRIANGLES);

        for (int i = 0; i < 3; ++i) {
                Vertex v = vertices[i];
                if (v.material)
                        v.material->set_material();
                if (v.normal)
                        glNormal3f(v.normal->x, v.normal->y, v.normal->z);
                if (v.texture)
                        glTexCoord2f(v.texture->x, v.texture->y);

                glVertex3f(v.point->x, v.point->y, v.point->z);
        }
        glEnd();
}

And then the last piece is the set_material method:
void Material::set_material() {
        GLfloat amb[] = {amb_r, amb_g,amb_b,1.0};
        GLfloat dif[] = {dif_r, dif_g,dif_b,1.0};
        GLfloat spc[] = {spc_r, spc_g,spc_b,1.0};
        glMaterialfv(GL_FRONT,GL_AMBIENT, amb);
        glMaterialfv(GL_FRONT,GL_DIFFUSE, dif);
        glMaterialfv(GL_FRONT,GL_SPECULAR,spc);
        glMaterialf(GL_FRONT,GL_SHININESS,ns);

        if (image)
                image->bind(sf::Texture::Normalized);
}

I hope this helps?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: textures in opengl
« Reply #3 on: January 02, 2013, 02:25:17 pm »
Quote
I hope this helps?
Why do people always copy and paste incomplete parts of their big project when we ask for a complete and minimal example? :(
I know it's a lot easier to just throw your code, but if we ask for a complete and minimal code, there's a reason...

Please read this:
http://en.sfml-dev.org/forums/index.php?topic=5559.msg36368#msg36368
Laurent Gomila - SFML developer

genzm

  • Newbie
  • *
  • Posts: 26
    • View Profile
Re: textures in opengl
« Reply #4 on: January 02, 2013, 03:55:15 pm »
ok, you're right, I'm sorry about that. Here is a more simple example:

void test() {
        sf::Window window(sf::VideoMode(1024, 576), "texture example");

        sf::Texture* texture = new sf::Texture();
        if (!texture->loadFromFile("texture.png")) {
                cout << "file was not found or could not be read" << endl;
                delete texture;
                texture = NULL;
        } else {
                texture->setRepeated(true);
        }

        texture->bind();
        bool running = true;

        while (running)
        {
               
                glBegin(GL_QUADS);
                        glTexCoord2f(0.0,0.0);
                        glVertex3f(-0.2,-0.2,0);

                        glTexCoord2f(1.0,0.0);
                        glVertex3f(0.2,-0.2,0);

                        glTexCoord2f(1.0,1.0);
                        glVertex3f(0.2,0.2,0);

                        glTexCoord2f(0.0,1.0);
                        glVertex3f(-0.2,0.2,0);
                glEnd();

                window.display();

        }

        delete texture;

        window.close();
}

It simply creates a window with 1 quad. I want the texture to appear on the quad, but my output right now is a white square at the center of the screen.

This will probably be a lot clearer? :p

masskiller

  • Sr. Member
  • ****
  • Posts: 284
  • Pointers to Functions rock!
    • MSN Messenger - kyogre_jb@hotmail.com
    • View Profile
    • Email
Re: textures in opengl
« Reply #5 on: January 02, 2013, 04:12:05 pm »
If the texture ptr is NULL you should return -1 and not just a console output. Are you sure the texture is loading correctly?
Programmer, Artist, Composer and Storyline/Script Writer of "Origin of Magic". If all goes well this could turn into a commercial project!

Finally back into the programming world!

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: textures in opengl
« Reply #6 on: January 02, 2013, 04:12:22 pm »
Thanks. Now it's too minimal :P

If you use OpenGL you have a few states to define if you want to see a relevant result on screen. Add glEnable(GL_TEXTURE_2D) and you should see the texture.
Laurent Gomila - SFML developer

genzm

  • Newbie
  • *
  • Posts: 26
    • View Profile
Re: textures in opengl
« Reply #7 on: January 02, 2013, 04:35:07 pm »
you're right. I forgot to enable the textures  :P
But this means that I got the find the difference between my minimal example and my original application.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: textures in opengl
« Reply #8 on: January 02, 2013, 04:40:44 pm »
Yep. Now that you have a minimal code that works, it wil be easier to find what's wrong in your original code.
Laurent Gomila - SFML developer

genzm

  • Newbie
  • *
  • Posts: 26
    • View Profile
Re: textures in opengl
« Reply #9 on: January 02, 2013, 06:13:44 pm »
Ok, I'm still having some trouble finding my mistake...  :-\
I've extended the minimal example a little bit:

sf::Window window(sf::VideoMode(1024, 576), "texture example");

sf::Texture* texture = new sf::Texture();
if (!texture->loadFromFile("texCube.png")) {
        cout << "file was not found or could not be read" << endl;
        delete texture;
        texture = NULL;
} else {
        texture->setRepeated(true);
}

texture->bind();
gl_init();

Object* obj = new Object(NULL,NULL,NULL, new Gr_Object("models\\castle2.cac"));

while (true)
{

        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

        obj->draw();

        window.display();
}

window.close();

The only thing that changed is that I use a self defined class "Object" to load my external file. When the draw function of "obj" is called it will use the bind() function of sfml textures. But somehow only the first texture (declared at line 3 and bound at line 12) is used. So my conclusion would be that when a texture is bound no other texture can overwrite that or something. But the sfml documentation clearly says otherwise. So I'm a little stuck at what my next step in debugging would be. Any suggestions?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: textures in opengl
« Reply #10 on: January 02, 2013, 06:36:23 pm »
Integrate the code of the Object class directly into the main(), then reduce it until it is minimal again.
Laurent Gomila - SFML developer

genzm

  • Newbie
  • *
  • Posts: 26
    • View Profile
Re: textures in opengl
« Reply #11 on: January 02, 2013, 06:44:56 pm »
But I don't know if I need to search in the load functions or the draw functions.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: textures in opengl
« Reply #12 on: January 02, 2013, 06:47:05 pm »
Do it for both, or one after the other.
Laurent Gomila - SFML developer

genzm

  • Newbie
  • *
  • Posts: 26
    • View Profile
Re: textures in opengl
« Reply #13 on: January 02, 2013, 07:07:06 pm »
Ok, while doing that, I got the following error:

An internal OpenGL call failed in Texture.cpp (380) : GL_INVALID_OPERATION, the specified operation is not allowed in the current state

Any suggestions on what could cause this? And another question: is there a way to see which texture is bound at the moment (something you can print out with sfml,...)?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: textures in opengl
« Reply #14 on: January 02, 2013, 07:24:34 pm »
I can only help you if you post your minimal code everytime there's a different problem. And please include the main() and headers so that I just have to paste it and compile it ;)
Laurent Gomila - SFML developer

 

anything