The SDL forums have moved to discourse.libsdl.org.
This is just a read-only archive of the previous forums, to keep old links working.


SDL Forum Index
SDL
Simple Directmedia Layer Forums
SDL 2.0 OpenGL textures.
Cupcake_101


Joined: 19 Mar 2013
Posts: 2
Hello everyone, I recently took a look at SDL 2.0 and it is undoubtedly a really good improvement from the last version but I got a problem, i can't seem to properly use SDL_GL_BindTexture with glBegin(GL_QUADS). I initialize opengl as it should.

OpenGL Initialization
Code:

bool SDLWindow::InitOpenGL()
{
   this->glcontext = SDL_GL_CreateContext(this->window);
   int w, h;
   glEnable(GL_TEXTURE_2D);
   glEnable(GL_BLEND);
   glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
   glMatrixMode(GL_PROJECTION);
   glLoadIdentity();
   SDL_GetWindowSize(this->window, &w, &h);
   glViewport(0, 0, w, h);
   glOrtho(0.0f, w, h, 0.0f, -1.0f, 1.0f);
   glMatrixMode(GL_MODELVIEW);
   glLoadIdentity();

   GLenum error = glGetError();
   if(error != GL_NO_ERROR)
   {
      return false;
   }

   return true;
}


Drawing function
Code:

void Graphics::Draw(Vector2* position, Texture2D* texture)
{
   SDL_GL_BindTexture(texture->texture, (float*)texture->GetWidth(), (float*)texture->GetHeight());
   glBegin(GL_QUADS);
      glTexCoord2f(0, 0);
      glVertex2f(position->x, position->y);
      glTexCoord2f(1, 0);
      glVertex2f(position->x + texture->GetWidth(), position->y);
      glTexCoord2f(1, 1);
      glVertex2f(position->x + texture->GetWidth(), position->y + texture->GetHeight());
      glTexCoord2f(0, 1);
      glVertex2f(position->x, position->y + texture->GetHeight());
   glEnd();
}


This is my new function for drawing textures to the screen but it is drawing a blank rectangle -_-' can anyone tell me what is wrong?
razerwolf


Joined: 23 Aug 2013
Posts: 5
Location: United States
The last two arguments to SDL_GL_BindTexture aren't for passing data in, it is for passing data out, because on systems that support the GL_ARB_texture_rectangle OpenGL Extension the texture coordinates won't be in the range of 0.0-1.0 but in 0.0 to width/height
so rectangle drawing code should look something like this:
Code:

void Graphics::Draw(Vector2* position, Texture2D* texture)
{
   float w, h;
   SDL_GL_BindTexture(texture->texture, &w, &h);
   glBegin(GL_QUADS);
      glTexCoord2f(0.0, 0.0);
      glVertex2f(position->x, position->y);
      glTexCoord2f(1.0*w, 0.0);
      glVertex2f(position->x + texture->GetWidth(), position->y);
      glTexCoord2f(1.0*w, 1.0*h);
      glVertex2f(position->x + texture->GetWidth(), position->y + texture->GetHeight());
      glTexCoord2f(0.0, 1.0*h);
      glVertex2f(position->x, position->y + texture->GetHeight());
   glEnd();
   SDL_GL_UnbindTexture(texture->texture);

}