Public Group

# 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.

## 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); 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; } 

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 on other sites
You need to go back to glmayrixview from gl projection before translating

##### 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.

1. 1
2. 2
Rutin
19
3. 3
khawk
18
4. 4
5. 5
A4L
11

• 12
• 16
• 26
• 10
• 44
• ### Forum Statistics

• Total Topics
633767
• Total Posts
3013739
×