bool Grid::intersect(const Ray &r, Hit &h, float tmin) { //Prepare the Material objects Vec3f diffuseColor(1, 1, 1); Vec3f specularColor(0, 0, 0); float exponent = 1; Vec3f reflectiveColor(0, 0, 0); Vec3f transparentColor(0, 0, 0); float indexOfRefraction = 1; PhongMaterial *m = new PhongMaterial(diffuseColor, specularColor, exponent, reflectiveColor, transparentColor, indexOfRefraction); //Start to do DDA MarchingInfo mi; initializeRayMarch(mi, r, tmin); bool hitSomething = false; int i, j, k; do { mi.getIndices(i, j, k); printf("The current point is:%d,%d,%d\n", i, j, k); if (mObjects[offset(i, j, k)].getNumObjects() > 0) { h.set(mi.get_tmin(), m, mi.getNormal(), r); return true; } mi.nextCell(); } while(i < mXSize && j < mYSize && k < mZSize); return false; }
void SharedBitmap::draw(HDC hdc, const IntRect& dstRect, const IntRect& srcRect, CompositeOperator compositeOp, BlendMode blendMode) { if (!m_pixels) return; if (dstRect.isEmpty() || srcRect.isEmpty()) return; HBITMAP hbitmap = 0; OwnPtr<HBITMAP> hTempBitmap; bool usingHandle = compositeOp == CompositeSourceOver && (hasAlpha() && hasAlphaBlendSupport() || usesTransparentColor()); if (usingHandle) { if (ensureHandle()) hbitmap = m_hbitmap.get(); else { void* pixels; BitmapInfo bmpInfo; hTempBitmap = createHandle(&pixels, &bmpInfo, -1, usesTransparentColor()); hbitmap = hTempBitmap.get(); } } if (!hbitmap) { // FIXME: handle other composite operation types? DWORD rop = compositeOp == CompositeCopy ? SRCCOPY : compositeOp == CompositeXOR ? PATINVERT : compositeOp == CompositeClear ? WHITENESS : SRCCOPY; StretchDIBits(hdc, dstRect.x(), dstRect.y(), dstRect.width(), dstRect.height(), srcRect.x(), srcRect.y(), srcRect.width(), srcRect.height(), m_pixels, &m_bmpInfo, DIB_RGB_COLORS, rop); return; } OwnPtr<HDC> hmemdc = adoptPtr(CreateCompatibleDC(hdc)); HGDIOBJ hOldBmp = SelectObject(hmemdc.get(), hbitmap); if (!usesTransparentColor() && hasAlphaBlendSupport()) { static const BLENDFUNCTION blend = { AC_SRC_OVER, 0, 255, AC_SRC_ALPHA }; bool success = alphaBlendIfSupported(hdc, dstRect.x(), dstRect.y(), dstRect.width(), dstRect.height(), hmemdc.get(), srcRect.x(), srcRect.y(), srcRect.width(), srcRect.height(), blend); ASSERT_UNUSED(success, success); } else { TransparentBlt(hdc, dstRect.x(), dstRect.y(), dstRect.width(), dstRect.height(), hmemdc.get(), srcRect.x(), srcRect.y(), srcRect.width(), srcRect.height(), transparentColor()); } SelectObject(hmemdc.get(), hOldBmp); }
RgbMap* Sprite::rgbMap(frame_t frame, RgbMapFor forLayer) const { int maskIndex = (forLayer == RgbMapFor::OpaqueLayer ? -1: transparentColor()); if (m_rgbMap == NULL) { m_rgbMap = new RgbMap(); m_rgbMap->regenerate(palette(frame), maskIndex); } else if (!m_rgbMap->match(palette(frame)) || m_rgbMap->maskIndex() != maskIndex) { m_rgbMap->regenerate(palette(frame), maskIndex); } return m_rgbMap; }
//! [2] QRect BasicToolsPlugin::mouseMove(const QString &brush, QPainter &painter, const QPoint &oldPos, const QPoint &newPos) { painter.save(); int rad = painter.pen().width() / 2; QRect boundingRect = QRect(oldPos, newPos).normalized() .adjusted(-rad, -rad, +rad, +rad); QColor color = painter.pen().color(); int thickness = painter.pen().width(); QColor transparentColor(color.red(), color.green(), color.blue(), 0); //! [2] //! [3] if (brush == tr("Pencil")) { painter.drawLine(oldPos, newPos); } else if (brush == tr("Air Brush")) { int numSteps = 2 + (newPos - oldPos).manhattanLength() / 2; painter.setBrush(QBrush(color, Qt::Dense6Pattern)); painter.setPen(Qt::NoPen); for (int i = 0; i < numSteps; ++i) { int x = oldPos.x() + i * (newPos.x() - oldPos.x()) / (numSteps - 1); int y = oldPos.y() + i * (newPos.y() - oldPos.y()) / (numSteps - 1); painter.drawEllipse(x - (thickness / 2), y - (thickness / 2), thickness, thickness); } } else if (brush == tr("Random Letters")) { QChar ch('A' + (qrand() % 26)); QFont biggerFont = painter.font(); biggerFont.setBold(true); biggerFont.setPointSize(biggerFont.pointSize() + thickness); painter.setFont(biggerFont); painter.drawText(newPos, QString(ch)); QFontMetrics metrics(painter.font()); boundingRect = metrics.boundingRect(ch); boundingRect.translate(newPos); boundingRect.adjust(-10, -10, +10, +10); } painter.restore(); return boundingRect; }
Toy* ToyWindowTab::AddToy(Toy::EnumToyType type, const QSize &gridSize, const QPoint &pos, bool clipToBounds, Toy::Client *pClient) { sFrame frame; frame.toy = Toy::Create(type, pClient, this, /*flags*/0); if( frame.toy ) { frame.toy->setContentsMargins(0, 0, 0, 0); frame.toy->SetGridSize(gridSize); QColor transparentColor( frame.toy->GetColor() ); transparentColor.setAlpha(0); frame.toy->SetColor(transparentColor); frame.toy->setAutoFillBackground(false); QPoint toyPos(mapFromGlobal(pos) - QPoint(frame.toy->width()/2,frame.toy->height()/2)); Utils::Snap(ToyWindowTab::GRID_SPACING, toyPos); frame.toy->move(toyPos); frame.editFrame = new EditFrame( frame.toy ); if(m_Mode == ToyWidget::MODE_EDIT) frame.editFrame->InitEditMode(); else frame.editFrame->ShutdownEditMode(); connect(frame.editFrame, SIGNAL(pressed(EditFrame*,bool)), this, SLOT(onEditFramePressed(EditFrame*,bool))); connect(frame.editFrame, SIGNAL(translated(EditFrame*,const QPoint&)), this, SLOT(onEditFrameTranslated(EditFrame*,const QPoint&))); connect(frame.editFrame, SIGNAL(grabbed(EditFrame*)), this, SLOT(onEditFrameGrabbed(EditFrame*))); connect(frame.editFrame, SIGNAL(gridResized(EditFrame*,const QSize&)), this, SLOT(onEditFrameGridResized(EditFrame*,const QSize&))); connect(frame.editFrame, SIGNAL(raised(EditFrame*)), this, SLOT(onEditFrameRaised(EditFrame*))); connect(frame.editFrame, SIGNAL(lowered(EditFrame*)), this, SLOT(onEditFrameLowered(EditFrame*))); connect(frame.editFrame, SIGNAL(deleted(EditFrame*)), this, SLOT(onEditFrameDeleted(EditFrame*))); m_Frames.push_back(frame); if( clipToBounds ) ClipFrameToBounds(m_Frames.size()-1); frame.toy->show(); }
QRect AGenericBrush::move(const QString &brush, QPainter &painter,const QPoint &oldPos, const QPoint &newPos) { painter.save(); int rad = painter.pen().width(); QRect boundingRect = QRect(oldPos, newPos).normalized().adjusted(-rad, -rad, +rad, +rad); QColor color = painter.pen().color(); int thickness = painter.pen().width(); QColor transparentColor(color.red(), color.green(), color.blue(), 0); QPainterPath path; path.setFillRule ( Qt::WindingFill ); // m_path.setFillRule ( Qt::WindingFill ); path.moveTo(oldPos); path.lineTo(newPos); m_path.closeSubpath(); m_path.addPath(path); painter.drawPath(path); painter.restore(); return boundingRect; }
/* Parse the "Materials" token */ void scene::parseMaterials() { char token[MAX_PARSER_TOKEN_LENGTH]; char texname[MAX_PARSER_TOKEN_LENGTH]; getToken(token); assert (!strcmp(token, "{")); /* Loop over each Material */ bool working=true; while (working) { getToken(token); if (!strcmp(token, "}")) { working = false; } else if (!strcmp(token, "Material")) { getToken(token); assert (!strcmp(token, "{")); texname[0] = '\0'; Vec3f diffuseColor(1,1,1); Vec3f specularColor(0,0,0); float shininess = 1; Vec3f transparentColor(0,0,0); Vec3f reflectiveColor(0,0,0); float indexOfRefraction = 1; while (1) { getToken(token); if (!strcmp(token, "textureFilename")) { getToken(token); strcpy(texname, token); } else if (!strcmp(token, "diffuseColor")) diffuseColor = readVec3f(); else if (!strcmp(token, "specularColor")) specularColor = readVec3f(); else if (!strcmp(token, "shininess")) shininess = readFloat(); else if (!strcmp(token, "transparentColor")) transparentColor = readVec3f(); else if (!strcmp(token, "reflectiveColor")) reflectiveColor = readVec3f(); else if (!strcmp(token, "indexOfRefraction")) indexOfRefraction = readFloat(); else { assert (!strcmp(token, "}")); break; } } material temp; temp.diffuseCol=diffuseColor; temp.specularCol=specularColor; temp.shininess=shininess; temp.transparentCol=transparentColor; temp.reflectiveCol=reflectiveColor; temp.refractionIndex=indexOfRefraction; if(strcmp(texname, "NULL")) temp.texture=jpeg_read(texname, NULL); else temp.texture=NULL; myMaterials.push_back(temp); } } }
void SharedBitmap::drawPattern(HDC hdc, const AffineTransform& transform, const FloatRect& tileRectIn, const AffineTransform& patternTransform, const FloatPoint& phase, ColorSpace styleColorSpace, CompositeOperator op, const FloatRect& destRect, const IntSize& origSourceSize) { if (!m_pixels) return; if (tileRectIn.width() <= 0 || tileRectIn.height() <= 0) return; bool useAlpha = op == CompositeSourceOver && hasAlpha() && is32bit(); int bmpWidth = width(); int bmpHeight = height(); FloatRect tileRect(tileRectIn); if (bmpWidth != origSourceSize.width()) { double rate = static_cast<double>(bmpWidth) / origSourceSize.width(); double temp = tileRect.width() * rate; tileRect.setX(tileRect.x() * rate); tileRect.setWidth(temp); temp = tileRect.height() * rate; tileRect.setY(tileRect.y() * rate); tileRect.setHeight(temp); } OwnPtr<HBITMAP> clippedBmp; if (tileRect.x() || tileRect.y() || tileRect.width() != bmpWidth || tileRect.height() != bmpHeight) { BitmapInfo patternBmpInfo; void* patternPixels; clippedBmp = clipBitmap(IntRect(tileRect), useAlpha, patternBmpInfo, patternPixels); if (!clippedBmp) return; bmpWidth = tileRect.width(); bmpHeight = tileRect.height(); } AffineTransform tf = patternTransform * transform; FloatRect trRect = tf.mapRect(destRect); RECT clipBox; int clipType = GetClipBox(hdc, &clipBox); if (clipType == SIMPLEREGION) trRect.intersect(FloatRect(clipBox.left, clipBox.top, clipBox.right - clipBox.left, clipBox.bottom - clipBox.top)); else if (clipType == COMPLEXREGION) { OwnPtr<HRGN> clipRgn = adoptPtr(CreateRectRgn(0, 0, 0, 0)); if (GetClipRgn(hdc, clipRgn.get()) > 0) { DWORD regionDataSize = GetRegionData(clipRgn.get(), sizeof(RGNDATA), 0); if (regionDataSize) { Vector<RGNDATA> regionData(regionDataSize); GetRegionData(clipRgn.get(), regionDataSize, regionData.data()); RECT* rect = reinterpret_cast<RECT*>(regionData[0].Buffer); for (DWORD i = 0; i < regionData[0].rdh.nCount; ++i, ++rect) trRect.intersect(FloatRect(rect->left, rect->top, rect->right - rect->left, rect->bottom - rect->top)); } } } if (trRect.width() <= 0 || trRect.height() <= 0) return; trRect.inflate(1); IntRect visibleDstRect = enclosingIntRect(tf.inverse().mapRect(trRect)); visibleDstRect.intersect(IntRect(destRect)); if (visibleDstRect.width() <= 0 || visibleDstRect.height() <= 0) return; trRect = tf.mapRect(visibleDstRect); RECT dstRectWin = { stableRound(trRect.x()), stableRound(trRect.y()), stableRound(trRect.maxX()), stableRound(trRect.maxY()), }; if (dstRectWin.right <= dstRectWin.left || dstRectWin.bottom <= dstRectWin.top) return; SIZE bmpSize = { bmpWidth, bmpHeight }; // Relative to destination, in bitmap pixels POINT phaseWin = { stableRound(visibleDstRect.x() - phase.x()), stableRound(visibleDstRect.y() - phase.y()) }; phaseWin.x = normalizePhase(phaseWin.x, bmpSize.cx); phaseWin.y = normalizePhase(phaseWin.y, bmpSize.cy); RECT srcRectWin = { 0, 0, stableRound(visibleDstRect.maxX()) - stableRound(visibleDstRect.x()), stableRound(visibleDstRect.maxY()) - stableRound(visibleDstRect.y()) }; if (srcRectWin.right <= 0 || srcRectWin.bottom <= 0) return; BitmapInfo bmpInfo = BitmapInfo::createBottomUp(IntSize(srcRectWin.right, srcRectWin.bottom), useAlpha ? BitmapInfo::BitCount32 : BitmapInfo::BitCount16); void* pixels; OwnPtr<HBITMAP> hbmpTemp = adoptPtr(CreateDIBSection(0, &bmpInfo, DIB_RGB_COLORS, &pixels, 0, 0)); if (!hbmpTemp) return; OwnPtr<HDC> hmemdc = adoptPtr(CreateCompatibleDC(hdc)); HGDIOBJ oldBmp = SelectObject(hmemdc.get(), hbmpTemp.get()); if (clippedBmp) drawPatternSimple(hmemdc.get(), srcRectWin, clippedBmp.get(), phaseWin); else if ((op != CompositeSourceOver || canUseDIBits()) && srcRectWin.right <= bmpSize.cx * 2 && srcRectWin.bottom <= bmpSize.cy * 2) drawPatternSimple(hmemdc.get(), srcRectWin, this, bmpSize, phaseWin); else if (ensureHandle()) drawPatternSimple(hmemdc.get(), srcRectWin, getHandle(), phaseWin); else { void* pixels; BitmapInfo bmpInfo; OwnPtr<HBITMAP> hbmp = createHandle(&pixels, &bmpInfo, -1, false); if (hbmp) drawPatternSimple(hmemdc.get(), srcRectWin, hbmp.get(), phaseWin); else { SelectObject(hmemdc.get(), oldBmp); return; } } if (useAlpha && hasAlphaBlendSupport()) { static const BLENDFUNCTION blend = { AC_SRC_OVER, 0, 255, AC_SRC_ALPHA }; bool success = alphaBlendIfSupported(hdc, dstRectWin.left, dstRectWin.top, dstRectWin.right - dstRectWin.left, dstRectWin.bottom - dstRectWin.top, hmemdc.get(), 0, 0, srcRectWin.right, srcRectWin.bottom, blend); ASSERT_UNUSED(success, success); } else if (useAlpha && !hasAlphaBlendSupport() || op == CompositeSourceOver && usesTransparentColor()) { TransparentBlt(hdc, dstRectWin.left, dstRectWin.top, dstRectWin.right - dstRectWin.left, dstRectWin.bottom - dstRectWin.top, hmemdc.get(), 0, 0, srcRectWin.right, srcRectWin.bottom, transparentColor()); } else { DWORD bmpOp = op == CompositeCopy ? SRCCOPY : op == CompositeSourceOver ? SRCCOPY : op == CompositeXOR ? PATINVERT : op == CompositeClear ? WHITENESS : SRCCOPY; // FIXEME: other types? StretchDIBits(hdc, dstRectWin.left, dstRectWin.top, dstRectWin.right - dstRectWin.left, dstRectWin.bottom - dstRectWin.top, 0, 0, srcRectWin.right, srcRectWin.bottom, pixels, &bmpInfo, DIB_RGB_COLORS, bmpOp); } SelectObject(hmemdc.get(), oldBmp); }