1 /*
2  * File:         view_config.c
3  *
4  * Description:  handles initializing, storing, etc some values related to
5  *               the views' appearance and how they behave
6  *
7  *
8  * This source code is part of kludge3d, and is released under the
9  * GNU General Public License.
10  *
11  *
12  */
13 
14 
15 #include <stdio.h>
16 #include <math.h>
17 #include <string.h>
18 #include <gtk/gtk.h>
19 
20 #include "view_config.h"
21 #include "editor_models.h"
22 #include "view.h"
23 #include "prefs.h"
24 #include "globals.h"
25 #include "glpreview.h"
26 #include "vector.h"
27 
28 
29 #ifdef MEMWATCH
30 #include "memwatch.h"
31 #endif
32 
33 /* GLOBALS **************************************************************/
34 
35 model_view_config view_config;
36 
37 GLfloat *cursor_glarray = NULL;		/* GL arrays describing */
38 GLfloat *vertex_glarray = NULL;		/* helper objects */
39 GLfloat *girder_glarray = NULL;
40 
41 
42 /* PROTOTYPES ***********************************************************/
43 
44 gboolean view_delete_glarrays( gpointer data ) ;
45 
46 
47 /* VIEW-CONFIG STUFF ******************************************************/
48 
49 /* initializes the view_config's members to reasonable defaults, and does
50 some other one-time-only initializations */
view_config_init(void)51 void view_config_init( void ) {
52 
53 	/* register the signals */
54 	g_signal_connect(
55 		notificationObj, "notify::exit",
56 		G_CALLBACK(view_delete_glarrays), NULL );
57 	g_signal_connect(
58 		notificationObj, "notify::preferences-changed",
59 		G_CALLBACK(view_retrieve_preferences), NULL );
60 
61 	view_retrieve_preferences();
62 
63 	view_config.fov = 90.0f;
64 	view_config.near_plane = 1.0f;
65 	view_config.far_plane = 200.0f;
66 
67 	view_config.grid_spacing = 1.0f;
68 
69 	view_config.vertices_visible = TRUE;
70 	view_config.grid_visible = TRUE;
71 	view_config.normals_visible = FALSE;
72 
73 	view_config.cursor_visible = FALSE;
74 
75 	view_config.polygon_mode = MP_SHOW_WIREFRAME;
76 	view_config.render_mode = MP_MODE_RENDER;
77 	view_config.triangle_order = MP_TRI_ORDER_CCW;
78 
79 	view_config.cursor_rad = 0.2;
80 	view_config.vertex_rad = 0.1;
81 	view_config.girder_rad = 0.07;
82 
83 	view_rebuild_glarrays( );
84 
85 }
86 
87 
view_retrieve_preferences(void)88 int view_retrieve_preferences( void ) {
89 	float *temp;
90 	float default_bg[] =		{ 0.35, 0.35, 0.35 };
91 	float default_grid[] =		{ 0.25, 0.25, 0.25 };
92 	float default_nmlvert[] =	{ 1.00, 1.00, 1.00 };
93 	float default_selvert[] =	{ 1.00, 0.00, 0.00 };
94 	float default_nmlpoly[] =	{ 0.80, 0.80, 0.80 };
95 	float default_selpoly[] =	{ 1.00, 0.00, 0.00 };
96 
97 	pref_get_string( "View::Note", "Changes to the background color will take effect next time the program is started." );
98 	temp = pref_get_vector( "View::Background Color", default_bg );
99 	vector_copy( view_config.bkgnd_color, temp );
100 	temp = pref_get_vector( "View::Grid Color", default_grid );
101 	vector_copy( view_config.grid_color, temp );
102 	temp = pref_get_vector( "View::Vertex Color", default_nmlvert );
103 	vector_copy( view_config.vertex_unsel_color, temp );
104 	temp = pref_get_vector( "View::Selected Vertex Color", default_selvert );
105 	vector_copy( view_config.vertex_sel_color, temp );
106 	temp = pref_get_vector( "View::Polygon Color", default_nmlpoly );
107 	vector_copy( view_config.poly_unsel_color, temp );
108 	temp = pref_get_vector( "View::Selected Polygon Color", default_selpoly );
109 	vector_copy( view_config.poly_sel_color, temp );
110 
111 	view_config.draw_verts_as_cubes =
112 		pref_get_bool( "View::Draw Vertices Using Cubes (slower)", FALSE );
113 
114 	g_signal_emit_by_name( notificationObj,
115 		"notify::model-appearance-changed", NULL );
116 
117 	return FALSE;
118 }
119 
120 
121 /* FUNCS RELATED TO THE HELPER OBJS *************************************/
122 
view_delete_glarrays(gpointer data)123 gboolean view_delete_glarrays( gpointer data ) {
124 
125 printf( "%s : freeing cursor objects\n", __FUNCTION__ );
126 	if( cursor_glarray ) {
127 		free( cursor_glarray );
128 		cursor_glarray = NULL;
129 	}
130 	if( vertex_glarray ) {
131 		free( vertex_glarray );
132 		vertex_glarray = NULL;
133 	}
134 	if( girder_glarray ) {
135 		free( girder_glarray );
136 		girder_glarray = NULL;
137 	}
138 
139 	return FALSE;
140 }
141 
142 
view_rebuild_glarrays(void)143 void view_rebuild_glarrays( void ) {
144 	/* rebuilds the glarrays for the vertices (little cubes) the
145 	"girders" (little rectangular prisms) and the cursor (diamond-shaped) */
146 
147 	int i;
148 
149 	if ( cursor_glarray != NULL )
150 		free( cursor_glarray );
151 	if ( vertex_glarray != NULL )
152 		free( vertex_glarray );
153 	if ( girder_glarray != NULL )
154 		free( girder_glarray );
155 
156 	if ( view_config.triangle_order == MP_TRI_ORDER_CW ) {
157 		cursor_glarray = ( GLfloat * ) malloc( sizeof( view_cursor_cw ) );
158 		memcpy( cursor_glarray, view_cursor_cw, sizeof( view_cursor_cw ) );
159 		vertex_glarray = ( GLfloat * ) malloc( sizeof( view_vertex_cw ) );
160 		memcpy( vertex_glarray, view_vertex_cw, sizeof( view_vertex_cw ) );
161 		girder_glarray = ( GLfloat * ) malloc( sizeof( view_girder_cw ) );
162 		memcpy( girder_glarray, view_girder_cw, sizeof( view_girder_cw ) );
163 	} else {
164 		cursor_glarray = ( GLfloat * ) malloc( sizeof( view_cursor_ccw ) );
165 		memcpy( cursor_glarray, view_cursor_ccw, sizeof( view_cursor_ccw ) );
166 		vertex_glarray = ( GLfloat * ) malloc( sizeof( view_vertex_ccw ) );
167 		memcpy( vertex_glarray, view_vertex_ccw, sizeof( view_vertex_ccw ) );
168 		girder_glarray = ( GLfloat * ) malloc( sizeof( view_girder_ccw ) );
169 		memcpy( girder_glarray, view_girder_ccw, sizeof( view_girder_ccw ) );
170 	}
171 
172 
173 	/* size the arrays to fit the view_config.* parameters */
174 	for ( i = 0; i < ( sizeof( view_cursor_ccw ) / sizeof( GLfloat ) ); i += 10 ) {
175 		cursor_glarray[ i + 7 ] *= view_config.cursor_rad;
176 		cursor_glarray[ i + 8 ] *= view_config.cursor_rad;
177 		cursor_glarray[ i + 9 ] *= view_config.cursor_rad;
178 	}
179 	for ( i = 0; i < ( sizeof( view_vertex_ccw ) / sizeof( GLfloat ) ); i += 6 ) {
180 		vertex_glarray[ i + 3 ] *= view_config.vertex_rad;
181 		vertex_glarray[ i + 4 ] *= view_config.vertex_rad;
182 		vertex_glarray[ i + 5 ] *= view_config.vertex_rad;
183 	}
184 	for ( i = 0; i < ( sizeof( view_girder_ccw ) / sizeof( GLfloat ) ); i += 6 ) {
185 		girder_glarray[ i + 3 ] *= view_config.girder_rad;
186 		girder_glarray[ i + 4 ] *= view_config.girder_rad;
187 	}
188 
189 }
190 
191 
192 
193 /* SETS *****************************************************************/
194 
view_set_rendermode(int mode)195 void view_set_rendermode( int mode ) {
196 
197 	view_config.polygon_mode = mode;
198 }
199 
200 
view_set_triangleorder(int order)201 void view_set_triangleorder( int order ) {
202 
203 	int i;
204 	view_config.triangle_order = order;
205 	for( i = 0; i < VIEW_3D + 1; i++ ) {
206 		glp_set_triangleorder( views[i] );
207 	}
208 	view_rebuild_glarrays(  );
209 }
210 
211 
view_set_vertexsize(float size)212 void view_set_vertexsize( float size ) {
213 
214 	view_config.vertex_rad = size;
215 	view_config.cursor_rad = size * 2.0;
216 	view_config.girder_rad = size * 0.7;
217 
218 	view_rebuild_glarrays(  );
219 }
220 
221 
view_set_fov(float fov)222 void view_set_fov( float fov ) {
223 
224 	view_config.fov = fov;
225 	view_fov_changed();
226 }
227 
228