Jump to content
  • Advertisement
Sign in to follow this  
i_luv_cplusplus

SDL + opengl

This topic is 4249 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
Advertisement
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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!