Browse Source

颜色混合

master
blobt 5 years ago
parent
commit
bff053fa1d
  1. 4
      src/texture/Image.cc
  2. 20
      src/texture/Raster.cc
  3. 7
      src/texture/Rgba.cc
  4. 3
      src/texture/Rgba.h
  5. 12
      src/texture/texture.cc

4
src/texture/Image.cc

@ -36,8 +36,8 @@ Rgba Image::pixelAt(int x, int y) const {
} }
Rgba Image::pixelUv(float u, float v) const { Rgba Image::pixelUv(float u, float v) const {
int x = (u * _width) -1; //不管_width还是_height都是从0开始的,
int y = (v * _height) -1;//所以这里要减去1
int x = (u * _width) ;
int y = (v * _height) -2;//TODO ???
return pixelAt(x, y); return pixelAt(x, y);
//return Rgba(255,0,0); //return Rgba(255,0,0);

20
src/texture/Raster.cc

@ -113,6 +113,8 @@ void Raster::drawSpan(const Span &span, Image* image) {
//float length = tmax<int>(span.xStart - span.xEnd, 1); //取保length不为0 //float length = tmax<int>(span.xStart - span.xEnd, 1); //取保length不为0
Rgba color; Rgba color;
Rgba blendColor;
Rgba dstColor;
float2 uv; float2 uv;
float scale; float scale;
float length = span.xEnd - span.xStart; float length = span.xEnd - span.xStart;
@ -131,11 +133,18 @@ void Raster::drawSpan(const Span &span, Image* image) {
} else { } 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); uv = uvLerp(span.uvStart, span.uvEnd, scale);
color = image->pixelUv(uv.x, uv.y); color = image->pixelUv(uv.x, uv.y);
setPixel(i, span.y, color);
//颜色混合
dstColor = color + blendColor;
setPixel(i, span.y, dstColor);
} }
} }
@ -163,6 +172,9 @@ void Raster::drawEge(const Ege& e1, const Ege& e2, Image* image) {
float2 uvE1; float2 uvE1;
float2 uvE2; float2 uvE2;
Rgba color1;
Rgba color2;
for (int y = startY2; y > endY2; y--) { for (int y = startY2; y > endY2; y--) {
int x2; int x2;
@ -171,6 +183,7 @@ void Raster::drawEge(const Ege& e1, const Ege& e2, Image* image) {
} else { } else {
x2 = e2.x2 - xOffset2 * scale2; x2 = e2.x2 - xOffset2 * scale2;
} }
color2 = colorLerp(e2.color1, e2.color2, scale2);
uvE2 = uvLerp(e2.uv2, e2.uv1, scale2); uvE2 = uvLerp(e2.uv2, e2.uv1, scale2);
scale2 += setp2; scale2 += setp2;
//setPixel(x2, y, Rgba(255,0,0)); //setPixel(x2, y, Rgba(255,0,0));
@ -182,11 +195,12 @@ void Raster::drawEge(const Ege& e1, const Ege& e2, Image* image) {
} else { } else {
x1 = e1.x2 - xOffset1 * scale1; x1 = e1.x2 - xOffset1 * scale1;
} }
color1 = colorLerp(e1.color1, e1.color2, scale1);
uvE1 = uvLerp(e1.uv2, e1.uv1, scale1); uvE1 = uvLerp(e1.uv2, e1.uv1, scale1);
scale1 += setp1; scale1 += setp1;
//setPixel(x1, y, Rgba(255,0,0)); //setPixel(x1, y, Rgba(255,0,0));
Span span(x1, x2, y, Rgba(255, 255, 255), Rgba(255, 255, 255), uvE1, uvE2);
Span span(x1, x2, y, color1, color2, uvE1, uvE2);
drawSpan(span, image); drawSpan(span, image);
} }
} }

7
src/texture/Rgba.cc

@ -36,6 +36,13 @@ bool operator!=(Rgba &left, Rgba &right) {
return left._a != right._a || left._b != right._b || left._g != right._g || left._r != right._r; return left._a != right._a || left._b != right._b || left._g != right._g || left._r != right._r;
} }
Rgba operator+(Rgba &left, Rgba &right) {
return Rgba(left._r + right._r
,left._g + right._g
,left._b + right._b
,left._a + right._a);
}
Rgba::operator unsigned() { Rgba::operator unsigned() {
unsigned int ret; unsigned int ret;

3
src/texture/Rgba.h

@ -18,10 +18,11 @@
class Rgba { class Rgba {
public: public:
Rgba(unsigned char r = 0, unsigned char g = 0, unsigned char b = 0, unsigned char a = 255);
Rgba(unsigned char r = 255, unsigned char g = 255, unsigned char b = 255, unsigned char a = 255);
Rgba(unsigned int rgba); Rgba(unsigned int rgba);
friend bool operator==(Rgba &left, Rgba &right); friend bool operator==(Rgba &left, Rgba &right);
friend bool operator!=(Rgba &left, Rgba &righe); friend bool operator!=(Rgba &left, Rgba &righe);
friend Rgba operator+(Rgba &left, Rgba &righe);
operator unsigned(); operator unsigned();
operator int(); operator int();
operator long(); operator long();

12
src/texture/texture.cc

@ -21,9 +21,9 @@ void example() {
Image* image = Image::loadFromFile("/home/blobt/Documents/dev/cpp/3dbase/image/scale.jpg"); Image* image = Image::loadFromFile("/home/blobt/Documents/dev/cpp/3dbase/image/scale.jpg");
Raster::Vertex ver1 = { Raster::Vertex ver1 = {
int2(10, 10), float2(0.0f, 0.0f), Rgba(),
int2(10, 110), float2(0.0f, 1.0f), Rgba(),
int2(110, 10), float2(1.0f, 0.0f), Rgba()
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.drawTriangle(ver1, image);
@ -31,9 +31,9 @@ void example() {
Raster::Vertex ver2 = { Raster::Vertex ver2 = {
int2(10, 110), float2(0.0f, 1.0f), Rgba(),
int2(110, 110), float2(1.0f, 1.0f), Rgba(),
int2(110, 10), float2(1.0f, 0.0f), Rgba()
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.drawTriangle(ver2, image);

Loading…
Cancel
Save