Skip to content

Commit

Permalink
Added skybox
Browse files Browse the repository at this point in the history
Skybox and bug fix
  • Loading branch information
weidaru committed Apr 27, 2013
1 parent 7d10f18 commit d7fbc8a
Show file tree
Hide file tree
Showing 10 changed files with 68,627 additions and 18,976 deletions.
70 changes: 70 additions & 0 deletions water_rendering/include/ImageManager.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#ifndef IMAGE_MANAGER_H_
#define IMAGE_MANAGER_H_

#include <map>
#include <stdio.h>
#include <assert.h>

#include "Gz.h"

struct Image
{
int width, height;
GzColor* data;

inline const float* GetPixel(int x, int y)
{
return data[y*width + x];
}
};


/*
* Manager to create and store ppm images.
*/
class ImageManager
{
private:
typedef std::map<std::string, Image*> ImageMap;

public:
ImageManager() {}
~ImageManager() {}

static ImageManager* GetSingleton()
{
static ImageManager manager;
return &manager;
}

Image* GetImage(const std::string& name, const std::string& file_path)
{
if(data_.find(name) != data_.end())
return data_[name];
FILE* fd = fopen (file_path.c_str(), "rb");
assert(fd);
Image* image = new Image;
data_[name] = image;
char foo[8];
unsigned char pixel[3];
unsigned char dummy;
fscanf (fd, "%s %d %d %c", pixel, &image->width, &image->height, &dummy);
image->data = (GzColor*)malloc(sizeof(GzColor)*(image->width+1)*(image->height+1));
assert(image);

for (int i = 0; i <image->width*image->height; i++) { /* create array of GzColor values */
fread(pixel, sizeof(pixel), 1, fd);
image->data[i][RED] = (float)((int)pixel[RED]) * (1.0f / 255.0f);
image->data[i][GREEN] = (float)((int)pixel[GREEN]) * (1.0f / 255.0f);
image->data[i][BLUE] = (float)((int)pixel[BLUE]) * (1.0f / 255.0f);
}
fclose(fd);
return image;
}

private:
ImageMap data_;
};


#endif //IMAGE_MANAGER_H_
145 changes: 73 additions & 72 deletions water_rendering/include/rend.h
Original file line number Diff line number Diff line change
@@ -1,73 +1,74 @@
#ifndef RENDER_H_
#define RENDER_H_

#include "Gz.h"
#include "disp.h" /* include your own disp.h file (e.g. hw1)*/

#define MATLEVELS 100 /* how many matrix pushes allowed */
#define MAX_LIGHTS 10 /* how many lights allowed */
#define MAX_TEXTURE 10

#ifndef GZRENDER
#define GZRENDER

struct PixelShaderInput;
struct GzRender;

struct GzRender { /* define a renderer */
typedef void (*VertexShader)(GzRender *, int, const GzToken *, const GzPointer *, PixelShaderInput[3]);
typedef void (*PixelShader)(GzRender* , const PixelShaderInput&, GzColor);

GzRenderClass renderClass;
GzDisplay *display;
short open;
GzCamera camera;
short matlevel; /* top of stack - current xform */
GzMatrix Ximage[MATLEVELS]; /* stack of xforms (Xsm) */
GzMatrix Xnorm[MATLEVELS]; /* xforms for norms (Xim) */
GzMatrix Xsp; /* NDC to screen (pers-to-screen) */
GzMatrix Xwm;
GzMatrix Xsm;
GzMatrix Xsm_inverse;
GzMatrix Xn; //Transformation for normals from Object to World space.
GzMatrix Xn_inverse;
GzColor flatcolor; /* color state for flat shaded triangles */
int interp_mode;
int numlights;
GzLight lights[MAX_LIGHTS];
GzLight ambientlight;
GzColor Ka, Kd, Ks;
float spec; /* specular power */
GzTexture tex_fun[MAX_TEXTURE]; /* tex_fun(float u, float v, GzColor color) */
int tex_count;
VertexShader v_shader;
PixelShader p_shader;
bool show_wireframe;
GzDisplay* texture_display[MAX_TEXTURE];
};
#endif

// Function declaration
// HW2
int GzNewRender(GzRender **render, GzRenderClass renderClass, GzDisplay *display);
int GzFreeRender(GzRender *render);
int GzBeginRender(GzRender *render);
int GzPutAttribute(GzRender *render, int numAttributes, GzToken *nameList,
GzPointer *valueList);
int GzPutTriangle(GzRender *render, int numParts, GzToken *nameList,
GzPointer *valueList);

// HW3
int GzPutCamera(GzRender *render, GzCamera *camera);
int GzPushMatrix(GzRender *render, GzMatrix matrix);
int GzPopMatrix(GzRender *render);

// Object Translation
int GzRotXMat(float degree, GzMatrix mat);
int GzRotYMat(float degree, GzMatrix mat);
int GzRotZMat(float degree, GzMatrix mat);
int GzTrxMat(GzCoord translate, GzMatrix mat);
int GzScaleMat(GzCoord scale, GzMatrix mat);


#ifndef RENDER_H_
#define RENDER_H_

#include "Gz.h"
#include "disp.h" /* include your own disp.h file (e.g. hw1)*/

#define MATLEVELS 100 /* how many matrix pushes allowed */
#define MAX_LIGHTS 10 /* how many lights allowed */
#define MAX_TEXTURE 10

#ifndef GZRENDER
#define GZRENDER

struct PixelShaderInput;
struct GzRender;

struct GzRender { /* define a renderer */
typedef void (*VertexShader)(GzRender *, int, const GzToken *, const GzPointer *, PixelShaderInput[3]);
typedef void (*PixelShader)(GzRender* , const PixelShaderInput&, GzColor);

GzRenderClass renderClass;
GzDisplay *display;
short open;
GzCamera camera;
short matlevel; /* top of stack - current xform */
GzMatrix Ximage[MATLEVELS]; /* stack of xforms (Xsm) */
GzMatrix Xnorm[MATLEVELS]; /* xforms for norms (Xim) */
GzMatrix Xsp; /* NDC to screen (pers-to-screen) */
GzMatrix Xwm;
GzMatrix Xsm;
GzMatrix Xsm_inverse;
GzMatrix Xn; //Transformation for normals from Object to World space.
GzMatrix Xn_inverse;
GzColor flatcolor; /* color state for flat shaded triangles */
int interp_mode;
int numlights;
GzLight lights[MAX_LIGHTS];
GzLight ambientlight;
GzColor Ka, Kd, Ks;
float spec; /* specular power */
GzTexture tex_fun[MAX_TEXTURE]; /* tex_fun(float u, float v, GzColor color) */
char tex_name[MAX_TEXTURE][64];
int tex_count;
VertexShader v_shader;
PixelShader p_shader;
bool show_wireframe;
GzDisplay* texture_display[MAX_TEXTURE];
};
#endif

// Function declaration
// HW2
int GzNewRender(GzRender **render, GzRenderClass renderClass, GzDisplay *display);
int GzFreeRender(GzRender *render);
int GzBeginRender(GzRender *render);
int GzPutAttribute(GzRender *render, int numAttributes, GzToken *nameList,
GzPointer *valueList);
int GzPutTriangle(GzRender *render, int numParts, GzToken *nameList,
GzPointer *valueList);

// HW3
int GzPutCamera(GzRender *render, GzCamera *camera);
int GzPushMatrix(GzRender *render, GzMatrix matrix);
int GzPopMatrix(GzRender *render);

// Object Translation
int GzRotXMat(float degree, GzMatrix mat);
int GzRotYMat(float degree, GzMatrix mat);
int GzRotZMat(float degree, GzMatrix mat);
int GzTrxMat(GzCoord translate, GzMatrix mat);
int GzScaleMat(GzCoord scale, GzMatrix mat);


#endif //RENDER_H_
113 changes: 58 additions & 55 deletions water_rendering/include/shaders.h
Original file line number Diff line number Diff line change
@@ -1,56 +1,59 @@
#ifndef SHADERS_H_
#define SHADERS_H_

#include "rend.h"

struct PixelShaderInput
{
GzCoord positon;
GzCoord normal;
GzTextureIndex texture;
GzColor color;
//alpha vary from 0 to 100
GzIntensity alpha;

static bool lerp_normal;
static bool lerp_texture;
static bool lerp_color;

void Assign(const GzCoord& p, const GzCoord& n, const GzTextureIndex& t, const GzColor& c)
{
for(int i=0; i<3; i++)
{
positon[i] = p[i];
normal[i] = n[i];
color[i] = c[i];
}

for(int i=0; i<2; i++)
{
texture[i] = t[i];
}
}
};

void FlatVertexShader(GzRender *render, int numParts, const GzToken *nameList, const GzPointer *valueList, PixelShaderInput vs_output[3]);
void FlatPixelShader(GzRender* render, const PixelShaderInput& input, GzColor color);

void GouraudVertexShader(GzRender *render, int numParts, const GzToken *nameList, const GzPointer *valueList, PixelShaderInput vs_output[3]);
void GouraudPixelShader(GzRender* render, const PixelShaderInput& input, GzColor color);

void GouraudRefractionPixelShader(GzRender* render, const PixelShaderInput& input, GzColor color);
void GouraudReflectionPixelShader(GzRender* render, const PixelShaderInput& input, GzColor color);

void PhongVertexShader(GzRender *render, int numParts, const GzToken *nameList, const GzPointer *valueList, PixelShaderInput vs_output[3]);
void PhongPixelShader(GzRender* render, const PixelShaderInput& input, GzColor color);

void PhongTextureVS(GzRender *render, int numParts, const GzToken *nameList, const GzPointer *valueList, PixelShaderInput vs_output[3]);
void PhongTexturePS(GzRender* render, const PixelShaderInput& input, GzColor color);

void GlobalReflectionVS(GzRender *render, int numParts, const GzToken *nameList, const GzPointer *valueList, PixelShaderInput vs_output[3]);
void GlobalReflectionPS(GzRender* render, const PixelShaderInput& input, GzColor color);

void FinalWaterVS(GzRender *render, int numParts, const GzToken *nameList, const GzPointer *valueList, PixelShaderInput vs_output[3]);
void FinalWaterPS(GzRender* render, const PixelShaderInput& input, GzColor color);

#ifndef SHADERS_H_
#define SHADERS_H_

#include "rend.h"

struct PixelShaderInput
{
GzCoord positon;
GzCoord normal;
GzTextureIndex texture;
GzColor color;
//alpha vary from 0 to 100
GzIntensity alpha;

static bool lerp_normal;
static bool lerp_texture;
static bool lerp_color;

void Assign(const GzCoord& p, const GzCoord& n, const GzTextureIndex& t, const GzColor& c)
{
for(int i=0; i<3; i++)
{
positon[i] = p[i];
normal[i] = n[i];
color[i] = c[i];
}

for(int i=0; i<2; i++)
{
texture[i] = t[i];
}
}
};

void FlatVertexShader(GzRender *render, int numParts, const GzToken *nameList, const GzPointer *valueList, PixelShaderInput vs_output[3]);
void FlatPixelShader(GzRender* render, const PixelShaderInput& input, GzColor color);

void GouraudVertexShader(GzRender *render, int numParts, const GzToken *nameList, const GzPointer *valueList, PixelShaderInput vs_output[3]);
void GouraudPixelShader(GzRender* render, const PixelShaderInput& input, GzColor color);

void GouraudRefractionPixelShader(GzRender* render, const PixelShaderInput& input, GzColor color);
void GouraudReflectionPixelShader(GzRender* render, const PixelShaderInput& input, GzColor color);

void PhongVertexShader(GzRender *render, int numParts, const GzToken *nameList, const GzPointer *valueList, PixelShaderInput vs_output[3]);
void PhongPixelShader(GzRender* render, const PixelShaderInput& input, GzColor color);

void PhongTextureVS(GzRender *render, int numParts, const GzToken *nameList, const GzPointer *valueList, PixelShaderInput vs_output[3]);
void PhongTexturePS(GzRender* render, const PixelShaderInput& input, GzColor color);

void GlobalReflectionVS(GzRender *render, int numParts, const GzToken *nameList, const GzPointer *valueList, PixelShaderInput vs_output[3]);
void GlobalReflectionPS(GzRender* render, const PixelShaderInput& input, GzColor color);

void FinalWaterVS(GzRender *render, int numParts, const GzToken *nameList, const GzPointer *valueList, PixelShaderInput vs_output[3]);
void FinalWaterPS(GzRender* render, const PixelShaderInput& input, GzColor color);

void SkyboxVS(GzRender *render, int numParts, const GzToken *nameList, const GzPointer *valueList, PixelShaderInput vs_output[3]);
void SkyboxPS(GzRender* render, const PixelShaderInput& input, GzColor color);

#endif //SHADERS_H_
1 change: 1 addition & 0 deletions water_rendering/skybox.asc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
skybox_triangle----------left----------------8.0 8.0 -8.0 0.0 1.0 0.0 0.0 0.0-8.0 -8.0 -8.0 0.0 1.0 0.0 0.0 1.0-8.0 8.0 8.0 0.0 1.0 0.0 1.0 0.0skybox_triangle-8.0 8.0 8.0 0.0 1.0 0.0 1.0 0.0-8.0 -8.0 -8.0 0.0 1.0 0.0 0.0 1.0-8.0 -8.0 8.0 0.0 1.0 0.0 1.0 1.0skybox_triangle----------down----------------8.0 -8.0 8.0 0.0 1.0 0.0 0.0 0.0-8.0 -8.0 -8.0 0.0 1.0 0.0 0.0 1.08.0 -8.0 8.0 0.0 1.0 0.0 1.0 0.0skybox_triangle8.0 -8.0 8.0 0.0 1.0 0.0 1.0 0.0-8.0 -8.0 -8.0 0.0 1.0 0.0 0.0 1.08.0 -8.0 -8.0 0.0 1.0 0.0 1.0 1.0skybox_triangle----------back---------------8.0 8.0 -8.0 0.0 1.0 0.0 0.0 0.08.0 -8.0 -8.0 0.0 1.0 0.0 0.0 1.0-8.0 8.0 -8.0 0.0 1.0 0.0 1.0 0.0skybox_triangle-8.0 8.0 -8.0 0.0 1.0 0.0 1.0 0.08.0 -8.0 -8.0 0.0 1.0 0.0 0.0 1.0-8.0 -8.0 -8.0 0.0 1.0 0.0 1.0 1.0skybox_triangle----------right---------------8.0 8.0 8.0 0.0 1.0 0.0 0.0 0.08.0 -8.0 8.0 0.0 1.0 0.0 0.0 1.08.0 8.0 -8.0 0.0 1.0 0.0 1.0 0.0skybox_triangle8.0 8.0 -8.0 0.0 1.0 0.0 1.0 0.08.0 -8.0 8.0 0.0 1.0 0.0 0.0 1.08.0 -8.0 -8.0 0.0 1.0 0.0 1.0 1.0skybox_triangle----------up----------------8.0 8.0 8.0 0.0 1.0 0.0 0.0 1.0-8.0 8.0 -8.0 0.0 1.0 0.0 0.0 0.08.0 8.0 8.0 0.0 1.0 0.0 1.0 1.0skybox_triangle8.0 8.0 8.0 0.0 1.0 0.0 1.0 1.0-8.0 8.0 -8.0 0.0 1.0 0.0 0.0 0.08.0 8.0 -8.0 0.0 1.0 0.0 1.0 0.0skybox_triangle----------front----------------8.0 8.0 8.0 0.0 1.0 0.0 0.0 0.0-8.0 -8.0 8.0 0.0 1.0 0.0 0.0 1.08.0 8.0 8.0 0.0 1.0 0.0 1.0 0.0skybox_triangle8.0 8.0 8.0 0.0 1.0 0.0 1.0 0.0-8.0 -8.0 8.0 0.0 1.0 0.0 0.0 1.08.0 -8.0 8.0 0.0 1.0 0.0 1.0 1.0
Expand Down
Loading

0 comments on commit d7fbc8a

Please sign in to comment.