- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我是一名 CS 学生,在我们的期末考试中,我们被告知要通过光线追踪在多个球体上构建反射。这几乎就是我们得到的指导,除了完成后的外观图片。所以我需要球体,它们是反射(使用光线追踪)映射到它们上的,并带有来自光线的适当阴影。
好吧,除了有多个球体,而且它看起来不像他给我们的标题图片那样,我所有的东西都在工作。
我不太确定如何做多个球体,但我会说我需要将它们存储在二维数组中并修改几段代码。
我的想法是修改 sphere_intersect 和 find_reflect 以包括正在分析的球体。接下来,修改 find_reflect 以便在计算新 vector u 时也更新其起点 (P0)。然后,如果光线击中球体,它将必须计算光线被反射了多少次。在某个时候终止(可能在 10 次之后),然后我将只绘制像素。为了增加触感,我想为球体添加纯色,我认为这需要找到球体的法线。
无论如何,我要附上一张他的照片,一张我的照片,以及源代码。希望有人可以帮助我解决这个问题。
提前致谢!
教授领域
我的领域
#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 万次就可以解决这个问题......一些无需为您做功课即可提供帮助的线索
一步一步来,不要试图一步一步重现图片
关于C++ OpenGL : Ray Trace Shading Isn't Properly Shading,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8469363/
我已经盯着它看了好几个小时了,想不出解决办法;我通常使用正则表达式处理这种类型的验证,但我正在尝试使用内置解决方案进行更改(显然,我不经常这样做): private static double pro
有什么区别 grep -isn "String\.format" -R . 和 grep -isn String\.format -R . 当我使用后者时,结果包括String format和Stri
我有一部固件为 4.3 的 iPhone 3GS。我下载了 3.1.3 固件,并希望使用 Organizer 恢复 iPhone。但!我收到以下错误: “此设备不符合所请求的版本。” 知道出了什么问题
我创建了一个小界面: import ... abstract class IController { void navigateTo(BuildContext context, String ro
基础this Question我使用扩展方法在 dart 中创建了一个枚举: enum TagVisibility { public, shared, private, } extensi
我在 Play 商店中放置了一个应用程序,我的 friend 正在运行 4.1(Nexus 7),在尝试安装我的应用程序时收到以下消息:“您的设备与此版本不兼容”。这是为什么来的?请任何人帮助我。 M
我在 Play 商店中放置了一个应用程序,我的 friend 在两台设备上运行 4.0.3,在尝试安装我的应用程序时收到以下消息:“您的设备与此版本不兼容”。 一台设备允许安装,另一台不允许。我允许
#!/usr/bin/env perl use warnings; use 5.12.2; my $c = 'f'; # could be a number too if ( $c eq 'd' ||
我正在编写一个Cocoa应用程序,我想实现一个全局热键功能。我实现了 Waffle Software 的 ShortcutRecorder.framework,并向我的 xib 添加了一个 custo
当我在开发该应用程序时,我点击了“运行”,一旦应用程序启动,它就会在打开启动屏幕之前卡住,并向我显示“应用程序没有响应,您想关闭它吗” 对话。!! !我什至没有打开闪屏类。!我在 OnCreate 中
大约 12 小时前,我尝试使用“beautiful jekyll”创建个人博客。 '。我遵循最简单的三步方法来运行一个基本的博客,但是,每次我输入我的网站链接时,我都会得到 There isn't a
我正在解析制表符分隔的文件。有几列没有被识别为数字,即使它们显然是数字。当我尝试总结这些值时,会显示错误:Argument ""97""isn't numeric in addition (+) 并且
我有两个模型:posts 和 likings 它们具有一对多关系(因此,一个帖子有很多喜欢)。 Likings 模型还有一个 isActive 字段,它显示喜欢是主动的还是被动的。 我想获得(排序)获
我的 android 模拟器(没有 play store 的 api 30)不时显示这个错误对话框: 我点击“关闭应用程序”,屏幕瞬间变黑,然后一切恢复正常。 发生这种情况时,我会在 Logcat 中
我正在尝试使用 Electron 构建 Windows 和 Mac OS 应用程序,但遇到了障碍。 简而言之,如果我尝试在 Mac OS Big Sur 上直接使用 Electron 运行应用程序(而
use List::MoreUtils 'uniq'; print join ", ", sort uniq ("b", "a", "a"); 结果参数“a”在排序中不是数字... print joi
background: ShaderMask( shaderCallback: (rect) { return LinearGradient( begin: Ali
我正在尝试创建一个多项选择应用程序来计算正确和错误答案的数量。 一切都呈现良好,但应用程序无法正常工作: 用户的选择并不能定义为正确或错误的答案。 问题不会更改为下一个问题。 我好像在onClick(
我正在开发一个需要帐户链接的 Google 助理应用程序,我已经到了这样的步骤:如果我说“与 [我的应用程序名称] 交谈”,我会取回一张卡片带有文本“Link [my app name] to Goo
编译时出现以下错误: MenuNavigationApp.c:58: error: array type has incomplete element typeMenuNavigationApp.c:
我是一名优秀的程序员,十分优秀!