基础代码提交
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
|
||||
|
||||
|
||||
*/
|
||||
Reference in New Issue
Block a user