version 2.8
select.c
1 #include "zgl.h"
2 
3 int glRenderMode(int mode)
4 {
5  GLContext *c=gl_get_context();
6  int result=0;
7 
8  switch(c->render_mode) {
9  case GL_RENDER:
10  break;
11  case GL_SELECT:
12  if (c->select_overflow) {
13  result=-c->select_hits;
14  } else {
15  result=c->select_hits;
16  }
17  c->select_overflow=0;
18  c->select_ptr=c->select_buffer;
19  c->name_stack_size=0;
20  break;
21  default:
22  gl_assert(0);
23  }
24  switch(mode) {
25  case GL_RENDER:
26  c->render_mode=GL_RENDER;
27  break;
28  case GL_SELECT:
29  c->render_mode=GL_SELECT;
30  gl_assert( c->select_buffer != NULL);
31  c->select_ptr=c->select_buffer;
32  c->select_hits=0;
33  c->select_overflow=0;
34  c->select_hit=NULL;
35  break;
36  default:
37  gl_assert(0);
38  }
39  return result;
40 }
41 
42 void glSelectBuffer(int size,unsigned int *buf)
43 {
44  GLContext *c=gl_get_context();
45 
46  gl_assert(c->render_mode != GL_SELECT);
47 
48  c->select_buffer=buf;
49  c->select_size=size;
50 }
51 
52 
53 void glopInitNames(GLContext *c,GLParam *p)
54 {
55  (void)p;
56  if (c->render_mode == GL_SELECT) {
57  c->name_stack_size=0;
58  c->select_hit=NULL;
59  }
60 }
61 
62 void glopPushName(GLContext *c,GLParam *p)
63 {
64  if (c->render_mode == GL_SELECT) {
65  gl_assert(c->name_stack_size<MAX_NAME_STACK_DEPTH);
66  c->name_stack[c->name_stack_size++]=p[1].i;
67  c->select_hit=NULL;
68  }
69 }
70 
71 void glopPopName(GLContext *c,GLParam *p)
72 {
73  (void)p;
74  if (c->render_mode == GL_SELECT) {
75  gl_assert(c->name_stack_size>0);
76  c->name_stack_size--;
77  c->select_hit=NULL;
78  }
79 }
80 
81 void glopLoadName(GLContext *c,GLParam *p)
82 {
83  if (c->render_mode == GL_SELECT) {
84  gl_assert(c->name_stack_size>0);
85  c->name_stack[c->name_stack_size-1]=p[1].i;
86  c->select_hit=NULL;
87  }
88 }
89 
90 void gl_add_select(GLContext *c,unsigned int zmin,unsigned int zmax)
91 {
92  unsigned int *ptr;
93  int n,i;
94 
95  if (!c->select_overflow) {
96  if (c->select_hit==NULL) {
97  n=c->name_stack_size;
98  if ((c->select_ptr-c->select_buffer+3+n) >
99  c->select_size) {
100  c->select_overflow=1;
101  } else {
102  ptr=c->select_ptr;
103  c->select_hit=ptr;
104  *ptr++=c->name_stack_size;
105  *ptr++=zmin;
106  *ptr++=zmax;
107  for(i=0;i<n;i++) *ptr++=c->name_stack[i];
108  c->select_ptr=ptr;
109  c->select_hits++;
110  }
111  } else {
112  if (zmin<c->select_hit[1]) c->select_hit[1]=zmin;
113  if (zmax>c->select_hit[2]) c->select_hit[2]=zmax;
114  }
115  }
116 }