SDL Clipping Issue

Member
Posts: 215
Joined: 2008.06
Post: #1
I have enabled SDL in XCode, and it compiles and runs, and shows the triangle that I told it to draw. However, when I attempt a translation or rotation that takes any part of the triangle further than -1.0 in the Z direction, it begins to clip it.

I have asked around elsewhere and others have told me this is probably an issue with the far clipping plane. However, every attempt I have made to rectify this issue has done nothing. glOrtho and Perspective with arguments to set the far and near clipping plane don't change anything. Any advice?

Mac users swear by their computers, PC users swear at their computers. ~Unknown

iSayz
Quote this message in a reply
Moderator
Posts: 3,579
Joined: 2003.06
Post: #2
Any geometry which exceeds the near an far planes in depth will be clipped to the planes. This is hard to answer without seeing your code I guess. There are all kinds of things that could be going wrong if you're new to OpenGL.

What values are you setting the far and near planes to? Perhaps you are calling glOrtho/Perspective in the wrong place? Are you sure you are calling them with the matrix mode set to GL_PROJECTION first?
Quote this message in a reply
Moderator
Posts: 3,579
Joined: 2003.06
Post: #3
Here is some code to look at which illustrates how and where glOrtho is used, just to make sure we're on the same page. The glOrtho, as specified below, is what OpenGL uses for the default projection.

Code:
- (void)myInitialize
{
    glEnableClientState(GL_VERTEX_ARRAY);
    glEnableClientState(GL_COLOR_ARRAY);
}

- (void)myDrawFrame
{
    double near = -1.0;
    double far = 1.0;
    
    glClearColor(0.0f, 0.0f, 0.25f, 1.0f);
    glClear(GL_COLOR_BUFFER_BIT);
    
    glMatrixMode(GL_PROJECTION);
    glPushMatrix();
    glLoadIdentity();
    glOrtho(-1.0, 1.0, -1.0, 1.0, near, far);
    
    GLfloat vertices[] = {    -0.5f, -0.5f,     // lower left
               0.5f, -0.5f,         // lower right
            -0.5f,  0.5f,         // upper left
               0.5f,  0.5f };     // upper right
    GLfloat colors[] = {    1.0f, 0.0f, 0.0f, 1.0f, // red
               0.0f, 1.0f, 0.0f, 1.0f,     // green
               0.0f, 1.0f, 0.0f, 1.0f,     // green
               0.0f, 1.0f, 0.0f, 1.0f };     // green
    
    glVertexPointer(2, GL_FLOAT, 0, vertices);
    glColorPointer(4, GL_FLOAT, 0, colors);
    
    glMatrixMode(GL_MODELVIEW);
    glPushMatrix();
    glLoadIdentity();
    glRotatef(25.0f, 0.0f, 0.0f, 1.0f);
    glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
    glPopMatrix();
    
    glMatrixMode(GL_PROJECTION);
    glPopMatrix();
}
Quote this message in a reply
Member
Posts: 215
Joined: 2008.06
Post: #4
AnotherJake Wrote:Any geometry which exceeds the near an far planes in depth will be clipped to the planes. This is hard to answer without seeing your code I guess. There are all kinds of things that could be going wrong if you're new to OpenGL.

What values are you setting the far and near planes to? Perhaps you are calling glOrtho/Perspective in the wrong place? Are you sure you are calling them with the matrix mode set to GL_PROJECTION first?

Code:
/*
*  main.cpp
*  OGLw:SDL2
*
*  Created by Kyle Roucis on 6/21/08.
*  Copyright 2008 Home. All rights reserved.
*
*/

#include "main.h"
#include "SDL.h"
#include "SDL_opengl.h"

float rtri = 0.0f;
float dist = 0.0f;

int draw()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glLoadIdentity();
    glColor3f(0.2f, 0.0f, 0.8f);
    
    rtri += 0.8;
    dist += 0.01;
    
    glPushMatrix();
    glTranslatef(0.0f, 0.0f, dist);
    glRotatef(rtri, 1.0f, 0.6f, 0.2f);
    
    glBegin(GL_TRIANGLES);
    {
        glVertex3f(-1.0f, -1.0f, 1.0f);
        glVertex3f(0.0f, 1.0f, 0.0f);
        glVertex3f(1.0f, -1.0f, 1.0f);
        
        glColor3f(0.0f, 0.4f, 0.4f);
        glVertex3f(-1.0f, -1.0f, -1.0f);
        glVertex3f(0.0f, 1.0f, 0.0f);
        glVertex3f(1.0f, -1.0f, -1.0f);
        
        glColor3f(0.5f, 0.2f, 0.8f);
        glVertex3f(-1.0f, -1.0f, -1.0f);
        glVertex3f(0.0f, 1.0f, 0.0f);
        glVertex3f(-1.0f, -1.0f, 1.0f);
        
        glColor3f(0.8f, 0.8f, 0.8f);
        glVertex3f(1.0f, -1.0f, -1.0f);
        glVertex3f(0.0f, 1.0f, 0.0f);
        glVertex3f(1.0f, -1.0f, 1.0f);
    }
    glEnd();
    glPopMatrix();
    
    SDL_GL_SwapBuffers();
    
    return 1;
}

int main(int argc, char** argv)
{
    bool done = 0;
    SDL_Event event;
    
    int error;
    error = SDL_Init(SDL_INIT_EVERYTHING);
    
    SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
    
    SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
    
    
    
    //Don't need to set up color buffers.  Max OS X already takes care of this at 8 bytes each.
    
    SDL_Surface* drawContext;
    
    Uint32 flags;
    
    flags = SDL_OPENGL;
    drawContext = SDL_SetVideoMode(1024, 768, 0, flags);
    
    //glMatrixMode(GL_PROJECTION);
    
    gluPerspective(45.0, (double)1024 / (double)768, 1.0, 200.0);
    
    glMatrixMode(GL_MODELVIEW);
    glEnable(GL_DEPTH_TEST);
    glEnable(GL_AUTO_NORMAL);
    glEnable(GL_NORMALIZE);
    
    while (!done && (SDL_WaitEvent(&event)))
    {
        switch (event.type)
        {
            case SDL_KEYDOWN:
                done = 1;
                break;
            default:
                draw();
                break;
        }
    }
    
    return error;
}

If I use the glMatrixMode(GL_PERSPECTIVE) where I have it commented out, it's even worse than normal. I have set the SDL_GL_DEPTH_SIZE to ridiculous numbers like 1000000, and its still didn't do anything different. I'm stuck.

Out of curiosity, would you even recommend SDL? I seem to have much more success using just GLUT and normal OGL. What advantages does SDL provide as far OGL goes?

--Side bar, just real fast:
How do I set a custom avatar? Do I have to have a certain number of posts, or what?

Mac users swear by their computers, PC users swear at their computers. ~Unknown

iSayz
Quote this message in a reply
Moderator
Posts: 3,579
Joined: 2003.06
Post: #5
Try it something like this instead:
Code:
int draw()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    
    glEnable(GL_DEPTH_TEST);
    
    glMatrixMode(GL_PROJECTION);
    glPushMatrix();
    glLoadIdentity();
    gluPerspective(45.0, (double)1024 / (double)768, 1.0, 200.0);
    
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    glColor3f(0.2f, 0.0f, 0.8f);
    
    rtri += 0.8;
    //dist += 0.01;
    
    glPushMatrix();
    glTranslatef(0.0f, 0.0f, -5.0f); // <-- just doing this so you can see it for now, change back as you wish
    glRotatef(rtri, 1.0f, 0.0f, 0.0f); // <-- just use 1.0 in one of the axes at a time for normal rotation
    
    glBegin(GL_TRIANGLES);
    {
        glVertex3f(-1.0f, -1.0f, 1.0f);
        glVertex3f(0.0f, 1.0f, 0.0f);
        glVertex3f(1.0f, -1.0f, 1.0f);
        
        glColor3f(0.0f, 0.4f, 0.4f);
        glVertex3f(-1.0f, -1.0f, -1.0f);
        glVertex3f(0.0f, 1.0f, 0.0f);
        glVertex3f(1.0f, -1.0f, -1.0f);
        
        glColor3f(0.5f, 0.2f, 0.8f);
        glVertex3f(-1.0f, -1.0f, -1.0f);
        glVertex3f(0.0f, 1.0f, 0.0f);
        glVertex3f(-1.0f, -1.0f, 1.0f);
        
        glColor3f(0.8f, 0.8f, 0.8f);
        glVertex3f(1.0f, -1.0f, -1.0f);
        glVertex3f(0.0f, 1.0f, 0.0f);
        glVertex3f(1.0f, -1.0f, 1.0f);
    }
    glEnd();
    glPopMatrix();
    
    glMatrixMode(GL_PROJECTION);
    glPopMatrix();
}

And take these lines out of main:
Code:
//glMatrixMode(GL_PROJECTION);
    
    gluPerspective(45.0, (double)1024 / (double)768, 1.0, 200.0);
    
    glMatrixMode(GL_MODELVIEW);
    glEnable(GL_DEPTH_TEST);
    glEnable(GL_AUTO_NORMAL);
    glEnable(GL_NORMALIZE);

SDL is great! It is suitable for use in commercial games, whereas GLUT is not. GLUT is easier to learn with IMHO, but there is no terribly significant disadvantage to SDL that I can think of.

As far as the avatars: Carlos doesn't let you do that unless you upgrade by purchasing an "uber" membership.
Quote this message in a reply
Member
Posts: 215
Joined: 2008.06
Post: #6
AnotherJake Wrote:Try it something like this instead:
Code:
int draw()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    
    glEnable(GL_DEPTH_TEST);
    
    glMatrixMode(GL_PROJECTION);
    glPushMatrix();
    glLoadIdentity();
    gluPerspective(45.0, (double)1024 / (double)768, 1.0, 200.0);
    
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    glColor3f(0.2f, 0.0f, 0.8f);
    
    rtri += 0.8;
    //dist += 0.01;
    
    glPushMatrix();
    glTranslatef(0.0f, 0.0f, -5.0f); // <-- just doing this so you can see it for now, change back as you wish
    glRotatef(rtri, 1.0f, 0.0f, 0.0f); // <-- just use 1.0 in one of the axes at a time for normal rotation
    
    glBegin(GL_TRIANGLES);
    {
        glVertex3f(-1.0f, -1.0f, 1.0f);
        glVertex3f(0.0f, 1.0f, 0.0f);
        glVertex3f(1.0f, -1.0f, 1.0f);
        
        glColor3f(0.0f, 0.4f, 0.4f);
        glVertex3f(-1.0f, -1.0f, -1.0f);
        glVertex3f(0.0f, 1.0f, 0.0f);
        glVertex3f(1.0f, -1.0f, -1.0f);
        
        glColor3f(0.5f, 0.2f, 0.8f);
        glVertex3f(-1.0f, -1.0f, -1.0f);
        glVertex3f(0.0f, 1.0f, 0.0f);
        glVertex3f(-1.0f, -1.0f, 1.0f);
        
        glColor3f(0.8f, 0.8f, 0.8f);
        glVertex3f(1.0f, -1.0f, -1.0f);
        glVertex3f(0.0f, 1.0f, 0.0f);
        glVertex3f(1.0f, -1.0f, 1.0f);
    }
    glEnd();
    glPopMatrix();
    
    glMatrixMode(GL_PROJECTION);
    glPopMatrix();
}

And take these lines out of main:
Code:
//glMatrixMode(GL_PROJECTION);
    
    gluPerspective(45.0, (double)1024 / (double)768, 1.0, 200.0);
    
    glMatrixMode(GL_MODELVIEW);
    glEnable(GL_DEPTH_TEST);
    glEnable(GL_AUTO_NORMAL);
    glEnable(GL_NORMALIZE);

SDL is great! It is suitable for use in commercial games, whereas GLUT is not. GLUT is easier to learn with IMHO, but there is no terribly significant disadvantage to SDL that I can think of.

As far as the avatars: Carlos doesn't let you do that unless you upgrade by purchasing an "uber" membership.

That gives me a blank window. Here's my code now:

Code:
/*
*  main.cpp
*  OGLw:SDL2
*
*  Created by Kyle Roucis on 6/21/08.
*  Copyright 2008 Home. All rights reserved.
*
*/

#include "main.h"
#include "SDL.h"
#include "SDL_opengl.h"

float rtri = 0.0f;
float dist = 0.0f;

int draw()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    
    glEnable(GL_DEPTH_TEST);
    
    glMatrixMode(GL_PROJECTION);
    glPushMatrix();
    glLoadIdentity();
    gluPerspective(45.0, (double)1024 / (double)768, 1.0, 200.0);
    
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    glColor3f(0.2f, 0.0f, 0.8f);
    
    rtri += 0.8;
    //dist += 0.01;
    
    glPushMatrix();
    glTranslatef(0.0f, 0.0f, -5.0f); // <-- just doing this so you can see it for now, change back as you wish
    glRotatef(rtri, 1.0f, 0.0f, 0.0f); // <-- just use 1.0 in one of the axes at a time for normal rotation
    
    glBegin(GL_TRIANGLES);
    {
        glVertex3f(-1.0f, -1.0f, 1.0f);
        glVertex3f(0.0f, 1.0f, 0.0f);
        glVertex3f(1.0f, -1.0f, 1.0f);
        
        glColor3f(0.0f, 0.4f, 0.4f);
        glVertex3f(-1.0f, -1.0f, -1.0f);
        glVertex3f(0.0f, 1.0f, 0.0f);
        glVertex3f(1.0f, -1.0f, -1.0f);
        
        glColor3f(0.5f, 0.2f, 0.8f);
        glVertex3f(-1.0f, -1.0f, -1.0f);
        glVertex3f(0.0f, 1.0f, 0.0f);
        glVertex3f(-1.0f, -1.0f, 1.0f);
        
        glColor3f(0.8f, 0.8f, 0.8f);
        glVertex3f(1.0f, -1.0f, -1.0f);
        glVertex3f(0.0f, 1.0f, 0.0f);
        glVertex3f(1.0f, -1.0f, 1.0f);
    }
    glEnd();
    glPopMatrix();
    
    glMatrixMode(GL_PROJECTION);
    glPopMatrix();
    
    return 0;
}

int main(int argc, char** argv)
{
    bool done = 0;
    SDL_Event event;
    
    int error;
    error = SDL_Init(SDL_INIT_EVERYTHING);
    
    SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
    
    SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
    
    
    
    //Don't need to set up color buffers.  Max OS X already takes care of this at 8 bytes each.
    
    SDL_Surface* drawContext;
    
    Uint32 flags;
    
    flags = SDL_OPENGL;
    drawContext = SDL_SetVideoMode(1024, 768, 0, flags);
    
    while (!done && (SDL_WaitEvent(&event)))
    {
        switch (event.type)
        {
            case SDL_KEYDOWN:
                done = 1;
                break;
            default:
                draw();
                break;
        }
    }
    
    return error;
}

What makes SDL suitable for commercial games versus GLUT? I know that GLUT is more of a utility for setting up OGL and setting up scenes, but what does SDL provide that makes it worthwhile?

Mac users swear by their computers, PC users swear at their computers. ~Unknown

iSayz
Quote this message in a reply
Moderator
Posts: 3,579
Joined: 2003.06
Post: #7
Oops, sorry, forgot the SDL_GL_SwapBuffers(); at the bottom, before the return. Hope that helps... Blush

SDL does things like joystick input and properly handling all kinds of OS details like full screen mode switching. GLUT was never designed to be fully featured for commercial apps. Things like input and full screen are just not up to snuff. It always was intended for learning OpenGL and getting things up and running quickly and easily, not covering all windowing details. SDL, OTOH, is a commercial grade library which is used by many AAA games (such as Unreal Tournament).
Quote this message in a reply
Member
Posts: 215
Joined: 2008.06
Post: #8
AnotherJake Wrote:Oops, sorry, forgot the SDL_GL_SwapBuffers(); at the bottom, before the return. Hope that helps... Blush

SDL does things like joystick input and properly handling all kinds of OS details like full screen mode switching. GLUT was never designed to be fully featured for commercial apps. Things like input and full screen are just not up to snuff. It always was intended for learning OpenGL and getting things up and running quickly and easily, not covering all windowing details. SDL, OTOH, is a commercial grade library which is used by many AAA games (such as Unreal Tournament).

Well...That just makes my frustration at learning this that much more...frustrating! But that does help to explain the uses behind SDL. I have also heard that it has very efficient 2D drawing as well, which could be useful for an upstart game dev such as myself. Thanks for helping to clear this up AJ.

The swap fixed it! Any idea what was wrong with my original code? It's great that it's working now, but I would like to know where my errors were in the first place so that I can avoid similar mistakes. Thanks again for the help!

Mac users swear by their computers, PC users swear at their computers. ~Unknown

iSayz
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #9
SDL's 2D stuff is a waste of space on the Mac.
Quote this message in a reply
Member
Posts: 215
Joined: 2008.06
Post: #10
OneSadCookie Wrote:SDL's 2D stuff is a waste of space on the Mac.

Um, OK? Why do you say that?

Mac users swear by their computers, PC users swear at their computers. ~Unknown

iSayz
Quote this message in a reply
Moderator
Posts: 3,579
Joined: 2003.06
Post: #11
[EDIT] man you guys were busy while I was writing pseudo-code ...

Quote:Any idea what was wrong with my original code?

You have to set your GL_PROJECTION matrix every frame. It's a steep learning curve to pick up OpenGL at first, but don't worry, you'll get it. The basic pattern you'll do every frame will be:

Code:
- (void)myDrawFrame
{
    // clear buffer(s)
    glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
    glClear(GL_COLOR_BUFFER_BIT);
    
    // set projection
    glMatrixMode(GL_PROJECTION);
    glPushMatrix();
    glLoadIdentity();
    glOrtho(-1.0, 1.0, -1.0, 1.0, near, far);
    
    // switch to modelview matrix to draw
    glMatrixMode(GL_MODELVIEW);
    glPushMatrix();
    glLoadIdentity();

    // draw your stuff here

    // pop the modelview matrix
    glPopMatrix();
    
    // switch back to projection and pop that too
    glMatrixMode(GL_PROJECTION);
    glPopMatrix();
}

Don't use SDL's 2D drawing stuff, it's crap, use OpenGL for 2D.

BTW, you don't *have* to use SDL. You could create an OpenGL context yourself in Cocoa and do all the windowing yourself, but it's a little more involved.
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #12
Talyn Wrote:Um, OK? Why do you say that?

Because it's abysmally slow. On Windows, I believe it's accelerated via DirectDraw. On the Mac, it's all software blits.

I think in future versions of SDL they intend to implement their 2D stuff on top of OpenGL, but that's not in the released SDL.

On the Mac, OpenGL and CoreAnimation are the two ways you can get accelerated graphics. I personally find CoreAnimation's API confusing and often counterproductive for games.
Quote this message in a reply
Member
Posts: 215
Joined: 2008.06
Post: #13
Any links to some good SDL tuts? Trying to get a simple game rolling and it's taking a lot more than I had originally anticipated.

Mac users swear by their computers, PC users swear at their computers. ~Unknown

iSayz
Quote this message in a reply
Member
Posts: 53
Joined: 2007.08
Post: #14
http://www.libsdl.org/cgi/docwiki.cgi ...API reference, also has some example stuff.

http://www.meandmark.com/sdlopenglpart1.html ...Basic SDL/OpenGL Mac setup stuff

http://www.libsdl.org/cgi/docwiki.cgi/Examples ...The official SDL examples which may be useful.

There are lots of SDL code floating around out there. Is there something specific you are having trouble with?

//edit:
When you create a new SDL OpenGL project in XCode using the template it should give you that Atlantis program by default. Could look at that as well.
Quote this message in a reply
Member
Posts: 215
Joined: 2008.06
Post: #15
Terrydil Wrote:http://www.libsdl.org/cgi/docwiki.cgi ...API reference, also has some example stuff.

http://www.meandmark.com/sdlopenglpart1.html ...Basic SDL/OpenGL Mac setup stuff

http://www.libsdl.org/cgi/docwiki.cgi/Examples ...The official SDL examples which may be useful.

There are lots of SDL code floating around out there. Is there something specific you are having trouble with?

//edit:
When you create a new SDL OpenGL project in XCode using the template it should give you that Atlantis program by default. Could look at that as well.

Wow, thanks! I'm trying to get mouse control and input working properly. It is not as simple as others had said, but I will admit it is easier than using GLUT. That's pretty much it. Thanks again.

Mac users swear by their computers, PC users swear at their computers. ~Unknown

iSayz
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  SDL &amp; SGE Clipping Problems merrill541 1 2,377 Sep 12, 2009 10:27 PM
Last Post: merrill541
  glCopyTexImage2D and clipping james_the_bard 3 5,342 Nov 14, 2005 03:16 PM
Last Post: Skorche
  2D clipping problem McSebi 4 4,335 Feb 22, 2005 11:37 PM
Last Post: McSebi
  wierd...light clipping...or something [edited] ghettotek 2 2,735 Feb 11, 2003 07:10 AM
Last Post: ghettotek