开发者

Rendering from SDL and OpenGL at the Same Time

Hey all, I'm very new to OpenGL (just started seriously programming with it today) and I'm trying to use it to give my SDL games a 3D boost. I've setup a small test program below:

#include <SDL/SDL.h>
#include <gl/gl.h>

int main(int argc, char *argv[])
{
   SDL_Event event;
   float theta = 0.0f;

   SDL_Init(SDL_INIT_VIDEO);
   SDL_Surface *screen = SDL_SetVideoMode(800, 600, 32, SDL_OPENGL | SDL_HWSURFACE | SDL_RESIZABLE | SDL_FULLS开发者_StackOverflow中文版CREEN);

   glViewport(0, 0, 800, 600);
   glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
   glClearDepth(1.0);
   glDepthFunc(GL_LESS);
   glEnable(GL_DEPTH_TEST);
   glShadeModel(GL_SMOOTH);
   glMatrixMode(GL_PROJECTION);
   glMatrixMode(GL_MODELVIEW);

   int done;

   for(done = 0; !done;)
   {

      SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, 255, 0, 0));

      glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

      glLoadIdentity();
      glTranslatef(0.0f,0.0f,0.0f);
      glRotatef(theta, 0.0f, 0.0f, 1.0f);
      glBegin(GL_TRIANGLES);
      glColor3f(0.83f, 0.83f, 0.0f);
      glVertex2f(0.0f, 1.0f);
      glColor3f(0.83f, 0.83f, 0.0f);
      glVertex2f(0.87f, -0.5f);
      glColor3f(0.83f, 0.83f, 0.0f);
      glVertex2f(-0.87f, -0.5f);
      glEnd();

      theta += 10.0f;
      SDL_Flip(screen);
      SDL_GL_SwapBuffers();
      SDL_PollEvent(&event);
      if(event.key.keysym.sym == SDLK_ESCAPE)
         done = 1;
   }
}

My problem is that the red background I'm trying to rendered is never rendered, only the OpenGL Triangle is rendered.

Thanks in advance to anyone who can help me. It's much appreciated.


There's one simple rule about OpenGL: It doesn't play well with others. What happens in your case is, that the double buffer swap (initiated by SDL_GL_SwapBuffers) will in some way replace everything in the window, not being rendered by OpenGL.

Just draw everything using OpenGL.


You fill the back buffer on one line with SDL_FillRect then you clear it on the next with glClear. Have you tried swapping the order of the operations?

Not that I disagree with the accepted answer; in general trying to mix software rendering methods with OpenGL is a recipe for confusion at best, but you might get lucky in this case.

As for rending textured quads, you should be able to work it out from NeHe lesson 6. People complain about NeHe but it's a reasonable guide for getting started. Just don't use it as an example of good coding or of efficient modern OpenGL usage. Start here and move to more complex stuff later.


If you're using C++, SFML library might be a better option (it has C bindings though, but haven't tried those). It plays nicely with OpenGL and has functions to cooperatively work alongside GL. As far as I understood it, SFML functions themselves use GL to render. Although, I do suggest that you do rendering only with GL calls as noted above.


your SDL_FillRect isn't show as red, because you call glClear with GL_COLOR_BUFFER_BIT set afterwards

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜