1 | #include <stdio.h> |
---|
2 | #include <stdlib.h> |
---|
3 |
|
---|
4 | #include "../include/GL/glew.h"
|
---|
5 | #ifdef _WIN32
|
---|
6 | #include "../include/GL/wglew.h"
|
---|
7 | #endif |
---|
8 | #include "../include/global.h" |
---|
9 | #include "../include/histogram.h" |
---|
10 | #include "../include/metrics.h" |
---|
11 | |
---|
12 | using namespace VMI; |
---|
13 | |
---|
14 | void VMI::printHistogram(GLuint histoGram[][4]) { |
---|
15 | int i; |
---|
16 | |
---|
17 | printf("\n"); |
---|
18 | for (i=0; i<HISTOGRAM_SIZE; i++) { |
---|
19 | printf("H: %d RGBA(%d, %d, %d, %d)\n",i, histoGram[i][0], histoGram[i][1], histoGram[i][2], histoGram[i][3]); |
---|
20 | } |
---|
21 | } |
---|
22 | |
---|
23 | void VMI::plotHistogram(GLuint histoGram[][4]) { |
---|
24 | int i; |
---|
25 | GLuint iLargest = 0; // Largest histogram value |
---|
26 | GLfloat maxHeight = height / 2.0f; |
---|
27 | |
---|
28 | setOrthographicProjection(); |
---|
29 | |
---|
30 | // Find largest value for scaling graph down |
---|
31 | for (i=0; i<HISTOGRAM_SIZE; i++) { |
---|
32 | if(iLargest < histoGram[i][0]) |
---|
33 | iLargest = histoGram[i][0]; |
---|
34 | if(iLargest < histoGram[i][1]) |
---|
35 | iLargest = histoGram[i][1]; |
---|
36 | if(iLargest < histoGram[i][2]) |
---|
37 | iLargest = histoGram[i][2]; |
---|
38 | if(iLargest < histoGram[i][3]) |
---|
39 | iLargest = histoGram[i][3]; |
---|
40 | } |
---|
41 | printf("%d\n", iLargest); |
---|
42 | glBegin(GL_LINE_STRIP); |
---|
43 | glColor3f(1.0f, 0.0f, 0.0f); |
---|
44 | for (i=0; i < HISTOGRAM_SIZE; i++) |
---|
45 | //glVertex2s(i, histoGram[i][0]); |
---|
46 | glVertex2f((GLfloat)i, ((GLfloat)histoGram[i][0] / maxHeight) * maxHeight); |
---|
47 | glEnd(); |
---|
48 | |
---|
49 | glBegin(GL_LINE_STRIP); |
---|
50 | glColor3f(0.0f, 1.0f, 0.0f); |
---|
51 | for (i=0; i < HISTOGRAM_SIZE; i++) |
---|
52 | //glVertex2s(i, histoGram[i][1]); |
---|
53 | glVertex2f((GLfloat)i, ((GLfloat)histoGram[i][0] / maxHeight) * maxHeight); |
---|
54 | glEnd(); |
---|
55 | |
---|
56 | glBegin(GL_LINE_STRIP); |
---|
57 | glColor3f(0.0f, 0.0f, 1.0f); |
---|
58 | for (i=0; i < HISTOGRAM_SIZE; i++) |
---|
59 | //glVertex2s(i, histoGram[i][2]); |
---|
60 | glVertex2f((GLfloat)i, ((GLfloat)histoGram[i][0] / maxHeight) * maxHeight); |
---|
61 | glEnd(); |
---|
62 | glBegin(GL_LINE_STRIP); |
---|
63 | glColor3f(0.5f, 0.5f, 0.5f); // Grey color represents the alpha channel |
---|
64 | for (i=0; i < HISTOGRAM_SIZE; i++) |
---|
65 | //glVertex2s(i, histoGram[i][3]); |
---|
66 | glVertex2f((GLfloat)i, ((GLfloat)histoGram[i][0] / maxHeight) * maxHeight); |
---|
67 | glEnd(); |
---|
68 | |
---|
69 | resetPerspectiveProjection(); |
---|
70 | } |
---|
71 | |
---|
72 | GLuint **VMI::initHistogram(GLuint numTriangles, GLuint numCameras) { |
---|
73 | GLuint i; |
---|
74 | GLuint **histogram; |
---|
75 | |
---|
76 | histogram = (GLuint **)malloc(sizeof(GLuint *) * numCameras); |
---|
77 | |
---|
78 | if (histogram == NULL) { |
---|
79 | fprintf(stderr, "Error allocating memory\n"); |
---|
80 | exit(1); |
---|
81 | } |
---|
82 | |
---|
83 | for (i=0; i<numCameras; i++) |
---|
84 | histogram[i] = (GLuint *)malloc(sizeof(GLuint) * (numTriangles + 1)); // + 1 because the last is the numPixels of the background |
---|
85 | |
---|
86 | // Fill the histogram buffer with 0 |
---|
87 | for (i=0; i<numCameras; i++) |
---|
88 | memset(histogram[i], 0, sizeof(GLuint) * (numTriangles + 1)); |
---|
89 | |
---|
90 | return histogram; |
---|
91 | } |
---|
92 | |
---|
93 | void VMI::deleteHistogram(GLuint **histogram, GLuint numCameras) { |
---|
94 | GLuint i; |
---|
95 | |
---|
96 | for (i=0;i<numCameras;i++) |
---|
97 | if (histogram[i] != NULL) free(histogram[i]); |
---|
98 | |
---|
99 | free(histogram); |
---|
100 | } |
---|
101 | |
---|
102 | void VMI::printFullHistogram(GLuint **histogram, GLuint numCameras, GLuint numTriangles) { |
---|
103 | GLuint i, j; |
---|
104 | |
---|
105 | printf("\n"); |
---|
106 | for (i=0; i<numCameras; i++) { |
---|
107 | printf("c%d [t%d(%d) ", i, 0, histogram[i][0]); |
---|
108 | for (j=1; j<=numTriangles; j++) { |
---|
109 | printf(",t%d(%d) ", j, histogram[i][j]); |
---|
110 | } |
---|
111 | printf("]\n"); |
---|
112 | } |
---|
113 | printf("Total image area: %d\n", width * height); |
---|
114 | } |
---|
115 | |
---|
116 | void VMI::getSubHistogram(GLuint subHistoGram[][4]) { |
---|
117 | //printf("Computing histogram...\n"); |
---|
118 | // Define the histogram |
---|
119 | |
---|
120 | glEnable(GL_HISTOGRAM); |
---|
121 | //glEnable(GL_TEXTURE_2D); |
---|
122 | glHistogram(GL_HISTOGRAM, HISTOGRAM_SIZE, GL_RGBA, GL_TRUE); |
---|
123 | |
---|
124 | // Perform a pixel operation |
---|
125 | glCopyPixels(0, 0, width, height, GL_COLOR); |
---|
126 | |
---|
127 | // Get the histogram |
---|
128 | glGetHistogram(GL_HISTOGRAM, GL_FALSE, GL_RGBA, GL_UNSIGNED_INT, subHistoGram); |
---|
129 | glResetHistogram(GL_HISTOGRAM); |
---|
130 | glDisable(GL_HISTOGRAM); |
---|
131 | //glDisable(GL_TEXTURE_2D); |
---|
132 | |
---|
133 | //printHistogram(subHistoGram); |
---|
134 | //plotHistogram(subHistoGram); |
---|
135 | } |
---|
136 | |
---|
137 | void VMI::copySubHistogram(Color *colors, GLuint *histogram, GLuint begin, GLuint end, GLuint subHistoGram[][4]) { |
---|
138 | GLuint i; |
---|
139 | GLubyte r, g, b, a; |
---|
140 | GLuint h = 0; |
---|
141 | |
---|
142 | for (i=begin; i<end; i++) { |
---|
143 | |
---|
144 | r = colors[i].r; |
---|
145 | g = colors[i].g; |
---|
146 | b = colors[i].b; |
---|
147 | a = colors[i].a; |
---|
148 | |
---|
149 | if (r != 0) |
---|
150 | h = subHistoGram[r][0]; |
---|
151 | |
---|
152 | if (g != 0) |
---|
153 | h = subHistoGram[g][1]; |
---|
154 | |
---|
155 | if (b != 0) |
---|
156 | h = subHistoGram[b][2]; |
---|
157 | |
---|
158 | if (a != 0) |
---|
159 | h = subHistoGram[a][3]; |
---|
160 | |
---|
161 | histogram[i] = h; |
---|
162 | } |
---|
163 | } |
---|
164 | |
---|
165 | void VMI::getSWHistogram(GLuint *histogram, GLubyte *pixels) { |
---|
166 | GLubyte r, g, b, a; |
---|
167 | GLuint i, t, p, numPixels = width * height; |
---|
168 | |
---|
169 | glReadPixels(0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, pixels); |
---|
170 | |
---|
171 | for (i=0; i<numPixels; i++) { // pixel i |
---|
172 | p = i << 2; // 4 * i |
---|
173 | r = pixels[p]; |
---|
174 | g = pixels[p + 1]; |
---|
175 | b = pixels[p + 2]; |
---|
176 | a = pixels[p + 3]; |
---|
177 | |
---|
178 | t = r + (g << 8) + (b << 16) + (a << 24); // triangle color |
---|
179 | |
---|
180 | //printf("pixel:%d (%d,%d,%d,%d) t %d\n", i, r, g, b, a, t); |
---|
181 | |
---|
182 | histogram[t]++; |
---|
183 | } |
---|
184 | //getchar(); |
---|
185 | } |
---|
186 | |
---|
187 | void VMI::getSWHistoByOcclusionQuery(Mesh *mesh, Color *colors, GLuint *histogram) { |
---|
188 | GLuint i, v1, v2, v3; |
---|
189 | GLint area; |
---|
190 | |
---|
191 | glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE); |
---|
192 | glDepthMask(GL_FALSE); |
---|
193 | |
---|
194 | for (i=0; i<mesh->numTriangles; i++) { // For every triangle |
---|
195 | if (mesh->triangles[i].enable == TRUE) { |
---|
196 | glBeginQueryARB(GL_SAMPLES_PASSED_ARB, queries[i]); |
---|
197 | |
---|
198 | // Render triangle i |
---|
199 | glBegin(GL_TRIANGLES); |
---|
200 | v1= mesh->triangles[i].indices[0]; |
---|
201 | v2= mesh->triangles[i].indices[1]; |
---|
202 | v3= mesh->triangles[i].indices[2]; |
---|
203 | |
---|
204 | glColor4ub(colors[i].r, colors[i].g, colors[i].b, colors[i].a); |
---|
205 | |
---|
206 | glVertex3f(mesh->vertices[v1].x, mesh->vertices[v1].y, mesh->vertices[v1].z); |
---|
207 | glVertex3f(mesh->vertices[v2].x, mesh->vertices[v2].y, mesh->vertices[v2].z); |
---|
208 | glVertex3f(mesh->vertices[v3].x, mesh->vertices[v3].y, mesh->vertices[v3].z); |
---|
209 | glEnd(); |
---|
210 | |
---|
211 | glEndQueryARB(GL_SAMPLES_PASSED_ARB); |
---|
212 | } |
---|
213 | } |
---|
214 | glFlush(); |
---|
215 | |
---|
216 | glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE); |
---|
217 | glDepthMask(GL_TRUE); |
---|
218 | |
---|
219 | for (i=0; i<mesh->numTriangles; i++) { // For every triangle |
---|
220 | if (mesh->triangles[i].enable == TRUE) { |
---|
221 | glGetQueryObjectivARB(queries[i], GL_QUERY_RESULT_ARB, &area); |
---|
222 | |
---|
223 | if (area < 0) { |
---|
224 | printf(" t:%d a:%d\n", i, histogram[i + 1]); |
---|
225 | histogram[i + 1] = 0; |
---|
226 | } else { |
---|
227 | histogram[i + 1] = area; |
---|
228 | } |
---|
229 | } |
---|
230 | } |
---|
231 | histogram[0] = computeBackgroundArea(mesh, histogram); // Compute area of the background |
---|
232 | |
---|
233 | } |
---|
234 | |
---|
235 | void VMI::resetSWHistogram(GLuint *histogram, GLuint numTriangles) { |
---|
236 | |
---|
237 | // Fill the histogram buffer with 0 |
---|
238 | memset(histogram, 0, sizeof(GLuint) * (numTriangles + 1)); |
---|
239 | } |
---|