#include <GL/glfw.h>
#include <stdlib.h>
#include <stdio.h>
#include "perspectiveCamera.h"
#include "csphere.h"
#include "intersectresult.h"
#include "plane.h"
#include "union.h"
#define WINDOW_WIDTH 600
#define WINDOW_HEIGHT 600
using namespace std;
void initScene(int w,int h)
{
// 启用阴影平滑
glShadeModel( GL_SMOOTH );
// 黑色背景
glClearColor( 0.0, 0.0, 0.0, 0.0 );
// 设置深度缓存
glClearDepth( 1.0 );
// 启用深度测试
glEnable( GL_DEPTH_TEST );
// 所作深度测试的类型
glDepthFunc( GL_LEQUAL );
// 告诉系统对透视进行修正
glHint( GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST );
}
//这里进行所有的绘图工作
void drawScene() {
float colorSpan=0.0005f;
float color=0.0f;
float pixelSize=2.0f;
float posY=-1.0f;
float posX=-1.0f;
long maxDepth=20;
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
//将原点移动到左下角
glTranslatef(-0.5f,-0.5f,-1.0f);
glPointSize(2.0);
glBegin(GL_POINTS);
double dx=1.0f/WINDOW_WIDTH;
double dy=1.0f/WINDOW_HEIGHT;
float dD=255.0f/maxDepth;
glBegin(GL_POINTS);
for (long y = 0; y < WINDOW_HEIGHT; ++y)
{
double sy = 1-dy*y;
for (long x = 0; x < WINDOW_WIDTH; ++x)
{
double sx =dx*x;
float colorR=x*1.0/WINDOW_WIDTH*255;
float colorB=y*1.0/WINDOW_HEIGHT*255;
glColor3ub(colorR,0,colorB);
glVertex2f(sx,sy);
}
}
// 交换缓冲区
glfwSwapBuffers();
}
void renderDepth()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity(); // Reset The View
glTranslatef(-0.5f,-0.5f,-1.0f);
glPointSize(2.0);
PerspectiveCamera camera( GVector3(0, 10, 10),GVector3(0, -0.5, -1),GVector3(0, 1, 0), 90);
Plane plane1(GVector3(0, 1, 0),1.0);
CSphere sphere1(GVector3(0, 5, -10), 5.0);
plane1.material=new CheckerMaterial(0.1f);
sphere1.material=new PhongMaterial(Color::red(), Color::white(), 16);
long maxDepth=20;
float dx=1.0f/WINDOW_WIDTH;
float dy=1.0f/WINDOW_HEIGHT;
float dD=255.0f/maxDepth;
glBegin(GL_POINTS);
for (long y = 0; y < WINDOW_HEIGHT; ++y)
{
float sy = 1 - dy*y;
for (long x = 0; x < WINDOW_WIDTH; ++x)
{
float sx =dx*x;
CRay ray(camera.generateRay(sx, sy));
IntersectResult result = sphere1.isIntersected(ray);
//IntersectResult result = plane1.isIntersected(ray);
if (result.isHit)
{
Color color = sphere1.material->sample(ray, result.position, result.normal);
//Color color =plane1.material->sample(ray, result.position, result.normal);
color.saturate();
//color.show();
glColor3ub(color.r*255,color.g*255,color.b*255);
glVertex2f(sx,sy);
}
}
}
glEnd();
// 交换缓冲区
glfwSwapBuffers();
}
Color rayTraceRecursive(CObject* scene,CRay& ray,long maxReflect) {
IntersectResult result = scene->isIntersected(ray);
if (result.object){
float reflectiveness = result.object->material->getRef();
Color color = result.object->material->sample(ray, result.position, result.normal);
color = color.multiply(1 - reflectiveness);
if ((reflectiveness > 0) && (maxReflect > 0)) {
GVector3 r = result.normal*(-2 * result.normal.dotMul(ray.getDirection()))+ray.getDirection();
CRay ray = CRay(result.position, r);
Color reflectedColor = rayTraceRecursive(scene, ray, maxReflect - 1);
color = color.add(reflectedColor.multiply(reflectiveness));
}
return color;
}else
return Color::black();
}
void renderUnion()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity(); // Reset The View
glTranslatef(-0.5f,-0.5f,-1.0f);
glPointSize(2.0);
PerspectiveCamera camera( GVector3(0, 10, 10),GVector3(0, -0.5, -1),GVector3(0, 1, 0), 90);
Plane* plane1=new Plane(GVector3(0, 1, 0),1.0);
CSphere* sphere1=new CSphere(GVector3(-2, 5, -10), 5.0);
CSphere* sphere2=new CSphere(GVector3(5, 5, -10), 3.0);
plane1->material=new CheckerMaterial(0.1f);
sphere1->material=new PhongMaterial(Color::red(), Color::white(), 16);
sphere2->material=new PhongMaterial(Color::blue(), Color::white(), 16);
Union scence;
scence.push(plane1);
scence.push(sphere1);
scence.push(sphere2);
long maxDepth=20;
float dx=1.0f/WINDOW_WIDTH;
float dy=1.0f/WINDOW_HEIGHT;
float dD=255.0f/maxDepth;
glBegin(GL_POINTS);
for (long y = 0; y < WINDOW_HEIGHT; ++y)
{
float sy = 1 - dy*y;
for (long x = 0; x < WINDOW_WIDTH; ++x)
{
float sx =dx*x;
CRay ray(camera.generateRay(sx, sy));
IntersectResult result = scence.isIntersected(ray);
//IntersectResult result = plane1.isIntersected(ray);
if (result.isHit)
{
Color color = result.object->material->sample(ray, result.position, result.normal);
//Color color =plane1.material->sample(ray, result.position, result.normal);
color.saturate();
//color.show();
glColor3ub(color.r*255,color.g*255,color.b*255);
glVertex2f(sx,sy);
}
}
}
glEnd();
// 交换缓冲区
glfwSwapBuffers();
}
void renderRecursive()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity(); // Reset The View
glTranslatef(-0.5f,-0.5f,-1.0f);
glPointSize(2.0);
PerspectiveCamera camera( GVector3(0, 10, 10),GVector3(0, -0.5, -1),GVector3(0, 1, 0), 90);
Plane* plane1=new Plane(GVector3(0, 1, 0),1.0);
CSphere* sphere1=new CSphere(GVector3(-2, 5, -2), 4.0);
CSphere* sphere2=new CSphere(GVector3(5, 5, -7), 3.0);
plane1->material=new CheckerMaterial(0.1f,0.5f);
sphere1->material=new PhongMaterial(Color::red(), Color::white(), 16,0.25);
sphere2->material=new PhongMaterial(Color::green(), Color::white(), 16,0.25);
Union scene;
scene.push(plane1);
scene.push(sphere1);
scene.push(sphere2);
long maxDepth=20;
long maxReflect=5;
float dx=1.0f/WINDOW_WIDTH;
float dy=1.0f/WINDOW_HEIGHT;
float dD=255.0f/maxDepth;
glBegin(GL_POINTS);
for (long y = 0; y < WINDOW_HEIGHT; ++y)
{
float sy = 1 - dy*y;
for (long x = 0; x < WINDOW_WIDTH; ++x)
{
float sx =dx*x;
CRay ray(camera.generateRay(sx, sy));
IntersectResult result = scene.isIntersected(ray);
//IntersectResult result = plane1.isIntersected(ray);
if (result.isHit)
{
Color color = rayTraceRecursive(&scene, ray, maxReflect);
//Color color = result.object->material->sample(ray, result.position, result.normal);
//Color color =plane1.material->sample(ray, result.position, result.normal);
color.saturate();
//color.show();
glColor3ub(color.r*255,color.g*255,color.b*255);
glVertex2f(sx,sy);
}
}
}
glEnd();
// 交换缓冲区
glfwSwapBuffers();
}
//重置窗口大小后的回调函数
void GLFWCALL resizeGL(int width, int height )
{
// 防止窗口大小变为0
if ( height == 0 )
{
height = 1;
}
// 重置当前的视口
glViewport( 0, 0, (GLint)width, (GLint)height );
// 选择投影矩阵
glMatrixMode( GL_PROJECTION );
// 重置投影矩阵
glLoadIdentity();
// 设置视口的大小
gluPerspective( 45.0, (GLfloat)width/(GLfloat)height, 0.1, 100.0 );
// 选择模型观察矩阵
glMatrixMode( GL_MODELVIEW );
glLoadIde
- 1
- 2
- 3
前往页