Stilllegung des Forums
Das Forum wurde am 05.06.2023 nach über 20 Jahren stillgelegt (weitere Informationen und ein kleiner Rückblick).
Registrierungen, Anmeldungen und Postings sind nicht mehr möglich. Öffentliche Inhalte sind weiterhin zugänglich.
Das Team von spieleprogrammierer.de bedankt sich bei der Community für die vielen schönen Jahre.
Wenn du eine deutschsprachige Spieleentwickler-Community suchst, schau doch mal im Discord und auf ZFX vorbei!
Werbeanzeige
Administrator
C-/C++-Quelltext |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
sf::Color Renderer::colorAt(const sf::Vector3f& position, const sf::Vector3f& direction, unsigned int reflectionTimes) { float nearestDistance = 100.0f; RenderObject* nearestObject = nullptr; sf::Vector3f nearestPoint = position; sf::Vector2f nearestUVCoordinates = sf::Vector2f(0.0f, 0.0f); for(std::vector<RenderObject*>::iterator itRenderObjects(renderObjects.begin()), itEnd(renderObjects.end()); itRenderObjects!=itEnd; ++itRenderObjects) { sf::Vector3f curPoint; sf::Vector2f curUVCoordinates; int outside = (*itRenderObjects)->testRay(position, direction, curPoint, curUVCoordinates); if(outside>0) { float curDistance = length(curPoint - position); if(curDistance < nearestDistance) { nearestDistance = curDistance; nearestObject = *itRenderObjects; nearestPoint = curPoint; nearestUVCoordinates = curUVCoordinates; } } } sf::Color color = backgroundColor; if(nearestObject != nullptr) { sf::Vector3f normal = nearestObject->getNormal(nearestPoint); sf::Vector3f directionToLight = normalize(lightPos-nearestPoint); color = nearestObject->getMaterial().getColorByTexture(nearestUVCoordinates); bool lighten = true; for(std::vector<RenderObject*>::iterator itRenderObjects(renderObjects.begin()), itEnd(renderObjects.end()); itRenderObjects!=itEnd; ++itRenderObjects) { sf::Vector3f curPoint; sf::Vector2f curUVCoordinates; int outside = (*itRenderObjects)->testRay(nearestPoint + 0.00001f * normal , directionToLight, curPoint, curUVCoordinates); if(outside>0 && length(curPoint - nearestPoint) < length(lightPos - nearestPoint)) { color = sf::Color::Black; lighten = false; break; } } if(lighten) { float dotProduct = dot(normal, directionToLight); if(dotProduct>0) { color = sf::Color(static_cast<sf::Uint8>(color.r * dotProduct), static_cast<sf::Uint8>(color.g * dotProduct), static_cast<sf::Uint8>(color.b * dotProduct)); } else { color = sf::Color::Black; } float refl = nearestObject->getMaterial().reflection; if(refl>0) { if(reflectionTimes>0) { reflectionTimes--; sf::Vector3f reflected = direction - 2.0f * dot(direction, normal) * normal; sf::Color reflectedColor = colorAt(nearestPoint, reflected, reflectionTimes); color += sf::Color(static_cast<sf::Uint8>(refl * reflectedColor.r), static_cast<sf::Uint8>(refl * reflectedColor.g), static_cast<sf::Uint8>(refl * reflectedColor.b)); } } } } return color; } void Renderer::render() { sf::Image image; image.create(800, 600); for(unsigned int y = 0; y < window.getSize().y; ++y) { for(unsigned int x = 0; x < window.getSize().x; ++x) { sf::Vector3f curDirection = normalize(sf::Vector3f(x-window.getSize().x/2.0f, window.getSize().y/2.0f-y, 400.0f)); sf::Color pixelColor = colorAt(position, curDirection, 3); image.setPixel(x, y, pixelColor); } } renderResult.loadFromImage(image); window.draw(sf::Sprite(renderResult)); } |
Dieser Beitrag wurde bereits 3 mal editiert, zuletzt von »poorsider« (24.11.2013, 17:47)
Werbeanzeige