Re: Problem with textures in OpenGL

[email protected] (Kartik Thakore)
Newsgroups perl.sdl.devel
Message-ID <[email protected]>
HI Daniel,

I have fixed the issue with the get_pixels_ptr() function. This fix will
appear in SDL 2.5. Moreover in SDL 2.5 I have removed SDL::OpenGL, instead
you can use POGL (cpan OpenGL). It is more complete and works with SDLx::App
(renamed SDL::App) seamlessly. You can get the new SDL for now from
http://github.com/kthakore/SDL_perl . Here is the example script that use
SDLx::App, OpenGL, and the new $surface->get_pixels_ptr();

http://github.com/kthakore/SDL_perl/blob/master/examples/cookbook/pogl_sdl_texture.pl

run it like so

pogl_sdl_texture.pl someimage.[png][bmp] ...

Regards,
Kartik Thakore


2010/6/6 Daniel Turczański <[email protected]>

> Hi,
> I tried to load BMP file from disk and display a quad textured with it.
> Here's the full script: http://paste-it.net/public/nc1f11b/
>
> All I get is quad with weird colors:
> http://img192.imageshack.us/img192/9177/sdlopengl.png
>
> Any ideas?
>
> Regards,
> Daniel
>
>
>
>
> #!/usr/bin/perl
> use warnings;
> use strict;
>
> use SDL;
> use SDL::App;
> use SDL::Surface;
> use SDL::Event;
> use SDL::OpenGL;
> use SDL::Image;
> use SDL::Color;
>
> my $app;  #sdl window
> my $sdl_timer;
> my $event;
>
> package main;
> init_SDL();
>
> while(1) {
>        pump_sdl();
> }
>
>
>
> ###################################################
>
> sub init_SDL{
> $app = new SDL::App -w => 1024, -h => 768, -d => 16, -gl =>1;
>
> print "Initializing OpenGL settings\n";
> printf "%-24s%s\n", "GL_RED_SIZE ", $app->attribute( SDL_GL_RED_SIZE());
> printf "%-24s%s\n", "GL_GREEN_SIZE ", $app->attribute(
> SDL_GL_GREEN_SIZE());
> printf "%-24s%s\n", "GL_BLUE_SIZE ", $app->attribute( SDL_GL_BLUE_SIZE() );
> printf "%-24s%s\n", "GL_DEPTH_SIZE ", $app->attribute( SDL_GL_DEPTH_SIZE()
> );
> printf "%-24s%s\n", "GL_DOUBLEBUFFER ", $app->attribute(
> SDL_GL_DOUBLEBUFFER() );
>
>
> #glEnable(GL_CULL_FACE);
> #glFrontFace(GL_CCW);
> #glCullFace(GL_BACK);
>
> InitView();
> DrawScene();
> $app->sync();
>
> $event = new SDL::Event;
>
> #$sdl_timer = Glib::Timeout->add (100,\&pump_sdl);
>
> }
> #######################################################
> sub pump_sdl{
> #for (0 .. 5) {
>                        my $sdl_event = SDL::Event->new();
>                        SDL::Events::pump_events();    #get events from SDL
> queue
>                        my $poll = SDL::Events::poll_event($event);    #get
> the first one
>                        DrawScene();
>  #    }
>
> return 1;
> }
> #######################################################
> sub DrawScene {
>
>    glClear( GL_DEPTH_BUFFER_BIT()
>        | GL_COLOR_BUFFER_BIT());
>
>    glLoadIdentity();
>
>    glTranslate(0,0,-6.0);
>    glColor(1,1,1);
>
>        glBegin(GL_QUADS);
>                glTexCoord(0.0, 0.0); glVertex(-1.0, -1.0, 0);
>                glTexCoord(1.0, 0.0); glVertex(1.0, -1.0, 0);
>                glTexCoord(1.0, 1.0); glVertex(1.0, 1.0, 0);
>                glTexCoord(0.0, 1.0); glVertex(-1.0, 1.0, 0);
>        glEnd();
>
>    $app->sync();
> }
>
> sub InitView {
>        my $width = 1024;
>        my $height = 800;
>
>        glClearColor(0.0, 0.0, 0.0, 0.0);
>        glShadeModel(GL_SMOOTH);
>        glClearDepth(1.0);
>        glDisable(GL_DEPTH_TEST);
>        glBlendFunc(GL_SRC_ALPHA, GL_ONE);
>        glEnable(GL_BLEND);
>
>        glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
>
>        glEnable(GL_TEXTURE_2D);
>        LoadTexture();
>
>        glViewport(0,0,1024,768);
>        glMatrixMode(GL_PROJECTION);
>        glLoadIdentity();
>        gluPerspective(45.0, $width/$height, 1.0, 100.0);
>
>        glMatrixMode(GL_MODELVIEW);
>        glLoadIdentity();
> }
>
>
> sub LoadTexture {
>  my $surface;
>  my $nOfColors;
>  my $texture_format;
>  my $texture = 0;
>
>
> if ( ($surface = SDL::Image::load("spark.bmp"))){
>        # Check that the image's width is a power of 2
>        if ( ($surface->w & ($surface->w - 1)) != 0 ) {
>                print "warning: image.bmp's width is not a power of 2\n";
>        }
>
>        # Also check if the height is a power of 2
>        if ( ($surface->h & ($surface->h - 1)) != 0 ) {
>                print "warning: image.bmp's height is not a power of 2\n";
>        }
>
>        #get the number of channels in the SDL surface
>        $nOfColors = $surface->format->BytesPerPixel;
>        if ($nOfColors == 4)     # contains an alpha channel
>        {
>                if ($surface->format->Rmask == 0x000000ff){
>                        $texture_format = GL_RGBA;}
>                else{
>                        $texture_format = GL_BGRA;}
>        } elsif ($nOfColors == 3)     # no alpha channel
>        {
>                if ($surface->format->Rmask == 0x000000ff){
>                        $texture_format = GL_RGB;}
>                else{
>                        $texture_format = GL_BGR;}
>        } else {
>                print "warning: the image is not truecolor..  this will
> probably break\n";
>                #this error should not go unhandled
>        }
>
>        # Have OpenGL generate a texture object handle for us
>        glGenTextures(1);
>
>        # Bind the texture object
>        glBindTexture( GL_TEXTURE_2D, 1);
>
>        #Set the texture's stretching properties
>        glTexParameter( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
>        glTexParameter( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
>
>        # Edit the texture object's image data using the information
> SDL_Surface gives us
>        glTexImage2D( GL_TEXTURE_2D, 0, $nOfColors,
>                                        $surface->w, $surface->h,
>                                        0, $texture_format,
> GL_UNSIGNED_BYTE, $surface->get_pixels_ptr() );
>
> #       glTexImage2D(GL_TEXTURE_2D, 0, 3, $surface->w, $surface->h,  0,
> $texture_format, GL_UNSIGNED_BYTE, $surface->get_pixels_ptr());
> }
> else {
>        print("SDL could not load image.bmp: %s\n".SDL_GetError());
>        SDL::Quit();
>        return 1;
> }
>
> # Free the SDL_Surface only if it was successfully created
> if ( $surface ) {
>        #SDL::FreeSurface( $surface );
> }
>
> }
>
>
>
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.