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

Author Topic: How to draw a simple sprite with OpenGL?  (Read 6845 times)

0 Members and 1 Guest are viewing this topic.

Assassinbeast

  • Jr. Member
  • **
  • Posts: 62
    • View Profile
    • Email
How to draw a simple sprite with OpenGL?
« 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.

AlexAUT

  • Sr. Member
  • ****
  • Posts: 396
    • View Profile
Re: How to draw a simple sprite with OpenGL?
« Reply #1 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
« Last Edit: February 15, 2015, 01:21:29 pm by AlexAUT »

Assassinbeast

  • Jr. Member
  • **
  • Posts: 62
    • View Profile
    • Email
Re: How to draw a simple sprite with OpenGL?
« Reply #2 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?

Ztormi

  • Jr. Member
  • **
  • Posts: 71
  • Web developer by day. Game developer by night.
    • View Profile
Re: How to draw a simple sprite with OpenGL?
« Reply #3 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
« Last Edit: February 16, 2015, 09:58:55 am by Ztormi »

Assassinbeast

  • Jr. Member
  • **
  • Posts: 62
    • View Profile
    • Email
Re: How to draw a simple sprite with OpenGL?
« Reply #4 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!  :)

Gambit

  • Sr. Member
  • ****
  • Posts: 283
    • View Profile
Re: How to draw a simple sprite with OpenGL?
« Reply #5 on: February 16, 2015, 03:30:11 pm »
You have no UV co-ordinates for your vertices.

Assassinbeast

  • Jr. Member
  • **
  • Posts: 62
    • View Profile
    • Email
Re: How to draw a simple sprite with OpenGL?
« Reply #6 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

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
Re: How to draw a simple sprite with OpenGL?
« Reply #7 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:

Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Assassinbeast

  • Jr. Member
  • **
  • Posts: 62
    • View Profile
    • Email
Re: How to draw a simple sprite with OpenGL?
« Reply #8 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.
« Last Edit: February 16, 2015, 06:00:51 pm by Assassinbeast »

Jesper Juhl

  • Hero Member
  • *****
  • Posts: 1405
    • View Profile
    • Email
Re: How to draw a simple sprite with OpenGL?
« Reply #9 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.

Assassinbeast

  • Jr. Member
  • **
  • Posts: 62
    • View Profile
    • Email
Re: How to draw a simple sprite with OpenGL?
« Reply #10 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.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: How to draw a simple sprite with OpenGL?
« Reply #11 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 ;)
Laurent Gomila - SFML developer

 

anything