⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 3ddeskd.cpp

📁 一个3D桌面的实现源码
💻 CPP
📖 第 1 页 / 共 4 页
字号:
    glEnable(GL_BLEND);    glTexEnvi(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,GL_MODULATE);    glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);    glBindTexture(GL_TEXTURE_2D, digit_texture );    glBegin(GL_QUADS);        glTexCoord2f (x1, y2);    glVertex3f(-w,-w,0);        glTexCoord2f (x2, y2);    glVertex3f(w,-w,0);        glTexCoord2f (x2, y1);    glVertex3f(w,w,0);        glTexCoord2f (x1, y1);    glVertex3f(-w,w,0);        glEnd();    // Done with this special projection matrix.  Throw it away.    glPopMatrix();}static void draw_digit (int d){    // draw a digit in the middle    draw_digit (d,                 GLWin.get_width() / 2,                 GLWin.get_height() - (cfg->options->digit_size / 2) );}// this is where the digit is located in the texturefloat digit_texture_coords[] = { 0, 0, .25, 0, .5, 0, .75, 0, 0, .25, .25, .25, .5, .25, .75, .25,   0, .5, .25, .5 };static voiddraw_digit (int d, float x_trans, float y_trans){    float x1, y1, x2, y2;    if (d >= 10) {        msgout (DEBUG, "^^^^^^^^ above 10\n");        x1 = 0.25;  // show zero        y1 = 0.5;    } else {        x1 = digit_texture_coords[(d-1) * 2];        y1 = digit_texture_coords[(d-1) * 2 + 1];    }        x2 = x1 + 0.25;    y2 = y1 + 0.25;    //msgout (DEBUG, "oooooooo d=%d  x1=%f  y1=%f  x2=%f  y2=%f\n",    //        d, x1, y1, x2, y2);    float w = cfg->options->digit_size/2.0;    // But we like our current view too; so we save it here.    glPushMatrix();        // Now we set up a new projection for the text.    glLoadIdentity();    glOrtho(0,GLWin.get_width(),0,GLWin.get_height(),-1.0,1.0);    glTranslatef(x_trans, y_trans, 0.0f);    set_color (cfg->options->digit_color, digit_visibility);    glEnable(GL_BLEND);    glTexEnvi(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,GL_MODULATE);    glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);    glBindTexture(GL_TEXTURE_2D, digit_texture );    glBegin(GL_QUADS);        glTexCoord2f (x1, y2);    glVertex3f(-w,-w,0);        glTexCoord2f (x2, y2);    glVertex3f(w,-w,0);        glTexCoord2f (x2, y1);    glVertex3f(w,w,0);        glTexCoord2f (x1, y1);    glVertex3f(-w,w,0);        glEnd();    // Done with this special projection matrix.  Throw it away.    glPopMatrix();}// ------// Routine which actually does the drawingstatic voidrender_scene(void){    //char buf[80]; // For our strings.    long current_time;    // Enables, disables or otherwise adjusts as     // appropriate for our current settings.    if (Texture_On)        glEnable(GL_TEXTURE_2D);    else        glDisable(GL_TEXTURE_2D);    if (Light_On)         glEnable(GL_LIGHTING);    else         glDisable(GL_LIGHTING);    if (Alpha_Add)        //glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);        //glBlendFunc(GL_ONE, GL_SRC_ALPHA);         //glBlendFunc(GL_ONE, GL_CONSTANT_ALPHA);                 // this one is good with NO color material, disable depth test, blend on        glBlendFunc(GL_SRC_ALPHA, GL_ONE); // GOOD!    else        glBlendFunc(GL_ONE, GL_ONE_MINUS_DST_ALPHA);        //glBlendFunc(GL_ONE_MINUS_DST_ALPHA,GL_SRC_ALPHA);     // If we're blending, we don't want z-buffering.    if (Blend_On) {        //glEnable(GL_DEPTH_TEST);         glDisable(GL_DEPTH_TEST);         glEnable(GL_BLEND);    } else {         glEnable(GL_DEPTH_TEST);         glDisable(GL_BLEND);         //glEnable(GL_DEPTH_TEST);     }    glTexEnvi(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,TexModes[Curr_TexMode]);    // only effects gl_blend    //float text_env_color[] = { .1, .1, .1, .1 };    //glTexEnvfv(GL_TEXTURE_ENV,GL_TEXTURE_ENV_COLOR, text_env_color);    // Need to manipulate the ModelView matrix to move our model around.    glMatrixMode(GL_MODELVIEW);    // Reset to 0,0,0; no rotation, no scaling.    glLoadIdentity();     current_time = get_milli_time();    // this gives us semi-precise/consistent timing of the movements    //     if (current_time - last_time > cfg->options->animation_speed) {        if (state == STATE_EXIT) {            //if (!cfg->options->entry_exit_movement || faces->is_exit_movement_done()) {                        if (faces->is_exit_movement_done()) {                // quit (the z offset was changed to match the screen)                end_X_event_loop = True;                em.trigger_event (EXIT_MOVEMENT_STOP);                return;                            } else {                faces->entry_exit_move();            }        } else if (state == STATE_ENTRY) {            if ( faces->is_entry_movement_done() ) {                state = STATE_NORMAL;                em.trigger_event (ENTRY_MOVEMENT_STOP);            } else {                faces->entry_exit_move();            }        }         // change state to normal from goto if done goto        if (state == STATE_GOTO) {            if ( !faces->in_goto() ) {                state = STATE_NORMAL;                em.trigger_event (GOTO_FACE_COMPLETE);            }        }                if (cfg->options->use_breathing) {            if (breathing.at_destination()) {                breathing.init(&ambient_value, 1.7, 3.0, 200, Movement::MOVE_TYPE_SINE);            } else {                breathing.change_a_bit();                light_ambient2[0] = light_ambient2[1] = light_ambient2[2] = ambient_value;            }        }        if (!digit_vis.at_destination())            digit_vis.change_a_bit();        faces->do_movement();        last_time = get_milli_time();    } // end animation step     // FIXME: use event to accomplish this?    // after some seconds start some fun ;)    if (cfg->options->random_fun_delay &&        current_time - last_keypress > cfg->options->random_fun_delay * 1000)     {        if ((unsigned long)current_time >= (unsigned long)next_action) {            //msgout(DEBUG, "having some fun...\n");            faces->goto_random_face();                        next_action = current_time + get_randomi(350, 600);        }    }    glLoadIdentity();    // adjust the faces the necessary amount    faces->do_position_GL();    // Clear the color and depth buffers.    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);    faces->render();    //glPushMatrix();    glLoadIdentity();    glColor3f(0.0, 0.0, 1.0);    glPointSize(5.0);    glBegin(GL_POINTS);    glVertex4fv(light_position2);    glVertex4fv(light_position);    glEnd();    // lighting (light2 is for ambient only)    glLightfv(GL_LIGHT2, GL_POSITION, light_position2);    glLightfv(GL_LIGHT2, GL_AMBIENT,  light_ambient2);    //glLightfv(GL_LIGHT2, GL_DIFFUSE,  Light_Diffuse2);     glEnable (GL_LIGHT2);    // lighting (light1 is diffuse only)    glLightfv(GL_LIGHT1, GL_POSITION, light_position);    //glLightfv(GL_LIGHT1, GL_AMBIENT,  Light_Ambient);    //glLightfv(GL_LIGHT1, GL_DIFFUSE,  light_diffuse);    //float halfs[] = { .50, .50, .50, .50 };    float ones[] = { 1.0, 1.0, 1.0, 1.0 };    glLightfv(GL_LIGHT1, GL_DIFFUSE,  ones);    glLightfv(GL_LIGHT1, GL_SPECULAR,  ones);    glEnable (GL_LIGHT1);    //glPopMatrix();    // We need to change the projection matrix for the text rendering.      glMatrixMode(GL_PROJECTION);    // But we like our current view too; so we save it here.    glPushMatrix();    // Now we set up a new projection for the text.    glLoadIdentity();    glOrtho(0,GLWin.get_width(),0,GLWin.get_height(),-1.0,1.0);    // Lit or textured text looks awful.    glDisable(GL_TEXTURE_2D);    glDisable(GL_LIGHTING);    // We don't want depth-testing either.    glDisable(GL_DEPTH_TEST);     // Now we want to render the calulated FPS at the top.       // To ease, simply translate up.  Note we're working in screen    // pixels in this projection.   #if 0    if (cfg->options->show_fps) {        glTranslatef(6.0f, GLWin.get_height() - 14,0.0f);                // Make sure we can read the FPS section by first placing a         // dark, mostly opaque backdrop rectangle.        glColor4f(0.2,0.2,0.2,0.75);                glBegin(GL_QUADS);        glVertex3f(  0.0f, -2.0f, 0.0f);        glVertex3f(  0.0f, 12.0f, 0.0f);        glVertex3f(140.0f, 12.0f, 0.0f);        glVertex3f(140.0f, -2.0f, 0.0f);        glEnd();                glColor4f(0.9,0.2,0.2,.75);        char buf[50];        sprintf(buf,"FPS: %f F: %2d", FrameRate, FrameCount);        glRasterPos2i(6,0);        ourPrintString(GLUT_BITMAP_HELVETICA_12,buf);    }#endif // if 0    // Done with this special projection matrix.  Throw it away.    glPopMatrix();    if (cfg->options->show_digits) {        // We need to change the projection matrix for the text rendering.          glMatrixMode(GL_PROJECTION);        // Lit or textured text looks awful.        glEnable(GL_TEXTURE_2D);        glDisable(GL_LIGHTING);        // We don't want depth-testing either.        glDisable(GL_DEPTH_TEST);         // Now we want to render the calulated FPS at the top.           // To ease, simply translate up.  Note we're working in screen        // pixels in this projection.           if (faces->get_number_of_columns() > 1 &&            faces->get_number_of_rows() > 1)         {            draw_digits (faces->get_current_row() + 1,                         faces->get_current_column() + 1);        } else {            int n = 1;            if (faces->get_number_of_columns() > 1)                n = faces->get_current_column() + 1;            else                n = faces->get_current_row() + 1;            draw_digit (n);        }#if 0  // use a string rather then the digit        // FIXME: do this if the digit texture didn't load        char buf[50];        // use N x N if both rows and columns otherwise use juse N        // (representing columns or rows depending on which is being        // used)        if (faces->get_number_of_columns() > 1 &&            faces->get_number_of_rows() > 1)         {            sprintf(buf,"%d x %d",                     faces->get_current_row() + 1,                    faces->get_current_column() + 1);        } else {            int n = 1;            if (faces->get_number_of_columns() > 1)                n = faces->get_current_column() + 1;            else                n = faces->get_current_row() + 1;            sprintf(buf,"%d", n);        }                glRasterPos2i(6,0);        ourPrintString(GLUT_BITMAP_TIMES_ROMAN_24,buf);#endif            }    // All done drawing.  Let's show it.    glXSwapBuffers(GLWin.get_display(), GLWin.get_window());    // And collect our statistics.    if (cfg->options->show_fps)        calc_fps();    last_renderscene_time = get_milli_time();} // END render_scenestatic voidmouse_pressed(unsigned int button){    last_keypress = get_milli_time();    //msgout (DEBUG, "button pressed %d - 0x%x\n", button, button);    switch (button) {    case 1: // left    case 5: // mouse wheel down        faces->goto_left();        break;    case 3: // right    case 4: // mouse wheel up        faces->goto_right();        break;    case 2: // middle        begin_exit();        break;    }} // END mouse_pressed// ------// Callback function called when a normal key is pressed.static voidkey_pressed(KeySym key){    static KeySym last_key_pressed = 0;    if (cfg->disable_keys_in_goto &&        goto_on_flag)        return;    last_keypress = get_milli_time();    next_action = 0;    switch (key) {    case 130: case 98: // B - Blending.        Blend_On = Blend_On ? 0 : 1;         if (!Blend_On)            glDisable(GL_BLEND);         else            glEnable(GL_BLEND);        break;    case 108: case 76:  // L - Lighting        Light_On = Light_On ? 0 : 1;         break;    case 109: case 77:  // M - Mode of Blending        if ( ++ Curr_TexMode > 3 )            Curr_TexMode=0;        glTexEnvi(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,TexModes[Curr_TexMode]);        msgout (DEBUG, "glTexEnv mode=%s (%d)\n", TexModesStr[Curr_TexMode],                TexModes[Curr_TexMode]);        break;    case 116: case 84: // T - Texturing.        Texture_On = Texture_On ? 0 : 1;         break;    case 97: case 65:  // A - Alpha-blending hack.        Alpha_Add = Alpha_Add ? 0 : 1;         break;    case 114: case 82: // R - random        faces->goto_random_face();        break;#if 1 // for debugging best light position...    case 119: // w        light_position[0] -= 0.5;        msgout (DEBUG, "light x at %f\n", light_position[0]);        break;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -