Exemple #1
0
static int cdfont(cdCtxCanvas *ctxcanvas, const char *type_face, int style, int size)
{
  char filename[10240];

  /* try the pre-defined names and pre-defined style suffix */
  if (!cdGetFontFileNameDefault(type_face, style, filename))
  {
    /* try to find the file in the native system */
    if (!cdGetFontFileNameSystem(type_face, style, filename))
    {
      /* try the type_face as file name,
         here assume type_face is a full path */
      strcpy(filename, type_face);
    }
  }

  ctxcanvas->font = ftglCreateTextureFont(filename);
  if (!ctxcanvas->font)
    return 0;

  if (size < 0)
    size = cdGetFontSizePoints(ctxcanvas->canvas, size);

  /* FTGL: One point in pixel space maps to 1 unit in opengl space, 
     so a glyph that is 18 points high should be 18.0 units high. */
  /* CD: that means 1 point is actually being mapped to 1 pixel */
  size = cdGetFontSizePixels(ctxcanvas->canvas, size);
  ftglSetFontFaceSize(ctxcanvas->font, size, (int)(ctxcanvas->canvas->xres*25.4));
  ftglSetFontCharMap(ctxcanvas->font, ft_encoding_unicode);

  return 1;
}
Exemple #2
0
int font_new(lua_State *L, const char *path, const char *name) {
    FTGLfont *ftgl_font = ftglCreateTextureFont(path);
    if (!ftgl_font)
        return luaL_error(L, "cannot load font file %s", path);

    ftglSetFontDisplayList(ftgl_font, 1);
    ftglSetFontFaceSize(ftgl_font, SCALE, SCALE);
    ftglSetFontCharMap(ftgl_font, ft_encoding_unicode);

    font_t *font = push_font(L);
    font->font = ftgl_font;
    return 1;
}
Exemple #3
0
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
}
Exemple #4
0
void draw_background(void) {

    glClearColor(0,0,0,0);
    glClear(GL_COLOR_BUFFER_BIT);

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(0,1,1,0,-1,1);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

    glBegin(GL_LINES);
    glColor4f(1,1,1,1);
    glVertex2f(0,0.1);
    glVertex2f(1,0.1);

    size_t num_items= sizeof(menu_items)/sizeof(*menu_items);
    float spacing = 1.0 / num_items;

    for (size_t item=0; item<num_items; item++)
    {
        glVertex2f((item*spacing),0);
        glVertex2f((item*spacing),0.1);
    }

    glEnd();

    /* Set the font size and render a small text. */
    ftglSetFontFaceSize(font, 25, 12);

    for (size_t item=0; item<num_items; item++)
    {
        if ((int)item == current_screen)
            glColor4f(1,0,0,1);
        else
            glColor4f(1,1,1,1);

        glPrintf(0.009+(item*spacing),0.09,"%s",menu_items[item].label);
    }
}
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 = &parameters[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;
}