基础代码提交
This commit is contained in:
+160
@@ -0,0 +1,160 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <math.h>
|
||||
|
||||
#include "engine.c"
|
||||
|
||||
unsigned short int video_rate = 60;
|
||||
unsigned int video_length = 60;
|
||||
unsigned long simulation_rate = 60000;
|
||||
unsigned short int resolution_x = 1920;
|
||||
unsigned short int resolution_y = 1080;
|
||||
|
||||
void frame_render(char* _o) {
|
||||
unsigned int _index=0;
|
||||
for(int y=resolution_y/2;y>-resolution_y/2;y--){
|
||||
for(int x=-resolution_x/2;x<resolution_x/2;x++){
|
||||
struct vector pixel_location=get_pixel_location(x,y,resolution_x,resolution_y);
|
||||
int render_entity=-1;long double render_length=-1;
|
||||
for(int i=0;i<entityarray.count;i++){
|
||||
long double cosangle = (distance_vec2vecs(camera.location,entityarray.entity[i]->location)+distance_vec2vecs(camera.location,pixel_location)-distance_vec2vecs(pixel_location,entityarray.entity[i]->location))/(2*distance_vec2vec(camera.location,pixel_location)*distance_vec2vec(camera.location,entityarray.entity[i]->location));
|
||||
if(cosangle<0){
|
||||
continue;
|
||||
}
|
||||
long double angle = acosl(cosangle);
|
||||
if(entityarray.entity[i]->r<distance_vec2vec(camera.location,entityarray.entity[i]->location)*sinl(angle)){
|
||||
continue;
|
||||
}
|
||||
long double _a = cosangle*distance_vec2vec(camera.location,entityarray.entity[i]->location)-sqrtl(pow(entityarray.entity[i]->r,2)-distance_vec2vecs(camera.location,entityarray.entity[i]->location)*pow(sinl(angle),2));
|
||||
if(render_length<=0||_a<render_length){
|
||||
render_length=_a;
|
||||
render_entity=i;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
if(render_entity<=-1){
|
||||
memset(_o+_index*3,world.background.r,1);
|
||||
memset(_o+_index*3+1,world.background.g,1);
|
||||
memset(_o+_index*3+2,world.background.b,1);
|
||||
}else{
|
||||
struct entity render;
|
||||
memcpy(&render,entityarray.entity[render_entity],sizeof(struct entity));
|
||||
struct vector _f = calculate_sphere_normal(camera.location,pixel_location,render.location,render.r);
|
||||
long double _x = veclen2(vecsubvec(_f,world.light))/4;
|
||||
memset(_o+_index*3,render.coluor.r*_x,1);
|
||||
memset(_o+_index*3+1,render.coluor.g*_x,1);
|
||||
memset(_o+_index*3+2,render.coluor.b*_x,1);
|
||||
}
|
||||
_index++;
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
void video_render(char* _i,size_t _fsize,FILE* _p){
|
||||
size_t written = fwrite(_i,1,resolution_x*resolution_y*3,_p);
|
||||
if (written != _fsize) {
|
||||
fprintf(stderr, "Failed to write frame\n");
|
||||
return;
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
void simulate(){
|
||||
struct vector** entity_acceleration = malloc(sizeof(struct vector*)*entityarray.count);
|
||||
for(int i=0;i<entityarray.count;i++){
|
||||
struct vector* entity = malloc(sizeof(struct vector));
|
||||
entity->x=0;
|
||||
entity->y=0;
|
||||
entity->z=0;
|
||||
entity_acceleration[i]=entity;
|
||||
}
|
||||
for(int i=0;i<entityarray.count;i++){
|
||||
for(int j=i+1;j<entityarray.count;j++){
|
||||
long double ds = pow((entityarray.entity[i]->location.x-entityarray.entity[j]->location.x),2)+pow((entityarray.entity[i]->location.y-entityarray.entity[j]->location.y),2)+pow((entityarray.entity[i]->location.z-entityarray.entity[j]->location.z),2);
|
||||
long double d = sqrtl(ds);
|
||||
long double F = G*(entityarray.entity[i]->mass*entityarray.entity[j]->mass)/ds;
|
||||
entity_acceleration[i]->x += (F*(entityarray.entity[j]->location.x-entityarray.entity[i]->location.x)/d)/entityarray.entity[i]->mass;
|
||||
entity_acceleration[i]->y += (F*(entityarray.entity[j]->location.y-entityarray.entity[i]->location.y)/d)/entityarray.entity[i]->mass;
|
||||
entity_acceleration[i]->z += (F*(entityarray.entity[j]->location.z-entityarray.entity[i]->location.z)/d)/entityarray.entity[i]->mass;
|
||||
entity_acceleration[j]->x += (F*(entityarray.entity[i]->location.x-entityarray.entity[j]->location.x)/d)/entityarray.entity[j]->mass;
|
||||
entity_acceleration[j]->y += (F*(entityarray.entity[i]->location.y-entityarray.entity[j]->location.y)/d)/entityarray.entity[j]->mass;
|
||||
entity_acceleration[j]->z += (F*(entityarray.entity[i]->location.z-entityarray.entity[j]->location.z)/d)/entityarray.entity[j]->mass;
|
||||
}
|
||||
entityarray.entity[i]->velocity.x+=entity_acceleration[i]->x/simulation_rate;
|
||||
entityarray.entity[i]->velocity.y+=entity_acceleration[i]->y/simulation_rate;
|
||||
entityarray.entity[i]->velocity.z+=entity_acceleration[i]->z/simulation_rate;
|
||||
entityarray.entity[i]->location.x+=entityarray.entity[i]->velocity.x/simulation_rate;
|
||||
entityarray.entity[i]->location.y+=entityarray.entity[i]->velocity.y/simulation_rate;
|
||||
entityarray.entity[i]->location.z+=entityarray.entity[i]->velocity.z/simulation_rate;
|
||||
free(entity_acceleration[i]);
|
||||
}
|
||||
free(entity_acceleration);
|
||||
return;
|
||||
}
|
||||
|
||||
int main() {
|
||||
entityarray_initialize();
|
||||
struct entity* sun = entity_initialize();
|
||||
sun->mass=1000000;
|
||||
sun->r=20;
|
||||
sun->coluor.r=255;
|
||||
sun->coluor.g=192;
|
||||
sun->coluor.b=32;
|
||||
struct entity* earth = entity_initialize();
|
||||
earth->mass=15000;
|
||||
earth->location.x=200;
|
||||
earth->location.y=10;
|
||||
earth->velocity.z=150;
|
||||
earth->coluor.r=16;
|
||||
earth->coluor.g=64;
|
||||
earth->coluor.b=255;
|
||||
struct entity* mars = entity_initialize();
|
||||
mars->mass=10000;
|
||||
mars->location.x=-300;
|
||||
mars->location.y=15;
|
||||
mars->velocity.z=-100;
|
||||
mars->coluor.r=255;
|
||||
mars->coluor.g=64;
|
||||
mars->coluor.b=64;
|
||||
add_entity(sun);
|
||||
add_entity(earth);
|
||||
add_entity(mars);
|
||||
world_initialize();
|
||||
camera.location.x=200;
|
||||
camera.location.y=1;
|
||||
camera.location.z=200;
|
||||
camera.focal_length=1;
|
||||
struct vector light = {-1,-1,-1};
|
||||
change_light(light);
|
||||
char cmd[512];
|
||||
snprintf(cmd, sizeof(cmd),"ffmpeg -y -f rawvideo -pixel_format rgb24 -video_size %dx%d -framerate %d -i pipe:0 -c:v libx264 -preset fast -crf 23 -pix_fmt yuv420p output.mp4",resolution_x, resolution_y, video_rate);
|
||||
FILE *ffmpeg = popen(cmd, "w");
|
||||
if (!ffmpeg) {
|
||||
perror("popen failed");
|
||||
return 1;
|
||||
}
|
||||
size_t frame_size = resolution_x * resolution_y * 3;
|
||||
unsigned char *frame = malloc(frame_size);
|
||||
if (!frame) {
|
||||
perror("malloc failed");
|
||||
pclose(ffmpeg);
|
||||
return 1;
|
||||
}
|
||||
for (int i = 0; i < video_rate*video_length; i++) {
|
||||
frame_render(frame);
|
||||
video_render(frame,frame_size,ffmpeg);
|
||||
for(int j=0;j<simulation_rate/video_rate;j++){
|
||||
simulate();
|
||||
}
|
||||
}
|
||||
free(frame);
|
||||
pclose(ffmpeg);
|
||||
return 0;
|
||||
}
|
||||
/*
|
||||
ffmpeg -y -f rawvideo -pixel_format rgb24 -video_size %dx%d -framerate %d -i - -c:v libx264 -preset fast -crf 23 -pix_fmt yuv420p output.mp4
|
||||
|
||||
|
||||
*/
|
||||
@@ -0,0 +1,2 @@
|
||||
#需安装ffmpeg
|
||||
gcc -o 3drender 3drender.c -lm
|
||||
@@ -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;
|
||||
}
|
||||
Reference in New Issue
Block a user