c++ triangle rasterization using edge detection - c++

I am trying to implement triangle rasterization using this article as reference
https://www.scratchapixel.com/lessons/3d-basic-rendering/rasterization-practical-implementation/rasterization-stage
as such I have implemented an edge function with an input of 3 points. 2 defining a line and one as the point to be tested, annotated as p
bool SoftwareRendererImp::edgeFunction(float xa, float ya,
float xb, float yb,
float xp, float yp)
{
return ((xp - xa) * (yb - ya) - (yp - ya) * (xb - xa) >= 0);
}
in my rasterization function I am iterating over a set of integers ranging from the lowest to highest x and y values. This is to try and iterate over a box of best fit containing the triangle and test each point to see if it is contained in the triangle.
void SoftwareRendererImp::rasterize_triangle(float x0, float y0,
float x1, float y1,
float x2, float y2,
Color color)
{
// Task 3:
// Implement triangle rasterization
cout << "triangle----------------------------------------------------------------------------------------------------------------------- \n";
float minX = std::min(x0, x1);
minX = std::min(minX, x2);
float minY = std::min(y0, y1);
minY = std::min(minY, y2);
float maxX = std::max(x0, x1);
maxX = std::max(maxX, x2);
float maxY = std::max(y0, y1);
maxY = std::max(maxY, y2);
bool inside;
float px, py;
for (int x = minX; x < maxX; x++)
{
for (int y = minY; y < maxY; y++)
{
inside = true;
px = x + 0.5f;
py = y + 0.5f;
inside &= SoftwareRendererImp::edgeFunction(x0, y0, x1, y1, px, py);
inside &= SoftwareRendererImp::edgeFunction(x1, y1, x2, y2, px, py);
inside &= SoftwareRendererImp::edgeFunction(x2, y2, x0, y0, px, py);
if (inside)
{
SoftwareRendererImp::rasterize_point(x, y, color);
cout << "inside: " << x << ", " << y << "\n";
}
else
{
// cout << "outside: " << x << ", " << y << "\n";
}
}
}
}
From my understanding of the linked article this should work however it is not. Can anyone spot what it is that I am screwing up?

this expression was backwards
(xp - xa) * (yb - ya) - (yp - ya) * (xb - xa)
it should have been
(yp - ya) * (xb - xa) - (xp - xa) * (yb - ya)

Related

Polygon Clipping Error?

I have been trying to work out this problem with my Sutherland-Hodgman Polygon Clipper algorithm. When filling in the polygon i keep getting this result:
I cant seem to figure out what causing the bug, here is my code:
// #param in the number of vertices in the polygon to be clipped
// #param inV the incoming vertex list
// #param outV the outgoing vertex list
// #param ll the lower-left corner of the clipping rectangle
// #param ur the upper-right corner of the clipping rectangle
//
// #return number of vertices in the polygon resulting after clipping
int Clipper::clipPolygon( int in, const Vertex inV[], Vertex outV[],
Vertex ll, Vertex ur )
{
int out = 0, out2 = 0, out3 = 0, out4 = 0;
Vertex *outV2, *outV3, *outV4;
// We clip entire figure by clipping on the 4 edges of the clipping window
// each step will be stored, in *out pointers.
SHPC(inV, outV, in, out, ll.x, ur.y, ll.x, ll.y); // left
outV2 = new Vertex[out];
SHPC(outV, outV2, out, out2, ll.x, ll.y, ur.x, ll.y); // bottom
outV3 = new Vertex[out2];
SHPC(outV2, outV3, out2, out3, ur.x, ll.y, ur.x, ur.y); // right
outV4 = new Vertex[out3];
SHPC(outV3, outV4, out3, out4, ur.x, ur.y, ll.x, ur.y); // top
// we want outx and outy to store the final clipped polygons, which
// means we need to get content of outx4 and outy4
for (int i = 0; i < out; i++) { outV[i].x = outV4[i].x; outV[i].y = outV4[i].y; }
// same principle for out value
out = out4;
delete[] outV2;
delete[] outV3;
delete[] outV4;
return out;
}
void Clipper::SHPC(const Vertex inV[],
Vertex outV[], int in, int &out,
float x0, float y0, float x1, float y1) {
float px = inV[in - 1].x, py = inV[in - 1].y; // last vertex is the initial “predecessor”
float _x = px, _y = py;
for (int j = 0; j < in; j++) {
if (inside(inV[j].x, inV[j].y, x0, y0, x1, y1)) { // Cases 1 & 4
if (inside(px, py, x0, y0, x1, y1)) { // Case 1
output(inV[j].x, inV[j].y, out, outV);
}
else { // Case 4
intersect(px, py, inV[j].x, inV[j].y, x0, y0, x1, y1, _x, _y);
output(_x, _y, out, outV);
output(inV[j].x, inV[j].y, out, outV);
}
}
else { // Cases 2 & 3
if (inside(px, py, x0, y0, x1, y1)) { // Case 2
intersect(px, py, inV[j].x, inV[j].y, x0, y0, x1, y1, _x, _y);
output(_x, _y, out, outV);
} // Case 3 has no output
}
px = inV[j].x;
py = inV[j].y;
} // for
}
// is point inside boundary?
bool Clipper::inside(float _x, float _y, float x0, float y0, float x1, float y1) {
if (y0 == y1) { // horizontal edge
if (x0 < x1) return _y >= y0;
if (x0 > x1) return _y <= y0;
} else { // vertical edge
if (y1 > y0) return _x <= x0;
if (y0 > y1) return _x >= x0;
}
return false;
}
// put point into vector, update length
void Clipper::output(float _x, float _y, int &out, Vertex outV[]) {
outV[out].x = _x;
outV[out++].y = _y;
}
// compute intersection point, put point into newpoint parameter
void Clipper::intersect(float sx, float sy, float ex, float ey, float x0, float y0, float x1, float y1, float &_x, float &_y) {
if (x0 == x1) { // if it's a vertical edge
_x = x0;
_y = sy + (x0 - sx) * (ey - sy) / (ex - sx);
}
else { // if it's a horizontal edge
_y = y0;
_x = sx + (y0 - sy) * (ex - sx) / (ey - sy);
}
}
If I would have to guess I would say it is an issue with my inside function, but that raises the question of why is it only not working for the star and not for the rest of the shapes? I have tried rewriting the inside function, but it usually results in the same type of error only in a different direction. If anyone can point me in the right direction I would appreciate it.

Drawing solid triange in a cube result in flicker

I'm trying to draw a cube using software rendering. So I have I'm drawing a cube out of triangles. So I render a solid triangle. The cube is rendered successfully, but there are flicker while rotating it.
here is my solid triangle filling, can somebody notice a problem in the algorithm?
void Rasterizer::DrawSolidTriangle(int x0, int y0, int x1, int y1, int x2, int y2, Color&color)
{
// Sort our points into order of y
// 0 top
// 2 middle
// 1 bottom
if (y1 < y0)
{
swap(y1, y0);
swap(x1, x0);
}
if (y2 < y0)
{
swap(y2, y0);
swap(x2, x0);
}
if (y1 < y2)
{
swap(y2, y1);
swap(x2, x1);
}
float xl_edge = (float)x0; // left edge
float xr_edge = (float)x0; // right edge
float dxldy;
float dxrdy;
float dxdy1 = (float)(x2 - x0) / (y2 - y0);
float dxdy2 = (float)(x1 - x0) / (y1 - y0);
if (dxdy1 < dxdy2)
{
dxldy = dxdy1;
dxrdy = dxdy2;
}
else
{
dxldy = dxdy2;
dxrdy = dxdy1;
}
// Top of the triangle
for (int y = y0; y<y2; y++)
{
for (int x = xl_edge; x<xr_edge; x++)
{
SetPixel((unsigned int)x, (unsigned int)y, color);
}//end for loop x
xl_edge = xl_edge + dxldy;
xr_edge = xr_edge + dxrdy;
}// end for loop y
// Bottom half of the triangle
if (dxdy1 < dxdy2)
{
dxldy = (float)(x2 - x1) / (y2 - y1);
}
else
{
dxrdy = (float)(x2 - x1) / (y2 - y1);
}
for (int y = y2; y<y1; y++)
{
for (int x = xl_edge; x<xr_edge; x++)
{
SetPixel((unsigned int)x, (unsigned int)y, color);
}//end for loop x
xl_edge = xl_edge + dxldy;
xr_edge = xr_edge + dxrdy;
}// end for loop y
}
here is a screenshot of the problem, when rotating the cube so slow it does appear here:

Drawing a flat bottom triangle, results in flat top triangle

I'm trying to draw a flat bottom triangle, but its drawn as flat top triangle.
I would like to know what I'm doing wrong, in terms of math
here is my code
void Rasterizer::DrawBottomTriangle(int x0, int y0, int x1, int y1, int x2, int y2, Color color)
{
int temp_x;
// test order of x1 and x2
if (x2 < x1)
{
temp_x = x1;
x1 = x2;
x2 = temp_x;
} //
float dxy_left = (float)(x2 - x0) / (y2 - y0);
float dxy_right = (float)(x1 - x0) / (y1 - y0);
// set starting and ending points for edge trace
float xs = x0;
float xe = x0;
// draw each scanline
for (int y = y0; y >= y1; y--)
{
// draw a line from xs to xe at y in color c
DrawLine(color, (int)xs, y, color, (int)xe, (int)y);
// move down one scanline
xs += dxy_left;
xe += dxy_right;
} // end for y
}

Implementation of Line Drawing Algorithm doesn't work properly

First question, I have tried to calculate the expression, di+1=di+2*Δy−2*Δx(yi+1−yi) for the four quadrants. Irrespective of the quadrant, the expression was found to be the same, including signs.
Am I right, or, there has been some mistakes in my calculations (hence, I am wrong)?
Second question, if this expression is only applicable for the first octet, how can I apply this to other octets? To me, there is no way to determine which octet I am working on. Coz, the value of m always represent two opposite octets. For example, if 0<m<1, it represents 1st and 5th octet. Right?
Thirdly, how can we determine the initial/starting value of di?
#include <iostream>
#include "utils.h"
void BresenhamLine(double x1, double y1, double x2, double y2, int color)
{
if(x1>x2 || y1>y2)
{
Swap(x1, x2);
Swap(y1, y2);
}
double x = x1;
double y = y1;
double dx = x2 - x1;
double dy = y2 - y1;
double dt = 2 * (dy - dx);
double ds = 2 * dy;
double d = 2*dy - dx;
PlotPixel(x, y, color);
if(dx>=dy)
{
while(x<=x2)
{
x++;
if(d<0)
{
d = d + ds;
}
else
{
y++;
d = d + dt;
}
PlotPixel(x, y, color);
}
}
else
{
while(y<=y2)
{
y++;
if(d<0)
{
x++;
d = d + dt;
}
else
{
d = d + ds;
}
PlotPixel(x, y, color);
}
}
}
int main()
{
int gm = DETECT;
int gd = DETECT;
initgraph(&gm, &gd, "");
double x1 = 0;
double y1 = 0;
double r = 50;
double x2 = 0;
double y2 = 0;
double signx = 0;
double signy = 0;
for(int theta=0 ; theta<=360 ; theta++)
{
x2 = r * cos(DegreeToRad((double) theta));
y2 = r * sin(DegreeToRad((double) theta));
x1 = 5 * cos(DegreeToRad((double) theta));
y1 = 5 * sin(DegreeToRad((double) theta));
BresenhamLine(x1, y1, x2, y2, YELLOW);
}
getch();
closegraph();
return 0;
}
The lines that go through 2nd and 4th quadrant are not showing up.
How to fix that with some minor changes in my code?
With this input: x1: 100 y1: -100 x2: -100 y2: 100
this logic:
if(x1>x2 || y1>y2)
{
Swap(x1, x2);
Swap(y1, y2);
}
fails.
This page is a good place to start. It shows code as well for 1 of the octants:
http://www.cs.helsinki.fi/group/goa/mallinnus/lines/bresenh.html
I think you need to swap the x if x1 > x2 and swap the y if y1 > y2 but not swap both if only 1 of those is true.
The external links section of the Wikipedia page contains several links to ready-made implementations that you can study.
Try this:
void BresenhamLine( double x1, double y1, double x2, double y2, int color )
{
const bool steep = (std::abs(y2 - y1) > std::abs(x2 - x1));
if(steep)
{
std::swap(x1, y1);
std::swap(x2, y2);
}
if(x1 > x2)
{
std::swap(x1, x2);
std::swap(y1, y2);
}
double dx = x2 - x1;
double dy = std::abs(y2 - y1);
double error = dx / 2;
int ystep = (y1 < y2) ? 1 : -1;
int y = (int)y1;
int maxX = (int)x2;
for(int x=(int)x1; x<maxX; x++)
{
if(steep)
{
PlotPixel(y, x, color);
}
else
{
PlotPixel(x, y, color);
}
error -= dy;
if(error < 0)
{
y += ystep;
error += dx;
}
}
}
I got this by slightly modifying the code here:http://rosettacode.org/wiki/Bitmap/Bresenham's_line_algorithm#C.2B.2B

My Gravitational Pull Algorithm behaves Oddly in Certain Situations

While trying to create my own physics engine (don't try persuading me not to), I decided to create a class for each pixel, called Particle, this system has an x and a y, and a x and y velocity, as shown below. Unfortunately, the code for calculateGravitationalVelocity doesn't abide by the laws of physics in certain situations. For example, if the x of the particle and the x of the other particle is the same, the particle will fall towards the object realistically, but when the particle gets too close, it pings off towards the positive x. I am only going to include the class source code, but I can include the source code of the other file, though it is partly written in SFML
Particle.cpp:
#include <iostream>
#include <string>
#include <math.h>
class Particle
{
private:
//Coords:
double x, y;
//Velocities:
double xVelocity = 0;
double yVelocity = 0;
//Material:
std::string material = "Generic";
//Mass:
double mass = 0;
public:
//Coords:
void setCoords(double, double);
float getCoords(char);
//Velocities:
void giveVelocity(char, float);
void setVelocity(char, float);
float getVelocity(char);
//Gravitational Velocity:
void calculateGravitationalVelocity(Particle);
//Material:
void setMaterial(std::string);
std::string getMaterial();
//Mass:
void setMass(double);
double getMass();
//Update:
void update();
};
//Coords:
void Particle::setCoords(double newX, double newY)
{
x = newX;
y = newY;
}
float Particle::getCoords(char axis)
{
if (axis == 'x')
{
//return floor(x);
return x;
}
else if (axis == 'y')
{
//return floor(y);
return y;
}
}
//Velocities:
void Particle::giveVelocity(char axis, float addedVelocity)
{
if (axis == 'x') {xVelocity = xVelocity + addedVelocity;}
else if (axis == 'y') {yVelocity = yVelocity + addedVelocity;}
}
void Particle::setVelocity(char axis, float newVelocity)
{
if (axis == 'x') {xVelocity = newVelocity;}
else if (axis == 'y') {yVelocity = newVelocity;}
}
float Particle::getVelocity(char axis)
{
if (axis == 'x') {return xVelocity;}//floor(xVelocity);}
else if (axis == 'y') {return xVelocity;}//floor(yVelocity);}
}
//Gravitational Velocity (Where the problems probably are):
void Particle::calculateGravitationalVelocity(Particle distantParticle)
{
//Physics constants:
const double pi = 3.14159265359; //Pi
const double G = 0.00000000006673; //Gravitational Constant (or Big G)
//Big Triangle Trigonometry:
//Get coords of moving particle:
double x1 = x;
double y1 = y;
//Get coords of particle with gravity:
double x2 = distantParticle.getCoords('x');
double y2 = distantParticle.getCoords('y');
if (x1 != x2)
{
//Work out the angle:
double A = atan((y2 - y1) / (x2 - x1)) * 180 / pi;
//Remove the minus sign:
A = fabs(A);
//Small Triangle Trigonometry:
//Work out the hypotenuse of the big triangle:
double hyp = sqrt(pow(x2 - x1, 2) + pow(y2 - y1, 2));
//Work out the gravitational field (hyp of small triangle):
long double gravitationalField = G * (distantParticle.getMass() / pow(hyp, 2));
//For testing purposes:
//std::cout << "X: " << (cos(A) * gravitationalField) / 1000 << std::endl;
//std::cout << "Y: " << (sin(A) * gravitationalField) / 1000 << std::endl;
//Work out the X velocity:
xVelocity = xVelocity + (cos(A) * gravitationalField) / 1000;
//Work out the Y velocity:
yVelocity = yVelocity + (sin(A) * gravitationalField) / 1000;
}
else
{
//Work out the hypotenuse of the big triangle:
double hyp = sqrt(pow(x2 - x1, 2) + pow(y2 - y1, 2));
//Work out the gravitational field (hyp of small triangle):
long double gravitationalField = G * (distantParticle.getMass() / pow(hyp, 2));
yVelocity = yVelocity + gravitationalField / 1000;
}
}
//Material:
void Particle::setMaterial(std::string newMaterialType)
{
material = newMaterialType;
}
std::string Particle::getMaterial()
{
return material;
}
//Mass:
void Particle::setMass(double newMass)
{
mass = newMass;
}
double Particle::getMass()
{
return mass;
}
//Update:
void Particle::update()
{
x = x + xVelocity;
y = y + yVelocity;
}
I am sorry for the very open question, and it probably goes against the rules somewhere, but I couldn't find it. The code for working out mostly uses a two triangles to make a x and y velocity. Here is an image of what I was hoping the code would do as a triangle (sorry it doesn't look great, but I like using a whiteboard):
You don't need to perform any trigonometric calculation.
...
//Get coords of particle with gravity:
double x2 = distantParticle.getCoords('x');
double y2 = distantParticle.getCoords('y');
// Get difference vector
double rx = x1 - x2;
double ry = y1 - y2;
// square of distance
double r2 = rx * rx + ry * ry;
// distance
double r = sqrt (r2);
if (r != 0) {
// normalize difference vector
double ux = rx / r;
double uy = ry / r;
// acceleration of gravity
double a = - G * distantParticle.getMass() / r2;
xVelocity += a * ux / 1000;
yVelocity += a * uy / 1000;
}
}