How can I draw a half torus? - opengl

I am teaching myself OpenGL game programming from tutorials on the net. I want to draw a half torus, such that it can look like a gateway. How can I do this, does any one know the math involved? most tutorials online show how to draw a full torus.

Here's an answer that adapts the OpenGL Redbook torus.c tutorial
Here's their code for drawing a torus:
static void torus(int numc, int numt)
{
int i, j, k;
double s, t, x, y, z, twopi;
twopi = 2 * PI_;
for (i = 0; i < numc; i++) {
glBegin(GL_QUAD_STRIP);
for (j = 0; j <= numt; j++) {
for (k = 1; k >= 0; k--) {
s = (i + k) % numc + 0.5;
t = j % numt;
x = (1+.1*cos(s*twopi/numc))*cos(t*twopi/numt);
y = (1+.1*cos(s*twopi/numc))*sin(t*twopi/numt);
z = .1 * sin(s * twopi / numc);
glVertex3f(x, y, z);
}
}
glEnd();
}
}
What this does is draws a volume of rotation. You can use this same idea, except stop this loop halfway through (i.e.,
for(i = 0; i < numc/2; i++)
)

Set a clip plane appropriately ([0,0,1,0] ought to work, assuming +Z is 'up') and draw a full torus.

Related

Finding the 3rd vetices location on the coordinate system

So, here is the problem.
I am given the lengths of 3 sides of a triangle.
The program calculates the area of the given triangle using determinates.
I assume that one vertex of the triangle is in the (0,0) point and the 2nd one is in the (c,0), where c is the length of the longest side. So what would be the easiest way to get the 3rd vertices coordinates.
I tried cosine theorem to get the line equation the side is going through, but it is a bit off
I have the determination solver program if you need it down here:
float det(int n, float mat[3][3])
{
int d=0;
int c, subi, i, j, subj;
float submat[3][3];
if(n == 2) {
return( (mat[0][0] * mat[1][1]) - (mat[1][0] * mat[0][1]));
}
else{
for(c = 0; c < n; c++){
subi = 0;
for(i = 1; i < n; i++){
subj = 0;
for(j = 0; j < n; j++){
if (j == c){
continue;
}
submat[subi][subj] = mat[i][j];
subj++;
}
subi++;
}
d = d + (pow(-1 ,c) * mat[0][c] * det(n - 1 ,submat));
}
}
return d;
}
.
.
.
ans=det.det(3,coords)*0.5;
Example picture of the triangle constructed in GeoGebra:

Image Rotation gives grayscale image

I got a problem with my Rotate image function in C++, Using OpenCV and Qt.
It kinda does his job, but not as expected, apart of being in grayscale, a part of the image seems to be duplicated at the top right.
Before
After
void ImgProcessing::rotate(cv::Mat &img, cv::Mat &tmp, int angle){
float rads = angle*3.1415926/180.0;
float cs = cos(-rads);
float ss = sin(-rads);
float xcenter = (float)(img.cols)/2.0;
float ycenter = (float)(img.rows)/2.0;
for(int i = 0; i < img.rows; i++)
for(int j = 0; j < img.cols; j++){
int rorig = ycenter + ((float)(i)-ycenter)*cs - ((float)(j)-xcenter)*ss;
int corig = xcenter + ((float)(i)-ycenter)*ss + ((float)(j)-xcenter)*cs;
int pixel = 0;
if (rorig >= 0 && rorig < img.rows && corig >= 0 && corig < img.cols) {
tmp.at<int>(i ,j) = img.at<int>(rorig, corig);
}else tmp.at<int>(i ,j) = 0;
}
}
Can the problem be in accessing to the image pixels?
It depends on how you read in the image but I think you are accessing it incorrectly. It should be something like this:
Vec3b intensity = image.at<Vec3b>(j, i);

C++ Pattern Matching with FFT cross-correlation (Images)

everyone I am trying to implement patter matching with FFT but I am not sure what the result should be (I think I am missing something even though a read a lot of stuff about the problem and tried a lot of different implementations this one is the best so far). Here is my FFT correlation function.
void fft2d(fftw_complex**& a, int rows, int cols, bool forward = true)
{
fftw_plan p;
for (int i = 0; i < rows; ++i)
{
p = fftw_plan_dft_1d(cols, a[i], a[i], forward ? FFTW_FORWARD : FFTW_BACKWARD, FFTW_ESTIMATE);
fftw_execute(p);
}
fftw_complex* t = (fftw_complex*)fftw_malloc(rows * sizeof(fftw_complex));
for (int j = 0; j < cols; ++j)
{
for (int i = 0; i < rows; ++i)
{
t[i][0] = a[i][j][0];
t[i][1] = a[i][j][1];
}
p = fftw_plan_dft_1d(rows, t, t, forward ? FFTW_FORWARD : FFTW_BACKWARD, FFTW_ESTIMATE);
fftw_execute(p);
for (int i = 0; i < rows; ++i)
{
a[i][j][0] = t[i][0];
a[i][j][1] = t[i][1];
}
}
fftw_free(t);
}
int findCorrelation(int argc, char* argv[])
{
BMP bigImage;
BMP keyImage;
BMP result;
RGBApixel blackPixel = { 0, 0, 0, 1 };
const bool swapQuadrants = (argc == 4);
if (argc < 3 || argc > 4) {
cout << "correlation img1.bmp img2.bmp" << endl;
return 1;
}
if (!keyImage.ReadFromFile(argv[1])) {
return 1;
}
if (!bigImage.ReadFromFile(argv[2])) {
return 1;
}
//Preparations
const int maxWidth = std::max(bigImage.TellWidth(), keyImage.TellWidth());
const int maxHeight = std::max(bigImage.TellHeight(), keyImage.TellHeight());
const int rowsCount = maxHeight;
const int colsCount = maxWidth;
BMP bigTemp = bigImage;
BMP keyTemp = keyImage;
keyImage.SetSize(maxWidth, maxHeight);
bigImage.SetSize(maxWidth, maxHeight);
for (int i = 0; i < rowsCount; ++i)
for (int j = 0; j < colsCount; ++j) {
RGBApixel p1;
if (i < bigTemp.TellHeight() && j < bigTemp.TellWidth()) {
p1 = bigTemp.GetPixel(j, i);
} else {
p1 = blackPixel;
}
bigImage.SetPixel(j, i, p1);
RGBApixel p2;
if (i < keyTemp.TellHeight() && j < keyTemp.TellWidth()) {
p2 = keyTemp.GetPixel(j, i);
} else {
p2 = blackPixel;
}
keyImage.SetPixel(j, i, p2);
}
//Here is where the transforms begin
fftw_complex **a = (fftw_complex**)fftw_malloc(rowsCount * sizeof(fftw_complex*));
fftw_complex **b = (fftw_complex**)fftw_malloc(rowsCount * sizeof(fftw_complex*));
fftw_complex **c = (fftw_complex**)fftw_malloc(rowsCount * sizeof(fftw_complex*));
for (int i = 0; i < rowsCount; ++i) {
a[i] = (fftw_complex*)fftw_malloc(colsCount * sizeof(fftw_complex));
b[i] = (fftw_complex*)fftw_malloc(colsCount * sizeof(fftw_complex));
c[i] = (fftw_complex*)fftw_malloc(colsCount * sizeof(fftw_complex));
for (int j = 0; j < colsCount; ++j) {
RGBApixel p1;
p1 = bigImage.GetPixel(j, i);
a[i][j][0] = (0.299*p1.Red + 0.587*p1.Green + 0.114*p1.Blue);
a[i][j][1] = 0.0;
RGBApixel p2;
p2 = keyImage.GetPixel(j, i);
b[i][j][0] = (0.299*p2.Red + 0.587*p2.Green + 0.114*p2.Blue);
b[i][j][1] = 0.0;
}
}
fft2d(a, rowsCount, colsCount);
fft2d(b, rowsCount, colsCount);
result.SetSize(maxWidth, maxHeight);
for (int i = 0; i < rowsCount; ++i)
for (int j = 0; j < colsCount; ++j) {
fftw_complex& y = a[i][j];
fftw_complex& x = b[i][j];
double u = x[0], v = x[1];
double m = y[0], n = y[1];
c[i][j][0] = u*m + n*v;
c[i][j][1] = v*m - u*n;
int fx = j;
if (fx>(colsCount / 2)) fx -= colsCount;
int fy = i;
if (fy>(rowsCount / 2)) fy -= rowsCount;
float r2 = (fx*fx + fy*fy);
const double cuttoffCoef = (maxWidth * maxHeight) / 37992.;
if (r2<128 * 128 * cuttoffCoef)
c[i][j][0] = c[i][j][1] = 0;
}
fft2d(c, rowsCount, colsCount, false);
const int halfCols = colsCount / 2;
const int halfRows = rowsCount / 2;
if (swapQuadrants) {
for (int i = 0; i < halfRows; ++i)
for (int j = 0; j < halfCols; ++j) {
std::swap(c[i][j][0], c[i + halfRows][j + halfCols][0]);
std::swap(c[i][j][1], c[i + halfRows][j + halfCols][1]);
}
for (int i = halfRows; i < rowsCount; ++i)
for (int j = 0; j < halfCols; ++j) {
std::swap(c[i][j][0], c[i - halfRows][j + halfCols][0]);
std::swap(c[i][j][1], c[i - halfRows][j + halfCols][1]);
}
}
for (int i = 0; i < rowsCount; ++i)
for (int j = 0; j < colsCount; ++j) {
const double& g = c[i][j][0];
RGBApixel pixel;
pixel.Alpha = 0;
int gInt = 255 - static_cast<int>(std::floor(g + 0.5));
pixel.Red = gInt;
pixel.Green = gInt;
pixel.Blue = gInt;
result.SetPixel(j, i, pixel);
}
BMP res;
res.SetSize(maxWidth, maxHeight);
result.WriteToFile("result.bmp");
return 0;
}
Sample output
This question would probably be more appropriately posted on another site like cross validated (metaoptimize.com used to also be a good one, but it appears to be gone)
That said:
There's two similar operations you can perform with FFT: convolution and correlation. Convolution is used for determining how two signals interact with each-other, whereas correlation can be used to express how similar two signals are to each-other. Make sure you're doing the right operation as they're both commonly implemented throught a DFT.
For this type of application of DFTs you usually wouldn't extract any useful information in the fourier spectrum unless you were looking for frequencies common to both data sources or whatever (eg, if you were comparing two bridges to see if their supports are spaced similarly).
Your 3rd image looks a lot like the power domain; normally I see the correlation output entirely grey except where overlap occurred. Your code definitely appears to be computing the inverse DFT, so unless I'm missing something the only other explanation I've come up with for the fuzzy look could be some of the "fudge factor" code in there like:
if (r2<128 * 128 * cuttoffCoef)
c[i][j][0] = c[i][j][1] = 0;
As for what you should expect: wherever there are common elements between the two images you'll see a peak. The larger the peak, the more similar the two images are near that region.
Some comments and/or recommended changes:
1) Convolution & correlation are not scale invariant operations. In other words, the size of your pattern image can make a significant difference in your output.
2) Normalize your images before correlation.
When you get the image data ready for the forward DFT pass:
a[i][j][0] = (0.299*p1.Red + 0.587*p1.Green + 0.114*p1.Blue);
a[i][j][1] = 0.0;
/* ... */
How you grayscale the image is your business (though I would've picked something like sqrt( r*r + b*b + g*g )). However, I don't see you doing anything to normalize the image.
The word "normalize" can take on a few different meanings in this context. Two common types:
normalize the range of values between 0.0 and 1.0
normalize the "whiteness" of the images
3) Run your pattern image through an edge enhancement filter. I've personally made use of canny, sobel, and I think I messed with a few others. As I recall, canny was "quick'n dirty", sobel was more expensive, but I got comparable results when it came time to do correlation. See chapter 24 of the "dsp guide" book that's freely available online. The whole book is worth your time, but if you're low on time then at a minimum chapter 24 will help a lot.
4) Re-scale the output image between [0, 255]; if you want to implement thresholds, do it after this step because the thresholding step is lossy.
My memory on this one is hazy, but as I recall (edited for clarity):
You can scale the final image pixels (before rescaling) between [-1.0, 1.0] by dividing off the largest power spectrum value from the entire power spectrum
The largest power spectrum value is, conveniently enough, the center-most value in the power spectrum (corresponding to the lowest frequency)
If you divide it off the power spectrum, you'll end up doing twice the work; since FFTs are linear, you can delay the division until after the inverse DFT pass to when you're re-scaling the pixels between [0..255].
If after rescaling most of your values end up so black you can't see them, you can use a solution to the ODE y' = y(1 - y) (one example is the sigmoid f(x) = 1 / (1 + exp(-c*x) ), for some scaling factor c that gives better gradations). This has more to do with improving your ability to interpret the results visually than anything you might use to programmatically find peaks.
edit I said [0, 255] above. I suggest you rescale to [128, 255] or some other lower bound that is gray rather than black.

Opengl glRotatef rotating a polygon, won't rotate in place

I'm trying to rotate a polygon in place, but it keeps revolving instead.
To rotate, I calculate the center by finding the average location of each vertex. I call the rotation functions and then call a translation using the center to move it to the middle of the screen. It does end up centered, but it still rotates as if it's not. Any ideas as to what I could be doing wrong?
Here's my code:
void Polygon::DrawPolygon()
{
glPushMatrix();
glLoadMatrixf(matrix);
glTranslatef(displace[0], displace[1], displace[2]);
glRotatef(rotation[0], 1, 0, 0);
glRotatef(rotation[1], 0, 1, 0);
glRotatef(rotation[2], 0, 0, 1);
glTranslatef(-displace[0], -displace[1], displace[2]);
displace[0] = 0; displace[1] = 0; displace[2] = 0;
glGetFloatv(GL_MODELVIEW_MATRIX, matrix);
DrawMaterial();
DrawFaces();
ConnectFaces();
glPopMatrix();
}
Here's how I calculate the center:
void Polygon::FindCenter()
{
float x = 0;
float y = 0;
float z = 0;
for(int j = 0; j < 2; j++)
{
for(int i = 0; i < vertexCount; i++)
{
x += vertices[i][0];
y += vertices[i][1];
z += vertices[i][2] + extrusionDistance * j;
}
}
x = x / (vertexCount * 2);
y = y / (vertexCount * 2);
z = z / (vertexCount * 2);
displace[0] = x;
displace[1] = y;
displace[2] = z;
}
Because of the way my extrusion works I don't need to add the x and y for the vertices of both faces, but I did anyway to keep it consistent.
Here is how I draw the shape:
void Polygon::DrawFaces()
{
for(int j = 0; j < 2; j++)
{
glBegin(GL_POLYGON);
for(int i = 0; i < vertexCount; i++)
{
glVertex3f(vertices[i][0], vertices[i][1], j*extrusionDistance);
}
glEnd();
}
}
void Polygon::ConnectFaces()
{
for(int i = 0; i < vertexCount; i++)
{
glBegin(GL_POLYGON);
glVertex3f(vertices[i][0], vertices[i][1], 0);
glVertex3f(vertices[i][0], vertices[i][1], extrusionDistance);
glVertex3f(vertices[(i+1)%vertexCount][0], vertices[(i+1)%vertexCount][1], extrusionDistance);
glVertex3f(vertices[(i+1)%vertexCount][0], vertices[(i+1)%vertexCount][1], 0);
glEnd();
}
}
I see a few things that stand out to me as being odd:
1) You're calling glLoadMatrixf(matrix) before the call to glTranslate() and glRotate(). Depending on what's in the matrix you're loading, that changes things.
2) You're FindCenter() method calculates the center by including the vertex[i][2] in the calculation of z, but when you actually draw the faces in DrawFaces(), you don't include the vertex[i][2] part, just the extrusion * j part. So you're not drawing the same thing that you're calculating the center of.

Cubic Bezier Implementation

I am trying to create a cubic bezier patch and am having a little bit of trouble figuring out where I am messing up the calculations. I have my 16 control points in a 2d array and I am looking to make the grid have 400 points each separated in increments of .05.
The control points are in increments of 20 (so P0,0 is at (0,0,0) and P3,3 is at (60, 0, 60). The grid will be on the xz plane with the y coordinates all 0.
Right now I have the following which gives the same values for x and z on every point:
float Bezier3(int j, float k)
{
switch(j) {
case 0:
return (1 - k) * (1 - k) * (1 - k);
break;
case 1:
return 3*k*((1 - k) * (1 - k));
break;
case 2:
return 3 * (k * k) * (1 - k);
break;
case 3:
return k * k * k;
break;
}
}
void CalcBezier()
{
float u;
float v;
for (int i = 0; i < 20; i++) {
u = 0;
v = 0;
for (int j = 0; j < 20; j++) {
for (int x = 0; x < 4; x++) {
for (int y = 0; y < 4; y++) {
grid[i][j].x += control[x][y].x * Bezier3(x, u) * Bezier3(y, v);
grid[i][j].y += control[x][y].y * Bezier3(x, u) * Bezier3(y, v);
grid[i][j].z += control[x][y].z * Bezier3(x, u) * Bezier3(y, v);
}
}
u+=.05;
v+=.05;
}
}
}
I am not exactly sure where I am going wrong. I am trying to walk columnwise through the grid and calculate each bezier point.
I am also attempting to draw the resulting mesh as a triangle mesh:
void bezier_plane()
{
CalcBezier();
for (int i = 0; i < 19; i++) {
for (int j = 0; j < 19; j++) {
glBegin(GL_TRIANGLE_STRIP);
glVertex3f(grid[i][j].x, grid[i][j].y, grid[i][j].z);
glVertex3f(grid[i][j+1].x, grid[i][j+1].y, grid[i][j+1].z);
glVertex3f(grid[i+1][j].x, grid[i+1][j].y, grid[i+1][j].z);
glVertex3f(grid[i+1][j+1].x, grid[i+1][j+1].y, grid[i+1][j+1].z);
glEnd();
}
}
}
It looks like you are incrementing both u and v inside the inner loop.
Try changing
u+=.05;
v+=.05;
}
to
v+=.05;
}
u+=.05;
And move the reset to zero outside of the loop.
for (int i = 0; i < 20; i++) {
u = 0;
v = 0;
to
u = 0;
for (int i = 0; i < 20; i++) {
v = 0;