Blender V2.61 - r43446

util_view.cpp

Go to the documentation of this file.
00001 /*
00002  * Copyright 2011, Blender Foundation.
00003  *
00004  * This program is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU General Public License
00006  * as published by the Free Software Foundation; either version 2
00007  * of the License, or (at your option) any later version.
00008  *
00009  * This program is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  * GNU General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software Foundation,
00016  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00017  */
00018 
00019 #include <stdio.h>
00020 #include <stdlib.h>
00021 #include <string.h>
00022 
00023 #include "util_opengl.h"
00024 #include "util_time.h"
00025 #include "util_view.h"
00026 
00027 #ifdef __APPLE__
00028 #include <GLUT/glut.h>
00029 #else
00030 #include <GL/glut.h>
00031 #endif
00032 
00033 CCL_NAMESPACE_BEGIN
00034 
00035 /* structs */
00036 
00037 struct View {
00038     ViewInitFunc initf;
00039     ViewExitFunc exitf;
00040     ViewResizeFunc resize;
00041     ViewDisplayFunc display;
00042     ViewKeyboardFunc keyboard;
00043 
00044     bool first_display;
00045     bool redraw;
00046 
00047     int width, height;
00048 } V;
00049 
00050 /* public */
00051 
00052 static void view_display_text(int x, int y, const char *text)
00053 {
00054     const char *c;
00055 
00056     glRasterPos3f(x, y, 0);
00057 
00058     for(c=text; *c != '\0'; c++)
00059         glutBitmapCharacter(GLUT_BITMAP_HELVETICA_10, *c);
00060 }
00061 
00062 void view_display_info(const char *info)
00063 {
00064     const int height = 20;
00065 
00066     glEnable(GL_BLEND);
00067     glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
00068     glColor4f(0.1f, 0.1f, 0.1f, 0.8f);
00069     glRectf(0.0f, V.height - height, V.width, V.height);
00070     glDisable(GL_BLEND);
00071 
00072     glColor3f(0.5f, 0.5f, 0.5f);
00073 
00074     view_display_text(10, 7 + V.height - height, info);
00075 
00076     glColor3f(1.0f, 1.0f, 1.0f);
00077 }
00078 
00079 static void view_display()
00080 {
00081     if(V.first_display) {
00082         if(V.initf) V.initf();
00083         if(V.exitf) atexit(V.exitf);
00084 
00085         V.first_display = false;
00086     }
00087 
00088     glClearColor(0.05f, 0.05f, 0.05f, 0.0f);
00089     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
00090 
00091     glMatrixMode(GL_PROJECTION);
00092     glLoadIdentity();
00093     gluOrtho2D(0, V.width, 0, V.height);
00094 
00095     glMatrixMode(GL_MODELVIEW);
00096     glLoadIdentity();
00097 
00098     glRasterPos3f(0, 0, 0);
00099 
00100     if(V.display)
00101         V.display();
00102 
00103     glutSwapBuffers();
00104 }
00105 
00106 static void view_reshape(int width, int height)
00107 {
00108     if(width <= 0 || height <= 0)
00109         return;
00110     
00111     V.width = width;
00112     V.height = height;
00113 
00114     glViewport(0, 0, width, height);
00115 
00116     glMatrixMode(GL_PROJECTION);
00117     glLoadIdentity();
00118 
00119     glMatrixMode(GL_MODELVIEW);
00120     glLoadIdentity();
00121 
00122     if(V.resize)
00123         V.resize(width, height);
00124 }
00125 
00126 static void view_keyboard(unsigned char key, int x, int y)
00127 {
00128     if(V.keyboard)
00129         V.keyboard(key);
00130 
00131     if(key == 'm')
00132         printf("mouse %d %d\n", x, y);
00133     if(key == 'q') {
00134         if(V.exitf) V.exitf();
00135         exit(0);
00136     }
00137 }
00138 
00139 void view_idle()
00140 {
00141     if(V.redraw) {
00142         V.redraw = false;
00143         glutPostRedisplay();
00144     }
00145 
00146     time_sleep(0.1f);
00147 }
00148 
00149 void view_main_loop(const char *title, int width, int height,
00150     ViewInitFunc initf, ViewExitFunc exitf,
00151     ViewResizeFunc resize, ViewDisplayFunc display,
00152     ViewKeyboardFunc keyboard)
00153 {
00154     const char *name = "app";
00155     char *argv = (char*)name;
00156     int argc = 1;
00157 
00158     memset(&V, 0, sizeof(V));
00159     V.width = width;
00160     V.height = height;
00161     V.first_display = true;
00162     V.redraw = false;
00163     V.initf = initf;
00164     V.exitf = exitf;
00165     V.resize = resize;
00166     V.display = display;
00167     V.keyboard = keyboard;
00168 
00169     glutInit(&argc, &argv);
00170     glutInitWindowSize(width, height);
00171     glutInitWindowPosition(0, 0);
00172     glutInitDisplayMode(GLUT_RGB|GLUT_DOUBLE|GLUT_DEPTH);
00173     glutCreateWindow(title);
00174 
00175 #ifndef __APPLE__
00176     glewInit();
00177 #endif
00178 
00179     view_reshape(width, height);
00180 
00181     glutDisplayFunc(view_display);
00182     glutIdleFunc(view_idle);
00183     glutReshapeFunc(view_reshape);
00184     glutKeyboardFunc(view_keyboard);
00185 
00186     glutMainLoop();
00187 }
00188 
00189 void view_redraw()
00190 {
00191     V.redraw = true;
00192 }
00193 
00194 CCL_NAMESPACE_END
00195