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

Author Topic: loading a png from sf::Image to an openGL useable format  (Read 4401 times)

0 Members and 1 Guest are viewing this topic.

dartosgamer

  • Newbie
  • *
  • Posts: 14
    • View Profile
Re: loading a png from sf::Image to an openGL useable format
« Reply #15 on: July 01, 2013, 08:14:22 pm »
Well on the screen it shows a white box. But the texture should be black...

Currently this is the screen offset

SCREEN_WIDTH - tex->getTextureWidth() / 2, SCREEN_HEIGHT - tex->getTextureHeight() / 2

That is fed into glTranslatef before glBegin (shown below)

 
void Texture::render(GLfloat x, GLfloat y)
          {
                  if(mTextureID != 0)
                  {
                          glLoadIdentity();
 
                          glTranslatef(x, y, 0);
 
                          glBindTexture(GL_TEXTURE_2D, mTextureID);
                  }
 
                  glBegin(GL_QUADS);
                          glTexCoord2f(0, 0); glVertex3f(0, 0, 0);
                          glTexCoord2f(1, 0); glVertex3f(mTextureWidth, 0, 0);
                          glTexCoord2f(1, 1); glVertex3f(mTextureWidth, mTextureHeight, 0);
                          glTexCoord2f(0, 1); glVertex3f(0, mTextureHeight, 0);
                  glEnd();
          }
 

But there is only like a quarter of a white box in the bottom right corner of the screen.
« Last Edit: July 01, 2013, 08:17:50 pm by dartosgamer »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: loading a png from sf::Image to an openGL useable format
« Reply #16 on: July 01, 2013, 08:40:20 pm »
I can't help you if you don't show a complete an minimal example that reproduces the problem. A single OpenGL state can mess up your whole rendering, so it's important to see everything.
Laurent Gomila - SFML developer

dartosgamer

  • Newbie
  • *
  • Posts: 14
    • View Profile
Re: loading a png from sf::Image to an openGL useable format
« Reply #17 on: July 01, 2013, 08:48:39 pm »
Oh sorry...

main.cpp
#include "LazySpace/LazySpace.hpp"

int main()
{
        if(!ls::initGL()) return -1;

        ls::update();
}
 

LazySpace.hpp
#ifndef LAZY_HEADER
#define LAZY_HEADER

#include "SFML/OpenGL.hpp"
#include "SFML/Window.hpp"
#include "SFML/Graphics.hpp"

#include <stdio.h>

#include "../Texture/Texture.hpp"

namespace ls
{
        extern sf::Window* mainWin;

        const int SCREEN_WIDTH = 400;
        const int SCREEN_HEIGHT = 480;
        const int SCREEN_BPP = 32;

        bool initGL();
        bool loadMedia();
        void update();
        void render();
}

#endif
 

LazySpace.cpp
#include "LazySpace.hpp"

namespace ls
{
        sf::Window* mainWin;
        sf::Image image;
        Texture* tex;

        bool initGL()
        {
                mainWin = new sf::Window(sf::VideoMode(800, 600), "This");

                glMatrixMode(GL_PROJECTION);
                glLoadIdentity();
                glOrtho(0, SCREEN_WIDTH, SCREEN_HEIGHT, 0, 1, -1);

                glMatrixMode(GL_MODELVIEW);
                glLoadIdentity();

                GLuint error = glGetError();
                if(error != GL_NO_ERROR)
                {
                        printf("GL init error: %s\n", gluErrorString(error));
                        return false;
                }

                if(!loadMedia()) return false;

                return true;
        }

        bool loadMedia()
        {
                // if(!image.loadFromFile("assets/smile.png"))
                // {
                //         printf("CAN'T LOAD THIS THING");
                //         return false;
                // }

                GLubyte* pixels = new GLubyte[128*128];

                for(int i = 0; i < 128*128; i++)
                {
                        pixels[i] = 1;
                }

                tex = new Texture();
                tex->loadTextureFromPixels32(pixels, 128, 128);
               
                return true;
        }

        void update()
        {
                sf::Event ev;

                while(mainWin->isOpen())
                {
                        if(mainWin->pollEvent(ev))
                        {
                                if(ev.type == sf::Event::Closed) mainWin->close();
                        }

                        render();
                }
        }

        void render()
        {
                // glClear(GL_COLOR_BUFFER_BIT);

                // glMatrixMode(GL_MODELVIEW);
                // glLoadIdentity();

                // glTranslatef(SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2, 0);

                // glBegin(GL_QUADS);
                //         glTexCoord2f(0, 0); glVertex2i(50, 50);
                //         glVertex2i(50, -50);
                //         glVertex2i(-50, -50);
                //         glVertex2i(-50, 50);
                // glEnd();
               
                tex->render(SCREEN_WIDTH - tex->getTextureWidth() / 2, SCREEN_HEIGHT - tex->getTextureHeight() / 2);
             

                mainWin->display();
        }
};
 

Texture.hpp
#ifndef TEXTURE_H
#define TEXTURE_H

#include "../LazySpace/LazySpace.hpp"

namespace ls
{
        class Texture
        {
        public:
                Texture();
                ~Texture();

                bool loadTextureFromPixels32(GLubyte* pixels, GLuint width, GLuint height);

                void freeTexture();

                void render(GLfloat x, GLfloat y);

                GLuint getTextureID();

                GLuint getTextureWidth();

                GLuint getTextureHeight();

        private:
                GLuint mTextureID;

                GLuint mTextureWidth;
                GLuint mTextureHeight;
        };
}
#endif
 

Texture.cpp
#include "Texture.hpp"

namespace ls
{
        Texture::Texture()
        {
                mTextureID = 0;

                mTextureWidth = 0;
                mTextureHeight = 0;
        }

        Texture::~Texture()
        {
                freeTexture();
        }

        GLuint Texture::getTextureWidth()
        {
                return mTextureWidth;
        }

        GLuint Texture::getTextureHeight()
        {
                return mTextureHeight;
        }

        bool Texture::loadTextureFromPixels32(GLubyte* pixels, GLuint width, GLuint height)
        {
                freeTexture();

                mTextureWidth = width;
                mTextureHeight = height;

                glGenTextures(1, &mTextureID);

                glBindTexture(GL_TEXTURE_2D, mTextureID);

                glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixels);

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

                glBindTexture(GL_TEXTURE_2D, NULL);

                GLenum error = glGetError();

                if(error != GL_NO_ERROR);
                {
                        printf("Error loading texture from %p. %s\n", pixels, gluErrorString(error));
                        return false;
                }

                return true;
        }

        void Texture::freeTexture()
        {
                if(mTextureID != 0)
                {
                        glDeleteTextures(1, &mTextureID);
                        mTextureID = 0;
                }

                mTextureWidth = 0;
                mTextureHeight = 0;
        }

        void Texture::render(GLfloat x, GLfloat y)
        {
                if(mTextureID != 0)
                {
                        glLoadIdentity();

                        glTranslatef(x, y, 0);

                        glBindTexture(GL_TEXTURE_2D, mTextureID);
                }

                glBegin(GL_QUADS);
                        glTexCoord2f(0, 0); glVertex3f(0, 0, 0);
                        glTexCoord2f(1, 0); glVertex3f(mTextureWidth, 0, 0);
                        glTexCoord2f(1, 1); glVertex3f(mTextureWidth, mTextureHeight, 0);
                        glTexCoord2f(0, 1); glVertex3f(0, mTextureHeight, 0);
                glEnd();
        }
}
 

There... thats all of it.


btw, thanks so much for helping with this. I've used SFML for a few years and it's really nice to see that its creator is so involved :D

Thank you.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: loading a png from sf::Image to an openGL useable format
« Reply #18 on: July 01, 2013, 08:54:27 pm »
If I wanted to see your code I would say "show your code". If I say "a complete and minimal code that reproduces the problem", there's a very good reason for it :P

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

dartosgamer

  • Newbie
  • *
  • Posts: 14
    • View Profile
Re: loading a png from sf::Image to an openGL useable format
« Reply #19 on: July 01, 2013, 10:14:54 pm »
DOH! DX sorry. I'm REALLY new to opengl so... idk what's relevant and what isn't (though I don't think main.cpp is important.) I'll post a minimal program later today.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: loading a png from sf::Image to an openGL useable format
« Reply #20 on: July 01, 2013, 10:20:45 pm »
Quote
idk what's relevant and what isn't
That's why you must experiment with your code, remove one part after the other, making sure that the bug is still there after each modification.

By doing this you will not only provide a code that is easy to test for us, but you will also understand your code better, and maybe even find the problem yourself.
Laurent Gomila - SFML developer

dartosgamer

  • Newbie
  • *
  • Posts: 14
    • View Profile
Re: loading a png from sf::Image to an openGL useable format
« Reply #21 on: July 01, 2013, 11:50:08 pm »
Well I am officialy silly.... I forgot to put in glEnable(GL_TEXTURE_2D) AND the error variably was a GLenum type not GLuint.... well.... dern... thanks for all the help though.