oputyk Temat założony przez niniejszego użytkownika |
Problem przy wyłączaniu się aplikacji » 2014-02-15 17:33:24 Mam taki problem kiedy wyłączę okno OpenGL. Nie rozumiem tego, bo kiedy wyłączyłem aplikację przez okienko konsolowe, wszystko działa jak należy. Kod: #include<iostream> #include<GLTools.h> #include<GLFrustum.h> #include<GLShaderManager.h> #define FREEGLUT_STATIC #include<GL/glut.h> #include<SFML\Graphics.hpp> #include<math3d.h> #include<GLMatrixStack.h> #include<GLGeometryTransform.h> #include<GLFrame.h> #include<corona.h> GLShaderManager shaderManager; GLTriangleBatch kula; GLGeometryTransform M; GLTriangleBatch torus; GLBatch a; GLBatch batch; GLMatrixStack M1; GLMatrixStack M2; GLint ShaderID; GLFrustum frustum; GLFrame camera; GLuint textureID; bool LoadTexture( const char * file_name, GLenum minFilter, GLenum magFilter, GLenum wrapMode ) { GLenum eFormat; corona::Image * img = corona::OpenImage( file_name ); switch( img->getFormat() ) { case corona::PF_B8G8R8A8: img = ConvertImage( img, corona::PF_R8G8B8A8 ); eFormat = GL_RGBA; break; case corona::PF_R8G8B8A8: eFormat = GL_RGBA; break; case corona::PF_R8G8B8: eFormat = GL_RGB; break; } FlipImage( img, corona::CA_X ); glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, wrapMode ); glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, wrapMode ); glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, minFilter ); glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, magFilter ); glPixelStorei( GL_UNPACK_ALIGNMENT, 1 ); glTexImage2D( GL_TEXTURE_2D, 0, eFormat, img->getWidth(), img->getHeight(), 0, eFormat, GL_UNSIGNED_BYTE, img->getPixels() ); delete img; if( minFilter == GL_LINEAR_MIPMAP_LINEAR || minFilter == GL_LINEAR_MIPMAP_NEAREST || minFilter == GL_NEAREST_MIPMAP_LINEAR || minFilter == GL_NEAREST_MIPMAP_NEAREST ) glGenerateMipmap( GL_TEXTURE_2D ); return true; } void ChangeSize( int w, int h ) { if( h == 0 ) h = 1; glViewport( 0, 0, w, h ); frustum.SetPerspective( 80.0f, float( w ) / float( h ), 1.0f, 1000.0f ); M2.LoadMatrix( frustum.GetProjectionMatrix() ); M.SetMatrixStacks( M1, M2 ); } void SetupRC() { glClearColor( 0.0f, 0.0f, 1.0f, 1.0f ); shaderManager.InitializeStockShaders(); gltMakeSphere( kula, 0.5f, 30, 50 ); gltMakeTorus( torus, 0.5f, 0.2f, 30, 50 ); glEnable( GL_DEPTH_TEST ); glGenTextures( 1, & textureID ); glBindTexture( GL_TEXTURE_2D, textureID ); if( !LoadTexture( "CAM00106.jpg", GL_LINEAR, GL_LINEAR, GL_CLAMP_TO_EDGE ) ) { std::cout << "Haven't file!!!" << std::endl; system( "pause" ); exit( 1 ); } a.Begin( GL_TRIANGLE_STRIP, 4, 1 ); a.MultiTexCoord2f( 0, 1.0f, 0.0f ); a.Vertex3f( 10.0f, - 5.0f, 10.0f ); a.MultiTexCoord2f( 0, 1.0f, 1.0f ); a.Vertex3f( 10.0f, 5.0f, 10.0f ); a.MultiTexCoord2f( 0, 0.0f, 0.0f ); a.Vertex3f( 10.0f, - 5.0f, - 10.0f ); a.MultiTexCoord2f( 0, 0.0f, 1.0f ); a.Vertex3f( 10.0f, 5.0f, - 10.0f ); a.End(); ShaderID = gltLoadShaderPairWithAttributes( "shd.vp", "shd.fp", 2, GLT_ATTRIBUTE_VERTEX, "vVertex", GLT_ATTRIBUTE_COLOR, "vColor" ); GLfloat vVerts[] = { - 0.5f, 0.0f, 0.0f, 0.5f, 0.0f, 0.0f, 0.0f, 0.5f, 0.0f }; GLfloat vColors[] = { 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f }; batch.Begin( GL_TRIANGLES, 3 ); batch.CopyVertexData3f( vVerts ); batch.CopyColorData4f( vColors ); batch.End(); } void ShutDown() { glDeleteTextures( 1, & textureID ); } void RenderScene() { float rads =( float )( m3dDegToRad( 0.1f ) ); M3DVector4f lp = { 0.0f, 10.0f, 5.0f, 1.0f }; M3DVector4f oko; if( sf::Keyboard::isKeyPressed( sf::Keyboard::Up ) ) camera.RotateWorld( rads, 1.0f, 0.0f, 0.0f ); if( sf::Keyboard::isKeyPressed( sf::Keyboard::Down ) ) exit( 0 ); if( sf::Keyboard::isKeyPressed( sf::Keyboard::Left ) ) camera.RotateWorld( rads, 0.0f, 1.0f, 0.0f ); if( sf::Keyboard::isKeyPressed( sf::Keyboard::Right ) ) camera.RotateWorld( - rads, 0.0f, 1.0f, 0.0f ); if( sf::Keyboard::isKeyPressed( sf::Keyboard::W ) ) camera.MoveForward( 0.01f ); if( sf::Keyboard::isKeyPressed( sf::Keyboard::S ) ) camera.MoveForward( - 0.01f ); if( sf::Keyboard::isKeyPressed( sf::Keyboard::A ) ) camera.MoveRight( 0.01f ); if( sf::Keyboard::isKeyPressed( sf::Keyboard::D ) ) camera.MoveRight( - 0.01f ); if( sf::Keyboard::isKeyPressed( sf::Keyboard::Space ) ) camera.MoveUp( 0.01f ); if( sf::Keyboard::isKeyPressed( sf::Keyboard::LShift ) ) camera.MoveUp( - 0.01f ); static sf::Clock timer; GLfloat vBrown[] = { 0.6f, 0.4f, 0.12f, 1.0f }; GLfloat vBlue[] = { 0.0f, 0.0f, 1.0f, 1.0f }; GLfloat vGreen[] = { 0.0f, 1.0f, 0.0f, 1.0f }; GLfloat vPurple[] = { 0.6f, 0.4f, 0.7f, 1.0f }; GLfloat vBlack[] = { 0.0f, 0.0f, 0.0f, 1.0f }; glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); M3DMatrix44f mCamera; camera.GetCameraMatrix( mCamera ); M1.PushMatrix(); M1.PushMatrix( mCamera ); m3dTransformVector4( oko, lp, mCamera ); glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA ); glEnable( GL_CULL_FACE ); glCullFace( GL_BACK ); glEnable( GL_BLEND ); glEnable( GL_MULTISAMPLE ); shaderManager.UseStockShader( GLT_SHADER_TEXTURE_REPLACE, M.GetModelViewProjectionMatrix(), 0 ); glBindTexture( GL_TEXTURE_2D, textureID ); a.Draw(); M1.Translate( 2.0f, 0.0f, - 6.0f ); M1.Rotate( timer.getElapsedTime().asSeconds() * 120.0f, 0.7f, 1.0f, 0.1f ); shaderManager.UseStockShader( GLT_SHADER_POINT_LIGHT_DIFF, M.GetModelViewMatrix(), M.GetProjectionMatrix(), oko, vPurple ); torus.Draw(); glDisable( GL_MULTISAMPLE ); glEnable( GL_LINE_SMOOTH ); glPolygonOffset( - 1.0f, - 1.0f ); glEnable( GL_POLYGON_OFFSET_LINE ); glPolygonMode( GL_FRONT_AND_BACK, GL_LINE ); glEnable( GL_CULL_FACE ); glPolygonMode( GL_FRONT_AND_BACK, GL_FILL ); glDisable( GL_POLYGON_OFFSET_LINE ); glDisable( GL_LINE_SMOOTH ); glDisable( GL_BLEND ); M1.PopMatrix(); M1.PopMatrix(); glDisable( GL_BLEND ); M1.PushMatrix(); M1.PushMatrix( mCamera ); glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA ); glEnable( GL_BLEND ); glEnable( GL_MULTISAMPLE ); M1.Translate( 0.0f, 0.0f, - 2.5f ); M1.Rotate( timer.getElapsedTime().asSeconds() * 90.0f, 1.0f, 1.0f, 0.0f ); shaderManager.UseStockShader( GLT_SHADER_POINT_LIGHT_DIFF, M.GetModelViewMatrix(), M.GetProjectionMatrix(), oko, vGreen ); kula.Draw(); glDisable( GL_MULTISAMPLE ); glEnable( GL_LINE_SMOOTH ); glPolygonOffset( 1.0f, 1.0f ); glEnable( GL_POLYGON_OFFSET_LINE ); glPolygonMode( GL_FRONT_AND_BACK, GL_LINE ); glEnable( GL_CULL_FACE ); glCullFace( GL_BACK ); shaderManager.UseStockShader( GLT_SHADER_POINT_LIGHT_DIFF, M.GetModelViewMatrix(), M.GetProjectionMatrix(), oko, vBlue ); kula.Draw(); glDisable( GL_CULL_FACE ); glPolygonMode( GL_FRONT_AND_BACK, GL_FILL ); glDisable( GL_POLYGON_OFFSET_LINE ); glDisable( GL_LINE_SMOOTH ); glDisable( GL_BLEND ); M1.PopMatrix(); M1.PopMatrix(); glutSwapBuffers(); glutPostRedisplay(); } int main( int argc, char * argv[] ) { gltSetWorkingDirectory( argv[ 0 ] ); glutInit( & argc, argv ); glutInitDisplayMode( GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH ); glutInitWindowSize( 800, 600 ); glutCreateWindow( "OpenGL - Window" ); glutReshapeFunc( ChangeSize ); glutDisplayFunc( RenderScene ); GLenum err = glewInit(); if( GLEW_OK != err ) { std::cout << err << std::endl; return 1; } SetupRC(); glutMainLoop(); ShutDown(); }
Z góry dzięki za pomoc! |