Welcome to mirror list, hosted at ThFree Co, Russian Federation.

uvproject.c « intern « blenlib « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: be7682a63d415829b260cbfcb87b3849f90306fc (plain)
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/*
 * $Id$
 *
 * ***** BEGIN GPL LICENSE BLOCK *****
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 * ***** END GPL LICENSE BLOCK *****
 */

/** \file blender/blenlib/intern/uvproject.c
 *  \ingroup bli
 */


#include <math.h>

#include "MEM_guardedalloc.h"

#include "DNA_camera_types.h"
#include "DNA_object_types.h"

#include "BLI_math.h"
#include "BLI_uvproject.h"

typedef struct UvCameraInfo {
	float camangle;
	float camsize;
	float xasp, yasp;
	float shiftx, shifty;
	float rotmat[4][4];
	float caminv[4][4];
	short do_persp, do_pano, do_rotmat;
} UvCameraInfo;

void project_from_camera(float target[2], float source[3], UvCameraInfo *uci)
{
	float pv4[4];

	copy_v3_v3(pv4, source);
	pv4[3]= 1.0;

	/* rotmat is the object matrix in this case */
	if(uci->do_rotmat)
		mul_m4_v4(uci->rotmat, pv4);

	/* caminv is the inverse camera matrix */
	mul_m4_v4(uci->caminv, pv4);

	if(uci->do_pano) {
		float angle= atan2f(pv4[0], -pv4[2]) / ((float)M_PI * 2.0f); /* angle around the camera */
		if (uci->do_persp==0) {
			target[0]= angle; /* no correct method here, just map to  0-1 */
			target[1]= pv4[1] / uci->camsize;
		}
		else {
			float vec2d[2]; /* 2D position from the camera */
			vec2d[0]= pv4[0];
			vec2d[1]= pv4[2];
			target[0]= angle * ((float)M_PI / uci->camangle);
			target[1]= pv4[1] / (len_v2(vec2d) * (uci->camsize * 2.0f));
		}
	}
	else {
		if (pv4[2]==0.0f) pv4[2]= 0.00001f; /* don't allow div by 0 */

		if (uci->do_persp==0) {
			target[0]= (pv4[0]/uci->camsize);
			target[1]= (pv4[1]/uci->camsize);
		}
		else {
			target[0]= (-pv4[0]*((1.0f/uci->camsize)/pv4[2])) / 2.0f;
			target[1]= (-pv4[1]*((1.0f/uci->camsize)/pv4[2])) / 2.0f;
		}
	}

	target[0] *= uci->xasp;
	target[1] *= uci->yasp;
	
	/* adds camera shift + 0.5 */
	target[0] += uci->shiftx;
	target[1] += uci->shifty;
}

/* could rv3d->persmat */
void project_from_view(float target[2], float source[3], float persmat[4][4], float rotmat[4][4], float winx, float winy)
{
	float pv[3], pv4[4], x= 0.0, y= 0.0;

	mul_v3_m4v3(pv, rotmat, source);

	copy_v3_v3(pv4, source);
	pv4[3]= 1.0;

	/* rotmat is the object matrix in this case */
	mul_m4_v4(rotmat, pv4); 

	/* almost project_short */
	mul_m4_v4(persmat, pv4);
	if(fabsf(pv4[3]) > 0.00001f) { /* avoid division by zero */
		target[0] = winx/2.0f + (winx/2.0f) * pv4[0] / pv4[3];
		target[1] = winy/2.0f + (winy/2.0f) * pv4[1] / pv4[3];
	}
	else {
		/* scaling is lost but give a valid result */
		target[0] = winx/2.0f + (winx/2.0f) * pv4[0];
		target[1] = winy/2.0f + (winy/2.0f) * pv4[1];
	}

	/* v3d->persmat seems to do this funky scaling */ 
	if(winx > winy) {
		y= (winx - winy)/2.0f;
		winy = winx;
	}
	else {
		x= (winy - winx)/2.0f;
		winx = winy;
	}

	target[0]= (x + target[0]) / winx;
	target[1]= (y + target[1]) / winy;
}

/* 'rotmat' can be obedit->obmat when uv project is used.
 * 'winx' and 'winy' can be from scene->r.xsch/ysch */ 
UvCameraInfo *project_camera_info(Object *ob, float (*rotmat)[4], float winx, float winy)
{
	UvCameraInfo uci;
	Camera *camera= ob->data;

	uci.do_pano = (camera->flag & CAM_PANORAMA);
	uci.do_persp = (camera->type==CAM_PERSP);

	uci.camangle= lens_to_angle(camera->lens) / 2.0f;
	uci.camsize= uci.do_persp ? tanf(uci.camangle) : camera->ortho_scale;

	/* account for scaled cameras */
	copy_m4_m4(uci.caminv, ob->obmat);
	normalize_m4(uci.caminv);

	if (invert_m4(uci.caminv)) {
		UvCameraInfo *uci_pt;

		/* normal projection */
		if(rotmat) {
			copy_m4_m4(uci.rotmat, rotmat);
			uci.do_rotmat= 1;
		}
		else {
			uci.do_rotmat= 0;
		}

		/* also make aspect ratio adjustment factors */
		if (winx > winy) {
			uci.xasp= 1.0f;
			uci.yasp= winx / winy;
		}
		else {
			uci.xasp= winy / winx;
			uci.yasp= 1.0f;
		}
		
		/* include 0.5f here to move the UVs into the center */
		uci.shiftx = 0.5f - (camera->shiftx * uci.xasp);
		uci.shifty = 0.5f - (camera->shifty * uci.yasp);
		
		uci_pt= MEM_mallocN(sizeof(UvCameraInfo), "UvCameraInfo");
		*uci_pt= uci;
		return uci_pt;
	}

	return NULL;
}

void project_from_view_ortho(float target[2], float source[3], float rotmat[4][4])
{
	float pv[3];

	mul_v3_m4v3(pv, rotmat, source);

	/* ortho projection */
	target[0] = -pv[0];
	target[1] = pv[2];
}


void project_camera_info_scale(UvCameraInfo *uci, float scale_x, float scale_y)
{
	uci->xasp *= scale_x;
	uci->yasp *= scale_y;
}