/ Published in: C
:'-(
Expand |
Embed | Plain Text
#include "SDL/SDL.h" #include "SDL/SDL_opengl.h" /* #include "SDL/SDL_image.h" */ #include <math.h> #include <stdio.h> #define SCREEN_WIDTH 640 #define SCREEN_HEIGHT 480 #define SCREEN_BPP 16 #define V_COUNT 5000 #define VT_COUNT 5000 #define F_COUNT 5000 int v_count = 0; int vt_count = 0; int f_count = 0; typedef struct { struct { float x, y, z; } vertexes[V_COUNT]; struct { float u, v; } text_coords[VT_COUNT]; struct { int v1, v2, v3, vt1, vt2 , vt3; } faces[F_COUNT]; } model; model mdl; SDL_Surface *surface; int video_flags; /* Flags to pass to SDL_SetVideoMode */ int done = 0; /* Main loop variable. */ GLfloat x_rot; GLfloat y_rot; GLfloat z_rot; GLuint texture[1]; /* Storage for one texture. */ void quit (int return_code) { SDL_Quit(); /* clean up the window */ exit(return_code); /* and exit appropriately */ } int load_gl_textures () /* load in bitmap as a GL texture */ { int Status = 0; /* Status indicator */ SDL_Surface *TextureImage[1]; /* Load The Bitmap, Check For Errors, If Bitmap's Not Found Quit */ TextureImage[0] = SDL_LoadBMP("soldat.bmp"); if (TextureImage[0]) { Status = 1; glGenTextures ( 1, &texture[0] ); /* Create The Texture */ /* Typical Texture Generation Using Data From The Bitmap */ glBindTexture ( GL_TEXTURE_2D, texture[0] ); /* Generate The Texture */ glTexImage2D ( GL_TEXTURE_2D, 0, 3, TextureImage[0]->w, TextureImage[0]->h, 0, GL_BGR, GL_UNSIGNED_BYTE, TextureImage[0]->pixels ); /* Linear Filtering */ glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR ); glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR ); } if ( TextureImage[0] ) SDL_FreeSurface ( TextureImage[0] ); return Status; } /* reset viewport after a window resize */ int resize_window ( int width, int height ) { GLfloat ratio; if ( height == 0 ) height = 1; ratio = (GLfloat) width / (GLfloat) height; glViewport ( 0, 0, (GLint) width, (GLint) height ); glMatrixMode ( GL_PROJECTION ); /* Change to the projection matrix */ glLoadIdentity (); gluPerspective ( 45.0f, ratio, 0.1f, 100.0f ); /* set viewing volume. */ /* Make sure we're chaning the model view and not the projection */ glMatrixMode ( GL_MODELVIEW ); /* just to make sure */ glLoadIdentity (); return 1; } void hanlde_events ( SDL_Event event ) { switch ( event.type ) { case SDL_VIDEORESIZE: surface = SDL_SetVideoMode ( event.resize.w, event.resize.h, 16, video_flags ); if ( !surface ) { fprintf ( stderr, "no surface after resize: %s\n", SDL_GetError() ); quit (1); } resize_window ( event.resize.w, event.resize.h ); break; case SDL_KEYDOWN: switch ( event.key.keysym.sym ) { case SDLK_q: case SDLK_ESCAPE: done = 1; quit (0); break; case SDLK_F1: SDL_WM_ToggleFullScreen ( surface ); break; default: break; } break; case SDL_QUIT: done = 1; break; default: puts ("unknown event!"); break; } } int init_opengl ( GLvoid ) { if (!load_gl_textures()) return 0; glEnable ( GL_TEXTURE_2D ); glShadeModel ( GL_SMOOTH ); glClearColor ( 0.0f, 0.0f, 0.0f, 0.5f ); glClearDepth ( 1.0f ); glEnable ( GL_DEPTH_TEST ); glDepthFunc ( GL_LEQUAL ); /* The Type Of Depth Test To Do */ /* Really Nice Perspective Calculations */ glHint ( GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST ); return 1; } /* --- */ int read_obj_file () { float resize_coefficient; // char filename[] = "cube.obj"; char filename[] = "soldat.obj"; // char filename[] = "elephav.obj"; char buffer[100]; FILE* obj_file = NULL; obj_file = fopen ( filename, "r" ); if ( obj_file == NULL ) return 1; while ( fgets (buffer, 100, obj_file) ) { /* puts(buffer); */ /* vertex coords */ if ( buffer[0] == 'v' && buffer [1] == ' ' ) { sscanf ( buffer, "v %f %f %f", &mdl.vertexes[v_count].x, &mdl.vertexes[v_count].y, &mdl.vertexes[v_count].z ); #if 1 //resize_coefficient = 0.002; resize_coefficient = 0.6; mdl.vertexes[v_count].x *= resize_coefficient; mdl.vertexes[v_count].y *= resize_coefficient; mdl.vertexes[v_count].z *= resize_coefficient; #endif v_count++; } /* vertex normals */ else if ( buffer[0] == 'v' && buffer[1] == 'n' ) { /* TODO */ } /* texture coords */ else if ( buffer[0] == 'v' && buffer[1] == 't' ) { sscanf ( buffer, "vt %f %f", &mdl.text_coords[vt_count].v, &mdl.text_coords[vt_count].u ); vt_count++; } /* faces */ else if ( buffer[0] == 'f' && buffer [1] == ' ' ) { // sscanf ( buffer, "f %i %i %i", sscanf ( buffer, "f %i/%i %i/%i %i/%i", // sscanf ( buffer, "f %i//%i %i//%i %i//%i", &mdl.faces[f_count].v1, &mdl.faces[f_count].vt1, &mdl.faces[f_count].v2, &mdl.faces[f_count].vt2, &mdl.faces[f_count].v3, &mdl.faces[f_count].vt3 ); f_count++; } } #if 1 #endif fclose ( obj_file ); return 0; } int draw ( GLvoid ) { int ii = 0; /* These are to calculate our fps */ static GLint T0 = 0; static GLint Frames = 0; glClear ( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); glLoadIdentity (); glTranslatef ( 0.0f, 0.0f, -5.0f ); /* Move Into The Screen 5 Units */ glRotatef ( x_rot, 1.0f, 0.0f, 0.0f ); glRotatef ( y_rot, 0.0f, 1.0f, 0.0f ); glRotatef ( z_rot, 0.0f, 0.0f, 1.0f ); glBindTexture ( GL_TEXTURE_2D, texture[0] ); /* Select Texture */ /* NOTE: * The x coordinates of the glTexCoord2f function need to inverted * for SDL because of the way SDL_LoadBmp loads the data. So where * it has glTexCoord2f( 1.0f, 0.0f ); it should now read * glTexCoord2f( 0.0f, 0.0f ); */ glBegin ( GL_TRIANGLES ); { for ( ii = 0; ii < f_count; ii++ ) { /* TODO: remake whole structure */ glTexCoord2f ( mdl.text_coords[ mdl.faces[ii].vt1 ].u, mdl.text_coords[ mdl.faces[ii].vt1 ].v ); glVertex3f ( mdl.vertexes[ mdl.faces[ii].v1 ].x, mdl.vertexes[ mdl.faces[ii].v1 ].y, mdl.vertexes[ mdl.faces[ii].v1 ].z ); glTexCoord2f ( mdl.text_coords[ mdl.faces[ii].vt2 ].u, mdl.text_coords[ mdl.faces[ii].vt2 ].v ); glVertex3f ( mdl.vertexes[ mdl.faces[ii].v2 ].x, mdl.vertexes[ mdl.faces[ii].v2 ].y, mdl.vertexes[ mdl.faces[ii].v2 ].z ); glTexCoord2f ( mdl.text_coords[ mdl.faces[ii].vt3 ].u, mdl.text_coords[ mdl.faces[ii].vt3 ].v ); glVertex3f ( mdl.vertexes[ mdl.faces[ii].v3 ].x, mdl.vertexes[ mdl.faces[ii].v3 ].y, mdl.vertexes[ mdl.faces[ii].v3 ].z ); } } glEnd (); SDL_GL_SwapBuffers (); /* Draw it to the screen */ /* Gather FPS */ Frames++; { GLint t = SDL_GetTicks (); if ( t - T0 >= 5000 ) { GLfloat seconds = ( t - T0 ) / 1000.0; GLfloat fps = Frames / seconds; Frames, seconds, fps ); T0 = t; Frames = 0; } } x_rot += 0.03f; y_rot += 0.03f; z_rot += 0.03f; return 1; } int init_sdl () { const SDL_VideoInfo *videoInfo; /* holds info about display */ if ( SDL_Init ( SDL_INIT_VIDEO ) < 0 ) { fprintf ( stderr, "Video initialization failed: %s\n", SDL_GetError () ); quit (1); } videoInfo = SDL_GetVideoInfo (); /* Fetch the video info */ if ( !videoInfo ) { fprintf ( stderr, "Video query failed: %s\n", SDL_GetError() ); quit (1); } /* the flags to pass to SDL_SetVideoMode */ video_flags = SDL_OPENGL; video_flags |= SDL_GL_DOUBLEBUFFER; video_flags |= SDL_HWPALETTE; /* Store the palette in hardware */ // video_flags |= SDL_RESIZABLE; /* Enable window resizing */ if ( videoInfo->hw_available ) video_flags |= SDL_HWSURFACE; else video_flags |= SDL_SWSURFACE; if ( videoInfo->blit_hw ) /* checks if hardware blits can be done */ video_flags |= SDL_HWACCEL; /* OpenGL double buffering */ SDL_GL_SetAttribute ( SDL_GL_DOUBLEBUFFER, 1 ); /* get a SDL surface */ surface = SDL_SetVideoMode ( SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, video_flags ); if ( !surface ) { /* Verify there is a surface */ fprintf ( stderr, "Video mode set failed: %s\n", SDL_GetError() ); quit (1); } resize_window ( SCREEN_WIDTH, SCREEN_HEIGHT ); return 1; } int main ( int argc, char **argv ) { SDL_Event event; /* used to collect events */ init_sdl (); init_opengl (); read_obj_file (); while ( !done ) { while ( SDL_PollEvent ( &event ) ) hanlde_events ( event ); draw (); } quit (0); /* clean ourselves up and exit */ return 0; }
You need to login to post a comment.
