PDA

View Full Version : Texture is not drawing On Rectangle



nickkorta
02-17-2014, 03:22 PM
Hi Let me first start off saying that you probably get this question often, so I'm sorry about that. I looked around the internet for a few hours yesterday and today. I'm new to OpenGL As you could probably tell. I'm using SDL2 in Visual studio 2012. I'm using SDL's surface to load in image data.

Code


#include "Game.h"

Game::Game(void)
{
Initalize();
LoadContent();

while (running)
{
Update();
Draw();
}
}


Game::~Game(void)
{
SDL_GL_DeleteContext(glContext);
SDL_DestroyWindow(window);
SDL_Quit();
}

void Game::Initalize()
{
running = true;

if (SDL_Init(SDL_INIT_EVERYTHING) == -1)
{
std::cout << SDL_GetError() << std::endl;
running = false;
return;
}

window = nullptr;
window = SDL_CreateWindow("Game",SDL_WINDOWPOS_CENTERED,SDL_WINDOWPOS_CENTERED,640 ,480, SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN);
if (window == nullptr)
{
std::cout << SDL_GetError() << std::endl;
running = false;
return;
}

glContext = SDL_GL_CreateContext(window);
SDL_GL_MakeCurrent (window,glContext);

CreateOrthographicProjection(16.0,12.0);
}

void Game::CreateOrthographicProjection(GLfloat width, GLfloat height)
{
glOrtho(0,width,0,height,-1,1);
}

void Game::LoadContent()
{
SDL_Surface *t = SDL_LoadBMP("test.bmp");
if (t == nullptr)
printf("Error: \"%s\"\n",SDL_GetError()); return;

glGenTextures(1, &texture);

// "Bind" the newly created texture : all future texture functions will modify this texture
glBindTexture(GL_TEXTURE_2D, texture);

// Give the image to OpenGL
glTexImage2D(GL_TEXTURE_2D, 0,GL_RGBA, t->w,t->h, 0, GL_BGR, GL_UNSIGNED_BYTE, t->pixels);

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);


SDL_FreeSurface(t);
}

void Game::Update()
{
while (SDL_PollEvent(&e))
{
if (e.type == SDL_QUIT)
{
running = false;
return;
}
}
}

void Game::Draw()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glClearColor(0.219f,0.58,0.93,1);

glBindTexture(GL_TEXTURE_2D, texture);
glEnable(GL_TEXTURE_2D);
glRectf(0.0,0.0,2,2);


glDisable(GL_TEXTURE_2D );


SDL_GL_SwapWindow(window);
}


Thanks for your time.

Edit:

I also tried

glBegin(GL_QUADS);
glTexCoord2f(0, 0); glVertex3f(0, 0, 0);
glTexCoord2f(1, 0); glVertex3f(0 + 1, 0, 0);
glTexCoord2f(1, 1); glVertex3f(0 + 1, 0 + 1, 0);
glTexCoord2f(0, 1); glVertex3f(0, 0 + 1, 0);
glEnd();

nickkorta
02-17-2014, 03:59 PM
Ok I got it working. I feel really stupid.

It was always returning here because I forgot the brackets.

if (t == nullptr)
printf("Error: \"%s\"\n",SDL_GetError()); return;

Sorry for my stupidity.