Goal: I am trying to create a ray tracer in C. I just added in a light source that should give each of my three spheres a shading effect based on where the light is. If the light is to the left of all of them, a shadow should be cased on the right.
Problem: When changing the light intensities and position of the light, all the spheres are changed uniformly. The spheres will be more or less lit equally and there is no variation of lighting on individual pixels on the sphere.
My debugging attempts: I have tried looking through the variable outputs by printing out a lot of different info and I think the source comes from my variable
diffuse_light_intensity
which does not change much (through all the iterations on the screen the value changes twice when it should be changing quite often due to the angles of the light on the surface changing quite a bit)
My Code: (my theory is the problem lies in scene_intersect() or cast_ray())
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <math.h>
#include <limits.h>
typedef struct {
float position[3];
float intensity;
} Light;
typedef struct {
float diffuse_color[3];
} Material;
typedef struct {
float center[3];
float radius;
Material material;
} Sphere;
int arrSub(const float arr1[], const float arr2[], float subArr[], int length) {
/*
Requires 3 equally sized arrays (denoted as length),
arr1 - arr2 will result in the third array subArr
*/
for (int i = 0; i < length; i++) {
subArr[i] = arr1[i] - arr2[i];
}
return 0;
}
int arrAdd(const float arr1[], const float arr2[], float addArr[], int length) {
/*
Requires 3 equally sized arrays (denoted as length),
arr1 + arr2 will result in the third array subArr
*/
for (int i = 0; i < length; i++) {
addArr[i] = arr1[i] + arr2[i];
}
return 0;
}
int arrScalarMult(const float arr1[], float scalar, float newArr[], int length) {
/*
Requires 3 equally sized arrays (denoted as length),
arr1 - arr2 will result in the third array subArr
*/
for (int i = 0; i < length; i++) {
newArr[i] = arr1[i] * scalar;
}
return 0;
}
float dotProduct(const float arr1[], const float arr2[], int length) {
/*
Returns the dot product of two equal sized arrays
(treated as vectors)
a (dot) b = a1b1 + a2b2 + ... anbn
*/
float result = 0;
for (int i = 0; i < length; i++) {
result += arr1[i] * arr2[i];
}
return result;
}
int normalize(float arr[], int len) {
//Normalize a vector (array)
float sumSqr;
float norm;
for (int i = 0; i < len; i++) {
sumSqr += arr[i] * arr[i];
}
norm = sqrt(sumSqr);
for (int i = 0; i < len; i++) {
arr[i] = arr[i] / norm;
}
return 0;
}
bool ray_intersect(const float origin[], const float dir[], float t0, Sphere s) {
/*
Ray-Sphere Intersection
Vectors:
origin (the zero vector)
dir (direction vector)
L (vector from origin to center of sphere)
Scalars:
tca
d2
thc
t0
t1
*/
float L[3] = {0,0,0}; //The zero vector
arrSub(s.center, origin, L, 3); //L is now the vector from origin to the sphere's center
float tca = dotProduct(L, dir, 3); //Projection of L onto dir
float d2 = dotProduct(L, L, 3) - tca*tca;
if (d2 > s.radius * s.radius) return false; //There is no intersection, so return false.
float thc = sqrtf((s.radius*s.radius - d2));
t0 = tca - thc;
float t1 = tca + thc;
if (t0 < 0) {
t0 = t1;
}
if (t0 < 0) return false;
return true;
}
bool scene_intersect(const float origin[], const float dir[], const Sphere s[], int len, float hit[], float N[], Material * ptr_m) {
float sphere_dist = INT_MAX;
for (size_t i=0; i < len; i++) {
float dist_i;
if (ray_intersect(origin, dir, dist_i, s[i]) && dist_i < sphere_dist) {
sphere_dist = dist_i;
float dirDist[3];
arrScalarMult(dir, dist_i, dirDist, 3);
arrAdd(origin, dirDist, hit, 3);
float hitMinusCenter[3];
arrSub(hit, s[i].center, hitMinusCenter, 3);
normalize(hitMinusCenter, 3);
N[0] = hitMinusCenter[0];
N[1] = hitMinusCenter[1];
N[2] = hitMinusCenter[2];
* ptr_m = s[i].material;
}
}
return sphere_dist<1000;
}
int cast_ray(const float origin[], const float dir[], const Sphere s[], const Light l[], int l_size, unsigned char colorArr[]) {
float point[3], N[3];
Material m;
Material * ptr_m = &m;
if (!scene_intersect(origin, dir, s, 3, point, N, ptr_m)) {
//background
colorArr[0] = 5; //red
colorArr[1] = 100; //green
colorArr[2] = 250; //blue
} else {
float diffuse_light_intensity = 0;
float light_dir[3];
for (size_t i = 0; i < l_size; i++) {
arrSub(l[i].position, point, light_dir, 3);
normalize(light_dir, 3);
diffuse_light_intensity += l[i].intensity * ((0.f >= dotProduct(light_dir, N, 3) ? (0.f) : (dotProduct(light_dir, N, 3))));
}
//light up pixel
colorArr[0] = m.diffuse_color[0] * diffuse_light_intensity;
colorArr[1] = m.diffuse_color[1] * diffuse_light_intensity;
colorArr[2] = m.diffuse_color[2] * diffuse_light_intensity;
}
return 0;
}
int render(const Sphere s[], const Light l[], int l_length) {
/*
Creates image in a new color each step.
*/
const int width = 1024;
const int height = 768;
FILE *fp = fopen("fourth.ppm", "wb"); // Write in binary mode
(void) fprintf(fp, "P6\n%d %d\n255\n", width, height);
float fov = 3.1415926535/2.; // Field of View
#pragma omp parallel for
for (size_t j = 0; j < height; j++) {
for (size_t i = 0; i < width; i++) {
float x = (2*(i+.5)/(float)width - 1)*tan(fov/2.)*width/(float)height;
float y = -(2*(j+.5)/(float)height - 1)*tan(fov/2.);
float dir[] = {x,y,-1};
normalize(dir, 3);
unsigned char color[3];
const float origin[] = {0,0,0};
cast_ray(origin, dir, s, l, l_length, color);
(void) fwrite(color, 1, 3, fp);
}
}
(void) fclose(fp);
return 0;
}
int main(void) {
Material red = {255,0,0};
Material pink = {150,10,150};
Material gold = {255, 195, 0};
//Populate with spheres
Sphere s[3];
Sphere originalS = {{-3,0,-16},2,gold};
Sphere bigS = {{-1.0, -1.5, -12}, 3, red};
Sphere anotherS = {{7,5,-18},2,pink};
s[0] = originalS;
s[1] = bigS;
s[2] = anotherS;
//Add light source
Light l[1];
Light test_light = {{-20,20,20}, 1.5};
l[0] = test_light;
render(s,l, 1);
printf("Run success!\n");
return 0;
}
If any clarification is needed on my code please let me know, I am quite new to both C and stackoverflow.
There's a fundamental error in ray_intersect where you're passing the t0 variable by value, and not as a pointer, and therefore in the scene_intersect function its value is always zero.
The other problem is that you don't initialize the sumSqr in the normalize function, resulting in that function returning NaN for each vector component.
With those two fixed I get something approximating shaded balls. The errors in that image are caused by failing to ensure that your output pixel values fall in the range [0, 255].
NB: both of these first errors are detected if you turn on full compiler error checking, warning you of uninitialised variables being used.
In 'C', I have:
typedef struct
{
int aaa;
int bbb;
} My_Struct;
And I want to make a constant script for a regression test that contains multiple copies of My_Struct with initialized values, but also with the labels (aaa & bbb) for readability and convenience.
I can't figure out the syntax, or if this is even possible in C.
The closest I can get it is this:
struct
{
struct {
int aaa = 111;
int bbb = 222;
} first_script;
struct {
int aaa = 333;
int bbb = 444;
} second_script;
} const my_script_array;
But how do I define my_script_array to be an array of type My_Struct so that I don't have to cast it like (My_Struct)my_script_array in the code?
(COMPILER NOTE: I'm editing this software using Visual-C++, but it runs on Xcode 7.3 for ios, and ultimately it needs to compile on an ARM embedded processor.)
NEXT ATTEMPT:
The following code has no errors on Visual-C++ but gets Xcode error "expected ;" before the first '=' and also for the next dozen lines.
// Script of multiple reference summaries
struct
{
struct
{
// WORDS:
int total_words = 1;
float all_absolute_impulse_gseconds = 2; // gravity X seconds
float all_average_absolute_impulse_gseconds = 3;
float all_positive_impulse_gseconds = 4;
float all_negative_impulse_gseconds = 5;
float x_absolute_impulse_gseconds = 6;
float x_average_absolute_impulse_gseconds = 7;
float x_positive_impulse_gseconds = 8;
float x_positive_average_impulse_gseconds = 9;
float y_absolute_impulse_gseconds = 10;
float y_average_absolute_impulse_gseconds = 11;
float z_absolute_impulse_gseconds = 12;
float z_average_absolute_impulse_gseconds = 13;
float minimum_word_duration_seconds = 14;
float average_word_duration_seconds = 15;
float maximum_word_duration_seconds = 16;
// EVENTS:
int total_events = 17;
int x_negative_transitions = 18;
int x_zero_transitions = 19;
int x_positive_transitions = 20;
int y_negative_transitions = 21;
int y_zero_transitions = 22;
int y_positive_transitions = 23;
int z_negative_transitions = 24;
int z_zero_transitions = 25;
int z_positive_transitions = 26;
int total_comparison_attributes = 27; // set by update_summary_attributes()
int final_script_record = 0;
} first;
struct
{
// WORDS:
int total_words = 28;
float all_absolute_impulse_gseconds = 29; // gravity X seconds
float all_average_absolute_impulse_gseconds = 30;
float all_positive_impulse_gseconds = 31;
float all_negative_impulse_gseconds = 32;
float x_absolute_impulse_gseconds = 33;
float x_average_absolute_impulse_gseconds = 34;
float x_positive_impulse_gseconds = 35;
float x_positive_average_impulse_gseconds = 36;
float y_absolute_impulse_gseconds = 37;
float y_average_absolute_impulse_gseconds = 38;
float z_absolute_impulse_gseconds = 39;
float z_average_absolute_impulse_gseconds = 40;
float minimum_word_duration_seconds = 41;
float average_word_duration_seconds = 42;
float maximum_word_duration_seconds = 43;
// EVENTS:
int total_events = 44;
int x_negative_transitions = 45;
int x_zero_transitions = 46;
int x_positive_transitions = 47;
int y_negative_transitions = 48;
int y_zero_transitions = 49;
int y_positive_transitions = 50;
int z_negative_transitions = 51;
int z_zero_transitions = 52;
int z_positive_transitions = 53;
int total_comparison_attributes = 54; // set by update_summary_attributes()
int final_script_record = 0;
} two;
int final_script_record = true;
} const REFERENCE_SUMMARY_SCRIPT
If you want an array, as you seem to say, then you need to declare an array. You appear to instead be trying to define a struct whose members are of the struct type you are interested in. In C, you could define your array like so:
/* elements are unmodifiable: */ const
/* base type: */ My_Struct
/* variable name and (implicit) dimension: */ my_script_array[]
/* initializer: */ = {
/* one element: */ { .aaa = 111, .bbb = 222 },
/* another: */ { .aaa = 333, .bbb = 444 }
};
Note the use of designated initializers for the struct members, presenting the member names as you said you wanted.
typedef struct
{
int aaa;
int bbb;
} My_Struct;
const My_Struct my_script_array[] = {
{111, 222},
{333, 444}
// and so on ...
};
The fastest solution I used was to just copy and paste from the .h structure definition to the implementations declaration, and comment out the symbols. Readable, and easy to implement and modify. I perform functionality testing of my embedded software by scripts, so I do this a lot. Script testing allows extremely rigorous test of functionality because scripts can test for situations impossible to reproduce in lab.
#include <stdio.h>
typedef struct
{
int aaa;
int bbb;
} My_Struct;
const My_Struct first_script = {111, 222};
const My_Struct second_script = {333,444};
struct
{
const My_Struct *first_script;
const My_Struct *second_script;
} info = {&first_script,&second_script};
const My_Struct* my_script_array[] =
{
&first_script,
&second_script
};
int main(void)
{
int ii;
printf("first {%d,%d}, second {%d,%d}\n",
info.first_script->aaa,
info.first_script->bbb,
info.second_script->aaa,
info.second_script->bbb);
for (ii - 0; ii < sizeof(my_script_array)/sizeof(*my_script_array); ii++)
printf("Entry %d {%d,%d}\n",
ii,
my_script_array[ii]->aaa,
my_script_array[ii]->bbb);
return 0;
}
Might be a starting point. It might be more obvious to put them all into an array and know the index into the array that you need. Or pick out a pointer to the instances that you need to talk about.
#define NULL 0
int main()
{
int *array1=NULL,*array2=NULL;
int x =add(array1[0],array2[0]);
int y =add(array1[1],array2[7]);
int x =add(array1[2],array2[3]);
int y =add(array1[3],array2[4]);
int x =add(array1[4],array2[6]);
int y =add(array1[5],array2[1]);
int x =add(array1[6],array2[5]);
int y =add(array1[7],array2[2]);
................
................
int x =add(array1[252],array2[0]);
int y =add(array1[253],array2[7]);
int x =add(array1[254],array2[3]);
int y =add(array1[255],array2[4]);
}
Basically index for array1 is incrementing by 1 starting from 0 to till 255
but the index for array2 is fixed from 0 to 7. So I want to optimize this multiple addition. How to optimize this?
what you can is
int j = 0,order[] = {0,7,3,4,6,1,5,2};
for(int i = 0;i <256; i +=2)
{
int x =add(array1[i],array2[order[j%8]]);
j++;
int y =add(array1[i+1],array2[order[j%8]]);
j++;
}
UPDATE
alternate solution can be (if you want without using i+=2)
int j = 0,order[] = {0,7,3,4,6,1,5,2};
for(int i = 0;i <256; i ++)
{
int x =add(array1[i],array2[order[j%8]]);
j++;
i++;
if(i>=256) break; //Improves it if you have non even condition
int y =add(array1[i],array2[order[j%8]]);
j++;
}
edit by sam
Now i want to compare this two values of x and y and selecting value based on comparision
CurrentTre=256;
if (x > y)
{
*array3[0]= x;
*array4[CurrentTre +0] = 0;
}
else
{
*array3[i] = y;
*array4[CurrentTre + 0] = 1;
}
..........
..........
if (x > y)
{
*array3[0]= x;
*array4[CurrentTre +127] = 254;
}
else
{
*array3[i] = y;
*array4[CurrentTre + 127] = 255;
}
/////////////
my approach is this way
if (x > y)
{
*array3[i]= x;
*array4[int CurrentTre +i] = int number[i]<<1;
}
else
{
array3[i] = y;
array4[int CurrentTre + i] = int number[i]<<1|1;
}
} //end function main
I want to optimize the code my optimization is given below
please check whether am i doing right or not..?
uint32 even_number[255] ={0};
uint32 loop_index1=0;
uint32 loop_index2=0;
uint16 order[256]={0,7,3,4,6,1,5,2,4,3,7,0,1,6,2,5,7,0,4,3,2,5,1,6,3,4,0
,7,6,1,5,2,4,3,7,0,1,6,2,5,0,7,3,4,5,2,6,1,3,4,0,7,6,1,5,2,7,0,4,3,2,5
,1,6,5,2,6,1,0,7,3,4,1,6,2,5,4,3,7,0,2,5,1,6,7,0,4,3,6,1,5,2,3,4,0,7,1
,6,2,5,4,3,7,0,5,2,6,1,0,7,3,4,6,1,5,2,3,4,0,7,2,5,1,6,7,0,4,3,3,4,0,7
,6,1,5,2,7,0,4,3,2,5,1,6,4,3,7,0,1,6,2,5,0,7,3,4,5,2,6,1,7,0,4,3,2,5,1
,6,3,4,0,7,6,1,5,2,0,7,3,4,5,2,6,1,4,3,7,0,1,6,2,5,6,1,5,2,3,4,0,7,2,5
,1,6,7,0,4,3,1,6,2,5,4,3,7,0,5,2,6,1,0,7,3,4,2,5,1,6,7,0,4,3,6,1,5,2,3
,4,0,7,5,2,6,1,0,7,3,4,1,6,2,5,4,3,7,0}; //all 256 values
for(loop_index1;loop_index1<256;loop_index1++)
{
m0= (CurrentState[loop_index1]+Branch[order[loop_index2]]);
loop_index2++;
loop_index1++;
if(loop_index1>=256)
break;
m1= (CurrentState[loop_index1]+Branch[order[loop_index2]]);
loop_index2++;
if (mo > m1)
{
NextState[loop_index1]= m0;
SurvivorState[CurrentTrellis + loop_index1] =
even_number[loop_index1]<<1;
}
else
{
NextState[loop_index1] = StateMetric1;
SurvivorState[CurrentTrellis + loop_index1] =
even_number[loop_index1<<1|1;
}
}
first step:
for (int i = 0; i < 256; i+=8) {
x = add(array1[i], array2[0]);
y = add(array1[i+1], array2[7]);
...
}
Use a pointer
int *array1 = NULL, *array2 = NULL;
int *ptr1 = array1;
int x = add(*ptr1++, array2[0]);
int y = add(*ptr1++, array2[7]);
int x = add(*ptr1++, array2[3]);
int y = add(*ptr1++, array2[4]);
int x = add(*ptr1++, array2[6]);
int y = add(*ptr1++, array2[1]);
int x = add(*ptr1++, array2[5]);
int y = add(*ptr1++, array2[2]);
................
................
int x = add(*ptr1++, array2[0]);
int y = add(*ptr1++, array2[7]);
int x = add(*ptr1++, array2[3]);
int y = add(*ptr1++, array2[4]);
But remember premature optimization is the root of all evil.
Before optimizing measure and make certain you need optimizing the accesses to the array.
After optimizing measure to make certain the optimization had any effect.
I am trying get a mandelbrot image clearly with the sequential programming in C++, but I am getting a segmentation fault during runtime. I have no idea about the seg. fault, but my program is perfectly compiling with no errors.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int file_write(unsigned int width, unsigned int height)
{
unsigned int **color = NULL;
FILE *fractal = fopen("mandelbrot_imageSequential.ppm","w+");
if(fractal != NULL)
{
fprintf(fractal,"P6\n");
fprintf(fractal,"# %s\n", "Mandelbrot_imageSequential.ppm");
fprintf(fractal,"%d %d\n", height, width);
fprintf(fractal,"40\n");
int x = 0, y = 0;
unsigned int R = 0, G = 0, B = 0;
for(x = 0; x < width; ++x)
{
for(y = 0; y < height; ++y)
{
R = (color[y][x]*10);
G = 255-((color[y][x]*10));
B = ((color[y][x]*10)-150);
if(R == 10)
R = 11;
if(G == 10)
G = 11;
if(B == 10)
B = 11;
putc(R, fractal);
putc(G, fractal);
putc(B, fractal);
}
}
fclose(fractal);
}
return 0;
}
int method(int x, int y, int height, int width, double min_re, double max_re, double min_im, double max_im, int max_iterations)
{
double threshold = 4;
double x_factor = (max_re-min_re)/(width-1);
double y_factor = (max_im-min_im)/(height-1);
double c_im = max_im - y*y_factor;
double c_re = min_re + x*x_factor;
double Z_re = c_re, Z_im = c_im;
unsigned int col = 0;
for(unsigned n = 0; n < max_iterations; ++n)
{
double Z_re2 = Z_re*Z_re, Z_im2 = Z_im*Z_im;
if(Z_re2 + Z_im2 > threshold)
{
col = n;
break;
}
Z_im = 2 * Z_re * Z_im + c_im;
Z_re = Z_re2 - Z_im2 + c_re;
}
return col;
}
int main(int argc, char *argv[])
{
unsigned int width;
unsigned int height;
unsigned int max_iterations;
unsigned int **color = NULL;
int x,y;
double threshold;
double min_re;
double max_re;
double min_im;
double max_im;
unsigned int NUM_OF_THREADS;
if(argc != 10)
{
printf("There is an error in the input given.\n");
return 0;
}
else
{
height = atoi(argv[1]);
width = atoi(argv[2]);
max_iterations = atoi(argv[3]);
min_re = atof(argv[4]);
max_re = atof(argv[5]);
min_im = atof(argv[6]);
max_im = atof(argv[7]);
threshold = atoi(argv[8]);
NUM_OF_THREADS = atoi(argv[9]);
}
color = (unsigned int**)malloc(height*sizeof(unsigned int*));
printf("height = %d\twidth = %d\tmaximum_iterations = %d\tminimum_x-value = %.2f\tmaximum_x-value = %.2f\tminimum_y-value = %.2f\tmaximum_y-value = %.2f\tthreshold_value = %.2f\tno. of threads = %d\t\n",height,width,max_iterations,min_re,max_re,min_im,max_im,threshold,NUM_OF_THREADS);
for(x = 0; x < height; x++)
{
color[x] = (unsigned int*)malloc(width*sizeof(unsigned int));
}
time_t ts,te;
time(&ts);
method(x,y,height,width,min_re,max_re,min_im,max_im,max_iterations);
time(&te);
double diff = difftime(te,ts);
file_write(width, height);
printf("Total Time elapsed: %f\n",diff);
return 0;
}
How to correct this segmentation fault?
At least one problem is in the file_write function.
unsigned int **color = NULL;
R = (color[y][x]*10);
I assume the color should be an input parameter.
If you are on Linux machine do the following :
$ulimit -c unlimited
Then run the code. Notice a core.[pid] file is generated. fire up gdb like following
$gdb ./your_app core.[pid]
It will take you the statement where segfault occurred. issue a "backtrace" command in gdb prompt to see the call hierarchy.
Remember compiling with "-g" flag to get more verbose gdb output.
There are two major problems with your code:
You allocate memory for the color array but then use a different color inside file_write() which is initialized to NULL.
You need to pass the first color as an argument to file_write():
int main(...)
{
...
file_write(color, width, height);
printf("Total Time elapsed: %f\n",diff);
return 0;
}
And declare the other color as an argument to file_write():
int file_write(unsigned int **color, unsigned int width, unsigned int height)
{
/* unsigned int **color = NULL; // Removed */
...
You're only calling method() once and not storing anything into color. You need to call it in a loop. Something similar to:
/* Untested */
for (y = 0; y < height; y++) {
for (x = 0; x < width; x++) {
color[y][x] = method(x,y,height,width,min_re,max_re,min_im,max_im,max_iterations);
}
}
Then, of course, you should check the return values of malloc(), fopen(), fprintf(), fclose(), ... , and check that the input variables have reasonable values and so on.
I also noticed that you're passing width and height in different order to file_write() and method(). To avoid future headaches, I would change the method() function to method(x, y, width, height) so that the horizontal and vertical arguments are passed in the same order.