Rendering from SDL and OpenGL at the same time

Hi everyone, I am very new to OpenGL (now I started seriously programming with it) and I am trying to use it to give my SDL games a 3D boost. I installed 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_FULLSCREEN);

   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 that I am trying to do is never displayed, only the OpenGL triangle is created.

Thanks in advance to everyone who can help me. It is very much appreciated.

+3
source share
4 answers

OpenGL: . , , ( SDL_GL_SwapBuffers) - , OpenGL.

, OpenGL.

+4

SDL_FillRect, glClear. ?

, ; , OpenGL - , .

, NeHe 6. NeHe, . OpenGL. .

0

++, SFML ( C-, ). OpenGL GL. , SFML GL . , , GL, .

0

SDL_FillRect , glClear GL_COLOR_BUFFER_BIT

0

Source: https://habr.com/ru/post/1781061/


All Articles