I have a problem with SDL and OpenGL.

I wrote a code to convert SDL Surfaces to OpenGL textures.
Now I have a function to Draw a rect(with opengl) on a SDL_Surface.
That's the code to draw a rect:

void DrawRect(SDL_Rect *rect, GLuint texture)
    float x = rect->x;
    float y = rect->y;
    float w = rect->w;
    float h = rect->h;
    // Bind the texture to which subsequent calls refer to
    glBindTexture( GL_TEXTURE_2D, texture );

    glBegin( GL_QUADS );
    //Bottom-left vertex (corner)
    glTexCoord2i( 0, 0 );
    glVertex3f( x, y+h, 0.0f );

    //Bottom-right vertex (corner)
    glTexCoord2i( 1, 0 );
    glVertex3f( x+w, y+h, 0.0f );

    //Top-right vertex (corner)
    glTexCoord2i( 1, 1 );
    glVertex3f( x+w, y, 0.0f );

    //Top-left vertex (corner)
    glTexCoord2i( 0, 1 );
    glVertex3f( x, y, 0.0f );

So... Now, where are the drawed Quads?! I know how to get them to sdl:


The problem is, I want to put these Quads into a surface or anything like that, so I can make multiple layers and put them together in another surface named screen.
Then I want to show only the surface screen in the window.

Is this possible?
How can I draw the quad to a SDL_Surface or a GLuint(texture), so I can combine them later?

I hope someone can help me ;)


Recommended Answers

All 2 Replies

Look into texture enviornments @ opengl.org (hosted by Kronos - they currently have the OpenGL license.)

When you use immediate mode in OpenGL glBegin/glEnd your using OpenGL 2.0. You might want to look into the material covering OpenGL 3.0 to 4.0 (the newer APIs - immediate mode is no longer supported - VBOs are your new best friend!). The newer stuff offer more flexibility for using textures.

>>How can I draw the quad to a SDL_Surface or a GLuint(texture), so I can combine them later?
I don't know exactly how to do that, but I know it is not the best/easiest way to do it (in fact, it is probably the worst way). What you want to do is render-to-texture. This can be done in OpenGL. Don't use SDL surfaces to do that, OpenGL is far more powerful, faster and more flexible. I'm not so up-to-date on OpenGL coding so I can't really give you specific functions to use and all, but I would suggest you look up the topic of "Render to Texture in OpenGL". As it says, you can basically render an entire 3D scene onto a texture (internally stored on the graphics card) and then apply it to another object in the scene that actually gets drawn on the screen. They can do some pretty cool effects with this nowadays (especially with shaders). But the last time I implemented this type of effects (almost 10 years ago), the graphics cards didn't have the render-to-texture feature (let alone shaders) and the code I managed to hack-up to achieve this was pretty ugly and slow (almost as bad as your idea of copying the SDL_surface onto a texture, but in those days there wasn't too many possibilities), so I can't recommend anything more contemporary.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.