Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 2 of 2

Thread: Texture is not drawing On Rectangle

  1. #1
    Junior Member Newbie
    Join Date
    Feb 2014
    Posts
    5

    Texture is not drawing On Rectangle

    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
    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();

  2. #2
    Junior Member Newbie
    Join Date
    Feb 2014
    Posts
    5
    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •