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

Author Topic: Images and OpenGL  (Read 3089 times)

0 Members and 1 Guest are viewing this topic.

Groogy

  • Hero Member
  • *****
  • Posts: 1469
    • MSN Messenger - groogy@groogy.se
    • View Profile
    • http://www.groogy.se
    • Email
Images and OpenGL
« on: January 16, 2009, 11:35:06 pm »
Couldn't find any topics about this when I made a search.

Can I use like the pointer to the pixel data in a sf::Image to use as an texture or image with OpenGL? Kinda like you can do with SDL_Structures and OpenGL.

Ow yeah, can you also use Sprites? To just draw an image on the display while using OpenGL? Would simplify a lot of stuff. I tried myself but the image was kinda incomplete and it didn't show the entire image. Some random squares were black.

Keep in mind I'm just trying out SFML and thinking about moving from SDL to SFML.
Developer and Maker of rbSFML and Programmer at Paradox Development Studio

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Images and OpenGL
« Reply #1 on: January 16, 2009, 11:45:45 pm »
You can do all this stuff. Just show us some code if something doesn't work ;)
Laurent Gomila - SFML developer

Groogy

  • Hero Member
  • *****
  • Posts: 1469
    • MSN Messenger - groogy@groogy.se
    • View Profile
    • http://www.groogy.se
    • Email
Images and OpenGL
« Reply #2 on: January 17, 2009, 12:00:47 am »
Almost got it working. Got very many dots instead now spread over the image in rectangles or something. Kinda like spray in paint. Ow yeah, may be nothing but just now when I pressed the close button my X-Server crashed and I failed back to the login screen.

Code: [Select]
#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>
#include <iostream>

using namespace std;

sf::RenderWindow window;
const unsigned int MAX_FPS = 20;

void SetupWindow();
void SetupGL();
void ResizeGLScene(unsigned int width, unsigned int height);
void HandleEvents();

int main() {
SetupWindow();
sf::Image img;
sf::Sprite sprite;

img.LoadFromFile("ubuntu-tan2.jpg");
sprite.SetImage(img);

while(window.IsOpened()) {
   window.Clear();
        glLoadIdentity();
        window.Draw(sprite);
        window.Display();
HandleEvents();
}

return 0;
}

void SetupWindow() {
    sf::WindowSettings settings;
    settings.DepthBits = 24;
    settings.StencilBits = 8;
    settings.AntialiasingLevel = 2;

window.Create(sf::VideoMode(800, 600, 32), "Groogy App", sf::Style::Close, settings);
window.SetFramerateLimit(MAX_FPS);
window.SetActive(true);
SetupGL();
}

void SetupGL() {
glShadeModel(GL_SMOOTH);
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glClearDepth(1.0f);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
}

void ResizeGLScene(unsigned int width, unsigned int height) {
    if(height == 0)
    {
        height = 1;
    }

    glViewport(0, 0, width, height);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();

    gluPerspective(45.0f, (GLfloat)width/(GLfloat)height,0.1f,100.0f);
}

void HandleEvents() {
sf::Event event;
while(window.GetEvent(event)) {
switch(event.Type) {
case sf::Event::Closed:
window.Close();
break;
            case sf::Event::Resized:
                ResizeGLScene(event.Size.Width, event.Size.Height);
                break;
            default:
                break;
}
}
}
Developer and Maker of rbSFML and Programmer at Paradox Development Studio

Groogy

  • Hero Member
  • *****
  • Posts: 1469
    • MSN Messenger - groogy@groogy.se
    • View Profile
    • http://www.groogy.se
    • Email
Images and OpenGL
« Reply #3 on: January 17, 2009, 12:06:09 am »
Wow, I fixed it, replaced: window.Clear() to glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); instead.
I used it before and it didn't help much, but it seems like I made something wrong that I must have changed.
Developer and Maker of rbSFML and Programmer at Paradox Development Studio

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Images and OpenGL
« Reply #4 on: January 17, 2009, 10:12:40 am »
window.Clear() doesn't clear the depth buffer.
Laurent Gomila - SFML developer