texture isn't working. ( first time ogl )

Started by
12 comments, last by rakoon2 19 years, 7 months ago
Hello guys! I am not doing fanzy 3d stuff.. I am trying to draw a simple quad with a texture using SDL + OpenGl! The problem is that my quad is white.. but I should have the texture on it! uh? :| I don't get error messages when I load the SDL_Surface! Thank you very much! :) Here is the source code: main.cpp


#include "game_header.hpp"

#ifdef _WIN32
#undef main
#endif

LogFile LOG("logfile.log");


int main(int argc, char **argv)
{
	SDL_Surface *screen;
	bool running(true);


        init_SDL_OGL( screen, 640, 480 );
	GL_Texture* texture = new GL_Texture( "galaxien.bmp" );
	
	Quad quad( texture );
	
	
	
	SDL_Event event;
	
	while( running == true )
    {
		

		while(SDL_PollEvent(&event))
        {
			switch(event.type)
            {
    			case SDL_KEYDOWN:
                    				running = false;
                    				break;
    			case SDL_QUIT:
                    				running = false;
                    				break;
			}
		}
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);		
        quad.draw( 300, 300 );

		SDL_GL_SwapBuffers();
	}
        delete texture;
        return 0;
}


game_header.hpp

#include "eng_header.hpp"


eng_header.hpp

#include <SDL.h>
#include <SDL_opengl.h>


#include "eng_logfile.hpp"
#include "eng_init.hpp"
#include "eng_texture.hpp"
#include "eng_quad.hpp"


initSDL_OGL.hpp

#ifndef __ENG_INIT_HPP__
#define __ENG_INIT_HPP__

bool init_SDL_OGL( SDL_Surface* screen, const int w, const int h );

#endif


initSDL_OGL.cpp

#include "eng_header.hpp"

bool init_SDL_OGL( SDL_Surface* screen, const int w, const int h )
{
    // SDL:
    if (SDL_Init(SDL_INIT_VIDEO) < 0)
    {
		LOG << "Could not init SDL" << ENDL;
		exit( 1 );
	}
	atexit( SDL_Quit );

	SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 5);
	SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 5);
	SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 5);
	SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
	SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);

	screen = SDL_SetVideoMode( w, h, 0, SDL_OPENGL);
	if ( !screen )
    {
		LOG << "Couldn't set video mode!" << ENDL;
		exit(1);
	}
    
    
    // OGL:
    glViewport(0, 0, screen->w, screen->h);

	glMatrixMode( GL_PROJECTION );
	glLoadIdentity();
	glOrtho(0, screen->w, screen->h, 0, -1.0, 1.0);

	glMatrixMode( GL_MODELVIEW );
	glLoadIdentity();
 
    return true;   
}    


eng_texture.hpp


#ifndef __ENG_TEXTURE_HPP__
#define __ENG_TEXTURE_HPP__

class GL_Texture
{
    public:
              GL_Texture( void );
              GL_Texture( const char* filename );
              
              ~GL_Texture( void );
              bool init( const char* filename );
              bool initTexture( void );
              
              int getW( void ) const{ return w; }
              int getH( void ) const{ return h; }
              unsigned getTexture( void ) const{ return texture; }
    private:
              SDL_Surface*  sprite;
              unsigned      texture;
              
              int w, h;
};
#endif


eng_texture.cpp

#include "eng_header.hpp"


GL_Texture::GL_Texture( void )
 :w(0), h(0), sprite( NULL )
{
    
}   

 
GL_Texture::GL_Texture( const char* filename )
 :w(0), h(0), sprite( NULL )
{
    init( filename );
    initTexture();
}    




// inits the SDl-surface
bool GL_Texture::init( const char* filename )
{
    if( sprite != NULL )
    {	
		  SDL_FreeSurface( sprite );
		  sprite = NULL;
	}
	
    // Load the BMP file into a surface
	sprite = SDL_LoadBMP( filename );

    if ( sprite == NULL )
    {
	    LOG << "Couldn't load " << filename << " -- "  << ENDL; 
        return false;
    }
    if( SDL_SetColorKey(sprite, SDL_SRCCOLORKEY | SDL_RLEACCEL, SDL_MapRGB(sprite->format, 255, 0, 255)) < 0)
    {
	    LOG << "Couldn't set the ColorKey for " << filename << " -- "  << ENDL;
		return false;
	}
    

	w = sprite->w;
	h = sprite->h;
	return true;       
}



bool GL_Texture::initTexture( void )
{
    SDL_Surface*  conv;
    
    // convert to 32-bit RGBA format
  	conv = SDL_CreateRGBSurface( SDL_SWSURFACE, sprite->w, sprite->h, 32,
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
	   0xff000000, 0x00ff0000, 0x0000ff00, 0x000000ff);
#else
       0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000);
#endif
    
    SDL_BlitSurface( sprite, 0, conv, 0);
    
    glGenTextures( 1, &texture );
	glBindTexture( GL_TEXTURE_2D, texture );
	
	
	glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NICEST );
	glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NICEST );
	
    glPixelStorei( GL_UNPACK_ROW_LENGTH, conv->pitch / conv->format->BytesPerPixel );
	glTexImage2D( GL_TEXTURE_2D, 0, 3, conv->w, conv->h, 0, GL_RGBA, GL_UNSIGNED_BYTE, conv->pixels);
	glPixelStorei( GL_UNPACK_ROW_LENGTH, 0 );

	SDL_FreeSurface(conv);
}   



GL_Texture::~GL_Texture( void )
{
    if( sprite )
    {
		 SDL_FreeSurface( sprite );
		 sprite = NULL;
    }	
}    


quad.hpp


#ifndef __ENG_QUAD_HPP__
#define __ENG_QUAD_HPP__

class Quad
{
    public:
             Quad( GL_Texture* const texture ); 
             void draw( const int x, const int y );
    private:
             GL_Texture* texture;
};

#endif


quad.cpp

#include "eng_header.hpp"

Quad::Quad( GL_Texture* const texture2 )
 :texture( texture2 )
{
}

void Quad::draw( const int x, const int y )
{
    
    glEnable( GL_TEXTURE_2D );
	glBindTexture( GL_TEXTURE_2D, texture->getTexture() );
    
    // Don't use texture-w / h later
    const int t_w = texture->getW();
    const int t_h = texture->getH();
    
    
    glBegin( GL_QUADS );
		glTexCoord2f( 0, 0 );	glVertex2i( x, y );              // x -- y
		glTexCoord2f( 1, 0 );	glVertex2i( x + t_w, y );        // x+w -- y         
  		glTexCoord2f( 1, 1 );	glVertex2i( x + t_w, y + t_h );  // x+w -- y+h
		glTexCoord2f( 0, 1 );	glVertex2i( x, y + t_h );        // x -- y+h  
	glEnd();
}        


[Edited by - rakoon2 on September 3, 2004 8:25:12 PM]
Advertisement
is your texture size a power of two? (ie. 64*64,128*64,256*256 etc)
yes, I tried 64*64,128*64, 128*128 and 256*256.. white quad :|

Thank you! :)
i dont see a glEnable(GL_TEXTURE_2D); annywhere in there.
I don't remember using SDL's built-in bmp loader to load textures. Its probably possible, but you should try a NEHE tutorial and use the GLAUX library first and setup a texture that way. SDL's bitmap loader might not have the data the way OpenGL needs it? Don't quote me on this, just an idea to help troubleshoot.


Quote:Original post by DerAnged
i dont see a glEnable(GL_TEXTURE_2D); annywhere in there.


its in the quad.cpp file in the Draw(). You should really put a glDisable(GL_TEXTURE_2D); at the end if you are done texturing to avoid issues...
I heard that I shouldn't use glaux(glut?!).. to load bmps. (so they said he should use sdl)

So.. what library should I use? *confused* :| :)
Quote:
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NICEST );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NICEST );

GL_NICEST is not valid texture filters. I suppose you mean GL_LINEAR.
Ah! That's it! Thank you! :)



One more question:

How can I let all quads rotate? ( they should rotate around their own center. )




I tried the rotate function.. but then my quads were gone after a sec!



I am using the same function as in the nehe tutorial.
Maybe I must use a different function?! ( ortho mode )

Oh:
glBegin( GL_QUADS );glTexCoord2f( 0, 0 );	glVertex2i( x, y );              // x -- y		glTexCoord2f( 1, 0 );	glVertex2i( x, y + t_h );        // x -- y + h          		glTexCoord2f( 1, 1 );	glVertex2i( x + t_w, y + t_h );  // x+w -- y+h		glTexCoord2f( 0, 1 );	glVertex2i( x + t_w, y );        // x+w-- y  	glEnd();

is this right to make a quad? my quad is not w * h why? :|

[Edited by - rakoon2 on September 4, 2004 3:02:12 AM]
its probably because you dont specify things in pixels, its some arbitrary unit, 1 is actually decently sized, but make sure you translate it back say, 10, thats what i always do for my little demos, in case you dont know transformations yet

glMatrixMode(GL_MODELVIEW);
glTranslatef(0.0f, 0.0f, -10.0f);
//do your rendering


ok wow, i must be tired, im not confident i have the constant for glMatrixMode() right, im pretty sure, but not 100%, i think that means its bed time, but anywho, hope that helps
-Dan
When General Patton died after World War 2 he went to the gates of Heaven to talk to St. Peter. The first thing he asked is if there were any Marines in heaven. St. Peter told him no, Marines are too rowdy for heaven. He then asked why Patton wanted to know. Patton told him he was sick of the Marines overshadowing the Army because they did more with less and were all hard-core sons of bitches. St. Peter reassured him there were no Marines so Patton went into Heaven. As he was checking out his new home he rounded a corner and saw someone in Marine Dress Blues. He ran back to St. Peter and yelled "You lied to me! There are Marines in heaven!" St. Peter said "Who him? That's just God. He wishes he were a Marine."

This topic is closed to new replies.

Advertisement