SFML community forums

Help => General => Topic started by: Assassinbeast on February 14, 2015, 08:34:16 pm

Title: How to draw a simple sprite with OpenGL?
Post by: Assassinbeast on February 14, 2015, 08:34:16 pm
Hello, im new to OpenGL, and so far, i am able to draw a triangle with this simple code:

glClearColor(1,0,0,1);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glBegin(GL_TRIANGLES);

glVertex2d(-0.5f, -0.5f);
glVertex2d(0, 0.5f);
glVertex2d(0.5f, -0.5f);

glEnd();

window.display();

But how do i put a texture on it? I have read the tutorial here at the bottom of the page
http://www.sfml-dev.org/tutorials/2.2/graphics-sprite.php

I have created a texture, loaded it, and then call
sf::Texture::bind(&texture);

But my triangle is still white.
Title: Re: How to draw a simple sprite with OpenGL?
Post by: AlexAUT on February 15, 2015, 01:19:36 pm
I have no clue about legacy opengl (only modern) but shouldn't there be something like glTexCoord to set the texture coordiantes?

btw there is an offical example: https://github.com/SFML/SFML/blob/master/examples/opengl/OpenGL.cpp


AlexAUT
Title: Re: How to draw a simple sprite with OpenGL?
Post by: Assassinbeast on February 16, 2015, 02:39:44 am
Oh, i have no idea it was legacy or modern. But the example link, is that the modern way to do it?
Title: Re: How to draw a simple sprite with OpenGL?
Post by: Ztormi on February 16, 2015, 09:50:15 am
Yes it is.

I believe AlexAUT is referring to the usage of glBegin, glVertex and glEnd which is an ancient way of drawing things. You can learn more about this on http://gamedev.stackexchange.com/questions/34108/opengl-vbo-or-glbegin-glend
http://www.songho.ca/opengl/gl_vertexarray.html and
http://duriansoftware.com/joe/An-intro-to-modern-OpenGL.-Table-of-Contents.html
Title: Re: How to draw a simple sprite with OpenGL?
Post by: Assassinbeast on February 16, 2015, 02:10:43 pm
Ahh, ok.

But right now, i just want to put a super simple texture on my triangle.
Do you know how to do it the old way then?

glClearColor(1,0,0,1);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glBegin(GL_TRIANGLES);
sf::Texture::bind(&texture); //This doesn't seem to work  :-(
glVertex2d(-0.5f, -0.5f);
glVertex2d(0, 0.5f);
glVertex2d(0.5f, -0.5f);

glEnd();

window.display();

Do i need to call something else for the Texture::bind() to work? It doesn't seem to work atm.
I appreciate your help though!  :)
Title: Re: How to draw a simple sprite with OpenGL?
Post by: Gambit on February 16, 2015, 03:30:11 pm
You have no UV co-ordinates for your vertices.
Title: Re: How to draw a simple sprite with OpenGL?
Post by: Assassinbeast on February 16, 2015, 04:11:02 pm
You have no UV co-ordinates for your vertices.

Ok :-)
So how can i apply my uv coordinates?
For example, i just want the whole texture put on my triangle
Title: Re: How to draw a simple sprite with OpenGL?
Post by: eXpl0it3r on February 16, 2015, 04:30:35 pm
SFML gives easy access to OpenGL, yet SFML doesn't teach you OpenGL. For that there are better resources around.
For example:

http://www.youtube.com/watch?v=6-9XFm7XAT8
Title: Re: How to draw a simple sprite with OpenGL?
Post by: Assassinbeast on February 16, 2015, 05:54:30 pm
Thanks for pointing the link out exploiter.
But is there a chance that you/anyone could show me the code i need to put in? Because right now im totally lost because in the sfml tutorial, there stand i shall use the sf::texture::bind() which is not directly opengl stuff.

So right now, i just want to put a texture on my triangle to get started (just quickly getting my "hands dirty" of opengl with sfml and see stuff).

Sorry for bothering so much on the forums, but i just need this last simple step to get going.
Title: Re: How to draw a simple sprite with OpenGL?
Post by: Jesper Juhl on February 16, 2015, 06:00:19 pm
If you just want to quickly "see stuff", why don't you just use a sf::texture with a sf::sprite and draw that?
I'm guessing your reason is that you want to learn raw OpenGL, but in case it's not then there's no need to mess with it just to draw something.
Title: Re: How to draw a simple sprite with OpenGL?
Post by: Assassinbeast on February 16, 2015, 06:07:09 pm
If you just want to quickly "see stuff", why don't you just use a sf::texture with a sf::sprite and draw that?
I'm guessing your reason is that you want to learn raw OpenGL, but in case it's not then there's no need to mess with it just to draw something.

 :-\ ....

I know how to do it with sprites in sfml. But right now, i just want to put a texture on, because im lost. And if i learn on how to put a texture on a triangle, then i get a better overview and understanding so i can continue my work and learn things easier.

I just need this one step.
Title: Re: How to draw a simple sprite with OpenGL?
Post by: Laurent on February 16, 2015, 06:44:51 pm
The answer was given in the first reply:

Quote
glTexCoord

I let you study the documentation of this (collection of) function(s), and I'm sure you'll figure out how to use it ;)