void testApp::saveFrameFBO(string filename, int width, int height) { // set the scale -- assume we kept width/height ratio with the screen float ratio = (float)width / (float)screen_width; //shader.setUniform1f("u_scale", ratio); // set projection matrix glMatrixMode(GL_PROJECTION); update_p_matrix(proj_mat, width, height); glLoadMatrixf(proj_mat); glMatrixMode(GL_MODELVIEW); ofFbo fbo; fbo.allocate(width, height); fbo.begin(); glClearColor(0,0,0,1); glClear(GL_COLOR_BUFFER_BIT); draw(); ofImage output_img; output_img.allocate(width, height, OF_IMAGE_COLOR); unsigned char * pixels = output_img.getPixels(); glReadPixels(0, 0, width, height, GL_RGB, GL_UNSIGNED_BYTE, pixels); output_img.mirror(true, false); fbo.end(); output_img.saveImage(filename); //shader.setUniform1f("u_scale", 1.0); // reset modelview matrix update_p_matrix(proj_mat, screen_width, screen_height); glMatrixMode(GL_PROJECTION); glLoadMatrixf(proj_mat); glMatrixMode(GL_MODELVIEW); }
static int make_resources(void) { GLuint vertex_shader, fragment_shader, program; g_resources.flag_vertex_array = init_flag_mesh(&g_resources.flag); init_background_mesh(&g_resources.background); g_resources.flag.texture = make_texture("flag.tga"); g_resources.background.texture = make_texture("background.tga"); if (!make_shadow_framebuffer( &g_resources.shadowmap_texture, &g_resources.shadowmap_framebuffer )) { return 0; } if (g_resources.flag.texture == 0 || g_resources.background.texture == 0) return 0; struct flag_shaders shaders; if (!make_flag_programs(&shaders)) return 0; enact_flag_programs(&shaders); g_resources.eye_offset[0] = 0.0f; g_resources.eye_offset[1] = 0.0f; g_resources.window_size[0] = INITIAL_WINDOW_WIDTH; g_resources.window_size[1] = INITIAL_WINDOW_HEIGHT; g_resources.light_direction[0] = 0.408248; g_resources.light_direction[1] = -0.816497; g_resources.light_direction[2] = 0.408248; update_p_matrix( g_resources.p_matrix, INITIAL_WINDOW_WIDTH, INITIAL_WINDOW_HEIGHT ); update_mv_matrix(g_resources.mv_matrix, g_resources.eye_offset); update_shadow_matrix(g_resources.shadow_matrix, g_resources.light_direction); return 1; }
//-------------------------------------------------------------- void testApp::draw(){ unsigned long long cur_time = ofGetSystemTime(); update_p_matrix(proj_mat, screen_width, screen_height); update_mv_matrix(mv_mat, 0); glMatrixMode(GL_PROJECTION); glLoadMatrixf(proj_mat); glMatrixMode(GL_MODELVIEW); glLoadMatrixf(mv_mat); if (!use_easycam) look(); else easycam.begin(); glVertexPointer(3,GL_FLOAT,sizeof(GLfloat)*3,(void*)0); glEnableClientState(GL_VERTEX_ARRAY); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, elementBuffer); glDrawElements( GL_QUADS, e_buffer_size, /* count */ GL_UNSIGNED_INT, (void*)0 /* element array buffer offset */ ); glDisableClientState(GL_VERTEX_ARRAY); // print framerate to stdout every second static unsigned long long last_fps_tick = 0; if (cur_time > last_fps_tick + 1000) { last_fps_tick = cur_time; cout << ofToString(ofGetFrameRate(), 2) << " fps" << endl; } GLenum err = glGetError(); if (err) { //breakpoint? if (false) cout << "error" << endl; } last_time = cur_time; if (use_easycam) easycam.end(); }
//-------------------------------------------------------------- void testApp::setup(){ g_element_buffer_data = NULL; g_vertex_buffer_data = NULL; move_camera = false; x_res = 100, z_res = 100; // number of *vertices* on x & z axis last_time = 0; // enable alpha blending glEnable( GL_BLEND ); glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA); // Draw in wireframe mode glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); ofSetLogLevel(OF_LOG_VERBOSE); ofBackground(0,0,0); // load shaders? // Initialize projection and model view matrices update_p_matrix(proj_mat, screen_width, screen_height); update_mv_matrix(mv_mat, 0); glMatrixMode(GL_PROJECTION); glLoadMatrixf(proj_mat); glMatrixMode(GL_MODELVIEW); glLoadMatrixf(mv_mat); // init GL buffers glGenBuffers(1, &vertexBuffer); glGenBuffers(1, &elementBuffer); // initialize shape shape = SHAPE_PLANE; generateWireframe(); bufferGrid(); // Easycam allows mouse interaction use_easycam=false; easycam.setNearClip( 0.0625); easycam.setDistance(1.0); }
//-------------------------------------------------------------- void testApp::setup(){ g_element_buffer_data = NULL; g_vertex_buffer_data = NULL; move_camera = false; x_res = 200, z_res = 200; // number of *vertices* on x & z axis last_time = 0; glDisable(GL_DEPTH_TEST); glEnable( GL_BLEND ); glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA); ofSetLogLevel(OF_LOG_VERBOSE); ofBackground(0,0,0,0); shader.load("vertex.glsl", "fragment.glsl"); vert_3d_attrib = shader.getAttributeLocation("position"); proj_mat_attrib = shader.getAttributeLocation("u_proj_matrix"); offset_attrib = shader.getAttributeLocation("u_offset"); shader.setUniform2f("u_offset", 0.0,0.0); //shader.begin(); shader.setUniform1f("u_scale", 1.0); update_p_matrix(proj_mat, screen_width, screen_height); update_mv_matrix(mv_mat, 0); /*shader.setUniformMatrix4f("u_proj_matrix", proj_mat); shader.setUniformMatrix4f("u_mv_matrix", mv_mat);*/ //glMatrixMode(GL_PROJECTION); //glLoadMatrixf(proj_mat); //glMatrixMode(GL_MODELVIEW); //glLoadMatrixf(mv_mat); // init GL buffers glGenBuffers(1, &vertexBuffer); glGenBuffers(1, &elementBuffer); }
// TODO doesn't center the shape properly when width and height are smaller than screen dimensions void testApp::saveFrameMultiFBO(string filename, int width, int height) { // these dims need to be multiples of screen dimensions, for now // making it independent of screen dims would involve 2d float u_scale uniform in shader int MAX_FBO_WIDTH = screen_width*2; int MAX_FBO_HEIGHT = screen_height*2; ofImage output_img; output_img.allocate(width, height, OF_IMAGE_COLOR); // set the scale -- assume we kept width/height ratio with the screen int num_y_axis_fbos = (int)ceilf((float)height / (float)MAX_FBO_HEIGHT); float ratio = ((float)height / (float)MAX_FBO_HEIGHT ); //float ratio = (float)width / (float)screen_width; /*double offset_ratio_x = 2.0 / (double)screen_width; double offset_ratio_y = 2.0 / (double)screen_height;*/ double offset_ratio_x = 2.0 / (double)MAX_FBO_WIDTH; double offset_ratio_y = 2.0 / (double)MAX_FBO_HEIGHT; shader.begin(); shader.setUniform1f("u_scale", ratio); ofFbo fbo; /*fbo.allocate(screen_width, screen_height); */ fbo.allocate(MAX_FBO_WIDTH, MAX_FBO_HEIGHT); float x_center = (float)width / (float)MAX_FBO_WIDTH; float y_center = (float)height / (float)MAX_FBO_HEIGHT; float x =0; while (x < width) { float y = 0; while (y < height) { // set the offset /*shader.setUniform2f("u_offset", (x - floorf(width /2.0)) * -offset_ratio_x - 1.0, (y - floorf(height/2.0)) * -offset_ratio_y - 1.0 );*/ float frame_x_center = (x/MAX_FBO_WIDTH)*2 + 1; float frame_y_center = (y/MAX_FBO_HEIGHT)*2 + 1; /*shader.setUniform2f("u_offset", ((float)num_y_axis_fbos/2.0)-(x/MAX_FBO_WIDTH)*2, ((float)num_y_axis_fbos/2.0)-(y/MAX_FBO_HEIGHT)*2 );*/ shader.setUniform2f("u_offset", x_center - frame_x_center, y_center - frame_y_center ); // start fbo fbo.begin(); glClearColor(0,0,0,1); glClear(GL_COLOR_BUFFER_BIT); // projection matrix glMatrixMode(GL_PROJECTION); update_p_matrix(proj_mat, width, height); glLoadMatrixf(proj_mat); glMatrixMode(GL_MODELVIEW); innerDraw(); // get pixels ofImage img_part; int fbo_width = std::min((int)(width - x), MAX_FBO_WIDTH); int fbo_height = std::min((int)(height - y), MAX_FBO_HEIGHT); img_part.allocate(fbo_width, fbo_height, OF_IMAGE_COLOR); unsigned char * pixels = img_part.getPixels(); glReadPixels(0, MAX_FBO_HEIGHT-fbo_height, fbo_width, fbo_height, GL_RGB, GL_UNSIGNED_BYTE, pixels); img_part.mirror(true, false); img_part.saveImage("part.png"); // debugging : save the image part // end fbo fbo.end(); // draw to output_image // TODO fid a more efficient way for (int i=0; i< fbo_width; i++) { for (int j=0; j<fbo_height; j++) { output_img.setColor((int)(x) + i,height - fbo_height - (int)(y) + j, img_part.getColor(i, j)); } } y += MAX_FBO_HEIGHT; } x += MAX_FBO_WIDTH; } output_img.saveImage(filename); shader.setUniform2f("u_offset",0,0); shader.setUniform1f("u_scale", 1.0); shader.end(); // reset modelview matrix update_p_matrix(proj_mat, screen_width, screen_height); glMatrixMode(GL_PROJECTION); glLoadMatrixf(proj_mat); glMatrixMode(GL_MODELVIEW); }
//-------------------------------------------------------------- void testApp::draw(){ static bool mat_init = false; if (!mat_init) { // not sure why this part is necessary as we init'd proj and mv matrices in setup() update_p_matrix(proj_mat, screen_width, screen_height); update_mv_matrix(mv_mat, 0); mat_init = true; } // Need to push proj and modelview matrices b4 drawing mesh, then pop after, to be able to draw text afterwards glMatrixMode(GL_PROJECTION); glPushMatrix(); glLoadMatrixf(proj_mat); glMatrixMode(GL_MODELVIEW); glPushMatrix(); ofDrawBitmapString("Test", 10,10); unsigned long long cur_time = ofGetSystemTime(); ofDrawBitmapString("Test3", 10,50); shader.begin(); shader.setUniform2f("u_offset", 0.0,0.0); shader.setUniform1f("u_scale", 1.0); innerDraw(); shader.end(); glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); glPopMatrix(); glMatrixMode(GL_PROJECTION); glPopMatrix(); glMatrixMode(GL_MODELVIEW); // show fps string str = "framerate: "; str += ofToString(ofGetFrameRate(), 2) + "fps"; ofDrawBitmapString(str, 20, 20); ofDrawBitmapString("HELLLOO", 40,40); //glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); // PROBLEM - using my own low level opengl calls seems to cause a problem with // openframeworks own text drawing //string str = "framerate: "; //str += ofToString(ofGetFrameRate(), 2) + "fps"; //ofDrawBitmapStringHighlight(str, 0, 0); //ofDrawBitmapStringHighlight("Test", 20, 10); // instead print framerate to stdout every second static unsigned long long last_fps_tick = 0; if (cur_time > last_fps_tick + 1000) { last_fps_tick = cur_time; cout << ofToString(ofGetFrameRate(), 2) << " fps" << endl; } GLenum err = glGetError(); if (err) { //breakpoint? if (false) cout << "error" << endl; } last_time = cur_time; }
static void reshape(int w, int h) { g_resources.window_size[0] = w; g_resources.window_size[1] = h; update_p_matrix(g_resources.p_matrix, w, h); }