Sign in to follow this  
i_luv_cplusplus

SDL + opengl

Recommended Posts

I want to do 2D in OpenGL. Here are my init, draw and resize functions... somehow it isn't working, I mean screen is whole red, there is no black rectangle. Even if I'll set Z coordinate of the rectangle to 0.1, the screen is red -.-
void init()
{
    glShadeModel(GL_SMOOTH);
    glClearColor(0.0f, 0.0f, 0.0f, 0.5f);
    glEnable(GL_DEPTH_TEST);
};

void draw()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glClearColor(0.0f,0.5f,0.75f,1.0f);
 
    glColor3f(1.0f, 0.0f, 0.0f);
    glBegin(GL_QUADS);
    glVertex3f(-100, -100, 0);
    glVertex3f(100, -100, 0);
    glVertex3f(100, 100, 0);
    glVertex3f(-100, 100, 0);
    glEnd();
    glColor3f(0.0f, 0.0f, 0.0f);
    glBegin(GL_QUADS);
    glVertex3f(-50, -50, 0);
    glVertex3f(50, -50, 0);
    glVertex3f(50, 50, 0);
    glVertex3f(-50, 50, 0);
    glEnd();
    SDL_GL_SwapBuffers( );
};

void resize(int width, int height)
{
	if (height==0) height=1;
	glViewport(0,0,width,height);


      glOrtho(0, width, 0, height, -1, 1);
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
};

Share this post


Link to post
Share on other sites
Have you tried looking at the source code of hxRender? It doesn't do automatic depth arrangement but if you sort your blits by depth manually it should get the job done much faster anyway (depending on the sort method you use).

Share this post


Link to post
Share on other sites
Your resize function seems to be a bit off as you should be switching your matrix mode to GL_PROJECTION, identify it (can I say that??), THEN call glOrtho. Here's what it should look like:

void resize(int width, int height)
{
if (height==0){
height=1;
}

glViewport(0,0,width,height);

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, width, 0, height, -1, 1);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
};

Also when rendering in Ortho mode it is better to use glVertex2f() as you don't need a z component.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this