c++ - How to pause this opengl program? -
i managed code compile in qtcreator on ubuntu. when run it, runs fast , window closes immediately. there warnings no errors in when compile.
advice me on how see if program crashes or runs fast.
here code:
this main function
// two-dimensional sierpinski gasket // generated using randomly selected vertices , bisection #include "angel.h" const int numpoints = 5000; //---------------------------------------------------------------------------- void init( void ) { vec2 points[numpoints]; // specifiy vertices triangle vec2 vertices[3] = { vec2( -1.0, -1.0 ), vec2( 0.0, 1.0 ), vec2( 1.0, -1.0 ) }; // select arbitrary initial point inside of triangle points[0] = vec2( 0.25, 0.50 ); // compute , store n-1 new points ( int = 1; < numpoints; ++i ) { int j = rand() % 3; // pick vertex @ random // compute point halfway between selected vertex // , previous point points[i] = ( points[i - 1] + vertices[j] ) / 2.0; } // create vertex array object gluint vao[1]; glgenvertexarraysapple( 1, vao ); glbindvertexarrayapple( vao[0] ); // create , initialize buffer object gluint buffer; glgenbuffers( 1, &buffer ); glbindbuffer( gl_array_buffer, buffer ); glbufferdata( gl_array_buffer, sizeof(points), points, gl_static_draw ); // load shaders , use resulting shader program gluint program = initshader( "vshader21.glsl", "fshader21.glsl" ); gluseprogram( program ); // initialize vertex position attribute vertex shader gluint loc = glgetattriblocation( program, "vposition" ); glenablevertexattribarray( loc ); glvertexattribpointer( loc, 2, gl_float, gl_false, 0, buffer_offset(0) ); glclearcolor( 1.0, 1.0, 1.0, 1.0 ); // white background } //---------------------------------------------------------------------------- void display( void ) { glclear( gl_color_buffer_bit ); // clear window gldrawarrays( gl_points, 0, numpoints ); // draw points glflush(); } //---------------------------------------------------------------------------- void keyboard( unsigned char key, int x, int y ) { switch ( key ) { case 033: exit( exit_success ); break; } } //---------------------------------------------------------------------------- int main( int argc, char **argv ) { glutinit( &argc, argv ); glutinitdisplaymode( glut_rgba ); glutinitwindowsize( 512, 512 ); glutcreatewindow( "sierpinski gasket" ); init(); glutdisplayfunc( display ); glutkeyboardfunc( keyboard ); glutmainloop(); return 0; }
this angel.h file:
////////////////////////////////////////////////////////////////////////////// // // --- angel.h --- // // main header file examples angel 6th edition // ////////////////////////////////////////////////////////////////////////////// #ifndef __angel_h__ #define __angel_h__ //---------------------------------------------------------------------------- // // --- include system headers --- // #include <cmath> #include <iostream> // define m_pi in case it's not defined in math header file #ifndef m_pi # define m_pi 3.14159265358979323846 #endif //---------------------------------------------------------------------------- // // --- include opengl header files , helpers --- // // location of these files vary operating system. we've included // copies of open-soruce project headers in "gl" directory local // this "include" directory. // #ifdef __apple__ // include mac os x verions of headers # include <opengl/opengl.h> # include <glut/glut.h> #else // non-mac os x operating systems # include <gl/glew.h> # include <gl/freeglut.h> # include <gl/freeglut_ext.h> #endif // __apple__ // define helpful macro handling offsets buffer objects #define buffer_offset( offset ) ((glvoid*) (offset)) //---------------------------------------------------------------------------- // // --- include our class libraries , constants --- // namespace angel { // helper function load vertex , fragment shader files gluint initshader( const char* vertexshaderfile, const char* fragmentshaderfile ); // defined constant when numbers small used in // denominator of division operation. used if // debug macro defined. const glfloat dividebyzerotolerance = glfloat(1.0e-07); // degrees-to-radians constant const glfloat degreestoradians = m_pi / 180.0; } // namespace angel #include "vec.h" #include "mat.h" //#include "checkerror.h" // #define print(x) { std::cerr << #x " = " << (x) << std::endl; } while(0) // globally use our namespace in our example programs. using namespace angel; #endif // __angel_h__
and here initshader.h include:
#include "angel.h" namespace angel { // create null-terminated string reading provided file static char* readshadersource(const char* shaderfile) { file* fp = fopen(shaderfile, "r"); if ( fp == null ) { return null; } fseek(fp, 0l, seek_end); long size = ftell(fp); fseek(fp, 0l, seek_set); char* buf = new char[size + 1]; fread(buf, 1, size, fp); buf[size] = '\0'; fclose(fp); return buf; } // create glsl program object vertex , fragment shader files gluint initshader(const char* vshaderfile, const char* fshaderfile) { struct shader { const char* filename; glenum type; glchar* source; } shaders[2] = { { vshaderfile, gl_vertex_shader, null }, { fshaderfile, gl_fragment_shader, null } }; gluint program = glcreateprogram(); ( int = 0; < 2; ++i ) { shader& s = shaders[i]; s.source = readshadersource( s.filename ); if ( shaders[i].source == null ) { std::cerr << "failed read " << s.filename << std::endl; exit( exit_failure ); } gluint shader = glcreateshader( s.type ); glshadersource( shader, 1, (const glchar**) &s.source, null ); glcompileshader( shader ); glint compiled; glgetshaderiv( shader, gl_compile_status, &compiled ); if ( !compiled ) { std::cerr << s.filename << " failed compile:" << std::endl; glint logsize; glgetshaderiv( shader, gl_info_log_length, &logsize ); char* logmsg = new char[logsize]; glgetshaderinfolog( shader, logsize, null, logmsg ); std::cerr << logmsg << std::endl; delete [] logmsg; exit( exit_failure ); } delete [] s.source; glattachshader( program, shader ); } /* link , error check */ gllinkprogram(program); glint linked; glgetprogramiv( program, gl_link_status, &linked ); if ( !linked ) { std::cerr << "shader program failed link" << std::endl; glint logsize; glgetprogramiv( program, gl_info_log_length, &logsize); char* logmsg = new char[logsize]; glgetprograminfolog( program, logsize, null, logmsg ); std::cerr << logmsg << std::endl; delete [] logmsg; exit( exit_failure ); } /* use program object */ gluseprogram(program); return program; } } // close namespace angel block
this program written in way, terminate on own account in case of failure. print error message when happens. suggest run terminal, can see output (i.e. don't double-click produced binary).
Comments
Post a Comment