-
Notifications
You must be signed in to change notification settings - Fork 1
/
click.c
80 lines (63 loc) · 1.5 KB
/
click.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
/* - OpenGL Libraries - */
#ifndef __APPLE__
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>
#endif
#include "click.h"
#include "render.h"
#define BUFFER_SIZE 512
Block*
processHits (GLint hits, GLuint buffer[], int *face)
{
GLuint depth;
GLint choose, bIndex;
int index;
Block *block;
if (hits == 0)
return NULL;
depth = (GLuint)buffer[1];
choose = (GLint)buffer[3];
for (index = 1; index < hits; index++)
{
if (buffer[index*4+1] < depth)
{
choose = (GLint)buffer[index*4+3];
depth = (GLuint)buffer[index*4+1];
}
}
bIndex = DECODE_INDEX(choose);
if (face != NULL)
*face = DECODE_FACE(choose);
// TODO: we can do this by embedding the pointer in the name
// instead of naively searching through all blocks
Q_FOREACH(block, getBlockList(), blockLink)
{
if (block->id == bIndex)
return block;
}
Q_ENDFOREACH(getBlockList());
return NULL;
}
Block *
click(int x, int y, int *face)
{
GLuint buffer[BUFFER_SIZE];
GLint viewport[4];
GLint hits;
glGetIntegerv(GL_VIEWPORT, viewport);
glSelectBuffer(BUFFER_SIZE, buffer);
glRenderMode(GL_SELECT);
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
gluPickMatrix((GLdouble) x, (GLdouble) (viewport[3]-y), 1.0f, 1.0f, viewport);
gluPerspective(FOV, (GLfloat) (WINDOW_WIDTH/WINDOW_HEIGHT), Z_NEAR, Z_FAR);
glMatrixMode(GL_MODELVIEW);
redraw_world();
glMatrixMode(GL_PROJECTION);
glPopMatrix();
glMatrixMode(GL_MODELVIEW);
hits=glRenderMode(GL_RENDER);
return processHits(hits, buffer, face);
}