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.


Texture isn't showing on the screen. Just the clear color of the rectangle its bound to. Opengl/SDL


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
1 reply to this topic

#1 GDsnakes   Members   -  Reputation: 145

Like
0Likes
Like

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

	
}



	
	
		

 



Sponsor:

#2 Yourself   Crossbones+   -  Reputation: 1144

Like
0Likes
Like

Posted 12 February 2013 - 03:23 AM

For these kind of issues you should really use a debugger. 

It has been a while since I used openGL but I preferred gDEBugger (http://www.gremedy.com/ ) to find out what was going wrong.

It will show you all OpenGL calls and what the current state of the rendering context is (for example is culling active ? what is the viewport size ? what textures are bind ? etc etc).

 






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS