From ce6f6beded335b4b42d3042aa5fce140ee4b756e Mon Sep 17 00:00:00 2001 From: blobt <380255922@qq.com> Date: Fri, 21 Feb 2020 15:28:05 +0800 Subject: [PATCH] =?UTF-8?q?=E7=9F=A9=E9=98=B5=E6=93=8D=E4=BD=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/2texture/Raster.cc | 12 +++-- src/2texture/texture.cc | 38 +++++++------ src/3statemachine/Raster.cc | 15 ++++-- src/3statemachine/statemachine.cc | 62 ++++++++++++++++++---- src/4matrix/Raster.cc | 8 ++- src/4matrix/common.h | 3 ++ src/4matrix/matrix.cc | 71 +++++++++++++++++++++---- src/4matrix/struct/tmat3x3.h | 88 +++++++++++++++++++++++++++++-- src/4matrix/struct/tvec2.h | 5 ++ src/4matrix/struct/tvec3.h | 13 +++-- 10 files changed, 260 insertions(+), 55 deletions(-) diff --git a/src/2texture/Raster.cc b/src/2texture/Raster.cc index a96da80..29cfef7 100644 --- a/src/2texture/Raster.cc +++ b/src/2texture/Raster.cc @@ -120,8 +120,11 @@ void Raster::drawSpan(const Span &span, Image* image) { float length = span.xEnd - span.xStart; - int startX = tmax(span.xStart, 0); - int endX = tmin(span.xEnd, _width); + //int startX = tmax(span.xStart, 0); + //int endX = tmin(span.xEnd, _width); + + int startX = span.xStart; + int endX = span.xEnd; int t = 0; //优化3 x轴越界处理 @@ -165,6 +168,7 @@ void Raster::drawEge(const Ege& e1, const Ege& e2, Image* image) { int startY2 = tmin(e2.y2, _height); int endY2 = tmax(e2.y1, 0); scale2 = (e2.y2 - startY2) / yOffset2; + int startY1 = tmin(e1.y2, _height); float s = (e1.y2 - startY1) / yOffset1; @@ -233,9 +237,9 @@ void Raster::drawTriangle(const Vertex& vertex, Image* image) { int iShort1 = (iMax + 1) % 3; int iShort2 = (iMax + 2) % 3; - + drawEge(eges[iMax], eges[iShort1], image); - drawEge(eges[iMax], eges[iShort2], image); + //drawEge(eges[iMax], eges[iShort2], image); } int Raster::size() { diff --git a/src/2texture/texture.cc b/src/2texture/texture.cc index e73e25f..70c68fd 100644 --- a/src/2texture/texture.cc +++ b/src/2texture/texture.cc @@ -20,26 +20,34 @@ Raster raster(width, height); void example() { Image* image = Image::loadFromFile("/home/blobt/Documents/dev/cpp/3dbase/image/scale.jpg"); - Raster::Vertex ver1 = { - int2(10, 10), float2(0.0f, 0.0f), Rgba(255,0,0), - int2(10, 110), float2(0.0f, 1.0f),Rgba(255,0,0), - int2(110, 10), float2(1.0f, 0.0f), Rgba(255,0,0) - }; - - raster.drawTriangle(ver1, image); + Raster::Vertex ver1 = { + int2(-50, 10), float2(0.0f, 0.0f), Rgba(255, 0, 0), + int2(-50, 110), float2(0.0f, 1.0f), Rgba(255, 0, 0), + int2(50, 10), float2(1.0f, 0.0f), Rgba(255, 0, 0) + }; + raster.drawTriangle(ver1, image); + +// Raster::Vertex verx = { +// int2(0, 110), float2(0.0f, 0.0f), Rgba(255, 0, 0), +// int2(0, 210), float2(0.0f, 1.0f), Rgba(255, 0, 0), +// int2(100, 110), float2(1.0f, 0.0f), Rgba(255, 0, 0) +// }; +// +// raster.drawTriangle(verx, image); - Raster::Vertex ver2 = { - int2(10, 110), float2(0.0f, 1.0f), Rgba(255,0,0), - int2(110, 110), float2(1.0f, 1.0f), Rgba(255,0,0), - int2(110, 10), float2(1.0f, 0.0f), Rgba(255,0,0) - }; - raster.drawTriangle(ver2, image); + // Raster::Vertex ver2 = { + // int2(10, 110), float2(0.0f, 1.0f), Rgba(255,0,0), + // int2(110, 110), float2(1.0f, 1.0f), Rgba(255,0,0), + // int2(110, 10), float2(1.0f, 0.0f), Rgba(255,0,0) + // }; + // + // raster.drawTriangle(ver2, image); -// raster.drawPoint(10, 110, Rgba(255, 0, 0, 0), 3); -// raster.drawPoint(110, 10, Rgba(255, 0, 0, 0), 3); + // raster.drawPoint(10, 110, Rgba(255, 0, 0, 0), 3); + // raster.drawPoint(110, 10, Rgba(255, 0, 0, 0), 3); //raster.drawLine(float2(10, 110), float2(110, 10), Rgba(255, 255, 255)); diff --git a/src/3statemachine/Raster.cc b/src/3statemachine/Raster.cc index 7971904..a02b799 100644 --- a/src/3statemachine/Raster.cc +++ b/src/3statemachine/Raster.cc @@ -157,8 +157,11 @@ void Raster::drawSpan(const Span &span, Image* image) { float length = span.xEnd - span.xStart; - int startX = tmax(span.xStart, 0); - int endX = tmin(span.xEnd, _width); + //int startX = tmax(span.xStart, 0); + //int endX = tmin(span.xEnd, _width); + + int startX = span.xStart; + int endX = span.xEnd; int t = 0; //优化3 x轴越界处理 @@ -168,12 +171,13 @@ void Raster::drawSpan(const Span &span, Image* image) { if (length == 0) { scale = 0; } else { - scale = (float) (i - startX) / length; + scale = (float) ((i - startX) / length); } + blendColor = colorLerp(span.color1, span.color2, scale); uv = uvLerp(span.uvStart, span.uvEnd, scale); - + color = image->pixelUv(uv.x, uv.y); //颜色混合 @@ -181,7 +185,7 @@ void Raster::drawSpan(const Span &span, Image* image) { - setPixel(i, span.y, dstColor); + setPixel(i, span.y, color); } } @@ -201,6 +205,7 @@ void Raster::drawEge(const Ege& e1, const Ege& e2, Image* image) { //优化2 y轴越界处理 int startY2 = tmin(e2.y2, _height); int endY2 = tmax(e2.y1, 0); + scale2 = (e2.y2 - startY2) / yOffset2; int startY1 = tmin(e1.y2, _height); diff --git a/src/3statemachine/statemachine.cc b/src/3statemachine/statemachine.cc index 9410114..a00d169 100644 --- a/src/3statemachine/statemachine.cc +++ b/src/3statemachine/statemachine.cc @@ -25,16 +25,15 @@ struct Vertex { */ void example() { - Vertex vertexs[] = - { - {10, 10, 0.0f, 0.0f, Rgba(255,0,0,255)}, - {410, 410, 1.0f, 1.0f, Rgba(255,0,0,255)}, - {410, 10, 1.0f, 0.0f, Rgba(255,0,0,255)}, + Vertex vertexs[] = { + {10, 10, 0.0f, 0.0f, Rgba(255, 0, 0, 255)}, + {410, 410, 1.0f, 1.0f, Rgba(255, 0, 0, 255)}, + {410, 10, 1.0f, 0.0f, Rgba(255, 0, 0, 255)}, - {10, 10, 0.0f, 0.0f, Rgba(255,255,255,255)}, - {410, 410, 1.0f, 1.0f, Rgba(255,255,255,255)}, - {10, 410, 0.0f, 1.0f, Rgba(255,255,255,255)}, - }; + {10, 10, 0.0f, 0.0f, Rgba(255, 255, 255, 255)}, + {410, 410, 1.0f, 1.0f, Rgba(255, 255, 255, 255)}, + {10, 410, 0.0f, 1.0f, Rgba(255, 255, 255, 255)}, + }; Image* image = Image::loadFromFile("/home/blobt/Documents/dev/cpp/3dbase/image/scale.jpg"); @@ -44,15 +43,56 @@ void example() { raster.vertexPointer(2, DT_FLOAT, sizeof (Vertex), &vertexs[0].x); raster.colorPointer(4, DT_BYTE, sizeof (Vertex), &vertexs[0].color); raster.textureCoordPointer(2, DT_FLOAT, sizeof (Vertex), &vertexs[0].u); - + raster.drawArrays(DM_TRIANGES, 0, 6); } +void example1() { + // Vertex vertexs[] = { + // {-50, 10, 0.0f, 0.0f, Rgba(0, 255, 0, 255)}, + // {150, 210, 1.0f, 1.0f, Rgba(0, 255, 0, 255)}, + // {150, 10, 1.0f, 0.0f, Rgba(0, 255, 0, 255)} + // }; + + Vertex vertexs[] = { + {-50, 10, 0.0f, 0.0f, Rgba(0, 255, 0, 255)}, + {-50, 210, 0.0f, 1.0f, Rgba(0, 255, 0, 255)}, + {150, 210, 1.0f, 1.0f, Rgba(0, 255, 0, 255)} + }; + + Image* image = Image::loadFromFile("/home/blobt/Documents/dev/cpp/3dbase/image/scale.jpg"); + + + raster.bindTexture(image); + + raster.vertexPointer(2, DT_FLOAT, sizeof (Vertex), &vertexs[0].x); + raster.colorPointer(4, DT_BYTE, sizeof (Vertex), &vertexs[0].color); + raster.textureCoordPointer(2, DT_FLOAT, sizeof (Vertex), &vertexs[0].u); + + raster.drawArrays(DM_TRIANGES, 0, 3); + + + Vertex vertexs2[] = { + {10, 210, 0.0f, 0.0f, Rgba(0, 255, 0, 255)}, + {10, 420, 0.0f, 1.0f, Rgba(0, 255, 0, 255)}, + {210, 420, 1.0f, 1.0f, Rgba(0, 255, 0, 255)} + }; + + + raster.bindTexture(image); + + raster.vertexPointer(2, DT_FLOAT, sizeof (Vertex), &vertexs2[0].x); + raster.colorPointer(4, DT_BYTE, sizeof (Vertex), &vertexs2[0].color); + raster.textureCoordPointer(2, DT_FLOAT, sizeof (Vertex), &vertexs2[0].u); + + raster.drawArrays(DM_TRIANGES, 0, 3); +} + unsigned char* makeBitmap() { raster.clean(); - example(); + example1(); return (unsigned char*) raster.buffer; } diff --git a/src/4matrix/Raster.cc b/src/4matrix/Raster.cc index 7971904..7952e15 100644 --- a/src/4matrix/Raster.cc +++ b/src/4matrix/Raster.cc @@ -157,8 +157,12 @@ void Raster::drawSpan(const Span &span, Image* image) { float length = span.xEnd - span.xStart; - int startX = tmax(span.xStart, 0); - int endX = tmin(span.xEnd, _width); + //TODO 这里越界和texture不兼容,没有解决 + //int startX = tmax(span.xStart, 0); + //int endX = tmin(span.xEnd, _width); + + int startX = span.xStart; + int endX = span.xEnd; int t = 0; //优化3 x轴越界处理 diff --git a/src/4matrix/common.h b/src/4matrix/common.h index c8e3433..6a3ff1a 100644 --- a/src/4matrix/common.h +++ b/src/4matrix/common.h @@ -20,13 +20,16 @@ #include "Rgba.h" #include "struct/tvec2.h" #include "struct/tvec3.h" +#include "struct/tmat3x3.h" #define die(m) do { perror(m); exit(EXIT_FAILURE); } while(0) #define def2rad(theta) (0.01745329251994329 * (theta)) //每个角度所对应的弧度 typedef tvec2 float2; typedef tvec2 int2; +typedef tvec3 float3; typedef unsigned char byte; +typedef tmat3x3 matrix3; template inline T tmin(T a, T b) { return a < b ? a : b; diff --git a/src/4matrix/matrix.cc b/src/4matrix/matrix.cc index d619240..7d90629 100644 --- a/src/4matrix/matrix.cc +++ b/src/4matrix/matrix.cc @@ -23,22 +23,71 @@ struct Vertex { /** * */ -void example() { - tvec3 m(1, 2, 3.2); - m++; - printf("%d %d %d\n", m.x, m.y, m.z); - - tvec3 n(1, 1, 1); - - n = m; - - printf("%d %d %d\n", n.x, n.y, n.z); +void example2() { + Vertex vertexs[] = { + {-10, -10, 0.0f, 0.0f, Rgba(255, 0, 0, 255)}, + {210, 210, 1.0f, 1.0f, Rgba(255, 0, 0, 255)}, + {210, -10, 1.0f, 0.0f, Rgba(255, 0, 0, 255)}, + + {-10, -10, 0.0f, 0.0f, Rgba(255, 255, 255, 255)}, + {210, 210, 1.0f, 1.0f, Rgba(255, 255, 255, 255)}, + {-10, 210, 0.0f, 1.0f, Rgba(255, 255, 255, 255)}, + }; + + + matrix3 tranMat1; + tranMat1.translate(-110, -110); + + matrix3 mat; + static float angle = 0; + mat.rotate(angle); + angle += 0.5; + + matrix3 scaleMat; + scaleMat.scale(0.5f, 0.5f); + + matrix3 tranMat2; + tranMat2.translate(110, 110); + + + matrix3 all = tranMat2 * (mat * scaleMat * tranMat1); + + for (int i = 0; i < 6; ++i) { + float3 pos(vertexs[i].x, vertexs[i].y, 1); + + pos = all * pos; + + vertexs[i].x = pos.x; + vertexs[i].y = pos.y; + } + + Image* image = Image::loadFromFile("/home/blobt/Documents/dev/cpp/3dbase/image/scale.jpg"); + + + raster.bindTexture(image); + + raster.vertexPointer(2, DT_FLOAT, sizeof (Vertex), &vertexs[0].x); + raster.colorPointer(4, DT_BYTE, sizeof (Vertex), &vertexs[0].color); + raster.textureCoordPointer(2, DT_FLOAT, sizeof (Vertex), &vertexs[0].u); + + + raster.drawArrays(DM_TRIANGES, 0, 6); +} + +void example1() { + matrix3 mat; + mat.translate(0, 0); + + float3 pos(10.0f, 100.0f, 1.0f); + pos = mat * pos; + + // tvec3(1); } unsigned char* makeBitmap() { raster.clean(); - example(); + example2(); return (unsigned char*) raster.buffer; } diff --git a/src/4matrix/struct/tmat3x3.h b/src/4matrix/struct/tmat3x3.h index 8a74606..435cc8d 100644 --- a/src/4matrix/struct/tmat3x3.h +++ b/src/4matrix/struct/tmat3x3.h @@ -14,7 +14,8 @@ #ifndef TMAT3X3_H #define TMAT3X3_H -#include"tvec3.h" +#include "../common.h" +#define def2rad(theta) (0.01745329251994329 * (theta)) template struct tmat3x3 { @@ -49,7 +50,7 @@ public: value[2] = col_type(zero, zero, one); } - tmat3x3(tmat3x3 const &m) { + tmat3x3(tmat3x3 const &m) { value[0] = m.value[0]; value[1] = m.value[1]; value[2] = m.value[2]; @@ -77,7 +78,88 @@ public: value[1] = v1; value[2] = v2; } -} + + col_type& operator[](size_type i) { + assert(i < this->length()); + return value[i]; + } + + col_type const & operator[](size_type i) const { + assert(i < this->length()); + return this->value[i]; + } + + type &operator=(type & s) { + value[0] = s[0]; + value[1] = s[1]; + value[2] = s[2]; + } + + tvec3 operator*(const tvec3 &s) const { + tvec3 ret( + value[0][0] * s[0] + value[1][0] * s[1] + value[2][0] * s[2], + value[0][1] * s[0] + value[1][1] * s[1] + value[2][1] * s[2], + value[0][2] * s[0] + value[1][2] * s[1] + value[2][2] * s[2] + ); + return ret; + } + + type operator*(const type &s) { + + T const srcA00 = value[0][0]; + T const srcA01 = value[0][1]; + T const srcA02 = value[0][2]; + T const srcA10 = value[1][0]; + T const srcA11 = value[1][1]; + T const srcA12 = value[1][2]; + T const srcA20 = value[2][0]; + T const srcA21 = value[2][1]; + T const srcA22 = value[2][2]; + + T const srcB00 = s[0][0]; + T const srcB01 = s[0][1]; + T const srcB02 = s[0][2]; + T const srcB10 = s[1][0]; + T const srcB11 = s[1][1]; + T const srcB12 = s[1][2]; + T const srcB20 = s[2][0]; + T const srcB21 = s[2][1]; + T const srcB22 = s[2][2]; + + tmat3x3 res; + res[0][0] = srcA00 * srcB00 + srcA10 * srcB01 + srcA20 * srcB02; + res[0][1] = srcA01 * srcB00 + srcA11 * srcB01 + srcA21 * srcB02; + res[0][2] = srcA02 * srcB00 + srcA12 * srcB01 + srcA22 * srcB02; + res[1][0] = srcA00 * srcB10 + srcA10 * srcB11 + srcA20 * srcB12; + res[1][1] = srcA01 * srcB10 + srcA11 * srcB11 + srcA21 * srcB12; + res[1][2] = srcA02 * srcB10 + srcA12 * srcB11 + srcA22 * srcB12; + res[2][0] = srcA00 * srcB20 + srcA10 * srcB21 + srcA20 * srcB22; + res[2][1] = srcA01 * srcB20 + srcA11 * srcB21 + srcA21 * srcB22; + res[2][2] = srcA02 * srcB20 + srcA12 * srcB21 + srcA22 * srcB22; + return res; + } + + void translate(T x, T y) { + value[0] = col_type(value_type(1), value_type(0), value_type(0)); + value[1] = col_type(value_type(0), value_type(1), value_type(0)); + value[2] = col_type(value_type(x), value_type(y), value_type(1)); + } + + void scale(T x, T y) { + value[0] = col_type(value_type(x), value_type(0), value_type(0)); + value[1] = col_type(value_type(0), value_type(y), value_type(0)); + value[2] = col_type(value_type(0), value_type(0), value_type(1)); + } + + void rotate(T angle) { + T rad = def2rad(angle); + T c = cos(rad); + T s = sin(rad); + value[0] = col_type(value_type(c), value_type(-s), value_type(0)); + value[1] = col_type(value_type(s), value_type(c), value_type(0)); + value[2] = col_type(value_type(0), value_type(0), value_type(1)); + } +}; #endif /* TMAT3X3_H */ diff --git a/src/4matrix/struct/tvec2.h b/src/4matrix/struct/tvec2.h index cbe30c3..39d2066 100644 --- a/src/4matrix/struct/tvec2.h +++ b/src/4matrix/struct/tvec2.h @@ -32,6 +32,11 @@ struct tvec2 { return (&x)[i]; } + value_type const operator[](size_type i) const { + assert(i < this->length()); + return (&x)[i]; + } + tvec2() { x = 0; y = 0; diff --git a/src/4matrix/struct/tvec3.h b/src/4matrix/struct/tvec3.h index 03e693a..216f667 100644 --- a/src/4matrix/struct/tvec3.h +++ b/src/4matrix/struct/tvec3.h @@ -35,6 +35,11 @@ struct tvec3 { return (&x)[i]; } + value_type const &operator[](size_type i) const { + assert(i < this->length()); + return (&x)[i]; + } + tvec3() { x = 0; y = 0; @@ -65,9 +70,9 @@ struct tvec3 { */ template tvec3(U s) { - x = value_type(s); - y = value_type(s); - z = value_type(s); +// x = value_type(s); +// y = value_type(s); +// z = value_type(s); } template @@ -85,7 +90,7 @@ struct tvec3 { return *this; } - tvec3 & operator++(T) { + tvec3 & operator++() { ++x; ++y; ++z;