File: nichtsnutz.c

package info (click to toggle)
s3d 0.2.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,708 kB
  • sloc: ansic: 23,609; python: 488; perl: 98; makefile: 31; sh: 29
file content (121 lines) | stat: -rw-r--r-- 2,629 bytes parent folder | download | duplicates (4)
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
// SPDX-License-Identifier: GPL-2.0-or-later
/* SPDX-FileCopyrightText: 2004-2015  Simon Wunderlich <sw@simonwunderlich.de>
 */




#include <s3d.h>
#include <s3d_keysym.h>
#include <stdio.h>
#include <math.h>
#include <time.h>

static struct timespec t = {
	0, 10*1000*1000
}; /* 10 mili seconds */

static int object, foll;
static float al, r, alpha = 0.0, angle;
static float CamPosition[2][3],
       TmpMove[3],
       Tmp[3],
       RotCam[2][3],
       CatPos[3];

static float length;

static void mainloop(void)
{

	al = (alpha * (float)M_PI / 180.f);
	r = 5.0f;

	CatPos[0] = sinf(al) * r;
	CatPos[1] = 0;
	CatPos[2] = cosf(al) * r;
	s3d_translate(object, CatPos[0] , CatPos[1], CatPos[2]);
	s3d_rotate(object, 0, alpha, 0);
	alpha = alpha + 0.1f;
	if (alpha > 360.0f) alpha = 0.0f;

	length = s3d_vector_length(CatPos);


	RotCam[0][0] = (CatPos[0] * 12.0f) / length;
	RotCam[0][1] = (CatPos[1] * 12.0f) / length;
	RotCam[0][2] = (CatPos[2] * 12.0f) / length;


	if (foll) {

		CamPosition[0][0] = ((CamPosition[0][0] * 4 + RotCam[0][0]) / 5);
		CamPosition[0][1] = ((CamPosition[0][1] * 4 + RotCam[0][1]) / 5);
		CamPosition[0][2] = ((CamPosition[0][2] * 4 + RotCam[0][2]) / 5);
		s3d_translate(0, CamPosition[0][0], CamPosition[0][1], CamPosition[0][2]);

		TmpMove[0] = 0.0;
		TmpMove[1] = 0.0;
		TmpMove[2] = -1.0;

		Tmp[0] = CamPosition[0][0] - CatPos[0];
		Tmp[1] = 0.0;
		Tmp[2] = CamPosition[0][2] - CatPos[2];

		angle = s3d_vector_angle(Tmp, TmpMove);
		angle = (CatPos[0] > 0) ? (180.f - (180.f / (float)M_PI * angle)) : (180.f + (180.f / (float)M_PI * angle));
		printf("%f %f\n", angle, al);

		CamPosition[1][1] = (CamPosition[1][1] * 4 + angle) / 5;
		s3d_rotate(0, CamPosition[1][0], CamPosition[1][1], CamPosition[1][2]);
	}



	nanosleep(&t, NULL);
}

static int object_info(struct s3d_evt *hrmz)
{
	struct s3d_obj_info *inf;
	inf = (struct s3d_obj_info *)hrmz->buf;

	if (inf->object == 0) {
		CamPosition[0][0] = inf->trans_x;
		CamPosition[0][1] = inf->trans_y;
		CamPosition[0][2] = inf->trans_z;
		CamPosition[1][0] = inf->rot_x;
		CamPosition[1][1] = inf->rot_y;
		CamPosition[1][2] = inf->rot_z;
	}
	return 0;
}



static int keypress(struct s3d_evt *event)
{
	int key;
	key = *((unsigned short *)event->buf);
	switch (key) {
	default:
		;
	}
	return 0;
}


int main(int argc, char **argv)
{

	if (!s3d_init(&argc, &argv, "running cat")) {
		s3d_set_callback(S3D_EVENT_KEY, keypress);
		s3d_set_callback(S3D_EVENT_OBJ_INFO, object_info);

		object = s3d_import_model_file("objs/katze_body.3ds");
		s3d_flags_on(object, S3D_OF_VISIBLE);
		s3d_mainloop(mainloop);
		s3d_quit();
	}
	return 0;
}