-
Notifications
You must be signed in to change notification settings - Fork 23
/
render_balls_so.cpp
58 lines (54 loc) · 1.33 KB
/
render_balls_so.cpp
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
#include <cstdio>
#include <vector>
#include <algorithm>
#include <math.h>
using namespace std;
struct PointInfo{
int x,y,z;
float r,g,b;
};
extern "C"{
void render_ball(int h,int w,unsigned char * show,int n,int * xyzs,float * c0,float * c1,float * c2,int r){
r=max(r,1);
vector<int> depth(h*w,-2100000000);
vector<PointInfo> pattern;
for (int dx=-r;dx<=r;dx++)
for (int dy=-r;dy<=r;dy++)
if (dx*dx+dy*dy<r*r){
double dz=sqrt(double(r*r-dx*dx-dy*dy));
PointInfo pinfo;
pinfo.x=dx;
pinfo.y=dy;
pinfo.z=dz;
pinfo.r=dz/r;
pinfo.g=dz/r;
pinfo.b=dz/r;
pattern.push_back(pinfo);
}
double zmin=0,zmax=0;
for (int i=0;i<n;i++){
if (i==0){
zmin=xyzs[i*3+2]-r;
zmax=xyzs[i*3+2]+r;
}else{
zmin=min(zmin,double(xyzs[i*3+2]-r));
zmax=max(zmax,double(xyzs[i*3+2]+r));
}
}
for (int i=0;i<n;i++){
int x=xyzs[i*3+0],y=xyzs[i*3+1],z=xyzs[i*3+2];
for (int j=0;j<int(pattern.size());j++){
int x2=x+pattern[j].x;
int y2=y+pattern[j].y;
int z2=z+pattern[j].z;
if (!(x2<0 || x2>=h || y2<0 || y2>=w) && depth[x2*w+y2]<z2){
depth[x2*w+y2]=z2;
double intensity=min(1.0,(z2-zmin)/(zmax-zmin)*0.7+0.3);
show[(x2*w+y2)*3+0]=pattern[j].b*c2[i]*intensity;
show[(x2*w+y2)*3+1]=pattern[j].g*c0[i]*intensity;
show[(x2*w+y2)*3+2]=pattern[j].r*c1[i]*intensity;
}
}
}
}
}//extern "C"