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 on SDL- OpenGL

  1. #1
    Junior Member Newbie
    Join Date
    Oct 2013
    Posts
    6

    Question Texture on SDL- OpenGL

    Hello everyone,
    thanks in advance for the answers.
    Use Visual Express 2013 on W7.
    Libraries: SDL, OpenGL.

    So this is the problem, if I load a texture in OpenGL I use SDL_GL_BindTexture which in turn needs a SDL_texture.
    To load SDL_texture I have to use this function SDL_CreateTextureFromSurface.
    SDL_CreateTextureFromSurface needs a render that I do not use because I do have to render OpenGL instead of SDL!

    myTexture.cpp
    Code :
    bool myTexture::loadTextureFromFile(std::string path)
    {
    	bool success = true;
    	SDL_Texture* newTexture = NULL;
     
    	//Load image at specified path
    	SDL_Surface* loadedSurface = IMG_Load(path.c_str());
    	if (loadedSurface == NULL)
    	{
    		printf("Unable to load image %s! SDL_image Error: %s\n", path.c_str(), IMG_GetError());
    	}
    	else
    	{
    		//Create texture from surface pixels
    		//newTexture = SDL_CreateTextureFromSurface(gRenderer, loadedSurface);//<######################problem!
    		if (newTexture == NULL)
    		{
    			printf("Unable to create texture from %s! SDL Error: %s\n", path.c_str(), SDL_GetError());
    		}
     
    		//Get rid of old loaded surface
    		SDL_FreeSurface(loadedSurface);
    	}
     
    	mTexture = newTexture;
    	return success;
    }

    myWindow.cpp
    Code :
    bool myWindow::init()
    {
    	printf("\nInitWindowSDL________________ \n");
    	//Create window
    	mWindow = SDL_CreateWindow("window", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_OPENGL | SDL_WINDOW_HIDDEN | SDL_WINDOW_RESIZABLE);
    	if (mWindow != NULL)
    	{
    		mMouseFocus = true;
    		mKeyboardFocus = true;
    		mWidth = SCREEN_WIDTH;
    		mHeight = SCREEN_HEIGHT;
     
    		//Create renderer for window
    		mContext = SDL_GL_CreateContext(mWindow);
    		if (mContext == NULL)
    		{
    			printf("Errore SDL_GL_CreateContext():\n%s\n", SDL_GetError());
    			SDL_DestroyWindow(mWindow);
    			mWindow = NULL;
    		}
    		else
    		{
    			printf("Context creato.\n");
     
    			//Grab window identifier
    			mWindowID = SDL_GetWindowID(mWindow);
    			printf("Finestra creata: %d.\n", mWindowID);
    			//Flag as opened
    			mShown = false;
    		}
    	}
    	else
    	{
    		printf("Errore SDL_CreateWindow():\n%s\n", SDL_GetError());
    	}
    	printf("_____________________________ \n");
    	return mWindow != NULL && mContext != NULL;
    }
    The question is I have to create a dummy invisible window in the class myTexture to have the render I need or there are other features that I can use in place of SDL_CreateTextureFromSurface?

    PS: Sorry for my english.

  2. #2
    Junior Member Newbie
    Join Date
    Oct 2013
    Posts
    6
    I tried this solution but it seems that glGenTexture does not work, does not change the variable asd...
    Code :
    bool myTexture::prova2(std::string path)
    {
    	SDL_Surface* Surface = IMG_Load(path.c_str());
     
    	GLuint asd = 120;
    	glGenTextures(1, &asd);
    	mID = asd;
    	glBindTexture(GL_TEXTURE_2D, mID);
     
    	int Mode = GL_RGB;
     
    	if (Surface->format->BytesPerPixel == 4) {
    		Mode = GL_RGBA;
    	}
     
    	glTexImage2D(GL_TEXTURE_2D, 0, Mode, Surface->w, Surface->h, 0, Mode, GL_UNSIGNED_BYTE, Surface->pixels);
     
    	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
     
    	printf("Texture(%d), W(%d), H(%d), Mode(%0x).\n", mID, Surface->w, Surface->h, Mode);
     
    	if (Surface == NULL) return false;
    	else return true;
     
    }

Posting Permissions

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