gpt4 book ai didi

C++ OpenGL : Ray Trace Shading Isn't Properly Shading

转载 作者:搜寻专家 更新时间:2023-10-31 01:55:24 29 4
gpt4 key购买 nike

我是一名 CS 学生,在我们的期末考试中,我们被告知要通过光线追踪在多个球体上构建反射。这几乎就是我们得到的指导,除了完成后的外观图片。所以我需要球体,它们是反射(使用光线追踪)映射到它们上的,并带有来自光线的适当阴影。

好吧,除了有多个球体,而且它看起来不像他给我们的标题图片那样,我所有的东西都在工作。

我不太确定如何做多个球体,但我会说我需要将它们存储在二维数组中并修改几段代码。

我的想法是修改 sphere_intersect 和 find_reflect 以包括正在分析的球体。接下来,修改 find_reflect 以便在计算新 vector u 时也更新其起点 (P0)。然后,如果光线击中球体,它将必须计算光线被反射了多少次。在某个时候终止(可能在 10 次之后),然后我将只绘制像素。为了增加触感,我想为球体添加纯色,我认为这需要找到球体的法线。

无论如何,我要附上一张他的照片,一张我的照片,以及源代码。希望有人可以帮助我解决这个问题。

提前致谢!

教授领域

enter image description here

我的领域

enter image description here

#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include <GL/glut.h>
#include <math.h>
#include <string>

#define screen_width 750
#define screen_height 750
#define true 1
#define false 0
#define perpendicular 0

int gridXsize = 20;
int gridZsize = 20;
float plane[] = {0.0, 1.0, 0.0, -50.0,};
float sphere[] = {250.0, 270.0, -100.0, 100.0};
float eye[] = {0.0, 400.0, 550.0};
float light[] = {250.0, 550.0, -200.0};

float dot(float *u, float *v)
{
return u[0]*v[0] + u[1]*v[1] + u[2]*v[2];
}

void norm(float *u)
{
float norm = sqrt(abs(dot(u,u)));

for (int i =0; i <3; i++)
{
u[i] = u[i]/norm;
}

}

float plane_intersect(float *u, float *pO)
{
float normt[3] = {plane[0], plane[1], plane[2]};

float s;

if (dot(u,normt) == 0)
{
s = -10;
}

else
{
s = (plane[3]-(dot(pO,normt)))/(dot(u,normt));
}

return s;
}

float sphere_intersect(float *u, float *pO)
{

float deltaP[3] = {sphere[0]-pO[0],sphere[1]-pO[1],sphere[2]-pO[2]};
float deltLen = sqrt(abs(dot(deltaP,deltaP)));
float t=0;
float answer;
float det;

if ((det =(abs(dot(u,deltaP)*dot(u,deltaP))- (deltLen*deltLen)+sphere[3]*sphere[3])) < 0)
{
answer = -10;
}

else
{
t =-1*dot(u,deltaP)- sqrt(det) ;

if (t>0)
{
answer = t;
}

else
{
answer = -10;
}
}

return answer;
}

void find_reflect(float *u, float s, float *pO)
{
float n[3] = {pO[0]+s *u[0]-sphere[0],pO[1]+s *u[1]-sphere[1],pO[2]+s *u[2]- sphere[2]};
float l[3] = {s *u[0],s *u[1],s *u[2]};
u[0] =(2*dot(l,n)*n[0])-l[0];
u[1] = (2*dot(l,n)*n[1])-l[1];
u[2] = (2*dot(l,n)*n[2])-l[2];
}

float find_shade(float *u,float s, float *pO)
{
float answer;
float lightVec[3] = {light[0]-(pO[0]+s *u[0]), light[1]-(pO[1]+s *u[1]), light[2]-(pO[2]+s *u[2])};
float n[3] = {pO[0]+s *u[0]-sphere[0],pO[1]+s *u[1]-sphere[1],pO[2]+s *u[2]-sphere[2]};
answer = -1*dot(lightVec,n)/(sqrt(abs(dot(lightVec,lightVec)))*sqrt(abs(dot(n,n))));
return answer;
}

void init()
{
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0,screen_width,0,screen_height);
}

void display()
{
glClear(GL_COLOR_BUFFER_BIT| GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

for (int i=0; i < screen_width; i++)
{
for (int j=0; j < screen_height; j++)
{
float ray[3] = {1*(eye[0]-i),-1*(eye[1]-j),1*eye[2]};
float point[3] = {i,j,0};
norm(ray);
int plotted = false;

while (!plotted)
{
float s_plane = plane_intersect(ray, point);
float s_sphere = sphere_intersect(ray, point);

if (s_plane <= 0 && s_sphere <=0)
{
glColor3f(0,0,0);
glBegin(GL_POINTS);
glVertex3f(i,j,0);
glEnd();
plotted = true;
}

else if (s_sphere >= 0 && (s_plane <=0 || s_sphere <= s_plane))
{
find_reflect(ray, s_sphere, point);
}

else if (s_plane >=0 && (s_sphere <=0 ||s_plane <= s_sphere))
{
float shade = find_shade(ray, s_plane, point);
float xx = s_plane*ray[0] + eye[0];
float z = s_plane*ray[2] + eye[2];

if (abs((int)xx/gridXsize)%2 == abs((int)z/gridZsize)%2)
{
glColor3f(shade,0,0);
}

else
{
glColor3f(shade,shade,shade);
}

glBegin(GL_POINTS);
glVertex3f(i,j,0);
glEnd();
plotted = true;
}
}
}
}

glFlush();
}

int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutCreateWindow("Ray Trace with Sphere.");
glutInitWindowSize(screen_width,screen_height);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutDisplayFunc(display);
init();
glutMainLoop();
return 0;
}

最佳答案

教授并没有告诉你太多,因为这样的主题在网络上被覆盖了数千次,只需查看“Whitted Raytracing”;)这是家庭作业,谷歌搜索 500 万次就可以解决这个问题......一些无需为您做功课即可提供帮助的线索

一步一步来,不要试图一步一步重现图片

  • 让一个球体工作,如果击中平面绿色像素,球体红色像素,什么都没有,黑色。足以让交叉点计算正确。首先,从你的照片看来,你的十字路口不对
  • 与之前相同,有几个球体。与一个球体相同:检查所有对象的交点,保持距离视点最近的交点。
  • 与前面相同,但还计算找到的每个交点接收到的光量,球体为红色阴影,平面为绿色阴影。 (提示:点积^^)
  • 飞机的纹理
  • 球体的反射。提示:镜子不会反射 100% 的光,只会反射一小部分。

关于C++ OpenGL : Ray Trace Shading Isn't Properly Shading,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8469363/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com