Making a ray tracer and the code was failing so I decided to redo the whole thing and change the logic of the object but now for some reason, it keeps giving me an error and I've already tried editing it multiple times. ImageMagick give me an error saying that there isn't enough data to produce the ppm
other than the bit about the object the main logic how the ppm is produced hasn't changed much so I can't really figure out where the error is.
main.cpp
vec color(const ray& r, float t, vec a, vec centre)
{
vector <Light> lighting;
lighting.push_back(Light(vec(0, 0, 50), vec(0, 0, -1)));
vec totalLight{0, 0, 0};
for(int i = 0; i <lighting.size(); i++){
if(t > 0.0){
vec hit = unit_vector(r.p_at_par(t) - centre);
vec L = unit_vector(lighting[i].position() - r.p_at_par(t));
vec R = L - 2.0*dot(L, hit)*hit;
vec S = vec(1, 1, 1)*pow(max(0.f, dot(R, vec(0, 0, -1))), 50);//Specular component
vec D = (a * max(0.f, dot(L, hit)) * 1.0);//Diffuse component
totalLight += S + D;
return totalLight;
}
}
}
float clamp(float a)
{
return (a > 255)? 255: (a < 0)? 0: a;
}
int main()
{
const int w = 200, h = 100;
FILE *fp;
fp = fopen("img.ppm", "wb");
fprintf(fp, "P6\n%d %d\n255\n", w, h);
vec lower_corner(-2.0, -1.0, -1.0);
vec horizontal(4.0, 0.0, 0.0);
vec vertical(0.0, 2.0, 0.0);
vec origin(0.0, 0.0, 0.0);
vector <sphere> objects;
objects.push_back(sphere(vec(0,-100.5,-1), 100, vec(0, 1, 0)));
objects.push_back(sphere(vec(0, 0, -1), 0.5, vec(1, 0, 0)));
objects.push_back(sphere(vec(5, 5,-2), 3, vec(1, 0, 0)));
for(int j = h - 1; j >= 0; j--)
{
for(int i = 0; i < w; i++)
{
vec col(0, 0, 0);
static unsigned char pixel[3];
sphere* ClosestObject = NULL;
float u = float(i + random_double())/float(w);
float v = float(j + random_double())/float(h);
ray r(origin, lower_corner + u*horizontal + v*vertical);
float t = 0.0;
float t_near = 200000.0;
vec pixelColor(0.52 , 0.52 ,0.48);
for(int j = 0; j < objects.size(); j++)
{
if(t = objects[j].intersect(r))
{
if(t < t_near)
{
ClosestObject = &objects[j];
t_near = t;
}
}
if( t = 200000.0)
col = pixelColor;
else
col = color(r, t, ClosestObject->color, ClosestObject->centre);
pixel[0] = int(clamp(col.r() * 255));
pixel[1] = int(clamp(col.g() * 255));
pixel[2] = int(clamp(col.b() * 255));
fwrite(pixel, 3, 1, fp);
}
}
fclose(fp);
return 0;
}
}
Actually, a comment but too much text for that…
Your indentation is broken.
Hence, you didn't notice that the return 0; appears in the outer for loop, this one:
for(int j = h - 1; j >= 0; j--)
{
ends with:
fclose(fp);
return 0;
}
Additionally, the
fwrite(pixel, 3, 1, fp);
appears in the most inner loop
for (int j = 0; j < objects.size(); j++)
which is IMHO wrong as well.
So, the produced .ppm file claims to have w×h pixels but instead it provides w×objects.size() pixels.
If objects.size() < h (what I would expect) then you will have too less pixels in the .ppm file and ImageMagick will notice and complain.
Your source code, auto-formatted in my VS2017:
int main()
{
const int w = 200, h = 100;
FILE *fp;
fp = fopen("img.ppm", "wb");
fprintf(fp, "P6\n%d %d\n255\n", w, h);
vec lower_corner(-2.0, -1.0, -1.0);
vec horizontal(4.0, 0.0, 0.0);
vec vertical(0.0, 2.0, 0.0);
vec origin(0.0, 0.0, 0.0);
vector <sphere> objects;
objects.push_back(sphere(vec(0, -100.5, -1), 100, vec(0, 1, 0)));
objects.push_back(sphere(vec(0, 0, -1), 0.5, vec(1, 0, 0)));
objects.push_back(sphere(vec(5, 5, -2), 3, vec(1, 0, 0)));
for (int j = h - 1; j >= 0; j--)
{
for (int i = 0; i < w; i++)
{
vec col(0, 0, 0);
static unsigned char pixel[3];
sphere* ClosestObject = NULL;
float u = float(i + random_double()) / float(w);
float v = float(j + random_double()) / float(h);
ray r(origin, lower_corner + u * horizontal + v * vertical);
float t = 0.0;
float t_near = 200000.0;
vec pixelColor(0.52, 0.52, 0.48);
for (int j = 0; j < objects.size(); j++)
{
if (t = objects[j].intersect(r))
{
if (t < t_near)
{
ClosestObject = &objects[j];
t_near = t;
}
}
if (t = 200000.0)
col = pixelColor;
else
col = color(r, t, ClosestObject->color, ClosestObject->centre);
pixel[0] = int(clamp(col.r() * 255));
pixel[1] = int(clamp(col.g() * 255));
pixel[2] = int(clamp(col.b() * 255));
fwrite(pixel, 3, 1, fp);
}
}
fclose(fp);
return 0;
}
}
Please, check indentation and place closing curly-brackets correctly. Then it should work as before…
I had glDrawElements working consistently, initially with a simple box and then with more complex shapes made up of a large amount of vertices. Then it simply stopped drawing the mesh. I have taken the code back to it's most basic, just drawing 2 triangles to make a 2D square. This also no longer works.
void createMesh(void) {
float vertices[12];
vertices[0] = -0.5; vertices[1] = -0.5; vertices[2] = 0.0; // Bottom left corner
vertices[3] = -0.5; vertices[4] = 0.5; vertices[5] = 0.0; // Top left corner
vertices[6] = 0.5; vertices[7] = 0.5; vertices[8] = 0.0; // Top Right corner
vertices[9] = 0.5; vertices[10] = -0.5; vertices[11] = 0.0; // Bottom right corner
short indices[] = { 0, 1, 2, 0, 2, 3};
glEnableClientState(GL_VERTEX_ARRAY); // Enable Vertex Arrays
glVertexPointer(3, GL_FLOAT, 0, vertices); // Set The Vertex Pointer To Our Vertex Data
glDrawElements(GL_TRIANGLES,6 , GL_UNSIGNED_SHORT, indices);
glDisableClientState(GL_VERTEX_ARRAY);
}
The more advanced code that used to work is shown below:
void createMesh(void) {
float vertices[(amountOfHorizontalScans * 480 * 3)];// Amount of vertices
//build the array of vertices from a matrix of data
int currentVertex = -1;
std::vector <std::vector<double>> currentPointCloudMatrix = distanceCalculator.getPointCloudMatrix();
double plotY = 0;
double plotX = 0;
for (int j = 0; j < currentPointCloudMatrix.size(); j++){
std::vector <double> singleDistancesVector = currentPointCloudMatrix.at(j);
for (int i = 0; i < singleDistancesVector.size(); i++){
if (singleDistancesVector.at(i) != 0){
vertices[++currentVertex] = plotX;
vertices[++currentVertex] = plotY;
vertices[++currentVertex] = singleDistancesVector.at(i);
}
plotX += 0.1;
}
plotX = 0;
plotY += 0.2; //increment y by 0.02
}
//Creating the array of indices, 480 is the amount of columns
int i = 0;
short indices2[(amountOfHorizontalScans * 480 * 3)];
for (int row = 0; row<amountOfHorizontalScans - 1; row++) {
if ((row & 1) == 0) { // even rows
for (int col = 0; col<480; col++) {
indices2[i++] = col + row * 480;
indices2[i++] = col + (row + 1) * 480;
}
}
else { // odd rows
for (int col = 480 - 1; col>0; col--) {
indices2[i++] = col + (row + 1) * 480;
indices2[i++] = col - 1 + +row * 480;
}
}
}
glEnableClientState(GL_VERTEX_ARRAY); // Enable Vertex Arrays
glVertexPointer(3, GL_FLOAT, 0, vertices); // Set The Vertex Pointer To Our Vertex Data
glDrawElements(GL_TRIANGLE_STRIP, (amountOfHorizontalScans * 480 * 3), GL_UNSIGNED_SHORT, indices2);
glDisableClientState(GL_VERTEX_ARRAY);
}
I am at a complete loss as to why it has stopped working as it was working perfectly for a good number of runs, then just completely stopped. I have debugged through and all the code is being reached, also the vertices and indices are populated with data. What could cause this to stop working?
EDIT:
So I am really quite confused now. I came back to this issue this morning, and everything worked fine again, as in the meshes would draw with no issues. After doing some tests and running the program a number of times it has simply stopped drawing meshes again!
Could this be something memory related? I am not 100% sure on how glDrawElements stores the data passed to it, so could it be that I have to clear something somewhere that I keep filling up with data?
You cannot allocate dynamically arrays in stack:
short indices2[(amountOfHorizontalScans * 480 * 3)];
In code:
short indices2[(amountOfHorizontalScans * 480 * 3)];
for (int row = 0; row<amountOfHorizontalScans - 1; row++) {
if ((row & 1) == 0) { // even rows
for (int col = 0; col<480; col++) {
indices2[i++] = col + row * 480;
indices2[i++] = col + (row + 1) * 480;
}
}
else { // odd rows
for (int col = 480 - 1; col>0; col--) {
indices2[i++] = col + (row + 1) * 480;
indices2[i++] = col - 1 + +row * 480;
}
}
}
Must be
short* indices2 = new short[(amountOfHorizontalScans * 480 * 3)];
than free allocated memory
delete [] indices2;
Triangle strip is pretty tricky mode did you try to work directly with GL_TRIANGLES.
I'm trying to program a simple animation using GLUT and OpenGL in c++. I've created three functions for this purpose: catSpline(), fillArray(), and fixedAngle(). catSpline() returns a GLfloat value, fillArray() is a void function that consists of a series of loops that fills a series of arrays with GLfloat values produced using catSpline, and fixedAngle() will return a 16-unit array. Here is the code for these three methods:
GLfloat * fixedAngle(GLfloat initialX, GLfloat initialY, GLfloat initialZ, GLfloat rotX, GLfloat rotY, GLfloat rotZ)
{
ArrayXXf z(4,4); //initializing the 4x4 rotation matrixes
ArrayXXf y(4,4);
ArrayXXf x(4,4);
x<<1, 0, 0, 0,
0, cos(rotX), -sin(rotX), 0,
0, sin(rotX), cos(rotX), 0,
0, 0, 0, 1;
y<<cos(rotY), 0, sin(rotY), 0,
0, 1, 0, 0,
-sin(rotY), 0, cos(rotY), 0,
0, 0, 0, 1;
z<< cos(rotZ), -sin(rotZ), 0, 0,
sin(rotZ), cos(rotZ), 0, 0,
0, 0, 1, 0,
0, 0, 0, 1;
ArrayXXf fin(4,4);
fin = x * y * z;
fin(0,3) = initialX;
fin(1,3) = initialY;
fin(2,3) = initialZ;
//now we've moved the translational information into the final matrix
// std::cout << fin;
fin(3,3) = 1;
GLfloat * arr;
arr = (GLfloat *) malloc(16*sizeof(GLfloat));
arr[0] = fin(0,0);
arr[1] = fin(0,1);
arr[2] = fin(0,2);
arr[3] = fin(0,3);
arr[4] = fin(1,0);
arr[5] = fin(1,1);
arr[6] = fin(1,2);
arr[7] = fin(1,3);
arr[8] = fin(2,0);
arr[9] = fin(2,1);
arr[10] = fin(2,2);
arr[11] = fin(2,3);
arr[12] = fin(3,0);
arr[13] = fin(3,1);
arr[14] = fin(3,2);
arr[15] = fin(3,3);
return arr;
}
GLfloat catSpline(GLfloat x1, GLfloat x2, GLfloat x3, GLfloat x4, GLfloat t)
{
ArrayXXf M(4,4); //4x4 M matrix
ArrayXXf P(4,1); //matrix to hold keyframe x values
P(0,0) = x1;
P(1,0) = x2;
P(2,0) = x3;
P(3,0) = x4;
//keyframe x values
M(0,0) = -0.5;
M(0,1) = 2-(-0.5);
M(0,2) = -0.5-2;
M(0,3) = 0.5;
M(1,0) = 2*0.5;
M(1,1) = 0.5-3;
M(1,2) = 3-(2*0.5);
M(1,3) = -0.5;
M(2,0) = -0.5;
M(2,1) = 0;
M(2,2) = 0.5;
M(2,3) = 0;
M(3,0) = 0;
M(3,1) = 1;
M(3,2)=0;
M(3,3)=0;
ArrayXXf T(1,4);
T(0,0) = t*t*t; //you can't cube a float, but you can get the same result by doing this
T(0,1) = t*t;
T(0,2) = t;
T(0,3)=1;
//now the T matrix is filled
ArrayXXf TM(1,4);
TM(0,0) = (T(0,0) * M(0,0)) + (T(0,1) * M(1,0)) + (T(0,2) * M(2,0)) + (T(0,3) * M(0,3));
TM(0,1) = (T(0,0) * M(0,1)) + (T(0,1) * M(1,1)) + (T(0,2) * M(2,1)) + (T(0,3) * M(3,1));
TM(0,2) = (T(0,0) * M(0,2)) + (T(0,1) * M(1,2)) + (T(0,2) * M(2,2)) + (T(0,3) * M(3,2));
TM(0,3) = (T(0,0) * M(0,3)) + (T(0,1) * M(1,3)) + (T(0,2) * M(2,3)) + (T(0,3) * M(3,3));
//first multiply T amd M
GLfloat TMP;
TMP = (TM(0,0) * P(0,0)) + (TM(0,1) *P(1,0)) + (TM(0,2) * P(2,0)) + (TM(0,3) * P(3,0));
return TMP;
}
void fillArrays()
{
/* zRot = catSpline(2, 4, 5, 6);
yRot = catSpline(1, 4, 6, 7);
xRot = catSpline(6, 3, 2, 6);
xArr = catSpline(9, 4, 3, 10);
yArr = catSpline(1, 2, 4, 8);
zArr = catSpline(8, 3, 1, 3);*/
for(int j=0; j>=100; j++)
{
xArr[j] = catSpline(2, 4, 5, 6, t);
t+=0.1;
}
for(int i=0; i>=100; i++)
{
yArr[i] = catSpline(2, 4, 5, 6, ty);
ty+=0.1;
}
for(int k=0; k>=100; k++)
{
xArr[k] = catSpline(2, 4, 5, 6, tz);
tz += 0.1;
}
for(int a=0; a>=100; a++)
{
xRot[a] = catSpline(2, 4, 5, 6, rx);
rx += 0.1;
}
for(int b=0; b>=100; b++)
{
yRot[b] = catSpline(2, 4, 5, 6, ry);
rz += 0.1;
}
for(int c=0; c>=100; c++)
{
zRot[c] = catSpline(2, 4, 5, 6, rz);
rz += 0.1;
}
}
The way I use these three functions is in a loop called Render, I set up OpenGL for displaying an animation of a teapot model, call fillArrays(), and then attempt to store the result of a single fixedAngle call (using a few entries form the arrays filled in just before) in a new array called foo. This seems to be the cause of the errors, with foo only containing a pointer address rather than pointing to the actual array so I can use it. This is the code I'm using to call these functions:
fillArrays();
GLfloat * foo;
foo = (GLfloat *) malloc(16*sizeof(GLfloat));
foo = fixedAngle(xArr[tp], yArr[tp], zArr[tp], xRot[tp], yRot[tp], zRot[tp]);
glLoadMatrixf(foo);
After having used multiple print statements to print out the results of my functions, I know that the problem has to be how I set up the pointers somewhere. Can anyone help me return the array correctly?
Please note: if some of the matrix syntax seems unfammiliar, it's because I'm using a c++ library called Eigen to do some of the matrix math. Once again, after having rigorously printed out the results of the functions, I know that the functions that use Eigen syntax are producing the right values.
You can replace...
GLfloat * arr;
arr = (GLfloat *) malloc(16*sizeof(GLfloat));
...with...
std::vector<GLfloat> arr(16);
...or even directly initialise it instead of copying elements afterwards...
std::vector<GLfloat> arr { fin(0,0), fin(0,1), fin(0,2), fin(0,3),
fin(1,0), fin(1,1), fin(1,2), fin(1,3),
fin(2,0), fin(2,1), fin(2,2), fin(2,3),
fin(3,0), fin(3,1), fin(3,2), fin(3,3) };
...which - FWIW - could also be done like this...
std::vector<GLfloat> arr;
for (int a = 0; a <= 3; ++a)
for (int b = 0; b <= 3; ++b)
arr.push_back(fin(a,b));
With any of the above, you'll need to change the return type correspondingly...
std::vector<GLfloat> fixedAngle(
GLfloat initialX, GLfloat initialY, GLfloat initialZ,
GLfloat rotX, GLfloat rotY, GLfloat rotZ)
{
...
...then you can call fixedAngle and use foo like this:
fillArrays();
std::vector<GLfloat> foo = fixedAngle(xArr[tp], yArr[tp], zArr[tp],
xRot[tp], yRot[tp], zRot[tp]);
glLoadMatrixf(foo.data()); // or `&foo[0]` if you prefer...
I am using this website on how to compute a hermite curve under the heading The Math in Matrix Form to make a hermite curve.
Here is my code so far...
// calculate hermite curve
GLfloat S[4][1];
GLfloat C[4][3] = {{height, 0, 0},
{0, radius, 0},
{0, 2 * radius, 0},
{-2 * height, 0, 0}};
GLfloat h[4][4] = {{ 2,-2, 1, 1},
{-3, 3,-2,-1},
{ 0, 0, 1, 0},
{ 1, 0, 0, 0}};
unsigned int rows;
unsigned int cols;
float val;
for (int x = 0; x < l_vertices; x++) {
float segment = (float)x / l_vertices;
S[0][0] = pow(segment, 3);
S[1][0] = pow(segment, 2);
S[2][0] = pow(segment, 1);
S[3][0] = 1;
GLfloat midwayArray[4][4] = {{ 0, 0, 0, 0},
{ 0, 0, 0, 0},
{ 0, 0, 0, 0},
{ 0, 0, 0, 0}};
GLfloat finalArray[4][1] = { 0, 0, 0, 0};
rows = 4;
cols = 4;
for (unsigned int i = 0; i < rows; i++) {
for (unsigned int j = 0; j < cols; j++) {
for (unsigned int k = 0; k < 1; k++) {
val = S[i][k] * h[k][j];
midwayArray[i][j] += val;
}
}
}
rows = 4;
cols = 1;
for (unsigned int i = 0; i < rows; i++) {
for (unsigned int j = 0; j < cols; j++) {
for (unsigned int k = 0; k < 4; k++) {
val = midwayArray[i][k] * C[k][j];
finalArray[i][j] += val;
}
}
}
profileCurve[0][x] = finalArray[0][0];
profileCurve[1][x] = finalArray[1][0];
profileCurve[2][x] = finalArray[2][0];
}
But I am getting mainly zeros for some reason. I think my matrix multiplication is not being done correctly.
Also, just some information on how my code currently works. The first inner nested for loop is multiplying S and h just like the website says. Then the second inner nested for loop is multiplying the result of the previous matrix multiplication with C just like the website says.
I think assign the result coordinates of the final array for that iteration of x to my profileCurve array which is going to store all the coordinates that make up the hermite curve.