基础代码提交

This commit is contained in:
sakitsuki_asahi
2025-12-16 22:52:01 +09:00
parent 2eade76777
commit 35db634c0b
4 changed files with 489 additions and 1 deletions
+325
View File
@@ -0,0 +1,325 @@
#define ball 0
#define PI 3.14159265358979323846
#define G 6.6
#include <stdio.h>
#include <math.h>
#include <string.h>
struct vector{
long double x;
long double y;
long double z;
};
struct RGB{
unsigned char r;
unsigned char g;
unsigned char b;
};
struct {
struct RGB background;
struct vector light;
}world;
struct entity{
long double r;
unsigned long mass;
struct RGB coluor;
struct vector location;
struct vector velocity;
};
struct {
struct entity** entity;
unsigned long count;
}entityarray;
struct {
long double focal_length;
long double focal_distance;
struct vector location;
struct vector target;
}camera;
struct entity* entity_initialize(){
struct entity* _e = malloc(sizeof(struct entity));
_e->r=1;
_e->mass=1;
_e->coluor.r=255;
_e->coluor.g=255;
_e->coluor.b=255;
_e->location.x=0;
_e->location.y=0;
_e->location.z=0;
_e->velocity.x=0;
_e->velocity.y=0;
_e->velocity.z=0;
return _e;
}
void entityarray_initialize(){
entityarray.entity=NULL;
entityarray.count=0;
return;
}
void world_initialize(){
world.background.r=0;
world.background.g=0;
world.background.b=0;
world.light.x=0;
world.light.y=-1;
world.light.z=0;
camera.target.x=0;
camera.target.y=0;
camera.target.z=0;
camera.focal_distance=0.8;
camera.focal_length=0.4;
camera.location.x=0;
camera.location.y=0;
camera.location.z=0;
return;
}
int add_entity(struct entity* _p) {
if (_p == NULL) {
perror("function int add_entity(struct entity* _p): _p is NULL.");
return -1;
}
if (entityarray.entity == NULL && entityarray.count == 0) {
struct entity** _np = malloc(sizeof(struct entity*));
if (_np == NULL) {
perror("function int add_entity(struct entity* _p): malloc failed.");
return -1;
}
entityarray.entity = _np;
entityarray.entity[0] = _p;
entityarray.count = 1;
return 0;
}
struct entity** _np = realloc(entityarray.entity, (entityarray.count + 1) * sizeof(struct entity*));
if (_np == NULL) {
perror("function int add_entity(struct entity* _p): realloc failed.");
return -1;
}
_np[entityarray.count] = _p;
entityarray.entity = _np;
entityarray.count++;
return 0;
}
int delete_entity(unsigned int _i){
if (_i >= entityarray.count) {
fprintf(stderr, "Error: Index out of bounds in delete_entity\n");
return -1;
}
free(entityarray.entity[_i]);
size_t num_to_move = entityarray.count - _i - 1;
memmove(&entityarray.entity[_i], &entityarray.entity[_i + 1], num_to_move * sizeof(struct entity*));
struct entity** _np = realloc(entityarray.entity,(entityarray.count-1)*sizeof(struct entity*));
if(_np==NULL){
perror("function int delete_entity(unsigned int _i): struct entity** _np = realloc(entityarray.entity,(entityarray.count-1)*sizeof(struct entity*)); failed.");
return -1;
}
entityarray.entity=_np;
entityarray.count--;
return 0;
}
long double distance_vec2vec(struct vector _a,struct vector _b){
return sqrtl(pow(_a.x-_b.x,2)+pow(_a.y-_b.y,2)+pow(_a.z-_b.z,2));
}
long double distance_vec2vecs(struct vector _a,struct vector _b){
return pow(_a.x-_b.x,2)+pow(_a.y-_b.y,2)+pow(_a.z-_b.z,2);
}
struct vector vecplusvec(struct vector _a,struct vector _b){
struct vector _o;
_o.x=_a.x+_b.x;
_o.y=_a.y+_b.y;
_o.z=_a.z+_b.z;
return _o;
}
struct vector vecsubvec(struct vector _a,struct vector _b){
struct vector _o;
_o.x=_a.x-_b.x;
_o.y=_a.y-_b.y;
_o.z=_a.z-_b.z;
return _o;
}
struct vector vecnorm(struct vector _a);
void change_light(struct vector _l){
world.light = vecnorm(_l);
return;
}
//以下是deepseek写的
// 向量点积
long double vecdotvec(struct vector _a, struct vector _b) {
return _a.x * _b.x + _a.y * _b.y + _a.z * _b.z;
}
// 向量长度平方
long double veclen2(struct vector _a) {
return vecdotvec(_a, _a);
}
// 向量数乘
struct vector vecmulscalar(struct vector _a, long double scalar) {
struct vector _o;
_o.x = _a.x * scalar;
_o.y = _a.y * scalar;
_o.z = _a.z * scalar;
return _o;
}
// 向量归一化
struct vector vecnorm(struct vector _a) {
long double len = sqrtl(veclen2(_a));
if (len == 0.0L) return _a;
return vecmulscalar(_a, 1.0L / len);
}
// 计算射线与球面交点及法向量
// 返回交点P处的球面法向量,如果无交点,返回{0,0,0}
struct vector calculate_sphere_normal(struct vector A, struct vector B,
struct vector O, long double r) {
struct vector d = vecsubvec(B, A); // 射线方向
struct vector OA = vecsubvec(A, O); // A到球心的向量
long double a = veclen2(d);
long double b = 2.0L * vecdotvec(OA, d);
long double c = veclen2(OA) - r * r;
// 计算判别式
long double discriminant = b * b - 4.0L * a * c;
if (discriminant < 0.0L) {
// 无实数根,射线与球面不相交
struct vector zero = {0.0L, 0.0L, 0.0L};
return zero;
}
long double sqrt_discriminant = sqrtl(discriminant);
long double t1 = (-b - sqrt_discriminant) / (2.0L * a);
long double t2 = (-b + sqrt_discriminant) / (2.0L * a);
// 选择离A最近的非负t值
long double t_star;
if (t1 >= 0.0L) {
t_star = t1;
} else if (t2 >= 0.0L) {
t_star = t2;
} else {
// 两个t值都为负,交点在射线反方向
struct vector zero = {0.0L, 0.0L, 0.0L};
return zero;
}
// 计算交点P
struct vector P;
P.x = A.x + t_star * d.x;
P.y = A.y + t_star * d.y;
P.z = A.z + t_star * d.z;
// 计算球面法向量(单位向量)
struct vector OP = vecsubvec(P, O); // P到O的向量
struct vector normal = vecnorm(OP); // 归一化
return normal;
}
// 如果你已经有了交点P,直接计算法向量的函数
struct vector calculate_normal_from_P(struct vector P, struct vector O, long double r) {
struct vector OP = vecsubvec(P, O);
struct vector normal = vecnorm(OP); // 归一化
return normal;
}
struct vector veccrossvec(struct vector a, struct vector b) {
struct vector result;
result.x = a.y * b.z - a.z * b.y;
result.y = a.z * b.x - a.x * b.z;
result.z = a.x * b.y - a.y * b.x;
return result;
}
struct vector get_pixel_location(int pixel_x, int pixel_y,
unsigned short resolution_x,
unsigned short resolution_y) {
// 计算前向向量并归一化
struct vector forward = vecsubvec(camera.target, camera.location);
long double dist_to_target = sqrtl(veclen2(forward));
if (dist_to_target > 0.0L) {
forward.x /= dist_to_target;
forward.y /= dist_to_target;
forward.z /= dist_to_target;
}
// 计算图像平面中心点
struct vector center_location;
center_location.x = camera.location.x + forward.x * camera.focal_length;
center_location.y = camera.location.y + forward.y * camera.focal_length;
center_location.z = camera.location.z + forward.z * camera.focal_length;
// 计算相机坐标系基向量
struct vector world_up = {0.0L, 1.0L, 0.0L};
// 计算右向量
struct vector right;
right.x = forward.y * world_up.z - forward.z * world_up.y;
right.y = forward.z * world_up.x - forward.x * world_up.z;
right.z = forward.x * world_up.y - forward.y * world_up.x;
// 归一化右向量
long double right_len = sqrtl(veclen2(right));
if (right_len > 0.0L) {
right.x /= right_len;
right.y /= right_len;
right.z /= right_len;
}
// 计算上向量
struct vector up;
up.x = right.y * forward.z - right.z * forward.y;
up.y = right.z * forward.x - right.x * forward.z;
up.z = right.x * forward.y - right.y * forward.x;
// 归一化上向量
long double up_len = sqrtl(veclen2(up));
if (up_len > 0.0L) {
up.x /= up_len;
up.y /= up_len;
up.z /= up_len;
}
// 计算图像平面尺寸(简化版,假设每个像素对应固定世界单位)
// 这里假设焦距为1时,图像宽度为2个单位
long double pixel_world_size = 2.0L / resolution_x;
// 计算像素偏移
long double x_offset = pixel_x * pixel_world_size;
long double y_offset = pixel_y * pixel_world_size;
// 计算像素位置
struct vector pixel_location = center_location;
// 添加水平偏移
pixel_location.x += right.x * x_offset;
pixel_location.y += right.y * x_offset;
pixel_location.z += right.z * x_offset;
// 添加垂直偏移
pixel_location.x += up.x * y_offset;
pixel_location.y += up.y * y_offset;
pixel_location.z += up.z * y_offset;
return pixel_location;
}