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

Author Topic: [Resolved][SFML2] OpenGL Drawing texture  (Read 3822 times)

0 Members and 1 Guest are viewing this topic.

Metapyziks

  • Newbie
  • *
  • Posts: 33
    • View Profile
[Resolved][SFML2] OpenGL Drawing texture
« on: January 14, 2010, 09:42:16 pm »
I have a sf::Image that I want to use as the faces of the standard example cube. What steps should I take in order to use it? I made a guess with this:
Code: [Select]
...

glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

image.Bind();

glBegin(GL_QUADS);

glVertex3f(-50.f, -50.f, -50.f);
glVertex3f(-50.f,  50.f, -50.f);
glVertex3f( 50.f,  50.f, -50.f);
glVertex3f( 50.f, -50.f, -50.f);

glVertex3f(-50.f, -50.f, 50.f);
glVertex3f(-50.f,  50.f, 50.f);
glVertex3f( 50.f,  50.f, 50.f);
glVertex3f( 50.f, -50.f, 50.f);

glVertex3f(-50.f, -50.f, -50.f);
glVertex3f(-50.f,  50.f, -50.f);
glVertex3f(-50.f,  50.f,  50.f);
glVertex3f(-50.f, -50.f,  50.f);

glVertex3f(50.f, -50.f, -50.f);
glVertex3f(50.f,  50.f, -50.f);
glVertex3f(50.f,  50.f,  50.f);
glVertex3f(50.f, -50.f,  50.f);

glVertex3f(-50.f, -50.f,  50.f);
glVertex3f(-50.f, -50.f, -50.f);
glVertex3f( 50.f, -50.f, -50.f);
glVertex3f( 50.f, -50.f,  50.f);

glVertex3f(-50.f, 50.f,  50.f);
glVertex3f(-50.f, 50.f, -50.f);
glVertex3f( 50.f, 50.f, -50.f);
glVertex3f( 50.f, 50.f,  50.f);

glEnd();

...


Where "image" exists, and by simply commenting out the image.Bind() line the cube draws (although white), so the OpenGL part works.

What should I do?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
[Resolved][SFML2] OpenGL Drawing texture
« Reply #1 on: January 14, 2010, 09:47:39 pm »
Did you call window.PreserveOpenGLStates(true) ?
Laurent Gomila - SFML developer

Metapyziks

  • Newbie
  • *
  • Posts: 33
    • View Profile
[Resolved][SFML2] OpenGL Drawing texture
« Reply #2 on: January 14, 2010, 09:51:11 pm »
Quote from: "Laurent"
Did you call window.PreserveOpenGLStates(true) ?


It's not a member of sf::RenderWindow or sf::Window, probably because I am using SFML 2. What is the equivalent in SFML 2, if that is the problem?

Edit: Changed the OP title

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
[Resolved][SFML2] OpenGL Drawing texture
« Reply #3 on: January 14, 2010, 09:53:17 pm »
Indeed, in SFML 2 you don't have to do anything. Can you provide a minimal and complete piece of code that reproduces the problem?
Laurent Gomila - SFML developer

Metapyziks

  • Newbie
  • *
  • Posts: 33
    • View Profile
[Resolved][SFML2] OpenGL Drawing texture
« Reply #4 on: January 14, 2010, 10:01:22 pm »
Quote from: "Laurent"
Indeed, in SFML 2 you don't have to do anything. Can you provide a minimal and complete piece of code that reproduces the problem?


I've got a lot of it strewn over several classes, but I'll try to make a simple app that does this.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
[Resolved][SFML2] OpenGL Drawing texture
« Reply #5 on: January 14, 2010, 10:04:04 pm »
Well, you can start with the OpenGL sample from the SDK. It creates an OpenGL texture from the image, but you can comment out this part and use the image and its Bind() function directly. It should be faster than writing an example from scratch :)
Laurent Gomila - SFML developer

Metapyziks

  • Newbie
  • *
  • Posts: 33
    • View Profile
[Resolved][SFML2] OpenGL Drawing texture
« Reply #6 on: January 14, 2010, 10:18:37 pm »
Quote from: "Laurent"
Well, you can start with the OpenGL sample from the SDK. It creates an OpenGL texture from the image, but you can comment out this part and use the image and its Bind() function directly. It should be faster than writing an example from scratch :)


Well I just finished one anyway, with the problem in the OP:
Code: [Select]
#include <SFML/Graphics.hpp>
#include <SFML/Window.hpp>
#include <SFML/OpenGL.hpp>

int main()
{
sf::RenderWindow app_window;
app_window.Create( sf::VideoMode( 768, 512 ), "Texture Test", sf::Style::Close );

sf::Image image;
image.LoadFromFile( "test.png" );

sf::Sprite sprite( image );

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

glEnable( GL_DEPTH_TEST );
glDepthMask( GL_TRUE );

glMatrixMode( GL_PROJECTION );
glLoadIdentity();
gluPerspective( 90.f, 1.f, 1.f, 500.f );

while( app_window.IsOpened())
{
sf::Event this_event;
while( app_window.GetEvent( this_event ))
{
switch( this_event.Type )
{
case sf::Event::Closed:
app_window.Close();
}
}

app_window.Clear();

/* Checking to see if the image has loaded fine */
app_window.Draw( sprite );

glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );

glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
glTranslatef( 0.f, 0.f, -200.f );

/* De-comment the next line, and the cube is not drawn for me */
//image.Bind();

glBegin( GL_QUADS );

glVertex3f(-50.f, -50.f, -50.f);
glVertex3f(-50.f,  50.f, -50.f);
glVertex3f( 50.f,  50.f, -50.f);
glVertex3f( 50.f, -50.f, -50.f);

glVertex3f(-50.f, -50.f, 50.f);
glVertex3f(-50.f,  50.f, 50.f);
glVertex3f( 50.f,  50.f, 50.f);
glVertex3f( 50.f, -50.f, 50.f);

glVertex3f(-50.f, -50.f, -50.f);
glVertex3f(-50.f,  50.f, -50.f);
glVertex3f(-50.f,  50.f,  50.f);
glVertex3f(-50.f, -50.f,  50.f);

glVertex3f(50.f, -50.f, -50.f);
glVertex3f(50.f,  50.f, -50.f);
glVertex3f(50.f,  50.f,  50.f);
glVertex3f(50.f, -50.f,  50.f);

glVertex3f(-50.f, -50.f,  50.f);
glVertex3f(-50.f, -50.f, -50.f);
glVertex3f( 50.f, -50.f, -50.f);
glVertex3f( 50.f, -50.f,  50.f);

glVertex3f(-50.f, 50.f,  50.f);
glVertex3f(-50.f, 50.f, -50.f);
glVertex3f( 50.f, 50.f, -50.f);
glVertex3f( 50.f, 50.f,  50.f);

glEnd();

app_window.Display();
}
}


But I'll try to use the method in the example now, like you said.

Metapyziks

  • Newbie
  • *
  • Posts: 33
    • View Profile
[Resolved][SFML2] OpenGL Drawing texture
« Reply #7 on: January 14, 2010, 10:26:08 pm »
Ok, I think I know the problem now. I needed to set the texture coordinates for each quad.

Thanks for your help!

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
[Resolved][SFML2] OpenGL Drawing texture
« Reply #8 on: January 14, 2010, 10:28:13 pm »
Ah, yes. I didn't notice in your code :)
Laurent Gomila - SFML developer