Update scene module for C++
This commit is contained in:
		
							parent
							
								
									17344bb4cd
								
							
						
					
					
						commit
						568e3f4c55
					
				
					 2 changed files with 151 additions and 21 deletions
				
			
		
							
								
								
									
										126
									
								
								src/scene.cc
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										126
									
								
								src/scene.cc
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
					@ -0,0 +1,126 @@
 | 
				
			||||||
 | 
					/* scene.c
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * Definition of Scene-related functions.
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * Eryn Wells <eryn@erynwells.me>
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#include <cmath>
 | 
				
			||||||
 | 
					#include "basics.h"
 | 
				
			||||||
 | 
					#include "object.h"
 | 
				
			||||||
 | 
					#include "scene.h"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					Scene::Scene()
 | 
				
			||||||
 | 
					    : height(640), width(480),
 | 
				
			||||||
 | 
					      pixels(NULL)
 | 
				
			||||||
 | 
					{ }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					Scene::~Scene()
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					    if (pixels != NULL) {
 | 
				
			||||||
 | 
					        delete[] pixels;
 | 
				
			||||||
 | 
					        _is_rendered = false;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					bool
 | 
				
			||||||
 | 
					Scene::is_rendered()
 | 
				
			||||||
 | 
					    const
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					    return _is_rendered;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/*
 | 
				
			||||||
 | 
					 * scene_load --
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * Load scene objects into this Scene from the given file.
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					void
 | 
				
			||||||
 | 
					Scene::read(FILE *file)
 | 
				
			||||||
 | 
					{ }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/*
 | 
				
			||||||
 | 
					 * scene_save --
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * Write a rendered scene to the given file.
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					void
 | 
				
			||||||
 | 
					Scene::write(FILE *file)
 | 
				
			||||||
 | 
					{ }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/*
 | 
				
			||||||
 | 
					 * Scene::render --
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * Render the given Scene.
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					void
 | 
				
			||||||
 | 
					Scene::render()
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					    pixels = new Color[width * height];
 | 
				
			||||||
 | 
					    if (pixels == NULL) {
 | 
				
			||||||
 | 
					        // TODO: Print an error.
 | 
				
			||||||
 | 
					        return;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    Ray primary_ray;
 | 
				
			||||||
 | 
					    Vector3 o, d;
 | 
				
			||||||
 | 
					    for (int y = 0; y < height; y++) {
 | 
				
			||||||
 | 
					        for (int x = 0; x < width; x++) {
 | 
				
			||||||
 | 
					            // Assemble a ray and trace it.
 | 
				
			||||||
 | 
					            o = Vector3(x, y, -1000);
 | 
				
			||||||
 | 
					            d = Vector3(0, 0, 1);
 | 
				
			||||||
 | 
					            d.normalize();
 | 
				
			||||||
 | 
					            primary_ray = Ray(o, d);
 | 
				
			||||||
 | 
					            pixels[y * width + x] = trace_ray(primary_ray, 0);
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    _is_rendered = true;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					void
 | 
				
			||||||
 | 
					Scene::add_shape(Shape *shape)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					    shapes.push_back(shape);
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					Color
 | 
				
			||||||
 | 
					Scene::trace_ray(const Ray &ray, const int depth)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					    Color out_color = Color::Black;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    // Find intersections of this ray with objects in the scene.
 | 
				
			||||||
 | 
					    Shape *intersected_shape = NULL;
 | 
				
			||||||
 | 
					    float *t = NULL;
 | 
				
			||||||
 | 
					    float nearest_t = INFINITY;
 | 
				
			||||||
 | 
					    int nints;
 | 
				
			||||||
 | 
					    for (Shape *s : shapes) {
 | 
				
			||||||
 | 
					        nints = s->does_intersect(ray, &t);
 | 
				
			||||||
 | 
					        if (nints > 0) {
 | 
				
			||||||
 | 
					            for (int i = 0; i < nints; i++) {
 | 
				
			||||||
 | 
					                if (t[i] < nearest_t) {
 | 
				
			||||||
 | 
					                    intersected_shape = s;
 | 
				
			||||||
 | 
					                    nearest_t = t[i];
 | 
				
			||||||
 | 
					                }
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					            delete[] t;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    // If there was no intersection, return black.
 | 
				
			||||||
 | 
					    if (intersected_shape == NULL) {
 | 
				
			||||||
 | 
					        return out_color;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    // TODO: Lighting.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    return Color::Red;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
							
								
								
									
										46
									
								
								src/scene.h
									
										
									
									
									
								
							
							
						
						
									
										46
									
								
								src/scene.h
									
										
									
									
									
								
							| 
						 | 
					@ -1,6 +1,9 @@
 | 
				
			||||||
/* scene.h
 | 
					/* scene.h
 | 
				
			||||||
 *
 | 
					 *
 | 
				
			||||||
 * Definition of Scene type and related functions.
 | 
					 * Definition of the Scene class.
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * Scenes are the top level object in charles. Scenes contain objects, lights, a camera,
 | 
				
			||||||
 | 
					 * etc. and can be rendered to pixel data and written to a file.
 | 
				
			||||||
 *
 | 
					 *
 | 
				
			||||||
 * Eryn Wells <eryn@erynwells.me>
 | 
					 * Eryn Wells <eryn@erynwells.me>
 | 
				
			||||||
 */
 | 
					 */
 | 
				
			||||||
| 
						 | 
					@ -8,32 +11,33 @@
 | 
				
			||||||
#ifndef __SCENE_H__
 | 
					#ifndef __SCENE_H__
 | 
				
			||||||
#define __SCENE_H__
 | 
					#define __SCENE_H__
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#include <stdio.h>
 | 
					#include <list>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#include "camera.h"
 | 
					class Shape;
 | 
				
			||||||
#include "object.h"
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
typedef struct _ObjectList ObjectList;
 | 
					class Scene
 | 
				
			||||||
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
typedef struct _Scene
 | 
					 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
    int height, width;      /* Pixel dimensions. */
 | 
					public:
 | 
				
			||||||
    Camera *camera;
 | 
					    Scene();
 | 
				
			||||||
 | 
					    ~Scene();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    ObjectList *objects;
 | 
					    bool is_rendered() const;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    int is_rendered;
 | 
					    void read(FILE *f);
 | 
				
			||||||
 | 
					    void write(FILE *f);
 | 
				
			||||||
 | 
					    void render();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    void add_shape(Shape *obj);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					private:
 | 
				
			||||||
 | 
					    Color trace_ray(const Ray &ray, const int depth);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    int height, width;
 | 
				
			||||||
 | 
					    std::list<Shape *> shapes;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    bool _is_rendered;
 | 
				
			||||||
    Color *pixels;
 | 
					    Color *pixels;
 | 
				
			||||||
} Scene;
 | 
					};
 | 
				
			||||||
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
Scene *scene_init();
 | 
					 | 
				
			||||||
void scene_destroy(Scene *scene);
 | 
					 | 
				
			||||||
void scene_load(Scene *scene, FILE *scene_file);
 | 
					 | 
				
			||||||
void scene_render(Scene *scene);
 | 
					 | 
				
			||||||
void scene_add_object(Scene *scene, Object *object);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
#endif
 | 
					#endif
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue