views:

240

answers:

2

Alright, so I'm using cairo to turn an SVG into image data for openGL textures.

That part works.

But now the texture I'm using won't map to the quad I'm making. It's just showing up as a blank square.

Is there something up with the order I'm calling things in or is there some secret function I forgot to use?

const int SCREEN_WIDTH = 1280;
const int SCREEN_HEIGHT = 720;
const int SCREEN_BPP = 32;

int frame = 0;
SDL_Event event;
bool quit;
GLuint texture[1];

int main(int argc, char *argv[]) {
    g_type_init();
    rsvg_init();

    SDL_Surface *screen = SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_OPENGL );

    SDL_WM_SetCaption ("Cairo", NULL);
    SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
    SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);

    SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
    SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
    SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
    SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 8);

/*2D stuff - it worked here
    glMatrixMode( GL_PROJECTION );
    glLoadIdentity();
    glOrtho( 0, SCREEN_WIDTH, SCREEN_HEIGHT, 0, -1, 1 );
    glMatrixMode( GL_MODELVIEW );
    glLoadIdentity();
    glEnable (GL_BLEND);
    glEnable (GL_TEXTURE_2D);
    glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    glEnable (GL_TEXTURE_2D);
    glEnable(GL_LIGHTING);
    glEnable(GL_COLOR_MATERIAL);
*/  

//An attempt at setting up 3D stuff
glEnable(GL_TEXTURE_2D);
glMatrixMode( GL_MODELVIEW );
glMatrixMode( GL_PROJECTION );
glViewport(0,0,SCREEN_WIDTH, SCREEN_HEIGHT);

glShadeModel(GL_SMOOTH);
glClearColor(0.0,0.0,0.0,0.0);
glClearDepth(1.0);
glBlendFunc(GL_SRC_ALPHA, GL_ONE);
glEnable(GL_BLEND);
//glLoadIdentity();


    float FlowerWidth = .5;
    float FlowerHeight = .5;

    float FlowerTextureWidth = 80;
    float FlowerTextureHeight = 80;

    float FlowerScaleWidth = 1;
    float FlowerScaleHeight = 1;
    cairo_surface_t* Flower;
    cairo_t* context;

    Flower = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, FlowerTextureWidth, FlowerTextureHeight);
    context = cairo_create(Flower);

    const gchar* Filename = "resources/area/haneda/lavender.svg";
    RsvgHandle* SvgData = rsvg_handle_new_from_file(Filename, NULL);

    rsvg_handle_render_cairo_sub(SvgData, context,"#1000");
    unsigned char *buffer = cairo_image_surface_get_data(Flower);
    cairo_surface_write_to_png(Flower,"flower.png");


//Make a texture
     glGenTextures(1, &texture[1]);
    glBindTexture(GL_TEXTURE_2D, texture[1]);
    glPixelStoref(GL_UNPACK_ALIGNMENT, 1);
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
    glTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
    glTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);

    glGetError();
    //or am I supposed to use GL_TEXTURE_2D?
    glTexImage2D(GL_TEXTURE_2D,
    0,
    GL_RGBA,
    FlowerHeight,
    FlowerWidth,
    0,
    GL_BGRA,
    GL_UNSIGNED_BYTE,
    buffer);
//done  


    while (quit==false) {
     while(SDL_PollEvent(&event)) {
      if(event.type == SDL_QUIT) { 
      quit = true; 
      }
      }




      /*
FlowerScaleWidth+=.001;
FlowerScaleHeight+=.001;
cairo_scale(context,FlowerScaleWidth,FlowerScaleHeight);
*/










glBindTexture (GL_TEXTURE_2D, texture[1]);

glBegin (GL_QUADS);
    glTexCoord2f (0.0, 0.0);
    glVertex3f (0.0, 0.0, 0.0);

    glTexCoord2f (FlowerWidth, 0.0);
    glVertex3f (FlowerWidth, 0.0, 0.0);

    glTexCoord2f (FlowerWidth, FlowerHeight);
    glVertex3f (FlowerWidth, FlowerHeight, 0.0);

    glTexCoord2f (0.0, FlowerHeight);
    glVertex3f (0.0, FlowerHeight, 0.0);
glEnd ();
glDeleteTextures(1, &texture[1]);


     cairo_save (context);
     cairo_set_source_rgba (context, 0, 0, 0, 0);
     cairo_set_operator (context, CAIRO_OPERATOR_SOURCE);
     cairo_paint (context);
     cairo_restore (context);

     SDL_GL_SwapBuffers();
     //glClear( GL_COLOR_BUFFER_BIT ); 
//SDL_Delay(100);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 
     glGetError(); 


SDL_Delay(400);

     }

}
+6  A: 

For some reason, you've made an int array of length 1, but you pass the (non-existing) element 2 to glGenTextures. That reads beyond the array bounds and is undefined behavior. You also seem to delete the texture name inside your rendering loop. The same illegal indexing is used there too, as well in your call to glBindTexture.

Mads Elvheim
the delete part is likely the reason for white. But the texture[1] is *really* bad. torch, C arrays are 0-indexed. so you need to write texture[0] to access the (only) element of GLuint texture[1].
Bahbar
A: 

ohhh. So I'm guessing that if an array only has one element, even if it's texture[5], that would have to be accesssed through texture[0]?

torch
No. Simply remember that the first index is 0, and the last one is length-1. So if you specify an array of length 5, the valid indices are 0,1,2,3,4. Perhaps you should brush up on your C++ skills before taking on yet another challenge.P.S:For later comments to your same question, please use your "edit" button on the question post. The "post answer" field is for answers only. Not new questions or comments. Comments are unlocked when you get 50 points.Welcome to Stack Overflow.
Mads Elvheim