Jump to content
  • Advertisement
Sign in to follow this  

SDL/OpenGL issue

This topic is 2681 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

Hi, I have been working through some OpenGL tutorials and have been able to easily draw polygons in 2D but then tried to use 3D:

#include <stdio.h>
#include <SDL/SDL.h>
#include <GL/gl.h>

int main(int argc, char *argv[]) {
SDL_Window *window;
SDL_GLContext context;
window = SDL_CreateWindow("OpenGL Window", 100, 100, 400, 400, SDL_WINDOW_SHOWN | SDL_WINDOW_OPENGL);
context = SDL_GL_CreateContext(window);
glClearColor (0.0, 0.0, 0.0, 0.0);
glShadeModel (GL_SMOOTH);
glMatrixMode (GL_PROJECTION);
glTranslatef(0.0f, 0.0f, -6.0f);
glVertex3f( 0.0f, 1.0f, -0.0f);
glVertex3f(-1.0f,-1.0f, 0.0f);
glVertex3f( 1.0f,-1.0f, 0.0f);
return 0;

This just results in a blank window and no triangle. If I change the third parameter of glTranslatef to 0.0f then the triangle does display and as expected fills most of the window. The tutorials I have read say to use -6.0f though.

Share this post

Link to post
Share on other sites
While your tutorial probably says you should use -6, it applies in the context defined by the tutorial, and you probably don't understand why it is -6 and why it won't work in your context.

Your tutorial perhaps defined a depth range of the projection matrix from, say, 1 to 100 along the negative Z-axis. This means you have to translate your object from the origin where it is not visible to, say, Z=-6, to position it within the visible depth range. That is why you have to translate it, and the specific value of -6 is probably chosen fairly arbitrarily chosen as long as the object is placed within the visible depth range.

Your setup, however, is an identity projection matrix, which means the visible depth range is from -1 to 1, so if you translate it by -6, you translate the object outside the visible depth range. Hence, it is not visible anymore.

You need to learn about the view and projection transforms to setup the view properly. You do neither at the moment.

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!