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

Author Topic: textures in opengl  (Read 6261 times)

0 Members and 1 Guest are viewing this topic.

genzm

  • Newbie
  • *
  • Posts: 26
    • View Profile
Re: textures in opengl
« Reply #15 on: January 02, 2013, 10:02:33 pm »
Hy, another simple example:
sf::Texture* load_texture(string filename) {
        sf::Texture* texture = new sf::Texture();
        if (!texture->loadFromFile(filename)) {
                cout << "file was not found or could not be read" << endl;
                delete texture;
                texture = NULL;
        } else {
                texture->setRepeated(true);
        }
       
        return texture;
}

int main() {
        sf::Window window(sf::VideoMode(1024, 576), "First Person Shooter");

        sf::Texture* texture = load_texture("texCube.png");
        sf::Texture* texture2 = load_texture("test.png");

        Material* m1 = new Material();
        Material* m2 = new Material();
        m1->image = texture;
        m2->image = texture2;

        Face3* face1 = new Face3(Vertex(new sf::Vector3f(-0.2,-0.2,0.0),new sf::Vector3f(0.0,0.0,1.0),m1,new sf::Vector2f(0.0,0.0)),                    // vertex constructor: position, normal, material, texture coordinates
                                                        Vertex(new sf::Vector3f(0.2,-0.2,0.0),  new sf::Vector3f(0.0,0.0,1.0),m1,new sf::Vector2f(1.0,0.0)),
                                                        Vertex(new sf::Vector3f(0.2,0.2,0.0),   new sf::Vector3f(0.0,0.0,1.0),m1,new sf::Vector2f(1.0,1.0)));
        Face3* face2 = new Face3(Vertex(new sf::Vector3f(0.2,0.2,0.0), new sf::Vector3f(0.0,0.0,1.0),m2,new sf::Vector2f(1.0,1.0)),
                                                        Vertex(new sf::Vector3f(-0.2,0.2,0.0), new sf::Vector3f(0.0,0.0,1.0),m2,new sf::Vector2f(0.0,1.0)),
                                                        Vertex(new sf::Vector3f(-0.2,-0.2,0.0),new sf::Vector3f(0.0,0.0,1.0),m2,new sf::Vector2f(0.0,0.0)));

        while (true) {
                glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

                face1->draw();
                face2->draw();

                window.display();
        }

        window.close();

        _CrtDumpMemoryLeaks();
        return EXIT_SUCCESS;
}

It's ready to compile (because it contains classes which I wrote myself). But assume that the classes work correctly, the this should work, right?
Well I'm not getting any textures at all. Can anyone see a mistake in this piece of code? or should I start looking the classes that I've implemented myself?

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
Re: textures in opengl
« Reply #16 on: January 02, 2013, 10:07:49 pm »
You use new everywhere, this is insane! :o

In C++, you should almost never need to manage memory manually. Always use RAII.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: textures in opengl
« Reply #17 on: January 02, 2013, 10:49:34 pm »
I think it could be a good idea to replace the draw() calls by what's inside them.
Laurent Gomila - SFML developer

masskiller

  • Sr. Member
  • ****
  • Posts: 284
  • Pointers to Functions rock!
    • MSN Messenger - kyogre_jb@hotmail.com
    • View Profile
    • Email
Re: textures in opengl
« Reply #18 on: January 03, 2013, 12:03:32 am »
You use new everywhere, this is insane! :o

In C++, you should almost never need to manage memory manually. Always use RAII.

Indeed, not to mention that it's not really necessary to allocate everything in heap, just few things that you consider important enough since allocation also takes time and resources. And then again there's RAII and most STL containers that do the work for you for when you truly need it.
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!

genzm

  • Newbie
  • *
  • Posts: 26
    • View Profile
Re: textures in opengl
« Reply #19 on: January 03, 2013, 10:09:02 am »
Don't worry about the heap allocation. In my original code most things are constructed on the stack. It was just a quick and dirty piece of code (and probably my "new" habbit from Java :P ).
Anyway, I've removed some of the heap allocation and pasted in the code for drawing:

void draw_face(Face3* f) {
   glBegin(GL_TRIANGLES);

   for (int i = 0; i < 3; ++i) {
      Vertex v = f->vertices[i];
      if (v.material) {
         GLfloat amb[] = {v.material->amb_r, v.material->amb_g,v.material->amb_b,1.0};
         GLfloat dif[] = {v.material->dif_r, v.material->dif_g,v.material->dif_b,1.0};
         GLfloat spc[] = {v.material->spc_r, v.material->spc_g,v.material->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,v.material->ns);

         if (v.material->image)
            v.material->image->bind();
      }
      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();
}

int main() {
   sf::Window window(sf::VideoMode(1024, 576), "First Person Shooter");

   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);
   }
   sf::Texture* texture2 = new sf::Texture();
   if (!texture2->loadFromFile("textures\\test.bmp")) {
      cout << "file was not found or could not be read" << endl;
      delete texture2;
      texture2 = NULL;
   } else {
      texture2->setRepeated(true);
   }

   Material m1; m1.image = texture;  
   Material m2; m2.image = texture2;

   Face3* face1 = new Face3(Vertex(new sf::Vector3f(-0.2,-0.2,0.0),new sf::Vector3f(0.0,0.0,1.0),&m1,new sf::Vector2f(0.0,0.0)),
                     Vertex(new sf::Vector3f(0.2,-0.2,0.0),  new sf::Vector3f(0.0,0.0,1.0),&m1,new sf::Vector2f(1.0,0.0)),
                     Vertex(new sf::Vector3f(0.2,0.2,0.0),   new sf::Vector3f(0.0,0.0,1.0),&m1,new sf::Vector2f(1.0,1.0)));
   Face3* face2 = new Face3(Vertex(new sf::Vector3f(0.2,0.2,0.0), new sf::Vector3f(0.0,0.0,1.0),&m2,new sf::Vector2f(1.0,1.0)),
                     Vertex(new sf::Vector3f(-0.2,0.2,0.0), new sf::Vector3f(0.0,0.0,1.0),&m2,new sf::Vector2f(0.0,1.0)),
                     Vertex(new sf::Vector3f(-0.2,-0.2,0.0),new sf::Vector3f(0.0,0.0,1.0),&m2,new sf::Vector2f(0.0,0.0)));

   while (true) {
      glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

      draw_face(face1);
      draw_face(face2);

      window.display();
   }

   window.close();

   delete texture,texture2;
   
   _CrtDumpMemoryLeaks();
   return EXIT_SUCCESS;
}

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: textures in opengl
« Reply #20 on: January 03, 2013, 10:12:32 am »
This is out of topic, but... each vertex can have a different material, this is really unusual (and overkill) :o

And... that's the problem actually! You are not allowed to bind a texture in the middle of a glBegin/glEnd block. That wouldn't make sense anyway, textures are applied to faces, not to individual vertices.
« Last Edit: January 03, 2013, 10:17:22 am by Laurent »
Laurent Gomila - SFML developer

genzm

  • Newbie
  • *
  • Posts: 26
    • View Profile
Re: textures in opengl
« Reply #21 on: January 03, 2013, 11:07:04 am »
You're right, it might be unusual, but it is possible?
And that would indeed be possible :P I will try to move the material data to the face instead of every vertex

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: textures in opengl
« Reply #22 on: January 03, 2013, 11:10:07 am »
Quote
You're right, it might be unusual, but it is possible?
And that would indeed be possible
Materials apply to faces (or even better: to groups of faces). I don't know what you're trying to do, but you're doing it wrong :P
Laurent Gomila - SFML developer

genzm

  • Newbie
  • *
  • Posts: 26
    • View Profile
Re: textures in opengl
« Reply #23 on: January 03, 2013, 11:43:49 am »
I'm just trying to support all possible inputs. That's why. :P
And yes, that did the trick.
My output so far :P


Thanks for all the help and tips

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: textures in opengl
« Reply #24 on: January 03, 2013, 11:48:43 am »
Quote
I'm just trying to support all possible inputs
Your code will get incredibly slower and more complicated, for a use case that will never occur. Are you sure that's what you want?

Quote
And yes, that did the trick.
I'm glad we solved your problem :)
Laurent Gomila - SFML developer

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
Re: textures in opengl
« Reply #25 on: January 03, 2013, 11:57:24 am »
Thanks for all the help and tips
Please consider them.

It is really bad practice to allocate every little object such as sf::Vector3f with the new operator. Even worse, if you don't delete them. In C++, new is used differently than in Java or C#. Manual memory management is inherently error-prone and needs a lot of boilerplate code to handle correctly, that's why we avoid it wherever possible.

It is much simpler, safer and more efficient to just use automatic objects and RAII. When you follow this idiom consequently, you needn't worry anymore about memory leaks. Here, I argumented in a detailed manner why RAII is superior to manual memory management.

Your main function would look like this, note that I didn't use a single new.
int main() {
   sf::Window window(sf::VideoMode(1024, 576), "First Person Shooter");

   sf::Texture texture;
   if (!texture.loadFromFile("texCube.png")) {
      cout << "file was not found or could not be read" << endl;
   } else {
      texture.setRepeated(true);
   }
   sf::Texture texture2;
   if (!texture2.loadFromFile("textures\\test.bmp")) {
      cout << "file was not found or could not be read" << endl;
   } else {
      texture2.setRepeated(true);
   }

   Material m1; m1.image = &texture;  
   Material m2; m2.image = &texture2;

   Face3 face1(Vertex(sf::Vector3f(-0.2,-0.2,0.0), sf::Vector3f(0.0,0.0,1.0),&m1, sf::Vector2f(0.0,0.0)),
                     Vertex(sf::Vector3f(0.2,-0.2,0.0), sf::Vector3f(0.0,0.0,1.0),&m1, sf::Vector2f(1.0,0.0)),
                     Vertex(sf::Vector3f(0.2,0.2,0.0), sf::Vector3f(0.0,0.0,1.0),&m1, sf::Vector2f(1.0,1.0)));
   Face3 face2(Vertex( sf::Vector3f(0.2,0.2,0.0),  sf::Vector3f(0.0,0.0,1.0),&m2, sf::Vector2f(1.0,1.0)),
                     Vertex(sf::Vector3f(-0.2,0.2,0.0), sf::Vector3f(0.0,0.0,1.0),&m2, sf::Vector2f(0.0,1.0)),
                     Vertex(sf::Vector3f(-0.2,-0.2,0.0), sf::Vector3f(0.0,0.0,1.0),&m2, sf::Vector2f(0.0,0.0)));

   while (true) {
      glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

      draw_face(face1);
      draw_face(face2);

      window.display();
   }

   return EXIT_SUCCESS;
} // RAII: Automatic closing of window and destruction of everything
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

genzm

  • Newbie
  • *
  • Posts: 26
    • View Profile
Re: textures in opengl
« Reply #26 on: January 03, 2013, 12:40:25 pm »
Thanks for your concern, but I know how to use C++ memory allocation.
I know it's not right to allocate everything, and as I said before: In my original code not everything is allocated on the heap.

As for the materials. You are right, cases like that will probably never occur. So I'm going to make some modificatioins around there.

genzm

  • Newbie
  • *
  • Posts: 26
    • View Profile
Re: textures in opengl
« Reply #27 on: January 04, 2013, 11:31:35 am »
Maybe one last question:
If I wanted to use multiple textures at once (for example: color texture and shadow map). How could I do this? Since no 2 textures can be bound at the same time?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: textures in opengl
« Reply #28 on: January 04, 2013, 11:45:17 am »
Quote
Since no 2 textures can be bound at the same time?
Why not?

glActiveTexture(GL_TEXTUREx); // x is the index of the texture unit
texture.bind();
 
Laurent Gomila - SFML developer