SlideShare a Scribd company logo
1 of 9
Download to read offline
/* draw a sphere and use raytracing on the sphere in OpenGL glut. */
#include
#include
#include
#include
#include
#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;
}
Solution
/* draw a sphere and use raytracing on the sphere in OpenGL glut. */
#include
#include
#include
#include
#include
#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;
}

More Related Content

Similar to draw a sphere and use raytracing on the sphere in OpenGL glut. .pdf

Im trying again -Okay, Im in need of some help - this is the c.pdf
Im trying again -Okay, Im in need of some help - this is the c.pdfIm trying again -Okay, Im in need of some help - this is the c.pdf
Im trying again -Okay, Im in need of some help - this is the c.pdfeyeonsecuritysystems
 
C Code and the Art of Obfuscation
C Code and the Art of ObfuscationC Code and the Art of Obfuscation
C Code and the Art of Obfuscationguest9006ab
 
ARM 7 LPC 2148 lecture
ARM 7 LPC 2148 lectureARM 7 LPC 2148 lecture
ARM 7 LPC 2148 lectureanishgoel
 
The Ring programming language version 1.5.3 book - Part 65 of 184
The Ring programming language version 1.5.3 book - Part 65 of 184The Ring programming language version 1.5.3 book - Part 65 of 184
The Ring programming language version 1.5.3 book - Part 65 of 184Mahmoud Samir Fayed
 
i need an input of this program.  anything good or bad.  what could .docx
i need an input of this program.  anything good or bad.  what could .docxi need an input of this program.  anything good or bad.  what could .docx
i need an input of this program.  anything good or bad.  what could .docxursabrooks36447
 
Robot Motion Source code
Robot Motion Source codeRobot Motion Source code
Robot Motion Source codeBrian Goggins
 
The Ring programming language version 1.9 book - Part 65 of 210
The Ring programming language version 1.9 book - Part 65 of 210The Ring programming language version 1.9 book - Part 65 of 210
The Ring programming language version 1.9 book - Part 65 of 210Mahmoud Samir Fayed
 
[3] 프로세싱과 아두이노
[3] 프로세싱과 아두이노[3] 프로세싱과 아두이노
[3] 프로세싱과 아두이노Chiwon Song
 
i have a code that runs, but it only lets the player to guess where .pdf
i have a code that runs, but it only lets the player to guess where .pdfi have a code that runs, but it only lets the player to guess where .pdf
i have a code that runs, but it only lets the player to guess where .pdfpoblettesedanoree498
 
Snake report ROHIT MALAV
Snake report ROHIT MALAVSnake report ROHIT MALAV
Snake report ROHIT MALAVRohit malav
 
Senior design project code for PPG
Senior design project code for PPGSenior design project code for PPG
Senior design project code for PPGFrankDin1
 
i have a runable code below that works with just guessing where the .pdf
i have a runable code below that works with just guessing where the .pdfi have a runable code below that works with just guessing where the .pdf
i have a runable code below that works with just guessing where the .pdfarmcomputers
 
The following code, is a one player battleship game in JAVA. Im tryi.pdf
The following code, is a one player battleship game in JAVA. Im tryi.pdfThe following code, is a one player battleship game in JAVA. Im tryi.pdf
The following code, is a one player battleship game in JAVA. Im tryi.pdffonecomp
 

Similar to draw a sphere and use raytracing on the sphere in OpenGL glut. .pdf (20)

Im trying again -Okay, Im in need of some help - this is the c.pdf
Im trying again -Okay, Im in need of some help - this is the c.pdfIm trying again -Okay, Im in need of some help - this is the c.pdf
Im trying again -Okay, Im in need of some help - this is the c.pdf
 
C Code and the Art of Obfuscation
C Code and the Art of ObfuscationC Code and the Art of Obfuscation
C Code and the Art of Obfuscation
 
Proga 0622
Proga 0622Proga 0622
Proga 0622
 
Rkf
RkfRkf
Rkf
 
Sbaw090623
Sbaw090623Sbaw090623
Sbaw090623
 
ARM 7 LPC 2148 lecture
ARM 7 LPC 2148 lectureARM 7 LPC 2148 lecture
ARM 7 LPC 2148 lecture
 
The Ring programming language version 1.5.3 book - Part 65 of 184
The Ring programming language version 1.5.3 book - Part 65 of 184The Ring programming language version 1.5.3 book - Part 65 of 184
The Ring programming language version 1.5.3 book - Part 65 of 184
 
TRICK
TRICKTRICK
TRICK
 
i need an input of this program.  anything good or bad.  what could .docx
i need an input of this program.  anything good or bad.  what could .docxi need an input of this program.  anything good or bad.  what could .docx
i need an input of this program.  anything good or bad.  what could .docx
 
openGl example
openGl exampleopenGl example
openGl example
 
Robot Motion Source code
Robot Motion Source codeRobot Motion Source code
Robot Motion Source code
 
StewartPlatform_cpp
StewartPlatform_cppStewartPlatform_cpp
StewartPlatform_cpp
 
The Ring programming language version 1.9 book - Part 65 of 210
The Ring programming language version 1.9 book - Part 65 of 210The Ring programming language version 1.9 book - Part 65 of 210
The Ring programming language version 1.9 book - Part 65 of 210
 
[3] 프로세싱과 아두이노
[3] 프로세싱과 아두이노[3] 프로세싱과 아두이노
[3] 프로세싱과 아두이노
 
i have a code that runs, but it only lets the player to guess where .pdf
i have a code that runs, but it only lets the player to guess where .pdfi have a code that runs, but it only lets the player to guess where .pdf
i have a code that runs, but it only lets the player to guess where .pdf
 
Snake report ROHIT MALAV
Snake report ROHIT MALAVSnake report ROHIT MALAV
Snake report ROHIT MALAV
 
Senior design project code for PPG
Senior design project code for PPGSenior design project code for PPG
Senior design project code for PPG
 
i have a runable code below that works with just guessing where the .pdf
i have a runable code below that works with just guessing where the .pdfi have a runable code below that works with just guessing where the .pdf
i have a runable code below that works with just guessing where the .pdf
 
The following code, is a one player battleship game in JAVA. Im tryi.pdf
The following code, is a one player battleship game in JAVA. Im tryi.pdfThe following code, is a one player battleship game in JAVA. Im tryi.pdf
The following code, is a one player battleship game in JAVA. Im tryi.pdf
 
Connect 4
Connect 4Connect 4
Connect 4
 

More from aquacosmossystems

DnaAORC This protein is responsible for initiation of DNA replicat.pdf
DnaAORC This protein is responsible for initiation of DNA replicat.pdfDnaAORC This protein is responsible for initiation of DNA replicat.pdf
DnaAORC This protein is responsible for initiation of DNA replicat.pdfaquacosmossystems
 
Answer(19)Mary’s parents are normal, therefore her brother must.pdf
Answer(19)Mary’s parents are normal, therefore her brother must.pdfAnswer(19)Mary’s parents are normal, therefore her brother must.pdf
Answer(19)Mary’s parents are normal, therefore her brother must.pdfaquacosmossystems
 
Answer Q1. Red mangroove is the last mangroove to colonise on the.pdf
Answer Q1. Red mangroove is the last mangroove to colonise on the.pdfAnswer Q1. Red mangroove is the last mangroove to colonise on the.pdf
Answer Q1. Red mangroove is the last mangroove to colonise on the.pdfaquacosmossystems
 
An Arrhenius acid produces H+ ions insolutionA Bronsted-Lowry acid.pdf
An Arrhenius acid produces H+ ions insolutionA Bronsted-Lowry acid.pdfAn Arrhenius acid produces H+ ions insolutionA Bronsted-Lowry acid.pdf
An Arrhenius acid produces H+ ions insolutionA Bronsted-Lowry acid.pdfaquacosmossystems
 
Ans. One of the main disadvantage is chromatic aberrasion. A more li.pdf
Ans. One of the main disadvantage is chromatic aberrasion. A more li.pdfAns. One of the main disadvantage is chromatic aberrasion. A more li.pdf
Ans. One of the main disadvantage is chromatic aberrasion. A more li.pdfaquacosmossystems
 
A coordination problem occurs in situations where there is no Nash E.pdf
A coordination problem occurs in situations where there is no Nash E.pdfA coordination problem occurs in situations where there is no Nash E.pdf
A coordination problem occurs in situations where there is no Nash E.pdfaquacosmossystems
 
1. When an integrated F plasmid leaves the chromosome it may take so.pdf
1. When an integrated F plasmid leaves the chromosome it may take so.pdf1. When an integrated F plasmid leaves the chromosome it may take so.pdf
1. When an integrated F plasmid leaves the chromosome it may take so.pdfaquacosmossystems
 
1. A DNA molecule contains two strands of poly deoxyribonucleotide s.pdf
1. A DNA molecule contains two strands of poly deoxyribonucleotide s.pdf1. A DNA molecule contains two strands of poly deoxyribonucleotide s.pdf
1. A DNA molecule contains two strands of poly deoxyribonucleotide s.pdfaquacosmossystems
 
Ethyl 4-aminobenzoate is a basic organic compound. In basic and neut.pdf
  Ethyl 4-aminobenzoate is a basic organic compound. In basic and neut.pdf  Ethyl 4-aminobenzoate is a basic organic compound. In basic and neut.pdf
Ethyl 4-aminobenzoate is a basic organic compound. In basic and neut.pdfaquacosmossystems
 
SnO2 (ore) is initially washed and dressed. then .pdf
                     SnO2 (ore) is initially washed and dressed. then .pdf                     SnO2 (ore) is initially washed and dressed. then .pdf
SnO2 (ore) is initially washed and dressed. then .pdfaquacosmossystems
 
d) Valine Note its side chain doesnt contain a.pdf
                     d) Valine Note its side chain doesnt contain a.pdf                     d) Valine Note its side chain doesnt contain a.pdf
d) Valine Note its side chain doesnt contain a.pdfaquacosmossystems
 
Amphiprotic. The preferred term is amphoteric. A .pdf
                     Amphiprotic. The preferred term is amphoteric. A .pdf                     Amphiprotic. The preferred term is amphoteric. A .pdf
Amphiprotic. The preferred term is amphoteric. A .pdfaquacosmossystems
 
at equivalence point, Moles OH- added = moles H+.pdf
                     at equivalence point,  Moles OH- added = moles H+.pdf                     at equivalence point,  Moles OH- added = moles H+.pdf
at equivalence point, Moles OH- added = moles H+.pdfaquacosmossystems
 
We Know that     Water is a Polar component as .pdf
                     We Know that      Water is a Polar component as .pdf                     We Know that      Water is a Polar component as .pdf
We Know that     Water is a Polar component as .pdfaquacosmossystems
 
molar mass of C2H5OH = 46.1 gmole moles of C2H5O.pdf
                     molar mass of C2H5OH = 46.1 gmole moles of C2H5O.pdf                     molar mass of C2H5OH = 46.1 gmole moles of C2H5O.pdf
molar mass of C2H5OH = 46.1 gmole moles of C2H5O.pdfaquacosmossystems
 
What property of PN code makes them suitable for use in spread spect.pdf
What property of PN code makes them suitable for use in spread spect.pdfWhat property of PN code makes them suitable for use in spread spect.pdf
What property of PN code makes them suitable for use in spread spect.pdfaquacosmossystems
 
TLC can be used to help determine the number of components in a mixt.pdf
TLC can be used to help determine the number of components in a mixt.pdfTLC can be used to help determine the number of components in a mixt.pdf
TLC can be used to help determine the number of components in a mixt.pdfaquacosmossystems
 
There may have some variables affect the model. So the error term wi.pdf
There may have some variables affect the model. So the error term wi.pdfThere may have some variables affect the model. So the error term wi.pdf
There may have some variables affect the model. So the error term wi.pdfaquacosmossystems
 
There are four major functional characteristics of skeletal muscle..pdf
There are four major functional characteristics of skeletal muscle..pdfThere are four major functional characteristics of skeletal muscle..pdf
There are four major functional characteristics of skeletal muscle..pdfaquacosmossystems
 
The distinction is important due to the following reasons.The data.pdf
The distinction is important due to the following reasons.The data.pdfThe distinction is important due to the following reasons.The data.pdf
The distinction is important due to the following reasons.The data.pdfaquacosmossystems
 

More from aquacosmossystems (20)

DnaAORC This protein is responsible for initiation of DNA replicat.pdf
DnaAORC This protein is responsible for initiation of DNA replicat.pdfDnaAORC This protein is responsible for initiation of DNA replicat.pdf
DnaAORC This protein is responsible for initiation of DNA replicat.pdf
 
Answer(19)Mary’s parents are normal, therefore her brother must.pdf
Answer(19)Mary’s parents are normal, therefore her brother must.pdfAnswer(19)Mary’s parents are normal, therefore her brother must.pdf
Answer(19)Mary’s parents are normal, therefore her brother must.pdf
 
Answer Q1. Red mangroove is the last mangroove to colonise on the.pdf
Answer Q1. Red mangroove is the last mangroove to colonise on the.pdfAnswer Q1. Red mangroove is the last mangroove to colonise on the.pdf
Answer Q1. Red mangroove is the last mangroove to colonise on the.pdf
 
An Arrhenius acid produces H+ ions insolutionA Bronsted-Lowry acid.pdf
An Arrhenius acid produces H+ ions insolutionA Bronsted-Lowry acid.pdfAn Arrhenius acid produces H+ ions insolutionA Bronsted-Lowry acid.pdf
An Arrhenius acid produces H+ ions insolutionA Bronsted-Lowry acid.pdf
 
Ans. One of the main disadvantage is chromatic aberrasion. A more li.pdf
Ans. One of the main disadvantage is chromatic aberrasion. A more li.pdfAns. One of the main disadvantage is chromatic aberrasion. A more li.pdf
Ans. One of the main disadvantage is chromatic aberrasion. A more li.pdf
 
A coordination problem occurs in situations where there is no Nash E.pdf
A coordination problem occurs in situations where there is no Nash E.pdfA coordination problem occurs in situations where there is no Nash E.pdf
A coordination problem occurs in situations where there is no Nash E.pdf
 
1. When an integrated F plasmid leaves the chromosome it may take so.pdf
1. When an integrated F plasmid leaves the chromosome it may take so.pdf1. When an integrated F plasmid leaves the chromosome it may take so.pdf
1. When an integrated F plasmid leaves the chromosome it may take so.pdf
 
1. A DNA molecule contains two strands of poly deoxyribonucleotide s.pdf
1. A DNA molecule contains two strands of poly deoxyribonucleotide s.pdf1. A DNA molecule contains two strands of poly deoxyribonucleotide s.pdf
1. A DNA molecule contains two strands of poly deoxyribonucleotide s.pdf
 
Ethyl 4-aminobenzoate is a basic organic compound. In basic and neut.pdf
  Ethyl 4-aminobenzoate is a basic organic compound. In basic and neut.pdf  Ethyl 4-aminobenzoate is a basic organic compound. In basic and neut.pdf
Ethyl 4-aminobenzoate is a basic organic compound. In basic and neut.pdf
 
SnO2 (ore) is initially washed and dressed. then .pdf
                     SnO2 (ore) is initially washed and dressed. then .pdf                     SnO2 (ore) is initially washed and dressed. then .pdf
SnO2 (ore) is initially washed and dressed. then .pdf
 
d) Valine Note its side chain doesnt contain a.pdf
                     d) Valine Note its side chain doesnt contain a.pdf                     d) Valine Note its side chain doesnt contain a.pdf
d) Valine Note its side chain doesnt contain a.pdf
 
Amphiprotic. The preferred term is amphoteric. A .pdf
                     Amphiprotic. The preferred term is amphoteric. A .pdf                     Amphiprotic. The preferred term is amphoteric. A .pdf
Amphiprotic. The preferred term is amphoteric. A .pdf
 
at equivalence point, Moles OH- added = moles H+.pdf
                     at equivalence point,  Moles OH- added = moles H+.pdf                     at equivalence point,  Moles OH- added = moles H+.pdf
at equivalence point, Moles OH- added = moles H+.pdf
 
We Know that     Water is a Polar component as .pdf
                     We Know that      Water is a Polar component as .pdf                     We Know that      Water is a Polar component as .pdf
We Know that     Water is a Polar component as .pdf
 
molar mass of C2H5OH = 46.1 gmole moles of C2H5O.pdf
                     molar mass of C2H5OH = 46.1 gmole moles of C2H5O.pdf                     molar mass of C2H5OH = 46.1 gmole moles of C2H5O.pdf
molar mass of C2H5OH = 46.1 gmole moles of C2H5O.pdf
 
What property of PN code makes them suitable for use in spread spect.pdf
What property of PN code makes them suitable for use in spread spect.pdfWhat property of PN code makes them suitable for use in spread spect.pdf
What property of PN code makes them suitable for use in spread spect.pdf
 
TLC can be used to help determine the number of components in a mixt.pdf
TLC can be used to help determine the number of components in a mixt.pdfTLC can be used to help determine the number of components in a mixt.pdf
TLC can be used to help determine the number of components in a mixt.pdf
 
There may have some variables affect the model. So the error term wi.pdf
There may have some variables affect the model. So the error term wi.pdfThere may have some variables affect the model. So the error term wi.pdf
There may have some variables affect the model. So the error term wi.pdf
 
There are four major functional characteristics of skeletal muscle..pdf
There are four major functional characteristics of skeletal muscle..pdfThere are four major functional characteristics of skeletal muscle..pdf
There are four major functional characteristics of skeletal muscle..pdf
 
The distinction is important due to the following reasons.The data.pdf
The distinction is important due to the following reasons.The data.pdfThe distinction is important due to the following reasons.The data.pdf
The distinction is important due to the following reasons.The data.pdf
 

Recently uploaded

The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
MENTAL STATUS EXAMINATION format.docx
MENTAL     STATUS EXAMINATION format.docxMENTAL     STATUS EXAMINATION format.docx
MENTAL STATUS EXAMINATION format.docxPoojaSen20
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docxPoojaSen20
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesFatimaKhan178732
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting DataJhengPantaleon
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfSumit Tiwari
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 
Class 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdfClass 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdfakmcokerachita
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsanshu789521
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxOH TEIK BIN
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon AUnboundStockton
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application ) Sakshi Ghasle
 
URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppCeline George
 

Recently uploaded (20)

The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
MENTAL STATUS EXAMINATION format.docx
MENTAL     STATUS EXAMINATION format.docxMENTAL     STATUS EXAMINATION format.docx
MENTAL STATUS EXAMINATION format.docx
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docx
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and Actinides
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 
Class 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdfClass 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdf
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha elections
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptx
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon A
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application )
 
URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website App
 

draw a sphere and use raytracing on the sphere in OpenGL glut. .pdf

  • 1. /* draw a sphere and use raytracing on the sphere in OpenGL glut. */ #include #include #include #include #include #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) {
  • 2. 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]};
  • 3. 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);
  • 4. 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(); }
  • 5. 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; } Solution /* draw a sphere and use raytracing on the sphere in OpenGL glut. */ #include #include #include #include #include #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)
  • 6. { 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)
  • 7. { 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);
  • 8. 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
  • 9. { 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; }