I'm new to OpenGL, SDL and OBJ-files and am fighting with an issue for many days now.
setting up the SDL-Window (i've dropped the basic SDL-Init-stuff)
sdl_window = SDL_CreateWindow( "Viewer", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 1024, 768, SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE ); if( sdl_window == NULL ) printf("SDL_CreateWindow failed: %s\n", SDL_GetError()); SDL_GLContext glContext = SDL_GL_CreateContext( sdl_window ); if( glContext == NULL ) printf("SDL_GL_CreateContext failed: %s\n", SDL_GetError()); SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 );
Starting the OpenGL functionality (where "obj" is my class for parsing OBJ and loading Texture)
int my_object; objloader obj; glClearColor( 0.5f, 0.5f, 0.5f, 1.0f ); glViewport( 0, 0, 1024, 768 ); glMatrixMode( GL_PROJECTION ); glLoadIdentity(); gluPerspective( 45, 1024.0/768.0, 1.0, 500.0 ); //glOrtho( 0, 512, 384, 0, -1, 1 ); glMatrixMode( GL_MODELVIEW ); glEnable( GL_DEPTH_TEST ); my_object = obj.load( "D:\myfile.obj" ); glEnable( GL_LIGHTING ); glEnable( GL_LIGHT0 ); float col[] = {1.0,1.0,1.0,1.0}; glLightfv( GL_LIGHT0, GL_DIFFUSE, col );
a short look into the OBJ-file... (no vn - normals in it)
mtllib ./test.mtl usemtl Haufwerk v -0.388155 5.580893 2.490415 v -0.336669 5.566026 2.507601 v -0.408834 5.515674 2.533414 v -0.403397 5.444500 2.540850 v -0.482161 5.577362 2.447329 . . . v -0.533898 5.328258 1.909646 v -0.439773 5.344517 1.849366 vt 0.710938 0.273924 vt 0.711743 0.270957 vt 0.712080 0.277021 vt 0.713371 0.278851 vt 0.711261 0.303822 . . . vt 0.845899 0.687021 vt 0.841930 0.687169 f 5616/687 5617/688 4642/686 f 5617/691 5618/692 4642/690 f 5616/687 4642/686 5618/689 f 5617/688 5616/687 5618/689 f 12657/633 12659/634 13191/637 . . .
Texture comes as BMP file with resolution 1024x1024
OBJ texture image:
running the display loop
while( running ) { start = SDL_GetTicks(); while( SDL_PollEvent(&event) ) { switch(event.type) { case SDL_QUIT: running = false; break; } } glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); glLoadIdentity(); float pos[] = {-1.0,1.0,-2.0,1.0}; glLightfv( GL_LIGHT0, GL_POSITION, pos ); //glScalef( 1.f, 1.f, 1.f ); glTranslatef(0.0, -30.0,-20.0); //glTranslatef( 10.0, 10.0, -50.0 ); //glTranslatef( 0.0, 1.0, -19.5 ); glCallList( myobject ); //and display it SDL_GL_SwapWindow( sdl_window ); if( 1000/30 > (SDL_GetTicks() - start) ) SDL_Delay( 1000/30 - (SDL_GetTicks() - start) ); }
...comes to this results:
Results: OpenGL-object in SDL-window:
below picture resulted from changing one display-code-line to:
glTranslatef( 0.0, 1.0, -19.5 );
My aim is to "fit" in the "OpenGL thing" (what's it called...? Model? Framebuffer? Object?) into the bounds of the SDL-window. Or maybe "resize" the SDL-window to the OpenGL object. Why? Because in the next step I'm "exporting" the whole SDL-window to a bitmap that is going to be used as digitize background in a GIS-software for georeferencing purposes.
Do I have to change the view-settings or is there a possibility to "grab" the size of the OpenGL object for adapting the SDL main window?
For example if I change one line in the displaying code to:
glTranslatef( 0.0, 1.0, -19.5 );
What are exactly the "coordinates" of the vertices "v" in the OBJ file? Which "measurement unit" do they own? Is it a local coosys?
What is the textured OpenGL object "pixel-size" (because the pixel-size of the texture template is 1024x1024)??
Many of the codelines I took from online tutorials, but I'm not sure how they affect my screen in detail. I'm too confused of all the possibilities to change views etc.
So any help is appreciated. This gives me bad sleep...