0% found this document useful (0 votes)
9 views2 pages

406 cg10

Cg practical

Uploaded by

supriyathakur744
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views2 pages

406 cg10

Cg practical

Uploaded by

supriyathakur744
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Experiment 10

Q. WAP in c++/c using Z-bu er Algorithm.

Code:

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

// De ne a simple Point structure to represent 3D coordinates


struct Point {
oat x, y, z;
};

// De ne a simple Polygon structure to represent a polygon in 3D space


struct Polygon {
vector<Point> vertices;
};

// De ne a simple ZBu er class to implement Z-bu er algorithm


class ZBu er {
private:
int width, height;
vector< oat> zBu er;

public:
ZBu er(int w, int h) : width(w), height(h) {
zBu er.resize(w * h, numeric_limits< oat>::in nity());
}

void clearBu er() {


ll(zBu er.begin(), zBu er.end(), numeric_limits< oat>::in nity());
}

void setPixel(int x, int y, oat z) {


if (x >= 0 && x < width && y >= 0 && y < height) {
int index = y * width + x;
if (z < zBu er[index]) {
zBu er[index] = z;
// Render pixel here (for simplicity, just print coordinates)
cout << "Pixel at (" << x << ", " << y << ") rendered with z = " << z << endl;
}
}
}
};

// Render function using Z-bu er algorithm


void renderScene(const vector<Polygon>& polygons, ZBu er& zBu er) {
// Iterate through each polygon
for (const auto& polygon : polygons) {
// Assume polygon is simple, not needing back-face culling or clipping
// For simplicity, let's just calculate the average z-coordinate of its vertices
oat avgZ = 0;
for (const auto& vertex : polygon.vertices) {
avgZ += vertex.z;
}
avgZ /= polygon.vertices.size();
fl
fi
fi
fi
fi
fl
ff
ff
ff
fl
ff
ff
ff
ff
ff
ff
fl
ff
ff
ff
fl
fi
ff
fl
ff
fi
ff
// Render each pixel in the polygon
for (const auto& vertex : polygon.vertices) {
// For simplicity, assume all vertices are within the screen bounds
// You may need additional clipping in a real-world scenario
int x = static_cast<int>(vertex.x);
int y = static_cast<int>(vertex.y);
zBu er.setPixel(x, y, avgZ);
}
}
}

int main() {
// De ne the size of the screen
const int screenWidth = 800;
const int screenHeight = 600;

// Create a ZBu er object


ZBu er zBu er(screenWidth, screenHeight);

// De ne a scene with some polygons


vector<Polygon> polygons;

// Add polygons to the vector


polygons.push_back({{ {100, 100, 0}, {200, 100, 0}, {150, 200, 0} }});
polygons.push_back({{ {300, 300, 0}, {400, 300, 0}, {350, 400, 0} }});

// Render the scene using Z-bu er algorithm


renderScene(polygons, zBu er);

return 0;
}
ff
fi
fi
ff
ff
ff
ff
ff

You might also like