Blender  V2.93
BKE_ccg.h
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  *
16  * The Original Code is Copyright (C) 2012 by Nicholas Bishop.
17  * All rights reserved.
18  */
19 
20 #pragma once
21 
26 /* defines BLI_INLINE */
27 #include "BLI_compiler_compat.h"
28 
29 /* declares fprintf() and abort(), needed for BLI_assert */
30 #include <stdio.h>
31 #include <stdlib.h>
32 
33 #ifdef __cplusplus
34 extern "C" {
35 #endif
36 
37 struct CCGSubSurf;
38 
39 /* Each CCGElem is CCGSubSurf's representation of a subdivided
40  * vertex. All CCGElems in a particular CCGSubSurf have the same
41  * layout, but the layout can vary from one CCGSubSurf to another. For
42  * this reason, CCGElem is presented as an opaque pointer, and
43  * elements should always be accompanied by a CCGKey, which provides
44  * the necessary offsets to access components of a CCGElem.
45  */
46 typedef struct CCGElem CCGElem;
47 
48 typedef struct CCGKey {
49  int level;
50 
51  /* number of bytes in each element (one float per layer, plus
52  * three floats for normals if enabled) */
53  int elem_size;
54 
55  /* number of elements along each side of grid */
56  int grid_size;
57  /* number of elements in the grid (grid size squared) */
58  int grid_area;
59  /* number of bytes in each grid (grid_area * elem_size) */
61 
62  /* currently always the last three floats, unless normals are
63  * disabled */
65 
66  /* offset in bytes of mask value; only valid if 'has_mask' is
67  * true */
69 
71  int has_mask;
73 
74 /* initialize 'key' at the specified level */
75 void CCG_key(CCGKey *key, const struct CCGSubSurf *ss, int level);
76 void CCG_key_top_level(CCGKey *key, const struct CCGSubSurf *ss);
77 
78 /* get a pointer to the coordinate, normal, or mask components */
79 BLI_INLINE float *CCG_elem_co(const CCGKey *key, CCGElem *elem);
80 BLI_INLINE float *CCG_elem_no(const CCGKey *key, CCGElem *elem);
81 BLI_INLINE float *CCG_elem_mask(const CCGKey *key, CCGElem *elem);
82 
83 /* get the element at 'offset' in an array */
84 BLI_INLINE CCGElem *CCG_elem_offset(const CCGKey *key, CCGElem *elem, int offset);
85 
86 /* get the element at coordinate (x,y) in a face-grid array */
87 BLI_INLINE CCGElem *CCG_grid_elem(const CCGKey *key, CCGElem *elem, int x, int y);
88 
89 /* combinations of above functions */
90 BLI_INLINE float *CCG_grid_elem_co(const CCGKey *key, CCGElem *elem, int x, int y);
91 BLI_INLINE float *CCG_grid_elem_no(const CCGKey *key, CCGElem *elem, int x, int y);
92 BLI_INLINE float *CCG_grid_elem_mask(const CCGKey *key, CCGElem *elem, int x, int y);
93 BLI_INLINE float *CCG_elem_offset_co(const CCGKey *key, CCGElem *elem, int offset);
94 BLI_INLINE float *CCG_elem_offset_no(const CCGKey *key, CCGElem *elem, int offset);
95 BLI_INLINE float *CCG_elem_offset_mask(const CCGKey *key, CCGElem *elem, int offset);
96 
97 /* for iteration, get a pointer to the next element in an array */
98 BLI_INLINE CCGElem *CCG_elem_next(const CCGKey *key, CCGElem *elem);
99 
100 /* inline definitions follow */
101 
102 BLI_INLINE float *CCG_elem_co(const CCGKey *UNUSED(key), CCGElem *elem)
103 {
104  return (float *)elem;
105 }
106 
107 BLI_INLINE float *CCG_elem_no(const CCGKey *key, CCGElem *elem)
108 {
109  BLI_assert(key->has_normals);
110  return (float *)((char *)elem + key->normal_offset);
111 }
112 
113 BLI_INLINE float *CCG_elem_mask(const CCGKey *key, CCGElem *elem)
114 {
115  BLI_assert(key->has_mask);
116  return (float *)((char *)elem + (key->mask_offset));
117 }
118 
119 BLI_INLINE CCGElem *CCG_elem_offset(const CCGKey *key, CCGElem *elem, int offset)
120 {
121  return (CCGElem *)(((char *)elem) + key->elem_size * offset);
122 }
123 
124 BLI_INLINE CCGElem *CCG_grid_elem(const CCGKey *key, CCGElem *elem, int x, int y)
125 {
126  // BLI_assert(x < key->grid_size && y < key->grid_size);
127  return CCG_elem_offset(key, elem, (y * key->grid_size + x));
128 }
129 
130 BLI_INLINE float *CCG_grid_elem_co(const CCGKey *key, CCGElem *elem, int x, int y)
131 {
132  return CCG_elem_co(key, CCG_grid_elem(key, elem, x, y));
133 }
134 
135 BLI_INLINE float *CCG_grid_elem_no(const CCGKey *key, CCGElem *elem, int x, int y)
136 {
137  return CCG_elem_no(key, CCG_grid_elem(key, elem, x, y));
138 }
139 
140 BLI_INLINE float *CCG_grid_elem_mask(const CCGKey *key, CCGElem *elem, int x, int y)
141 {
142  return CCG_elem_mask(key, CCG_grid_elem(key, elem, x, y));
143 }
144 
145 BLI_INLINE float *CCG_elem_offset_co(const CCGKey *key, CCGElem *elem, int offset)
146 {
147  return CCG_elem_co(key, CCG_elem_offset(key, elem, offset));
148 }
149 
150 BLI_INLINE float *CCG_elem_offset_no(const CCGKey *key, CCGElem *elem, int offset)
151 {
152  return CCG_elem_no(key, CCG_elem_offset(key, elem, offset));
153 }
154 
155 BLI_INLINE float *CCG_elem_offset_mask(const CCGKey *key, CCGElem *elem, int offset)
156 {
157  return CCG_elem_mask(key, CCG_elem_offset(key, elem, offset));
158 }
159 
161 {
162  return CCG_elem_offset(key, elem, 1);
163 }
164 
165 #ifdef __cplusplus
166 }
167 #endif
void CCG_key_top_level(CCGKey *key, const struct CCGSubSurf *ss)
void CCG_key(CCGKey *key, const struct CCGSubSurf *ss, int level)
BLI_INLINE CCGElem * CCG_elem_next(const CCGKey *key, CCGElem *elem)
Definition: BKE_ccg.h:160
BLI_INLINE CCGElem * CCG_grid_elem(const CCGKey *key, CCGElem *elem, int x, int y)
Definition: BKE_ccg.h:124
BLI_INLINE float * CCG_grid_elem_co(const CCGKey *key, CCGElem *elem, int x, int y)
Definition: BKE_ccg.h:130
BLI_INLINE float * CCG_elem_mask(const CCGKey *key, CCGElem *elem)
Definition: BKE_ccg.h:113
struct CCGKey CCGKey
BLI_INLINE CCGElem * CCG_elem_offset(const CCGKey *key, CCGElem *elem, int offset)
Definition: BKE_ccg.h:119
BLI_INLINE float * CCG_grid_elem_no(const CCGKey *key, CCGElem *elem, int x, int y)
Definition: BKE_ccg.h:135
BLI_INLINE float * CCG_elem_no(const CCGKey *key, CCGElem *elem)
Definition: BKE_ccg.h:107
struct CCGElem CCGElem
Definition: BKE_ccg.h:46
BLI_INLINE float * CCG_elem_offset_co(const CCGKey *key, CCGElem *elem, int offset)
Definition: BKE_ccg.h:145
BLI_INLINE float * CCG_grid_elem_mask(const CCGKey *key, CCGElem *elem, int x, int y)
Definition: BKE_ccg.h:140
BLI_INLINE float * CCG_elem_offset_no(const CCGKey *key, CCGElem *elem, int offset)
Definition: BKE_ccg.h:150
BLI_INLINE float * CCG_elem_co(const CCGKey *key, CCGElem *elem)
BLI_INLINE float * CCG_elem_offset_mask(const CCGKey *key, CCGElem *elem, int offset)
Definition: BKE_ccg.h:155
#define BLI_assert(a)
Definition: BLI_assert.h:58
#define BLI_INLINE
#define UNUSED(x)
_GL_VOID GLfloat value _GL_VOID_RET _GL_VOID const GLuint GLboolean *residences _GL_BOOL_RET _GL_VOID GLsizei GLfloat GLfloat GLfloat GLfloat const GLubyte *bitmap _GL_VOID_RET _GL_VOID GLenum const void *lists _GL_VOID_RET _GL_VOID const GLdouble *equation _GL_VOID_RET _GL_VOID GLdouble GLdouble blue _GL_VOID_RET _GL_VOID GLfloat GLfloat blue _GL_VOID_RET _GL_VOID GLint GLint blue _GL_VOID_RET _GL_VOID GLshort GLshort blue _GL_VOID_RET _GL_VOID GLubyte GLubyte blue _GL_VOID_RET _GL_VOID GLuint GLuint blue _GL_VOID_RET _GL_VOID GLushort GLushort blue _GL_VOID_RET _GL_VOID GLbyte GLbyte GLbyte alpha _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble alpha _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat alpha _GL_VOID_RET _GL_VOID GLint GLint GLint alpha _GL_VOID_RET _GL_VOID GLshort GLshort GLshort alpha _GL_VOID_RET _GL_VOID GLubyte GLubyte GLubyte alpha _GL_VOID_RET _GL_VOID GLuint GLuint GLuint alpha _GL_VOID_RET _GL_VOID GLushort GLushort GLushort alpha _GL_VOID_RET _GL_VOID GLenum mode _GL_VOID_RET _GL_VOID GLint y
Definition: BKE_ccg.h:48
int has_mask
Definition: BKE_ccg.h:71
int mask_offset
Definition: BKE_ccg.h:68
int grid_size
Definition: BKE_ccg.h:56
int grid_bytes
Definition: BKE_ccg.h:60
int grid_area
Definition: BKE_ccg.h:58
int level
Definition: BKE_ccg.h:49
int normal_offset
Definition: BKE_ccg.h:64
int elem_size
Definition: BKE_ccg.h:53
int has_normals
Definition: BKE_ccg.h:70