I can't work OpenGL...

Started by
5 comments, last by biscuitz 18 years, 8 months ago
I'm getting tired of looking at the tutorials I've found. They're making my brain hurt. I keep trying to figure out how to make it work in SDL and I'm lost. I think I'm not initializing the OpenGL right or something. What am I doing wrong?

#include <sdl\sdl.h>
#include <gl\gl.h>
#include <gl\glu.h>

//Setup variables
int width=800,height=600;

int main(int argc, char **argv)
{
  SDL_Init(SDL_INIT_VIDEO);
  SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE,32);
  SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER,1);
  SDL_Surface *screen=SDL_SetVideoMode(width,height,0,SDL_OPENGL|SDL_HWSURFACE);
  
    //Setup OpenGL
    glEnable(GL_CULL_FACE);
    glViewport(0,0,width,height);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(45.0,(GLfloat)width/(GLfloat)height,1.0,1024.0);
    glClearDepth(1.0);
    glEnable(GL_DEPTH_TEST);
    glHint(GL_PERSPECTIVE_CORRECTION_HINT,GL_NICEST);
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
    glBegin(GL_TRIANGLES);
      glVertex3f(0.0,1.0,2.0);
      glVertex3f(-1.0,-1.0,2.0);
      glVertex3f(1.0,-1.0,2.0);
    glEnd();
    glFlush();

  SDL_Event event;
  
  bool loop=1;
  while(loop)
  {
    if(SDL_PollEvent(&event))
    switch(event.type)
    {
      case SDL_QUIT:
        loop=0;
        break;
      case SDL_KEYDOWN:
        switch(event.key.keysym.sym)
        {
          case SDLK_ESCAPE:
            loop=0;
        }
    }
    
    SDL_Delay(1);
  }

   SDL_Quit();

   return 0;
}
Advertisement
What is the problem?
When the program starts it just shows a window but it's see through. There's no black screen. Just what's behind the window.

P.S. Is this supposed to be in the SDL section?
glTranslate (0,0,-20);

Before drawing.

And put an SDL_Flip after glFlush.
Hi.
Now the program closes immediately after starting.
I see some errors...

But you could use this...
#include <SDL/SDL.h>#include <gl/gl.h>int main(int Arg_N, char ** Arg_V){    SDL_Event event;    float theta = 0.0f;    SDL_Init(SDL_INIT_VIDEO);    SDL_SetVideoMode(800, 600, 0, SDL_OPENGL | SDL_HWSURFACE | SDL_NOFRAME);    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);    for(int done = 0; !done;)    {        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(1.0f, 0.0f, 0.0f);         glVertex2f(0.0f, 1.0f);         glColor3f(0.0f, 1.0f, 0.0f);         glVertex2f(0.87f, -0.5f);         glColor3f(0.0f, 0.0f, 1.0f);         glVertex2f(-0.87f, -0.5f);        glEnd();        theta += .5f;        SDL_GL_SwapBuffers();        SDL_PollEvent(&event);        if(event.key.keysym.sym == SDLK_ESCAPE)           done = 1;    }    SDL_Quit();    return(0);}
Okay thanks. Now I have something to build on. :)

Just wondering, where did you find that code? I saw that in a tutorial somewhere but I saw so many tutorials I forgot. lol

[EDIT]
Whoops that's why it's so familiar. It's a template for Dev-C++ (duh).

[Edited by - biscuitz on July 29, 2005 11:30:49 PM]

This topic is closed to new replies.

Advertisement