// recreate printf as a GL thing void glPrintf(float x,float y, char const * fmt, ...) { static char buf[1024]; va_list vl; va_start(vl, fmt); vsnprintf(buf, sizeof(buf), fmt, vl); va_end(vl); glRasterPos2f(x,y); ftglRenderFont(font, buf, FTGL_RENDER_ALL); }
void render() { #ifdef OPENGL glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glLoadIdentity(); glPushMatrix(); glScalef(zoomFactor, zoomFactor, 0.0); glTranslatef(-transX, -transY, 0.0); if (layer[currentLayer].glList) { glCallList(layer[currentLayer].glList); } else { layer[currentLayer].glList = glGenLists(1); glNewList(layer[currentLayer].glList, GL_COMPILE_AND_EXECUTE); glBegin(GL_QUADS); #else uint32_t yellow; yellow = SDL_MapRGB(Surf_Display->format, 224, 224, 128); SDL_LockSurface(Surf_Display); SDL_FillRect(Surf_Display, NULL, yellow); int lines = 0; #endif for (int i = shadow_layers; i >= 1; i--) { if (currentLayer - i > 0) render_layer(currentLayer - i, shadow_alpha - (i - 1) * (shadow_alpha / ((float) shadow_layers))); } render_layer(currentLayer, 1.0); #ifdef OPENGL glEnd(); glEndList(); } glPopMatrix(); glPushMatrix(); glTranslatef(0.0, 200.0 - (20.0 * 0.3), 0.0); glScalef(0.3, 0.3, 1.0); ftglSetFontFaceSize(font, 20, 20); ftglRenderFont(font, msgbuf, FTGL_RENDER_ALL); glPopMatrix(); glFlush(); glFinish(); SDL_GL_SwapBuffers(); glFinish(); #else SDL_UnlockSurface(Surf_Display); SDL_Flip(Surf_Display); #endif }
static int font_write(lua_State *L) { font_t *font = checked_font(L, 1); GLfloat x = luaL_checknumber(L, 2); GLfloat y = luaL_checknumber(L, 3); const char *text = luaL_checkstring(L, 4); // Protect FTGL if (!check_utf8(text)) return luaL_error(L, "invalid utf8"); GLfloat size = luaL_checknumber(L, 5) / SCALE; int type = lua_type(L, 6); if (type == LUA_TNUMBER) { GLfloat r = luaL_checknumber(L, 6); GLfloat g = luaL_checknumber(L, 7); GLfloat b = luaL_checknumber(L, 8); GLfloat a = luaL_optnumber(L, 9, 1.0); shader_set_gl_color(r, g, b, a); glBindTexture(GL_TEXTURE_2D, default_tex); } else if (type == LUA_TUSERDATA || type == LUA_TTABLE) { lua_pushliteral(L, "texid"); lua_gettable(L, 6); if (lua_type(L, -1) != LUA_TFUNCTION) return luaL_argerror(L, 6, "no texid() function"); lua_pushvalue(L, 6); lua_call(L, 1, 1); if (lua_type(L, -1) != LUA_TNUMBER) return luaL_argerror(L, 6, "texid() did not return number"); int tex_id = lua_tonumber(L, -1); lua_pop(L, 1); shader_set_gl_color(1.0, 1.0, 1.0, 1.0); glBindTexture(GL_TEXTURE_2D, tex_id); } else { return luaL_argerror(L, 6, "unsupported value. must be RGBA or texturelike"); } glPushMatrix(); glTranslatef(x, y, 0); glTranslatef(0, size * (SCALE * 0.8), 0); glScalef(size, -size, 1.0); ftglRenderFont(font->font, text, FTGL_RENDER_ALL); glPopMatrix(); lua_pushnumber(L, ftglGetFontAdvance(font->font, text) * size); return 1; }
static void cdftext(cdCtxCanvas *ctxcanvas, double x, double y, const char *s, int len) { int stipple = 0, reset_antialias = 0; float bounds[6]; int w, h, descent, baseline; double x_origin = x; double y_origin = y; if (!ctxcanvas->font) return; s = cdglStrConvertToUTF8(ctxcanvas, s, len); ftglGetFontBBox(ctxcanvas->font, s, len, bounds); descent = (int)ftglGetFontDescender(ctxcanvas->font); w = (int)ceil(bounds[3] - bounds[0]); h = (int)ceil(bounds[4] - bounds[1]); baseline = (int)ftglGetFontLineHeight(ctxcanvas->font) - (int)ftglGetFontAscender(ctxcanvas->font); switch (ctxcanvas->canvas->text_alignment) { case CD_BASE_RIGHT: case CD_NORTH_EAST: case CD_EAST: case CD_SOUTH_EAST: x = x - w; break; case CD_BASE_CENTER: case CD_CENTER: case CD_NORTH: case CD_SOUTH: x = x - w/2; break; case CD_BASE_LEFT: case CD_NORTH_WEST: case CD_WEST: case CD_SOUTH_WEST: x = x; break; } switch (ctxcanvas->canvas->text_alignment) { case CD_BASE_LEFT: case CD_BASE_CENTER: case CD_BASE_RIGHT: y = y; break; case CD_SOUTH_EAST: case CD_SOUTH_WEST: case CD_SOUTH: y = y - descent; break; case CD_NORTH_EAST: case CD_NORTH: case CD_NORTH_WEST: y = y - h/2 - baseline; break; case CD_CENTER: case CD_EAST: case CD_WEST: y = y - baseline; break; } if (ctxcanvas->canvas->text_orientation != 0) { double angle = CD_DEG2RAD * ctxcanvas->canvas->text_orientation; double cos_angle = cos(angle); double sin_angle = sin(angle); cdfRotatePoint(ctxcanvas->canvas, x, y, x_origin, y_origin, &x, &y, sin_angle, cos_angle); } if(glIsEnabled(GL_POLYGON_STIPPLE)) { stipple = 1; glDisable(GL_POLYGON_STIPPLE); } if (ctxcanvas->txt_antialias && !glIsEnabled(GL_LINE_SMOOTH)) { cdCanvasSetAttribute(ctxcanvas->canvas, "ANTIALIAS", "1"); reset_antialias = 1; } glPushMatrix(); glTranslated(x, y, 0.0); glRotated(ctxcanvas->canvas->text_orientation, 0, 0, 1); ftglRenderFont(ctxcanvas->font, s, FTGL_RENDER_ALL); glPopMatrix(); if (reset_antialias) cdCanvasSetAttribute(ctxcanvas->canvas, "ANTIALIAS", "0"); if(stipple) glEnable(GL_POLYGON_STIPPLE); }
void OpenGLModule::drawString(const std::string& string) const { ftglRenderFont(_font->getFtglFont(),string.c_str(),0xFFFF);//FTGL_RENDER_ALL); }
int main(int argc, char *argv[]) { char *shader = "mandelbrot (copy).frag"; if (argc > 1) { shader = argv[1]; } glfwInit(); /*int major, minor, rev; glfwGetGLVersion(&major, &minor, &rev); printf("using openGL %d.%d.%d\n", major, minor, rev);*/ GLFWvidmode mode; glfwGetDesktopMode( &mode ); window_width = mode.Width; window_height = mode.Height; printf("%dx%d\n", window_width, window_height); //glfwOpenWindow(window_width, window_height, 5, 6, 5, 0, 8, 0, GLFW_FULLSCREEN); glfwOpenWindow(mode.Width, mode.Height, mode.RedBits, mode.GreenBits, mode.BlueBits, 0, 0, 0, GLFW_FULLSCREEN); // setup opengl perspective stuff. glMatrixMode(GL_PROJECTION); float aspect_ratio = ((float)window_height) / window_width; glFrustum(.5, -.5, -.5 * aspect_ratio, .5 * aspect_ratio, 1, 50); glDisable( GL_DEPTH_TEST ); glTranslatef(0.0, 0.0, -1.00); glClearColor(0.0, 1.0, 1.0, 0.0); // will create and set shader program. GLhandleARB program = SetupFragmentShader(shader); // get the location of all the uniforms prepareUniforms(program); // must call use program before setting uniforms values glUseProgram(program); uint param_i = 0; parameters[WIDTH] = createParameter1i(program, "wW", window_width ); parameters[HEIGHT] = createParameter1i(program, "wH", window_height ); parameters[TIME] = createParameter1f(program, "time", 0.0 ); parameters[MINX] = createParameter1f(program, "minx", -2.0); parameters[MINY] = createParameter1f(program, "miny", 1.0); parameters[DELTA] = createParameter1f(program, "deltax", 3.0); parameters[THETA] = createParameter1f(program, "theta", 1.0); //synchParameters ( parameters ); /*for ( param_i = 0; param_i < NPARAMS; param_i++ ) { Parameter *param = ¶meters[param_i]; switch ( param->utype ) { case INT: glUniform1i(param->loc, param->u.ival); break; case FLOAT: glUniform1f(param->loc, param->u.fval); break; default: break; } }*/ // set the dimension, must be after program is used. //glUniform1i(wWLoc, window_width ); //glUniform1i(wHLoc, window_height ); printInstructions(); double frame_start = glfwGetTime(); double frame_time = 0.0; double temp_time = 0.0; //bool need_draw = true; bool need_view_synch = true; int mouseWheel = 0; int mouseWheelDelta = 0; double fIterations = (double)(iterations); int mouseX, mouseY, curMouseX, curMouseY; mouseX = mouseY = 0; bool joystickPresent = glfwGetJoystickParam( GLFW_JOYSTICK_1, GLFW_PRESENT ); if(joystickPresent) { printf("Joystick present. Can be used to navigate in addition to mouse.\n"); }else { printf("no joystick present.\n"); } // set up fonts FTGLfont *font = NULL; //font = ftglCreateBufferFont("/usr/share/fonts/truetype/freefont/FreeMono.ttf"); font = ftglCreatePixmapFont("/usr/share/fonts/truetype/freefont/FreeMono.ttf"); if( !font ) { printf("failed to load font."); } ftglSetFontFaceSize(font, 50, 50); //printf("%d\n", ftglGetFontError(font)); //ftglSetFontCharMap(font, ft_encoding_unicode); //printf("%d\n", ftglGetFontError(font)); bool show_fps = true; char text[256]; // keep on rendering the frame until escape is pressed. while(glfwGetKey(GLFW_KEY_ESC) != GLFW_PRESS) { temp_time = glfwGetTime(); frame_time = temp_time - frame_start; frame_start = temp_time; // process input if(glfwGetKey('E') == GLFW_PRESS) { printf("minx: %.31f\n", minx); printf("miny: %.31f\n", miny); printf("deltax: %.31f\n",deltax); printf("deltay: %.31f\n",deltay); printf("====================================\n"); }else if(glfwGetKey('P') == GLFW_PRESS) { saveFrameBuffer(); }else if(glfwGetKey('R') == GLFW_PRESS) { minx = -2.0; miny = -1.0; deltax = 3.0; deltay = 2.0; need_view_synch = true; }else if(glfwGetKey('F') == GLFW_PRESS) { show_fps=~show_fps; //printf("fps display toggled to: %d\n", show_fps); } else if(glfwGetKey(GLFW_KEY_RIGHT) == GLFW_PRESS) { fIterations += 5*(60*frame_time); iterations = floor(fIterations); need_view_synch = true; }else if(glfwGetKey(GLFW_KEY_LEFT) == GLFW_PRESS) { if(iterations>1) { fIterations -= 5*(60*frame_time); } iterations = floor(fIterations); need_view_synch = true; } // zooming using the arrow keys if(glfwGetKey(GLFW_KEY_UP) == GLFW_PRESS) { zoomIn(2.0*zoom_factor*(frame_time*60.0)); need_view_synch = true; }else if(glfwGetKey(GLFW_KEY_DOWN) == GLFW_PRESS) { zoomIn(-2.0*zoom_factor*(frame_time*60.0)); need_view_synch = true; } //changing the julia set constant if(glfwGetKey('W') == GLFW_PRESS) { realc += .0025*frame_time*60.0; need_view_synch = true; }else if(glfwGetKey('S') == GLFW_PRESS) { realc -= .0025*frame_time*60.0; need_view_synch = true; } if(glfwGetKey('A') == GLFW_PRESS) { imagc += .0025*frame_time*60.0; need_view_synch = true; }else if(glfwGetKey('D') == GLFW_PRESS) { imagc -= .0025*frame_time*60.0; need_view_synch = true; } // changing the area visible (panning left and right) glfwGetMousePos(&curMouseX, &curMouseY); int mouseDeltax = curMouseX - mouseX; int mouseDeltay = curMouseY - mouseY; if(mouseDeltax||mouseDeltay)//if one of them is different { //printf("delta coordinates: (%d, %d)\n", mouseDeltax, mouseDeltay); mouseX=curMouseX; mouseY=curMouseY; //printf("new mouse coordinates: (%d, %d)\n", x, y); pan(zoom_factor*deltax*mouseDeltax/20.0, -1*zoom_factor*deltay*mouseDeltay/20.0); need_view_synch = true; } if(joystickPresent) { float pos[4]; glfwGetJoystickPos(GLFW_JOYSTICK_1, pos, 4); //the above function gives us bad results for my particular joystick :(, must fiddle around with numbers to get correct answer. float xJoyPos = pos[0];//(pos[0]*32767)/128.0 - 1.0; float yJoyPos = pos[1];//(pos[1]*32767)/128.0 + 1.0; float zJoyPos = -1*pos[2];//-1*((pos[2]*32767)/128.0 - 1.0); float rJoyPos = pos[3]; float temp = 0.0; //printf("%f\n", xJoyPos); if(xJoyPos>.02||xJoyPos<-.02) { temp = zoom_factor*xJoyPos*deltax*1.0*frame_time*60.0; pan(temp, 0); need_view_synch = true; } if(yJoyPos>.02||yJoyPos<-.02) { temp = zoom_factor*yJoyPos*deltay*1.0*frame_time*60.0; pan(0, temp); need_view_synch = true; } if(zJoyPos>.04||zJoyPos<-.04) { zoomIn(zJoyPos*scroll_zoom_factor*(frame_time*10.0)); need_view_synch = true; } if(rJoyPos>.02 || rJoyPos<-.02) { rotate( rJoyPos/5.0*frame_time ); need_view_synch = true; } unsigned char buttons[5]; glfwGetJoystickButtons(GLFW_JOYSTICK_1, buttons, 4); if(buttons[0]==GLFW_PRESS) { saveFrameBuffer(); } if(buttons[1]==GLFW_PRESS) { rotate( 0.35*frame_time ); need_view_synch = true; }else if(buttons[2]==GLFW_PRESS) { rotate( -0.35*frame_time ); need_view_synch = true; } //printf("x:%f, y:%f\n", xJoyPos, yJoyPos); } // zooming using the mouse scroll mouseWheelDelta = glfwGetMouseWheel() - mouseWheel; mouseWheel = mouseWheel+mouseWheelDelta; if(mouseWheelDelta != 0) { zoomIn(mouseWheelDelta*scroll_zoom_factor*(frame_time*60.0)); need_view_synch = true; } //printf("mouse wheel: %d\n", mouseWheelDelta); if(need_view_synch) { synchVariableUniforms(); need_view_synch = false; } // update the time inside the shader for cool animations. parameters[2].val.fval = frame_start; parameters[2].needs_update = true; synchParameters ( parameters ); //glUniform1f(timeLoc, frame_start); // write fps and iterations to a string. sprintf(text, "FPS: %4.1d; ITER: %d; ZOOMX: %-10.1f", (int)(floor(1.0/frame_time)), iterations, (3.0/deltax)); //sprintf("fps %f\n", 1.0/frame_time); // clear the buffer glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); //since all calculations are being done in the fragment shader, all we draw is a surface. glUseProgram(program); glRects(-1, -1, 1, 1); // render font on top of this? if(show_fps != 0){ glUseProgram(0); glColor3f(0.0f, 0.0f, 0.0f); ftglRenderFont(font, text, FTGL_RENDER_ALL); glUseProgram(program); } // note: swap buffers also updates the input events for glfw glfwSwapBuffers(); } // clean up the font and exit. ftglDestroyFont(font); return 1; }