-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.c
292 lines (218 loc) · 9.9 KB
/
test.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
#include <stdio.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/cursorfont.h>
#include <string.h> // Include the string.h header for strlen
#include <math.h>
#include <stdlib.h>
#define voxel_width 10;
#define voxel_height 10;
#define voxel_length 10;
typedef struct voxel{
unsigned char one_x;
unsigned char one_y;
unsigned char one_z;
unsigned char two_x;
unsigned char two_y;
unsigned char two_z;
unsigned char thr_x;
unsigned char thr_y;
unsigned char thr_z;
unsigned char fou_x;
unsigned char fou_y;
unsigned char fou_z;
unsigned char fiv_x;
unsigned char fiv_y;
unsigned char fiv_z;
unsigned char six_x;
unsigned char six_y;
unsigned char six_z;
unsigned char sev_x;
unsigned char sev_y;
unsigned char sev_z;
unsigned char eig_x;
unsigned char eig_y;
unsigned char eig_z;
}voxel;
typedef struct camera{
int x;
int y;
int z;
unsigned char v;
unsigned char h;
signed char v_adj;
signed char h_adj_x;
signed char h_adj_y;
}camera;
double arctan(int numerator, int denominator) {
double x = (double)numerator / denominator;
return atan(x);
}
int main() {
Display *display;
display = XOpenDisplay(NULL);
Window root = DefaultRootWindow(display);
Window window = XCreateSimpleWindow(display, root, 0, 0, 800, 600, 0, 0, 0);
XWindowAttributes windowAttributes;
int screen = DefaultScreen(display);
XSelectInput(display, window, PointerMotionMask | StructureNotifyMask | ButtonPressMask | ButtonReleaseMask);
XMapWindow(display, window);
Pixmap blankBitmap;
XColor dummyColor;
char data[1] = {0};
blankBitmap = XCreateBitmapFromData(display, window, data, 1, 1);
Cursor invisibleCursor = XCreatePixmapCursor(display, blankBitmap, blankBitmap, &dummyColor, &dummyColor, 0, 0); // Create a blank (invisible) cursor
XDefineCursor(display, window, invisibleCursor); // Set the invisible cursor for the window
GC gc;
XGCValues values;
gc = XCreateGC(display, window, 0, &values);
XSetForeground(display, gc, WhitePixel(display, screen));
int voxel_x_offset = 100;
int voxel_y_offset = 0;
int voxel_z_offset = 100;
//initialize the jaunt
void *working_ptr = NULL;
working_ptr = malloc(sizeof(voxel));
((voxel*)working_ptr)->one_x = 0;
((voxel*)working_ptr)->one_y = 0;
((voxel*)working_ptr)->one_z = 0;
((voxel*)working_ptr)->two_x = voxel_width;
((voxel*)working_ptr)->two_y = 0;
((voxel*)working_ptr)->two_z = 0;
((voxel*)working_ptr)->thr_x = voxel_width;
((voxel*)working_ptr)->thr_y = 0;
((voxel*)working_ptr)->thr_z = voxel_height;
((voxel*)working_ptr)->fou_x = 0;
((voxel*)working_ptr)->fou_y = 0;
((voxel*)working_ptr)->fou_z = voxel_height;
((voxel*)working_ptr)->fiv_x = 0;
((voxel*)working_ptr)->fiv_y = voxel_length;
((voxel*)working_ptr)->fiv_z = 0;
((voxel*)working_ptr)->six_x = voxel_width;
((voxel*)working_ptr)->six_y = voxel_length;
((voxel*)working_ptr)->six_z = 0;
((voxel*)working_ptr)->sev_x = voxel_width;
((voxel*)working_ptr)->sev_y = voxel_length;
((voxel*)working_ptr)->sev_z = voxel_height;
((voxel*)working_ptr)->eig_x = 0;
((voxel*)working_ptr)->eig_y = voxel_length;
((voxel*)working_ptr)->eig_z = voxel_height;
void * camera_ptr = NULL;
camera_ptr = malloc(sizeof(camera));
((camera*)camera_ptr)->x = 0;
((camera*)camera_ptr)->y = 0;
((camera*)camera_ptr)->z = 0;
((camera*)camera_ptr)->v = 0;
((camera*)camera_ptr)->h = 0;
((camera*)camera_ptr)->v_adj = 0;
((camera*)camera_ptr)->h_adj_x = 0;
((camera*)camera_ptr)->h_adj_y = 0;
int windowWidth = 0;
int windowHeight = 0;
int h_angle = 0;
int v_angle = 0;
int arrowVertexX;
int arrowVertexY;
while (1) {
XEvent event;
XNextEvent(display, &event);
// Grab the keyboard input focus
XGrabKeyboard(display, root, False, GrabModeAsync, GrabModeAsync, CurrentTime);
int x = event.xmotion.x;
int y = event.xmotion.y;
XGetWindowAttributes(display, window, &windowAttributes);
windowWidth = windowAttributes.width;
windowHeight = windowAttributes.height;
h_angle = 255 * x / windowWidth;
v_angle = 255 * y / windowHeight;
((camera*)camera_ptr)->v = h_angle;
((camera*)camera_ptr)->h = v_angle;
((camera*)camera_ptr)->v_adj = abs(v_angle - 127);
((camera*)camera_ptr)->h_adj_x = abs(h_angle - 127);
((camera*)camera_ptr)->h_adj_y = 128 - abs(h_angle - 127);
switch (event.type) {
case MotionNotify: {
XClearWindow(display, window);
//front face top
XDrawLine(display,window,gc,((voxel*)working_ptr)->one_x+voxel_x_offset,((voxel*)working_ptr)->one_z+voxel_z_offset,((voxel*)working_ptr)->two_x+voxel_x_offset,((voxel*)working_ptr)->two_z+voxel_z_offset);
//front face bottom
//XDrawLine(display,window,gc,((voxel*)working_ptr)->fou_x+voxel_x_offset,((voxel*)working_ptr)->fou_z+voxel_z_offset,((voxel*)working_ptr)->thr_x+voxel_x_offset,((voxel*)working_ptr)->thr_z+voxel_z_offset);
//front face left
XDrawLine(display,window,gc,((voxel*)working_ptr)->one_x+voxel_x_offset,((voxel*)working_ptr)->one_z+voxel_z_offset,((voxel*)working_ptr)->fou_x+voxel_x_offset,((voxel*)working_ptr)->fou_z+voxel_z_offset);
//front face right
//XDrawLine(display,window,gc,((voxel*)working_ptr)->two_x+voxel_x_offset,((voxel*)working_ptr)->two_z+voxel_z_offset,((voxel*)working_ptr)->thr_x+voxel_x_offset,((voxel*)working_ptr)->thr_z+voxel_z_offset);
//origin point in point 1.
int separation_x = abs(((camera*)camera_ptr)->x - (((voxel*)working_ptr)->one_x+voxel_x_offset));
int separation_z = abs(((camera*)camera_ptr)->y - (((voxel*)working_ptr)->one_z+voxel_z_offset));
double at = arctan(separation_x, separation_z);
arrowVertexX = ((camera*)camera_ptr)->x+ (((camera*)camera_ptr)->v-128);
arrowVertexY = ((camera*)camera_ptr)->y+(((camera*)camera_ptr)->h-128);
//XDrawPoint(display, window, gc, ((camera*)camera_ptr)->x, ((camera*)camera_ptr)->y);
//draw direction arrow
XDrawLine(display, window, gc, ((camera*)camera_ptr)->x, ((camera*)camera_ptr)->y, arrowVertexX, arrowVertexY);
if(((camera*)camera_ptr)->y> arrowVertexY){
XDrawLine(display, window, gc, arrowVertexX-5, arrowVertexY-5, arrowVertexX+5, arrowVertexY-5);
}else{
XDrawLine(display, window, gc, arrowVertexX-5, arrowVertexY+5, arrowVertexX+5, arrowVertexY+5);
}
if(((camera*)camera_ptr)->x> arrowVertexX){
XDrawLine(display, window, gc, arrowVertexX-5, arrowVertexY+5, arrowVertexX-5, arrowVertexY-5);
}else{
XDrawLine(display, window, gc, arrowVertexX+5, arrowVertexY+5, arrowVertexX+5, arrowVertexY-5);
}
//XDrawRectangle(display, window, gc, 2000 - x, 1000 - y, width, height);
//XDrawLine(display,window,gc,0,0,2000 - x,1000 - y);
//XDrawLine(display,window,gc,2510,0,2000 - x+width,1000 - y);
//XDrawLine(display,window,gc,0,1038,2000 - x,1000 - y+height);
//XDrawLine(display,window,gc,2510,1038,2000 - x+width,1000 - y+height);
//printf("Arrowhead coordinates: (%d, %d)\n", arrowVertexX, arrowVertexY);
printf("Distance to origin point: (%d, %d)\n", separation_x, separation_z);
//printf("Cursor coordinates: (%d, %d)\n", ((camera*)camera_ptr)->h_adj_x-64, ((camera*)camera_ptr)->h_adj_y-64);
//printf("Looking At: (%d, %d)\n", h_angle, v_angle);
printf("camera angle x, y: (%d, %d, %f)\n", ((camera*)camera_ptr)->h_adj_x,((camera*)camera_ptr)->h_adj_y, at);
float viewMatrix[16];
XFlush(display);
break;
}
case KeyPress: {
char buffer[10];
KeySym key;
XLookupString(&event.xkey, buffer, sizeof(buffer), &key, NULL);
if (key == XK_w) {
((camera*)camera_ptr)->x = arrowVertexX;
((camera*)camera_ptr)->y = arrowVertexY;
break;
}else if (key == XK_a) {
//printf("The 'a' key was pressed!\n");
break;
} else if (key == XK_s) {
((camera*)camera_ptr)->x = ((camera*)camera_ptr)->x + (((camera*)camera_ptr)->x-arrowVertexX);
((camera*)camera_ptr)->y = ((camera*)camera_ptr)->y + (((camera*)camera_ptr)->y-arrowVertexY);
break;
} else if (key == XK_d) {
//printf("The 'd' key was pressed!\n");
break;
} else if (key == XK_space) {
//printf("The space key was pressed!\n");
break;
} else if (key == XK_Shift_L) {
//printf("The left shift key was pressed!\n");
break;
}
break;
}
case KeyRelease: {
break;
}
case MapNotify:{
break;
}
case ConfigureNotify: {
break;
}
}
}
XFreeCursor(display, invisibleCursor);
XCloseDisplay(display);
return 0;
}