/[escript]/branches/diaplayground/cusplibrary/cusp/opengl/spy/detail/glut_2d_canvas.inl
ViewVC logotype

Contents of /branches/diaplayground/cusplibrary/cusp/opengl/spy/detail/glut_2d_canvas.inl

Parent Directory Parent Directory | Revision Log Revision Log


Revision 4955 - (show annotations)
Tue May 20 04:33:15 2014 UTC (6 years, 5 months ago) by caltinay
File size: 4777 byte(s)
added pristine copy of cusplibrary (apache license) to be used by ripley.

1 /*
2 * Copyright 2008-2013 Steven Dalton
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 /**
18 * @file glut_2d_canvas.cc
19 * Implement a simple 2d canvas using glut.
20 */
21
22 #pragma once
23
24 #include <cusp/opengl/spy/glut_2d_canvas.h>
25 #include <cusp/opengl/spy/gl_util.h>
26
27 namespace cusp
28 {
29 namespace opengl
30 {
31 namespace spy
32 {
33
34 /**
35 * Simple constructor that just takes a width, height, and title.
36 *
37 * @param w the width of the window
38 * @param h the height of the window
39 * @param title the window title
40 */
41 glut_2d_canvas::glut_2d_canvas(int w, int h, const char* title)
42 : mouse_state(STATE_NONE),
43 center_x(0.0f), center_y(0.0f),
44 trans_x(0.0f), trans_y(0.0f),
45 width(w), height(h),
46 natural_width((float)w), natural_height((float)h),
47 virtual_width((float)w), virtual_height((float)h),
48 zoom(1.0f),
49 mouse_begin_x(0), mouse_begin_y(0),
50 aspect(1.0f),
51 display_finished(true),
52 frame(0)
53 {
54 set_background_color(0.0f,0.0f,0.0f);
55 glutInitWindowSize(w,h);
56 glut_id = glutCreateWindow(title);
57 register_with_glut();
58 }
59
60 void glut_2d_canvas::draw()
61 {
62 }
63
64
65 void glut_2d_canvas::display()
66 {
67 if (display_finished) {
68 frame = 0;
69 glClearColor(background_color[0],background_color[1],background_color[2],0.0);
70 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
71 } else {
72 frame++;
73 }
74
75 display_finished = true;
76
77 glMatrixMode(GL_MODELVIEW);
78 glLoadIdentity();
79
80 glTranslatef(center_x, center_y, 0);
81 glScalef(zoom,zoom,1);
82 glTranslatef(-center_x, -center_y, 0);
83 glTranslatef(trans_x, trans_y,0);
84
85 draw();
86
87 glutSwapBuffers();
88
89 if (!display_finished) {
90 glutPostRedisplay();
91 }
92 }
93
94 void glut_2d_canvas::reshape(int w, int h)
95 {
96 width = w;
97 height = h;
98
99 glViewport(0, 0, w, h);
100 glMatrixMode(GL_PROJECTION);
101 glLoadIdentity();
102
103 float scale1 = (float)h/natural_height;
104 float scale2 = (float)w/natural_width;
105 float scalew, scaleh;
106
107 if (scale1 <= scale2){
108 scalew = scale2/scale1;
109 //scaleh = 1;
110 scaleh = aspect;
111 } else {
112 //scalew = 1;
113 scalew = 1/aspect;
114 scaleh = scale1/scale2;
115 }
116
117 float x1 = center_x - natural_width*scalew/2;
118 float x2 = center_x + natural_width*scalew/2;
119 float y1 = center_y - natural_height*scaleh/2;
120 float y2 = center_y + natural_height*scaleh/2;
121
122 glOrtho(x1, x2, y2, y1, -1.0, 1.0);
123 virtual_width = natural_width*scalew;
124 virtual_height = natural_height*scaleh;
125 }
126
127 void glut_2d_canvas::key(unsigned char key, int x, int y)
128 {
129 }
130
131 void glut_2d_canvas::special_key(int key, int x, int y)
132 {
133 }
134
135 void glut_2d_canvas::motion(int x, int y)
136 {
137 switch (mouse_state)
138 {
139 case STATE_MOVE:
140 trans_x += (x - mouse_begin_x)*virtual_width/(float)width/zoom;
141 trans_y += (y - mouse_begin_y)*virtual_height/(float)height/zoom;
142
143 mouse_begin_x = x;
144 mouse_begin_y = y;
145 break;
146 case STATE_ZOOM:
147 // normal zoom
148 if(mouse_begin_y > y)
149 zoom *= 1.0f/.95f;
150 if(mouse_begin_y < y)
151 zoom *= 0.95f;
152
153 mouse_begin_y = y;
154 break;
155 default :
156 break;
157 }
158
159 glutPostRedisplay();
160 }
161
162
163 void glut_2d_canvas::mouse_click(int button, int state, int x, int y)
164 {
165 int mod_state = glutGetModifiers();
166
167 switch (state) {
168 case GLUT_UP:
169 mouse_state = STATE_NONE;
170
171 /*switch (button) {
172 case GLUT_WHEEL_DOWN:
173 zoom *= 0.75f;
174 break;
175
176 case GLUT_WHEEL_UP:
177 zoom *= 1.0f/0.75f;
178 break;
179 }*/
180
181 break;
182
183
184 case GLUT_DOWN:
185 if (button == GLUT_LEFT_BUTTON && mod_state == GLUT_ACTIVE_SHIFT)
186 {
187 mouse_state = STATE_ZOOM;
188 }
189 else if (button == GLUT_LEFT_BUTTON)
190 {
191 mouse_state = STATE_MOVE;
192 }
193
194 break;
195 }
196
197 begin_mouse_click(x,y);
198
199 glutPostRedisplay();
200 }
201
202 } // end spy
203 } // end opengl
204 } // end cusp

  ViewVC Help
Powered by ViewVC 1.1.26