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

Author Topic: masskiller's OpenGL noob questions  (Read 4307 times)

0 Members and 1 Guest are viewing this topic.

masskiller

  • Sr. Member
  • ****
  • Posts: 284
  • Pointers to Functions rock!
    • MSN Messenger - kyogre_jb@hotmail.com
    • View Profile
    • Email
masskiller's OpenGL noob questions
« on: October 22, 2012, 11:56:13 pm »
So I finally started working with openGL. What I want to do is to take an image and rotate it's z axis so that I can make it into a background that looks kind of 3D. I've searched around the web for tutorials over OpenGL with SFML, but all I've found is either bad explanations (or very advanced for me) or code for older SFML versions which doesn't really work even if you port it to 2.0.

Here's the code I am trying:

#include <SFML/OpenGL.hpp>
#include <SFML/Graphics.hpp>

int main()
{
   
    sf::RenderWindow window(sf::VideoMode(1280, 800, 32), "Bullet Testing");
    sf::Sprite spr;

    sf::Image M;
    M.loadFromFile("Resources\\Images\\Menus and Others\\PNG\\Stage 1 Screen Cover (Temporary).png");
    M.createMaskFromColor(sf::Color::Black);
    sf::Texture Tex;
    Tex.loadFromImage(M);
    spr.setTexture(Tex);

    sf::Event event;

    sf::Image Image;
    Image.loadFromFile("Resources\\Images\\Misc\\PNG\\Pattern Background 1.png");

    ///Attempt to work with OpenGL
        GLuint Texture = 1;
        glViewport(74, 748, 682, 678); ///Bottom-left corner, OpenGL workstage, could be the entire window for some effect working.
        glMatrixMode(GL_PROJECTION); ///Modo de la matriz.
        glLoadIdentity(); ///Matriz identidad de OpenGL.

        glClearDepth(1.f);
    glClearColor(0.f, 0.f, 0.f, 0.f);

    // Enable Z-buffer read and write
    glEnable(GL_DEPTH_TEST);
    glDepthMask(GL_TRUE);



    glGenTextures(1, &Texture);
    glBindTexture(GL_TEXTURE_2D, Texture);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, Image.getSize().x, Image.getSize().y, 0, GL_RGBA, GL_UNSIGNED_BYTE, Image.getPixelsPtr()); ///Added those zeros, still don't know what they do...
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);

    ///Tambien se puede hacer con glTexImage2D(mismos parametros).



window.setFramerateLimit(60);
sf::Clock FrameCount;
while (window.isOpen())
    {

        window.pushGLStates();
        window.draw(spr);
        window.popGLStates();

        glBegin();
        ///Stuff.
        glEnd();

        window.display();

        ///glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); Is it necessary even if you have window.clear???

        window.clear();


        while (window.pollEvent(event))
        { if (event.type == sf::Event::Closed) window.close(); }

     }

      return 0;
    }
 

I get some compiling errors, mainly an "Invalid conversion between unsigned char to GLenum" error and finally that glTexImage2D doesn't have enough arguments.

Edit: I kind of fixed the compilation errors I had (by adding 0 in two parameters I still don't know what they work for) and now I'm just getting a bunch of undefined references to all gl calls in the file.

Any help is appreciated.
« Last Edit: October 23, 2012, 02:18:15 am by masskiller »
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!

masskiller

  • Sr. Member
  • ****
  • Posts: 284
  • Pointers to Functions rock!
    • MSN Messenger - kyogre_jb@hotmail.com
    • View Profile
    • Email
Re: masskiller's OpenGL noob questions
« Reply #1 on: October 23, 2012, 03:21:52 am »
btw, I am linking dynamically and I have intel graphics, I don't think that has anything to do with the bunch of undefined references errors, but thought it might be useful for it to be known in any case.
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: 32504
    • View Profile
    • SFML's website
    • Email
Re: masskiller's OpenGL noob questions
« Reply #2 on: October 23, 2012, 06:29:36 am »
Quote
now I'm just getting a bunch of undefined references to all gl calls in the file.
You must link to the OpenGL library, as emphasized in the tutorial.
Laurent Gomila - SFML developer

masskiller

  • Sr. Member
  • ****
  • Posts: 284
  • Pointers to Functions rock!
    • MSN Messenger - kyogre_jb@hotmail.com
    • View Profile
    • Email
Re: masskiller's OpenGL noob questions
« Reply #3 on: October 23, 2012, 05:32:31 pm »
I just linked it and tried the openGL example and it works fine. I've learned a bit by playing around with it. But when it comes to the image loading code (I just want to display it first, then I'll fight with the z axis, rotating and stuff) all I get is a black screen.

int main()
{

    sf::RenderWindow window(sf::VideoMode(1280, 800, 32), "Bullet Testing");
    sf::Event event;

    sf::Image Image;
    Image.loadFromFile("Resources\\Images\\Misc\\PNG\\Pattern Background 1.png");

    ///Attempt to work with OpenGL
        GLuint Texture = 1;
        glViewport(0, 0, 1280, 800);
        ///glViewport(74, 748, 682, 678); Viewport I may use later.

        //glClearDepth(1.f);
    glClearColor(0.f, 0.f, 0.f, 0.f);

    // Enable Z-buffer read and write
    //glEnable(GL_DEPTH_TEST);
    //glDepthMask(GL_TRUE);

    glMatrixMode(GL_PROJECTION); ///Modo de la matriz.
    glLoadIdentity(); ///Matriz identidad de OpenGL.
    glOrtho(0, 1280, 800, 0, -1, 1);
    ///gluPerspective(45.f, 1.f, 1.f, 500.f);

    glEnable(GL_TEXTURE_2D);

    glGenTextures(1, &Texture);
    glBindTexture(GL_TEXTURE_2D, Texture); ///Asegura la textura al GL Context.
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, Image.getSize().x, Image.getSize().y, 0, GL_RGBA, GL_UNSIGNED_BYTE, Image.getPixelsPtr()); ///Linkea la imagen al GL Context.
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);

    ///Tambien se puede hacer con glTexImage2D(mismos parametros).



window.setFramerateLimit(60);
sf::Clock FrameCount;
while (window.isOpen())
    {
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
       

        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

        glBegin(GL_QUADS);
        glTexCoord2f(0.f, 0.f); glVertex2f(74.f, 70.f);
        glTexCoord2f(0.f, 1.f); glVertex2f(74.f, 748.f);
        glTexCoord2f(1.f, 1.f); glVertex2f(756.f, 748.f);
        glTexCoord2f(1.f, 0.f); glVertex2f(756.f, 70.f);
        glEnd();

        window.display();
        //window.clear();

        while (window.pollEvent(event))
        { if (event.type == sf::Event::Closed) window.close(); }

     }

      return 0;
    }

I'm not drawing anything with SFML yet to try to grasp what's going wrong. I've seen some people add other 2 glTexParameteri functions and even after reading a bit about the function I haven't to grasped it.

After linking I had no problem whatsoever and most google searches about how to work this out get me nowhere.
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: 32504
    • View Profile
    • SFML's website
    • Email
Re: masskiller's OpenGL noob questions
« Reply #4 on: October 23, 2012, 06:38:51 pm »
Since the OpenGL example works and does what you want to do (i.e. displays a simple textured entity), you should compare it to your program, and see what difference there are between them, and finally find what's wrong in yours.
Laurent Gomila - SFML developer

masskiller

  • Sr. Member
  • ****
  • Posts: 284
  • Pointers to Functions rock!
    • MSN Messenger - kyogre_jb@hotmail.com
    • View Profile
    • Email
Re: masskiller's OpenGL noob questions
« Reply #5 on: October 23, 2012, 06:44:36 pm »
I think the problem lies in actually putting the image in the quad I am defining. The example is very good for explaining the usage of vertexes with openGL, but has nothing to do with images. I'll keep on searching to see if I can make it work.

Also, are both clears necessary or just one will do? 
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: 32504
    • View Profile
    • SFML's website
    • Email
Re: masskiller's OpenGL noob questions
« Reply #6 on: October 23, 2012, 08:49:06 pm »
Quote
The example is very good for explaining the usage of vertexes with openGL, but has nothing to do with images
Hum? This is exactly what it does, it loads a texture and maps it on the rotating cube. The only difference is that you have a single quad instead of a cube -- which doesn't change anything.

Quote
Also, are both clears necessary or just one will do?
You mean, the color and depth buffers? No, since you have no depth buffer there's no need to clear it ;)
Laurent Gomila - SFML developer

masskiller

  • Sr. Member
  • ****
  • Posts: 284
  • Pointers to Functions rock!
    • MSN Messenger - kyogre_jb@hotmail.com
    • View Profile
    • Email
Re: masskiller's OpenGL noob questions
« Reply #7 on: October 23, 2012, 09:59:59 pm »
I was finally able to show the image, my problem was with the handling of matrixes. I setted the matrix mode in the loop which made it so my image wasn't shown.

Quote
Hum? This is exactly what it does, it loads a texture and maps it on the rotating cube. The only difference is that you have a single quad instead of a cube -- which doesn't change anything.

I found one somewhere (don't remember where) that did the same with colors, not a texture, so I had some trouble with working it out on my own, but now I found the one that uses a texture, that should help me correct what I have so far.

Quote
You mean, the color and depth buffers? No, since you have no depth buffer there's no need to clear it ;)

I mean window.clear and glClear. Surely they're pretty much the same deep down, but I am not sure, so I ask.
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: 32504
    • View Profile
    • SFML's website
    • Email
Re: masskiller's OpenGL noob questions
« Reply #8 on: October 23, 2012, 10:21:35 pm »
Quote
I mean window.clear and glClear. Surely they're pretty much the same deep down, but I am not sure, so I ask.
They are the same, yes.
You could have answered this question yourself quickly by looking at the source code of the clear function ;)
Laurent Gomila - SFML developer

masskiller

  • Sr. Member
  • ****
  • Posts: 284
  • Pointers to Functions rock!
    • MSN Messenger - kyogre_jb@hotmail.com
    • View Profile
    • Email
Re: masskiller's OpenGL noob questions
« Reply #9 on: October 24, 2012, 07:24:03 am »
I have a new question. I want to grant my background some effects and I thought of using a shader, the part of my window that uses openGL is small in comparison to the entirety of the window. How do I pass my openGL stuff to a renderTexture so that I can apply a shader to the texture afterwards?
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: 32504
    • View Profile
    • SFML's website
    • Email
Re: masskiller's OpenGL noob questions
« Reply #10 on: October 24, 2012, 08:14:31 am »
You don't pass your OpenGL stuff to the render-texture, you just draw to it directly, as you would do for a render-window.
Laurent Gomila - SFML developer

masskiller

  • Sr. Member
  • ****
  • Posts: 284
  • Pointers to Functions rock!
    • MSN Messenger - kyogre_jb@hotmail.com
    • View Profile
    • Email
Re: masskiller's OpenGL noob questions
« Reply #11 on: October 24, 2012, 03:34:48 pm »
I just read further in the doc. You can actually pass the shader to all openGL rendering and then you unbind it for anything that doesn't need it. I thought I had to pass it to a renderTexture so that I could apply the shader afterwards.
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!

masskiller

  • Sr. Member
  • ****
  • Posts: 284
  • Pointers to Functions rock!
    • MSN Messenger - kyogre_jb@hotmail.com
    • View Profile
    • Email
Re: masskiller's OpenGL noob questions
« Reply #12 on: October 30, 2012, 10:15:18 pm »
So I finally made my wrapper for the use I'll give my openGL backgrounds and I've got an interesting error. When I draw the background alone it's displayed properly (no transformation test yet), but when I draw something with SFML the only thing it's shown is a dark blue mat where the base image should be.

Here's the code of the main function that reproduces the problem:

int main()
{

    sf::RenderWindow window(sf::VideoMode(1280, 800, 32), "Bullet Testing", sf::Style::Fullscreen);
    sf::Sprite spr;
    sf::Time T = sf::seconds(20.f);
    sf::Event event;
    sf::Clock C;

    sf::Image M;
    M.loadFromFile("Resources\\Images\\Menus and Others\\PNG\\Stage 1 Screen Cover (Temporary).png");
    M.createMaskFromColor(sf::Color::Black);
    sf::Texture Tex;
    Tex.loadFromImage(M);
    spr.setTexture(Tex);

    sf::Texture Mask;
    Mask.loadFromFile("Resources\\Images\\Misc\\PNG\\Dione Background Pattern.png");
    sf::Image Image;
    Image.loadFromFile("Resources\\Images\\Misc\\PNG\\Flora Background Pattern.png");

    GLBackground B(Mask ,Image);
    B.prepareGL();
    B.loadBackground();

window.setFramerateLimit(60);
sf::Clock FrameCount;
while (window.isOpen())
    {
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);


        window.draw(B);
        window.draw(spr);
        window.display();


        while (window.pollEvent(event))
        { if (event.type == sf::Event::Closed || event.key.code == sf::Keyboard::Escape) window.close(); }

     }
      return 0;
}
 

The image and texture dependencies are necessary, since the wrapper works around them.

And here's the code for the wrapper functions used:

void GLBackground::draw(sf::RenderTarget& target, sf::RenderStates states) const
{ ///Posiciones fijas por uso.
    if(h_Effect) Eff->bind();
    glBegin(GL_QUADS);
    glTexCoord2f(0.f, 0.f); glVertex2f(68.f, 64.f);
    glTexCoord2f(0.f, 1.f); glVertex2f(68.f, 752.f);
    glTexCoord2f(1.f, 1.f); glVertex2f(760.f, 752.f);
    glTexCoord2f(1.f, 0.f); glVertex2f(760.f, 64.f);
    glEnd();
    if(h_Effect) Eff->unbind();
}

inline void prepareGL() const
        {
        glViewport(0, 0, 1280, 800);
        glClearColor(0.f, 0.f, 0.f, 0.f);

        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        glOrtho(0, 1280, 800, 0, -100, 100);

        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity();

        glEnable(GL_TEXTURE_2D);
        }

inline void loadBackground(bool MaskBlack = false)
        {
        if(MaskBlack) { Back.createMaskFromColor(sf::Color::Black); }

        glGenTextures(index, &index);
        glBindTexture(GL_TEXTURE_2D, index);
        glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, Back.getSize().x, Back.getSize().y, 0, GL_RGBA, GL_UNSIGNED_BYTE, Back.getPixelsPtr());
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
        }

 

Effect is a polymorphic effect class around sf::Shader, but for this test I am not using it.
The prepareGL and loadBackground are inline because I didn't know whether I would have scope issues with my openGL calls. I have a bunch of transformation functions, but I am not using any yet.

Any help is appreciated once again.

Edit: I made a small change and the background mask is shown now, alongside the dark blue mat, but the spr sprite now doesn't draw. I am not sure if there's something messing SFML contexts that makes it not get drawn. Even when the mask is drawn after the GL stuff it is still below the blue mat thing...
« Last Edit: October 30, 2012, 10:24:38 pm by masskiller »
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: 32504
    • View Profile
    • SFML's website
    • Email
Re: masskiller's OpenGL noob questions
« Reply #13 on: October 30, 2012, 10:23:52 pm »
Please read the "window - opengl" tutorial, especially the end about the graphics module.
Laurent Gomila - SFML developer

masskiller

  • Sr. Member
  • ****
  • Posts: 284
  • Pointers to Functions rock!
    • MSN Messenger - kyogre_jb@hotmail.com
    • View Profile
    • Email
Re: masskiller's OpenGL noob questions
« Reply #14 on: October 30, 2012, 10:28:44 pm »
Please read the "window - opengl" tutorial, especially the end about the graphics module.

Nevermind, it was just a pushGLstates/popGLstates that I forgot to add. Everything was working fine.
« Last Edit: October 30, 2012, 10:30:31 pm by masskiller »
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!