[go: nahoru, domu]

Skip to content

Commit

Permalink
Merge pull request #56 from keith2018/dev
Browse files Browse the repository at this point in the history
Fix software renderer primitive clipping
  • Loading branch information
keith2018 committed Nov 26, 2023
2 parents 928103b + 6af1620 commit d9328b1
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 43 deletions.
39 changes: 21 additions & 18 deletions src/Render/Software/RendererSoft.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,9 @@ void RendererSoft::processClipping() {
if (renderState_->polygonMode != PolygonMode_FILL) {
continue;
}
clippingTriangle(primitive);
std::vector<PrimitiveHolder> appendPrimitives;
clippingTriangle(primitive, appendPrimitives);
primitives_.insert(primitives_.end(), appendPrimitives.begin(), appendPrimitives.end());
break;
}
}
Expand Down Expand Up @@ -441,17 +443,21 @@ void RendererSoft::clippingPoint(PrimitiveHolder &point) {
void RendererSoft::clippingLine(PrimitiveHolder &line, bool postVertexProcess) {
VertexHolder *v0 = &vertexes_[line.indices[0]];
VertexHolder *v1 = &vertexes_[line.indices[1]];
auto clipMaskV0 = v0->clipMask;
auto clipMaskV1 = v1->clipMask;
auto clipPosV0 = v0->clipPos;
auto clipPosV1 = v1->clipPos;

bool fullClip = false;
float t0 = 0.f;
float t1 = 1.f;

int mask = v0->clipMask | v1->clipMask;
int mask = clipMaskV0 | clipMaskV1;
if (mask != 0) {
for (int i = 0; i < 6; i++) {
if (mask & FrustumClipMaskArray[i]) {
float d0 = glm::dot(FrustumClipPlane[i], v0->clipPos);
float d1 = glm::dot(FrustumClipPlane[i], v1->clipPos);
float d0 = glm::dot(FrustumClipPlane[i], clipPosV0);
float d1 = glm::dot(FrustumClipPlane[i], clipPosV1);

if (d0 < 0 && d1 < 0) {
fullClip = true;
Expand All @@ -472,18 +478,15 @@ void RendererSoft::clippingLine(PrimitiveHolder &line, bool postVertexProcess) {
return;
}

if (v0->clipMask) {
auto &vert = clippingNewVertex(line.indices[0], line.indices[1], t0, postVertexProcess);
line.indices[0] = vert.index;
if (clipMaskV0) {
line.indices[0] = clippingNewVertex(line.indices[0], line.indices[1], t0, postVertexProcess);
}

if (v1->clipMask) {
auto &vert = clippingNewVertex(line.indices[0], line.indices[1], t1, postVertexProcess);
line.indices[1] = vert.index;
if (clipMaskV1) {
line.indices[1] = clippingNewVertex(line.indices[0], line.indices[1], t1, postVertexProcess);
}
}

void RendererSoft::clippingTriangle(PrimitiveHolder &triangle) {
void RendererSoft::clippingTriangle(PrimitiveHolder &triangle, std::vector<PrimitiveHolder> &appendPrimitives) {
auto *v0 = &vertexes_[triangle.indices[0]];
auto *v1 = &vertexes_[triangle.indices[1]];
auto *v2 = &vertexes_[triangle.indices[2]];
Expand Down Expand Up @@ -523,8 +526,8 @@ void RendererSoft::clippingTriangle(PrimitiveHolder &triangle) {
if (std::signbit(dPre) != std::signbit(d)) {
float t = d < 0 ? dPre / (dPre - d) : -dPre / (d - dPre);
// create new vertex
auto &vert = clippingNewVertex(idxPre, idx, t);
indicesOut.push_back(vert.index);
auto vertIdx = clippingNewVertex(idxPre, idx, t);
indicesOut.push_back(vertIdx);
}

idxPre = idx;
Expand All @@ -545,8 +548,8 @@ void RendererSoft::clippingTriangle(PrimitiveHolder &triangle) {
triangle.indices[2] = indicesIn[2];

for (int i = 3; i < indicesIn.size(); i++) {
primitives_.emplace_back();
PrimitiveHolder &ph = primitives_.back();
appendPrimitives.emplace_back();
PrimitiveHolder &ph = appendPrimitives.back();
ph.discard = false;
ph.indices[0] = indicesIn[0];
ph.indices[1] = indicesIn[i - 1];
Expand Down Expand Up @@ -950,7 +953,7 @@ void RendererSoft::setFrameColor(int x, int y, const RGBA &color, int sample) {
}
}

VertexHolder &RendererSoft::clippingNewVertex(size_t idx0, size_t idx1, float t, bool postVertexProcess) {
size_t RendererSoft::clippingNewVertex(size_t idx0, size_t idx1, float t, bool postVertexProcess) {
vertexes_.emplace_back();
VertexHolder &vh = vertexes_.back();
vh.discard = false;
Expand All @@ -962,7 +965,7 @@ VertexHolder &RendererSoft::clippingNewVertex(size_t idx0, size_t idx1, float t,
viewportTransformImpl(vh);
}

return vh;
return vh.index;
}

void RendererSoft::vertexShaderImpl(VertexHolder &vertex) {
Expand Down
4 changes: 2 additions & 2 deletions src/Render/Software/RendererSoft.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ class RendererSoft : public Renderer {

void clippingPoint(PrimitiveHolder &point);
void clippingLine(PrimitiveHolder &line, bool postVertexProcess = false);
void clippingTriangle(PrimitiveHolder &triangle);
void clippingTriangle(PrimitiveHolder &triangle, std::vector<PrimitiveHolder> &appendPrimitives);

void interpolateVertex(VertexHolder &out, VertexHolder &v0, VertexHolder &v1, float t);
void interpolateLinear(float *varsOut, const float *varsIn[2], size_t elemCnt, float t);
Expand All @@ -94,7 +94,7 @@ class RendererSoft : public Renderer {
inline float *getFrameDepth(int x, int y, int sample);
inline void setFrameColor(int x, int y, const RGBA &color, int sample);

VertexHolder &clippingNewVertex(size_t idx0, size_t idx1, float t, bool postVertexProcess = false);
size_t clippingNewVertex(size_t idx0, size_t idx1, float t, bool postVertexProcess = false);
void vertexShaderImpl(VertexHolder &vertex);
void perspectiveDivideImpl(VertexHolder &vertex);
void viewportTransformImpl(VertexHolder &vertex);
Expand Down
44 changes: 21 additions & 23 deletions src/Viewer/ConfigPanel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -150,30 +150,28 @@ void ConfigPanel::drawSettings() {
ImGui::Checkbox("shadow floor", &config_.showFloor);
config_.shadowMap = config_.showFloor;

if (config_.wireframe) {
return;
}

// light
ImGui::Separator();
ImGui::Text("ambient color");
ImGui::ColorEdit3("ambient color", (float *) &config_.ambientColor, ImGuiColorEditFlags_NoLabel);

ImGui::Separator();
ImGui::Checkbox("point light", &config_.showLight);
if (config_.showLight) {
ImGui::Text("light color");
ImGui::ColorEdit3("light color", (float *) &config_.pointLightColor, ImGuiColorEditFlags_NoLabel);

ImGui::Text("light position");
ImGui::SliderAngle("##light position", &lightPositionAngle_, 0, 360.f);
}
if (!config_.wireframe) {
// light
ImGui::Separator();
ImGui::Text("ambient color");
ImGui::ColorEdit3("ambient color", (float *) &config_.ambientColor, ImGuiColorEditFlags_NoLabel);

ImGui::Separator();
ImGui::Checkbox("point light", &config_.showLight);
if (config_.showLight) {
ImGui::Text("light color");
ImGui::ColorEdit3("light color", (float *) &config_.pointLightColor, ImGuiColorEditFlags_NoLabel);

ImGui::Text("light position");
ImGui::SliderAngle("##light position", &lightPositionAngle_, 0, 360.f);
}

// mipmaps
ImGui::Separator();
if (ImGui::Checkbox("mipmaps", &config_.mipmaps)) {
if (resetMipmapsFunc_) {
resetMipmapsFunc_();
// mipmaps
ImGui::Separator();
if (ImGui::Checkbox("mipmaps", &config_.mipmaps)) {
if (resetMipmapsFunc_) {
resetMipmapsFunc_();
}
}
}

Expand Down

0 comments on commit d9328b1

Please sign in to comment.