Sign in to follow this  
Hooverguy

SDL/OpenGL issue

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:

[code]
#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);
glEnable(GL_DEPTH_TEST);
glShadeModel (GL_SMOOTH);
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode (GL_PROJECTION);
glLoadIdentity();
glTranslatef(0.0f, 0.0f, -6.0f);
glBegin(GL_TRIANGLES);
glVertex3f( 0.0f, 1.0f, -0.0f);
glVertex3f(-1.0f,-1.0f, 0.0f);
glVertex3f( 1.0f,-1.0f, 0.0f);
glEnd();
glFlush();
SDL_GL_SwapWindow(window);
SDL_Delay(10000);
SDL_Quit();
return 0;
}

[/code]

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 [i]why[/i] 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 [i]why[/i] 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

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