Jump to content

  • Log In with Google      Sign In   
  • Create Account

Interested in a FREE copy of HTML5 game maker Construct 2?

We'll be giving away three Personal Edition licences in next Tuesday's GDNet Direct email newsletter!

Sign up from the right-hand sidebar on our homepage and read Tuesday's newsletter for details!


We're also offering banner ads on our site from just $5! 1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


GDsnakes

Member Since 04 Aug 2011
Offline Last Active Apr 10 2013 09:42 AM

Topics I've Started

I need help with SDL/OPENGL Color Keying.....

18 March 2013 - 04:06 PM

I have an image that I load in SDL. My goal is to load the image in SDL, Color Key, and then send to Opengl to become texture.

 

 

I've read some tutorials and the following code is what I have come up with.

 

 

int loadImage()
{
    SDL_Surface *surface;
    
    if (surface = IMG_Load("AvatarR.jpg"))
    {
        nofcolors = surface->format->BytesPerPixel;
    
        if(nofcolors == 4)
        {
            if (surface->format->Rmask == 0x000000ff)
                texture_format = GL_RGBA;
            else
                texture_format = GL_BGRA;
        }
        else if (nofcolors == 3) //no alpha channel
        {
            // no alpha channel = no transparency = use color key
            Uint32 colorkey = SDL_MapRGB(surface, 255, 255, 255);
            SDL_SetColorKey(surface, SDL_SRCCOLORKEY, colorkey);
            
            // now convert image to 32 bits
            SDL_Surface *temp = SDL_DisplayFormatAlpha(surface);
            
            // release previous surface
            SDL_FreeSurface(surface);
            
            // use the new converted one
            surface = temp;
            
            // reassign nofcolors because image format changed
            nofcolors = surface->format->BytesPerPixel;
            
            if(surface->format->Rmask == 0x000000ff)
                texture_format = GL_RGB;
            else
                texture_format = GL_BGR;
        }
 
        glGenTextures(1, &texture);
        glBindTexture(GL_TEXTURE_2D, texture);
 
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
        glPixelStorei(GL_UNPACK_ALIGNMENT,4);
 
        glTexImage2D(GL_TEXTURE_2D, 0, nofcolors, surface->w, surface->h, 0, texture_format, GL_UNSIGNED_BYTE, surface->pixels);
    }
    else
    {
        return -1;
    }
 
    if (surface)
        SDL_FreeSurface(surface);
 
    return 1;

 

It doesn't work. The background color is white and I want it to be gone.

 

 

 

If anyone has any succesful methods in C++ they would like to share I'd greatly appreciate it.


Texture wont show on quad.

13 February 2013 - 01:49 PM

I made this thread in For Beginners, but noone there was able to help.

 

 

I was having trouble loading the image, but have since fixed that. Now I cant seem to get the image to actually show on the screen. Instead its just the blue rectangle I have drawn with no texture on it.

 

#include "SDL.h"
#include "SDL_image.h"
#include "SDL_opengl.h"
#include <math.h>
#include <stdio.h>

int loadImage();
void drawImage();

extern SDL_Event event;

GLuint texture = NULL;
GLenum texture_format=NULL;
GLint nofcolors;

bool initGL()
{
    //Initialize Projection Matrix
    glMatrixMode( GL_PROJECTION );
    glLoadIdentity();

    //Initialize Modelview Matrix
    glOrtho(0, 1200, 700, 0, -1, 1);
	glMatrixMode( GL_MODELVIEW );
    glLoadIdentity();

    //Initialize clear color
   glClearColor(1.0,.4,0.0,0.0);
   glEnable( GL_TEXTURE_2D);
    //Check for error
    GLenum error = glGetError();
    if( error != GL_NO_ERROR )
    {
        printf( "Error initializing OpenGL! %s\n", gluErrorString( error ) );
        return false;
    }

    return true;
}

bool init()
{
    //Initialize SDL
    if( SDL_Init( SDL_INIT_EVERYTHING ) < 0 )
    {
        return false;
    }

	SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER,1);

    //Create Window
    if( SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_OPENGL ) == NULL )
    {
        return false;
    }

    //Enable unicode
    SDL_EnableUNICODE( SDL_TRUE );

	


    //Initialize OpenGL
    if( initGL() == false )
    {
        return false;
    }

    //Set caption
    SDL_WM_SetCaption( "Project Birth Game Window. Guns&Player.", NULL );

    return true;
}




int main( int argc, char *argv[] )
{
    //Quit flag
    bool quit = false;

    //Initialize
    if( init() == false )
    {
        return 1;
    }

    //The frame rate regulator
    Timer fps;
	fps.start();
	loadImage(); 


	int x = 0, y = 0;
	//Wait for user exit
	while( quit == false )
	{
        //Start the frame timer
        
		
        //While there are events to handle
		while( SDL_PollEvent( &event ) )
		{
			if( event.type == SDL_QUIT )
			{
                quit = true;
            }
			
			else if( event.type == SDL_KEYDOWN )
            {
                //Handle keypress with current mouse position
                
				SDL_GetMouseState( &x, &y );
                
			}
		}

	
		drawImage(); 

       
        update(); 
		
		SDL_GL_SwapBuffers();
		
        //Cap the frame rate
        if( fps.get_ticks() < 1000 / FRAMES_PER_SECOND )
        {
            SDL_Delay( ( 1000 / FRAMES_PER_SECOND ) - fps.get_ticks() );
        }
	}

	//Clean up
	clean_up();

	return 0;
}




int loadImage()
{


	SDL_Surface* surface;
	
	
	if((surface = IMG_Load("Avatar Basic Character Model.jpg")))
	{
	
		

	//get number of channels in the SDL surface
	nofcolors = surface -> format -> BytesPerPixel;
	
   //contains an alpha channel
	if(nofcolors == 4)
	{
		if ( surface -> format -> Rmask == 0x000000ff)
			texture_format = GL_RGBA;
		else
			texture_format = GL_BGRA;
	}
	else if ( nofcolors == 3) //no alpha channel
	{
		if(surface ->format->Rmask==0x000000ff)
			texture_format=GL_RGB;
		else
			texture_format=GL_BGR;
	}

	//Have Opengl generate a texture object handle for us
	glGenTextures(1, &texture);

	//Bind the texture object
	glBindTexture( GL_TEXTURE_2D, texture);

	//Set the texture's stretching properties
	glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
	glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
	glPixelStorei(GL_UNPACK_ALIGNMENT,1);

	
	glTexImage2D( GL_TEXTURE_2D, 0, nofcolors, surface -> w, surface ->h, 0, texture_format, GL_UNSIGNED_BYTE, surface -> pixels );
	
	}
	else //could not load image
		return -1;


if (surface)
{
	SDL_FreeSurface(surface);
}

return 1; 

}


void drawImage()
{

	//Clear the screen before drawing 
	glClear( GL_COLOR_BUFFER_BIT);

	//Bind the texture to which subsequent calls refer to
	glBindTexture( GL_TEXTURE_2D, texture);

	glPushMatrix();
	glTranslatef(300,300,0.0f);
	glBegin(GL_QUADS);
	//Set color
	glColor4f(0.0,0.0,1.0,0.0);

	
	glTexCoord2i(1,1);
	glVertex2f(150.0,0);//bottom right
	
	glTexCoord2i(0,1);
	glVertex2f(150.0,-50);//top right
	
	glTexCoord2i(0,0);
	glVertex2f(0.0,-50);//top left
	
	glTexCoord2i(1,0);
	glVertex2f(0.0,0);//bottom left
    

	glEnd();
	
	glPopMatrix();

	
}

 

At first I was having issues with loading the image, but thats no longer an issue. Any idea on why the image wont show on the quad? Perhaps an issue with binding?

 


Texture isn't showing on the screen. Just the clear color of the rectangle its boun...

11 February 2013 - 04:44 PM

I was having trouble loading the image, but have since fixed that. Now I cant seem to get the image to actually show on the screen. Instead its just the blue rectangle I have drawn with no texture on it.

 

 

#include "SDL.h"
#include "SDL_image.h"
#include "SDL_opengl.h"
#include <math.h>
#include <stdio.h>

int loadImage();
void drawImage();

extern SDL_Event event;

GLuint texture = NULL;
GLenum texture_format=NULL;
GLint nofcolors;

bool initGL()
{
    //Initialize Projection Matrix
    glMatrixMode( GL_PROJECTION );
    glLoadIdentity();

    //Initialize Modelview Matrix
    glOrtho(0, 1200, 700, 0, -1, 1);
	glMatrixMode( GL_MODELVIEW );
    glLoadIdentity();

    //Initialize clear color
   glClearColor(1.0,.4,0.0,0.0);
   glEnable( GL_TEXTURE_2D);
    //Check for error
    GLenum error = glGetError();
    if( error != GL_NO_ERROR )
    {
        printf( "Error initializing OpenGL! %s\n", gluErrorString( error ) );
        return false;
    }

    return true;
}

bool init()
{
    //Initialize SDL
    if( SDL_Init( SDL_INIT_EVERYTHING ) < 0 )
    {
        return false;
    }

	SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER,1);

    //Create Window
    if( SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_OPENGL ) == NULL )
    {
        return false;
    }

    //Enable unicode
    SDL_EnableUNICODE( SDL_TRUE );

	


    //Initialize OpenGL
    if( initGL() == false )
    {
        return false;
    }

    //Set caption
    SDL_WM_SetCaption( "Project Birth Game Window. Guns&Player.", NULL );

    return true;
}




int main( int argc, char *argv[] )
{
    //Quit flag
    bool quit = false;

    //Initialize
    if( init() == false )
    {
        return 1;
    }

    //The frame rate regulator
    Timer fps;
	fps.start();
	loadImage(); 


	int x = 0, y = 0;
	//Wait for user exit
	while( quit == false )
	{
        //Start the frame timer
        
		
        //While there are events to handle
		while( SDL_PollEvent( &event ) )
		{
			if( event.type == SDL_QUIT )
			{
                quit = true;
            }
			
			else if( event.type == SDL_KEYDOWN )
            {
                //Handle keypress with current mouse position
                
				SDL_GetMouseState( &x, &y );
                
			}
		}

	
		drawImage(); 

       
        update(); 
		
		SDL_GL_SwapBuffers();
		
        //Cap the frame rate
        if( fps.get_ticks() < 1000 / FRAMES_PER_SECOND )
        {
            SDL_Delay( ( 1000 / FRAMES_PER_SECOND ) - fps.get_ticks() );
        }
	}

	//Clean up
	clean_up();

	return 0;
}




int loadImage()
{


	SDL_Surface* surface;
	
	
	if((surface = IMG_Load("Avatar Basic Character Model.jpg")))
	{
	
		

	//get number of channels in the SDL surface
	nofcolors = surface -> format -> BytesPerPixel;
	
   //contains an alpha channel
	if(nofcolors == 4)
	{
		if ( surface -> format -> Rmask == 0x000000ff)
			texture_format = GL_RGBA;
		else
			texture_format = GL_BGRA;
	}
	else if ( nofcolors == 3) //no alpha channel
	{
		if(surface ->format->Rmask==0x000000ff)
			texture_format=GL_RGB;
		else
			texture_format=GL_BGR;
	}

	//Have Opengl generate a texture object handle for us
	glGenTextures(1, &texture);

	//Bind the texture object
	glBindTexture( GL_TEXTURE_2D, texture);

	//Set the texture's stretching properties
	glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
	glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
	glPixelStorei(GL_UNPACK_ALIGNMENT,1);

	
	glTexImage2D( GL_TEXTURE_2D, 0, nofcolors, surface -> w, surface ->h, 0, texture_format, GL_UNSIGNED_BYTE, surface -> pixels );
	
	}
	else //could not load image
		return -1;


if (surface)
{
	SDL_FreeSurface(surface);
}

return 1; 

}


void drawImage()
{

	//Clear the screen before drawing 
	glClear( GL_COLOR_BUFFER_BIT);

	//Bind the texture to which subsequent calls refer to
	glBindTexture( GL_TEXTURE_2D, texture);

	glPushMatrix();
	glTranslatef(300,300,0.0f);
	glBegin(GL_QUADS);
	//Set color
	glColor4f(0.0,0.0,1.0,0.0);

	
	glTexCoord2i(1,1);
	glVertex2f(150.0,0);//bottom right
	
	glTexCoord2i(0,1);
	glVertex2f(150.0,-50);//top right
	
	glTexCoord2i(0,0);
	glVertex2f(0.0,-50);//top left
	
	glTexCoord2i(1,0);
	glVertex2f(0.0,0);//bottom left
    

	glEnd();
	
	glPopMatrix();

	
}



	
	
		

 


Reading on understanding Pixels/channels and what not? Help loading images.

07 February 2013 - 06:15 PM

I have code trying load/display an image and keep getting this Unhandled Exception Error. I think its because the image is not a multiple of 4 or something like that (no idea) here is my code:

 

 

#include "SDL.h"
#include "SDL_image.h"
#include "SDL_opengl.h"
#include <math.h>
#include <stdio.h>
 
int loadImage();
void drawImage();
 
extern SDL_Event event;
 
GLuint texture = NULL;
GLenum texture_format=NULL;
GLint nofcolors;
 
int loadImage()
{
 
    SDL_Surface* surface;
        
        if((surface = IMG_Load("AvatarBasicCharacterModel.jpg"))){
 
        //get number of channels in the SDL surface
        nofcolors = surface -> format -> BytesPerPixel;
        
   //contains an alpha channel
        if(nofcolors == 4)
        {
                if ( surface -> format -> Rmask == 0x000000ff)
                        texture_format = GL_RGBA;
                else
                        texture_format = GL_BGRA;
        }
        else if ( nofcolors == 3) //no alpha channel
        {
                if(surface ->format->Rmask==0x000000ff)
                        texture_format=GL_RGB;
                else
                        texture_format=GL_BGR;
        }
 
        //Have Opengl generate a texture object handle for us
        glGenTextures(1, &texture);
 
        //Bind the texture object
        glBindTexture( GL_TEXTURE_2D, texture);
 
        //Set the texture's stretching properties
        glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
        glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
 
 
        glTexImage2D( GL_TEXTURE_2D, 0, nofcolors, surface -> w, surface ->h, 0, texture_format, GL_UNSIGNED_BYTE, surface -> pixels );
 
        }
        else //could not load image
                SDL_Quit();
return 1;
 
if (surface){
        SDL_FreeSurface(surface);
}
 
}
 
 
void drawImage()
{
 
        //Clear the screen before drawing 
        glClear( GL_COLOR_BUFFER_BIT);
 
        //Bind the texture to which subsequent calls refer to
        glBindTexture( GL_TEXTURE_2D, texture);
 
        glPushMatrix();
        glTranslatef(60,700,0.0f);
        glBegin(GL_QUADS);
        //Set color
        glColor4f(0.0,0.0,1.0,0.0);
 
        
        glTexCoord2i(1,1);
        glVertex2f(150.0,0);//bottom right
        glTexCoord2i(0,1);
        glVertex2f(150.0,-50);//top right
        glTexCoord2i(0,0);
        glVertex2f(0.0,-50);//top left
        glTexCoord2i(1,0);
        glVertex2f(0.0,0);//bottom left
    
 
        glEnd();
        
        glPopMatrix();
 
        
}

I want to A) Understand why the image needs to be a certain width/hieght and B) How to make it work.

 

 

The alpha channels and stuff. Not following.


Problem with creating a class for drawing....

15 January 2013 - 03:43 PM

class QuadDrawing {
private:
    float QuadoffX;
    float QuadoffY;
    

public:
    QuadDrawing(float, float);
    void Draw();
};

QuadDrawing::QuadDrawing( float Xoff, float Yoff)
{
    Xoff = QuadoffX;
    Yoff = QuadoffY;
}

void QuadDrawing::Draw()
{
    glPushMatrix();
    glTranslatef(QuadOffX,QuadoffY,0.0f);
    glBegin(GL_QUADS);
    //Set color
    glColor4f(1.0,0.0,0.0,0.0);

    

    glVertex2f(90.0,50);
    glVertex2f(90.0,-80);
    glVertex2f(0.0,-80);
    glVertex2f(0.0,50);
    

    glEnd();
    
    glPopMatrix();

}

int main( int argc, char *argv[] )
{
    //Quit flag
    bool quit = false;

    //Initialize
    if( init() == false )
    {
        return 1;
    }

    //The frame rate regulator
    Timer fps;
    Drawing Square1(300.0, 300.0);
    int x = 0, y = 0;
    //Wait for user exit
    while( quit == false )
    {
        //Start the frame timer
        fps.start();
        
        //While there are events to handle
        while( SDL_PollEvent( &event ) )
        {
            if( event.type == SDL_QUIT )
            {
                quit = true;
            }
            
            else if( event.type == SDL_KEYDOWN )
            {
                //Handle keypress with current mouse position
                
                SDL_GetMouseState( &x, &y );

                handleKeys (event.key.keysym.unicode. x. y);
                
                }
        }

    MenuState();
    Square1.Draw();
    
    update();
        
       
        
        
        
        
        
        //Cap the frame rate
        if( fps.get_ticks() < 1000 / FRAMES_PER_SECOND )
        {
            SDL_Delay( ( 1000 / FRAMES_PER_SECOND ) - fps.get_ticks() );
        }
    }

    //Clean up
    clean_up();

    return 0;
}

 

The problem is I want to draw a square to test the class I made which should just take two floating point numbers and draw a square at those points, but the square is never drawn...instead the window comes up but stays completely blank. The window was even clear (no clear color) so I thought that was the problem and added glClear( GL_COLOR_BUFFER_BIT ); to the Draw function in the class making it...

 

void QuadDrawing::Draw()
{

   glClear( GL_COLOR_BUFFER_BIT);


    glPushMatrix();
    glTranslatef(QuadOffX,QuadoffY,0.0f);
    glBegin(GL_QUADS);
    //Set color
    glColor4f(1.0,0.0,0.0,0.0);

    

    glVertex2f(90.0,50);
    glVertex2f(90.0,-80);
    glVertex2f(0.0,-80);
    glVertex2f(0.0,50);
    

    glEnd();
    
    glPopMatrix();

}

 

Still didn't work and the window come up clear/blank (not even the clear color).

 

 

But the thing is if I just make it a function and not a class and pass the floating X/Y to draw the square there it works fine. So its a problem with the class.


PARTNERS