Primitive::Ptr Renderer::CreateTriangle( const sf::Vector2f& point0, const sf::Vector2f& point1, const sf::Vector2f& point2, const sf::Color& color ) { Primitive::Ptr primitive( new Primitive ); Primitive::Vertex vertex0; Primitive::Vertex vertex1; Primitive::Vertex vertex2; vertex0.position = point0; vertex1.position = point1; vertex2.position = point2; vertex0.color = color; vertex1.color = color; vertex2.color = color; vertex0.texture_coordinate = sf::Vector2f( 0.f, 0.f ); vertex1.texture_coordinate = sf::Vector2f( 0.f, 1.f ); vertex2.texture_coordinate = sf::Vector2f( 1.f, 0.f ); primitive->AddVertex( vertex0 ); primitive->AddVertex( vertex1 ); primitive->AddVertex( vertex2 ); AddPrimitive( primitive ); return primitive; }
// ------------------------------------------------------------------------ S4::Interpreter::Interpreter(const char *program) { static const char *coreWords = "42 ."; dictionary = new Dictionary(); program = program ? program : ""; sizeOfCore = NextPowerOfTwo(std::strlen(coreWords) + 1 + std::strlen(program)); core = new char[sizeOfCore]; endOfCore = core + sizeOfCore; *(endOfCore - 1) = 0; // load primitives AddPrimitive(new S4::Primitive::Add()); AddPrimitive(new S4::Primitive::CloseBrace()); AddPrimitive(new S4::Primitive::Dot()); AddPrimitive(new S4::Primitive::Dup()); AddPrimitive(new S4::Primitive::OpenBrace()); AddPrimitive(new S4::Primitive::StackHeight()); // put core words at the beginning of memory and the program after it core = StrCat(coreWords, " ", program); printf(".core:\t%s\n", core); // and initialize the program counter programCounter = core; }
void D_PAD::AddPrimitive( const SHAPE_POLY_SET& aPoly, int aThickness ) { std::vector<wxPoint> points; // If aPoly has holes, convert it to a polygon with no holes. SHAPE_POLY_SET poly_no_hole; poly_no_hole.Append( aPoly ); poly_no_hole.Fracture( SHAPE_POLY_SET::PM_STRICTLY_SIMPLE ); for( auto iter = poly_no_hole.CIterate(); iter; iter++ ) points.push_back( wxPoint( iter->x, iter->y ) ); AddPrimitive( points, aThickness ); }
void GetPrimitiveSpec( FILE *infile, ListNode *primitives ) { char nextchar; Primitives *newprim; fscanf( infile, "%s ", buffer ); newprim = AddPrimitive( buffer, primitives); if(( nextchar = skiplayout(infile)) == '=' ) { GetPrimitiveEntries( infile, newprim); PrimitiveDefined( newprim) = TRUE; } else { ungetc(nextchar,infile); } }
void Scene::CreateScene( std::string file ) { if ( file == "" ) return; srand( 1995 - 05 - 12 ); std::ifstream fin( file.c_str() ); std::string obj; while ( fin >> obj ) { Primitive* new_primitive = NULL; Light* new_light = NULL; if ( obj == "primitive" ) { std::string type; fin >> type; if ( type == "sphere" ) new_primitive = new Sphere; if ( type == "plane" ) new_primitive = new Plane; if (type == "Bezier") new_primitive = new Bezier; if (type == "NomalBall") new_primitive = new NomalBall; if (type == "NomalPlane")new_primitive = new NomalPlane; AddPrimitive( new_primitive ); } else if ( obj == "light" ) {
Primitive::Ptr Renderer::CreateImage( const sf::FloatRect& rect, const sf::Image& image, sf::Color background_color_hint ) { sf::Vector2f offset( LoadImage( image, background_color_hint ) ); Primitive::Ptr primitive( new Primitive ); Primitive::Vertex vertex0; Primitive::Vertex vertex1; Primitive::Vertex vertex2; Primitive::Vertex vertex3; vertex0.position = sf::Vector2f( std::floor( rect.left + .5f ), std::floor( rect.top + .5f ) ); vertex1.position = sf::Vector2f( std::floor( rect.left + .5f ), std::floor( rect.top + .5f ) + std::floor( rect.height + .5f ) ); vertex2.position = sf::Vector2f( std::floor( rect.left + .5f ) + std::floor( rect.width + .5f ), std::floor( rect.top + .5f ) ); vertex3.position = sf::Vector2f( std::floor( rect.left + .5f ) + std::floor( rect.width + .5f ), std::floor( rect.top + .5f ) + std::floor( rect.height + .5f ) ); vertex0.color = sf::Color( 255, 255, 255, 255 ); vertex1.color = sf::Color( 255, 255, 255, 255 ); vertex2.color = sf::Color( 255, 255, 255, 255 ); vertex3.color = sf::Color( 255, 255, 255, 255 ); vertex0.texture_coordinate = offset + sf::Vector2f( 0.f, 0.f ); vertex1.texture_coordinate = offset + sf::Vector2f( 0.f, static_cast<float>( image.getHeight() ) ); vertex2.texture_coordinate = offset + sf::Vector2f( static_cast<float>( image.getWidth() ), 0.f ); vertex3.texture_coordinate = offset + sf::Vector2f( static_cast<float>( image.getWidth() ), static_cast<float>( image.getHeight() ) ); primitive->AddVertex( vertex0 ); primitive->AddVertex( vertex1 ); primitive->AddVertex( vertex2 ); primitive->AddVertex( vertex2 ); primitive->AddVertex( vertex1 ); primitive->AddVertex( vertex3 ); AddPrimitive( primitive ); return primitive; }
Primitive::Ptr Renderer::CreateQuad( const sf::Vector2f& top_left, const sf::Vector2f& bottom_left, const sf::Vector2f& bottom_right, const sf::Vector2f& top_right, const sf::Color& color ) { Primitive::Ptr primitive( new Primitive ); Primitive::Vertex vertex0; Primitive::Vertex vertex1; Primitive::Vertex vertex2; Primitive::Vertex vertex3; vertex0.position = sf::Vector2f( std::floor( top_left.x + .5f ), std::floor( top_left.y + .5f ) ); vertex1.position = sf::Vector2f( std::floor( bottom_left.x + .5f ), std::floor( bottom_left.y + .5f ) ); vertex2.position = sf::Vector2f( std::floor( top_right.x + .5f ), std::floor( top_right.y + .5f ) ); vertex3.position = sf::Vector2f( std::floor( bottom_right.x + .5f ), std::floor( bottom_right.y + .5f ) ); vertex0.color = color; vertex1.color = color; vertex2.color = color; vertex3.color = color; vertex0.texture_coordinate = sf::Vector2f( 0.f, 0.f ); vertex1.texture_coordinate = sf::Vector2f( 0.f, 1.f ); vertex2.texture_coordinate = sf::Vector2f( 1.f, 0.f ); vertex3.texture_coordinate = sf::Vector2f( 1.f, 1.f ); primitive->AddVertex( vertex0 ); primitive->AddVertex( vertex1 ); primitive->AddVertex( vertex2 ); primitive->AddVertex( vertex2 ); primitive->AddVertex( vertex1 ); primitive->AddVertex( vertex3 ); AddPrimitive( primitive ); return primitive; }
HINATA_NAMESPACE_BEGIN CornellBoxScene::CornellBoxScene( double aspect ) { // Fixed scene definition (Cornell Box) // Right primitives.push_back( std::make_shared<Primitive>( Mat4d::Identity(), std::make_shared<Sphere>(1e5, Vec3d(1e5+1, 0, 0)), std::make_shared<DiffuseBSDF>(Vec3d(0.75, 0.25, 0.25)))); // Left primitives.push_back( std::make_shared<Primitive>( Mat4d::Identity(), std::make_shared<Sphere>(1e5, Vec3d(-1e5-1, 0, 0)), std::make_shared<DiffuseBSDF>(Vec3d(0.25, 0.25, 0.75)))); // Top primitives.push_back( std::make_shared<Primitive>( Mat4d::Identity(), std::make_shared<Sphere>(1e5, Vec3d(0, 1e5+1, 0)), std::make_shared<DiffuseBSDF>(Vec3d(0.75)))); // Bottom primitives.push_back( std::make_shared<Primitive>( Mat4d::Identity(), std::make_shared<Sphere>(1e5, Vec3d(0, -1e5-1, 0)), std::make_shared<DiffuseBSDF>(Vec3d(0.75)))); // Back primitives.push_back( std::make_shared<Primitive>( Mat4d::Identity(), std::make_shared<Sphere>(1e5, Vec3d(0, 0, 1e5+7)), std::make_shared<DiffuseBSDF>(Vec3d()))); // Front primitives.push_back( std::make_shared<Primitive>( Mat4d::Identity(), std::make_shared<Sphere>(1e5, Vec3d(0, 0, -1e5-1)), std::make_shared<DiffuseBSDF>(Vec3d(0.75)))); //std::make_shared<GlossyConductorBSDF>(Vec3d(1), Vec3d(0.1), Vec3d(1.67), 0.08))); // Light auto light = std::make_shared<AreaLight>(Vec3d(30)); //auto lp = // std::make_shared<Primitive>( // Mat4d(1.0), // std::make_shared<Sphere>(0.15, Vec3d(0.5, 0.7, 0)), // //std::make_shared<Sphere>(0.15, Vec3d(0, 0.85, 0)), // std::make_shared<DiffuseBSDF>(Vec3d(0.75)), // light); auto mesh = std::make_shared<TriangleMesh>(); mesh->positions.push_back(Vec3d( 0.2, 0.9, 0.2)); mesh->positions.push_back(Vec3d(-0.2, 0.9, 0.2)); mesh->positions.push_back(Vec3d(-0.2, 0.9, -0.2)); mesh->positions.push_back(Vec3d( 0.2, 0.9, -0.2)); mesh->normals.push_back(Vec3d(0, -1, 0)); mesh->normals.push_back(Vec3d(0, -1, 0)); mesh->normals.push_back(Vec3d(0, -1, 0)); mesh->normals.push_back(Vec3d(0, -1, 0)); auto lp1 = std::make_shared<Primitive>( Math::Rotate(30.0, Vec3d(0, 0, 1)), std::make_shared<Triangle>(mesh, 0, 1, 3), std::make_shared<DiffuseBSDF>(Vec3d(0.75)), //std::make_shared<DiffuseBSDF>(Vec3d()), light); primitives.push_back(lp1); light->AddPrimitive(lp1); auto lp2 = std::make_shared<Primitive>( Math::Rotate(30.0, Vec3d(0, 0, 1)), std::make_shared<Triangle>(mesh, 1, 2, 3), std::make_shared<DiffuseBSDF>(Vec3d(0.75)), //std::make_shared<DiffuseBSDF>(Vec3d()), light); primitives.push_back(lp2); light->AddPrimitive(lp2); light->Initialize(); lights.push_back(light); // Ball 1 primitives.push_back( std::make_shared<Primitive>( Mat4d(1.0), std::make_shared<Sphere>(0.35, Vec3d(0.45, -0.65, 0.5)), //std::make_shared<DiffuseBSDF>(Vec3d(0.75)))); std::make_shared<DielecticBSDF>(Vec3d(1), Vec3d(1), 1, 1.544))); // Ball 2 primitives.push_back( std::make_shared<Primitive>( Mat4d(1.0), std::make_shared<Sphere>(0.35, Vec3d(-0.45, -0.65, -0.3)), std::make_shared<DiffuseBSDF>(Vec3d(0.75)))); //std::make_shared<GlossyConductorBSDF>(Vec3d(1), Vec3d(0.1), Vec3d(1.67), 0.05))); // Camera camera = std::make_shared<PerspectiveCamera>( Math::LookAt(Vec3d(0, 0, 6.99), Vec3d(0, 0, 0), Vec3d(0, 1, 0)), Math::Perspective(20.0, aspect, 0.1, 1000.0)); }
Primitive::Ptr Renderer::CreateText( const sf::Text& text, sf::Color background_color_hint ) { Primitive::Ptr primitive( new Primitive ); const sf::Font& font = text.getFont(); unsigned int character_size = text.getCharacterSize(); sf::Color color = text.getColor(); if( m_preblend ) { color = sf::Color::White; } sf::Vector2f atlas_offset = LoadFont( font, character_size, background_color_hint, text.getColor() ); const sf::String& str = text.getString(); std::size_t length = str.getSize(); float horizontal_spacing = static_cast<float>( font.getGlyph( L' ', character_size, false ).advance ); float vertical_spacing = static_cast<float>( font.getLineSpacing( character_size ) ); sf::Vector2f position( std::floor( text.getPosition().x + .5f ), std::floor( text.getPosition().y + static_cast<float>( character_size ) + .5f ) ); const static float tab_spaces = 2.f; sf::Uint32 previous_character = 0; for( std::size_t index = 0; index < length; ++index ) { sf::Uint32 current_character = str[index]; position.x += static_cast<float>( font.getKerning( previous_character, current_character, character_size ) ); switch( current_character ) { case L' ': position.x += horizontal_spacing; continue; case L'\t': position.x += horizontal_spacing * tab_spaces; continue; case L'\n': position.y += vertical_spacing; position.x = 0.f; continue; case L'\v': position.y += vertical_spacing * tab_spaces; continue; default: break; } const sf::Glyph& glyph = font.getGlyph( current_character, character_size, false ); Primitive::Vertex vertex0; Primitive::Vertex vertex1; Primitive::Vertex vertex2; Primitive::Vertex vertex3; vertex0.position = position + sf::Vector2f( static_cast<float>( glyph.bounds.left ), static_cast<float>( glyph.bounds.top ) ); vertex1.position = position + sf::Vector2f( static_cast<float>( glyph.bounds.left ), static_cast<float>( glyph.bounds.top + glyph.bounds.height ) ); vertex2.position = position + sf::Vector2f( static_cast<float>( glyph.bounds.left + glyph.bounds.width ), static_cast<float>( glyph.bounds.top ) ); vertex3.position = position + sf::Vector2f( static_cast<float>( glyph.bounds.left + glyph.bounds.width ), static_cast<float>( glyph.bounds.top + glyph.bounds.height ) ); vertex0.color = color; vertex1.color = color; vertex2.color = color; vertex3.color = color; // Let SFML cast the Rect for us. sf::FloatRect texture_rect( glyph.textureRect ); vertex0.texture_coordinate = atlas_offset + sf::Vector2f( texture_rect.left, texture_rect.top ); vertex1.texture_coordinate = atlas_offset + sf::Vector2f( texture_rect.left, texture_rect.top + texture_rect.height ); vertex2.texture_coordinate = atlas_offset + sf::Vector2f( texture_rect.left + texture_rect.width, texture_rect.top ); vertex3.texture_coordinate = atlas_offset + sf::Vector2f( texture_rect.left + texture_rect.width, texture_rect.top + texture_rect.height ); primitive->AddVertex( vertex0 ); primitive->AddVertex( vertex1 ); primitive->AddVertex( vertex2 ); primitive->AddVertex( vertex2 ); primitive->AddVertex( vertex1 ); primitive->AddVertex( vertex3 ); position.x += static_cast<float>( glyph.advance ); previous_character = current_character; } AddPrimitive( primitive ); return primitive; }
Primitive::Ptr Renderer::CreatePane( const sf::Vector2f& position, const sf::Vector2f& size, float border_width, const sf::Color& color, const sf::Color& border_color, int border_color_shift ) { if( border_width <= 0.f ) { return CreateRect( position, position + size, color ); } Primitive::Ptr primitive( new Primitive ); sf::Color dark_border( border_color ); sf::Color light_border( border_color ); Context::Get().GetEngine().ShiftBorderColors( light_border, dark_border, border_color_shift ); float left = position.x; float top = position.y; float right = left + size.x; float bottom = top + size.y; Primitive::Ptr rect( CreateQuad( sf::Vector2f( left + border_width, top + border_width ), sf::Vector2f( left + border_width, bottom - border_width ), sf::Vector2f( right - border_width, bottom - border_width ), sf::Vector2f( right - border_width, top + border_width ), color ) ); Primitive::Ptr line_top( CreateLine( sf::Vector2f( left, top + border_width / 2.f ), sf::Vector2f( right - border_width, top + border_width / 2.f ), light_border, border_width ) ); Primitive::Ptr line_right( CreateLine( sf::Vector2f( right - border_width / 2.f, top ), sf::Vector2f( right - border_width / 2.f, bottom - border_width ), dark_border, border_width ) ); Primitive::Ptr line_bottom( CreateLine( sf::Vector2f( right, bottom - border_width / 2.f ), sf::Vector2f( left + border_width, bottom - border_width / 2.f ), dark_border, border_width ) ); Primitive::Ptr line_left( CreateLine( sf::Vector2f( left + border_width / 2.f, bottom ), sf::Vector2f( left + border_width / 2.f, top + border_width ), light_border, border_width ) ); primitive->Add( *rect.get() ); primitive->Add( *line_top.get() ); primitive->Add( *line_right.get() ); primitive->Add( *line_bottom.get() ); primitive->Add( *line_left.get() ); std::vector<Primitive::Ptr>::iterator iter( std::find( m_primitives.begin(), m_primitives.end(), rect ) ); assert( iter != m_primitives.end() ); iter = m_primitives.erase( iter ); // rect iter = m_primitives.erase( iter ); // line_top iter = m_primitives.erase( iter ); // line_right iter = m_primitives.erase( iter ); // line_bottom m_primitives.erase( iter ); // line_left AddPrimitive( primitive ); return primitive; }
Interpreter::Interpreter(IInterpreterHost & host) : mHost(host) { // Build the global scope. // Object. mObject = MakeGlobal("Object"); AddPrimitive(mObject, "===", ObjectSame); AddPrimitive(mObject, "to-string", ObjectToString); AddPrimitive(mObject, "parent", ObjectGetParent); // Arrays. mArrayPrototype = MakeGlobal("Arrays"); AddPrimitive(mArrayPrototype, "count", ArrayCount); AddPrimitive(mArrayPrototype, "add:", ArrayAdd); AddPrimitive(mArrayPrototype, "at:", ArrayAt); AddPrimitive(mArrayPrototype, "at:put:", ArrayAtPut); AddPrimitive(mArrayPrototype, "remove-at:", ArrayRemoveAt); // Blocks. mBlockPrototype = MakeGlobal("Blocks"); AddPrimitive(mBlockPrototype, "call", BlockCall); AddPrimitive(mBlockPrototype, "call:", BlockCall); AddPrimitive(mBlockPrototype, "call::", BlockCall); AddPrimitive(mBlockPrototype, "call:::", BlockCall); AddPrimitive(mBlockPrototype, "call::::", BlockCall); AddPrimitive(mBlockPrototype, "call:::::", BlockCall); AddPrimitive(mBlockPrototype, "call::::::", BlockCall); AddPrimitive(mBlockPrototype, "call:::::::", BlockCall); AddPrimitive(mBlockPrototype, "call::::::::", BlockCall); AddPrimitive(mBlockPrototype, "call:::::::::", BlockCall); AddPrimitive(mBlockPrototype, "call::::::::::", BlockCall); // Fibers. mFiberPrototype = MakeGlobal("Fibers"); AddPrimitive(mFiberPrototype, "running?", FiberRunning); AddPrimitive(mFiberPrototype, "done?", FiberDone); // Numbers. mNumberPrototype = MakeGlobal("Numbers"); AddPrimitive(mNumberPrototype, "abs", NumberAbs); AddPrimitive(mNumberPrototype, "neg", NumberNeg); AddPrimitive(mNumberPrototype, "mod:", NumberMod); AddPrimitive(mNumberPrototype, "floor", NumberFloor); AddPrimitive(mNumberPrototype, "ceiling", NumberCeiling); AddPrimitive(mNumberPrototype, "sqrt", NumberSqrt); AddPrimitive(mNumberPrototype, "sin", NumberSin); AddPrimitive(mNumberPrototype, "cos", NumberCos); AddPrimitive(mNumberPrototype, "tan", NumberTan); AddPrimitive(mNumberPrototype, "asin", NumberAsin); AddPrimitive(mNumberPrototype, "acos", NumberAcos); AddPrimitive(mNumberPrototype, "atan", NumberAtan); AddPrimitive(mNumberPrototype, "atan:", NumberAtan2); AddPrimitive(mNumberPrototype, "+number:", NumberAdd); AddPrimitive(mNumberPrototype, "-number:", NumberSubtract); AddPrimitive(mNumberPrototype, "*number:", NumberMultiply); AddPrimitive(mNumberPrototype, "/number:", NumberDivide); AddPrimitive(mNumberPrototype, "=number:", NumberEquals); AddPrimitive(mNumberPrototype, "!=", NumberNotEquals); AddPrimitive(mNumberPrototype, "<", NumberLessThan); AddPrimitive(mNumberPrototype, ">", NumberGreaterThan); AddPrimitive(mNumberPrototype, "<=", NumberLessThanOrEqual); AddPrimitive(mNumberPrototype, ">=", NumberGreaterThanOrEqual); // Strings. mStringPrototype = MakeGlobal("Strings"); AddPrimitive(mStringPrototype, "count", StringCount); AddPrimitive(mStringPrototype, "at:", StringAt); AddPrimitive(mStringPrototype, "from:count:", StringFromCount); AddPrimitive(mStringPrototype, "hash-code", StringHashCode); AddPrimitive(mStringPrototype, "index-of:", StringIndexOf); // Ether. MakeGlobal("Ether"); // Io. Value io = MakeGlobal("Io"); AddPrimitive(io, "read-file:", IoReadFile); // Bare primitive object. Value primitives = MakeGlobal("*primitive*"); AddPrimitive(primitives, "string-concat:and:", PrimitiveStringConcat); AddPrimitive(primitives, "string-compare:to:", PrimitiveStringCompare); AddPrimitive(primitives, "write:", PrimitiveWrite); /* AddPrimitive(primitives, "new-fiber:", PrimitiveNewFiber); AddPrimitive(primitives, "current-fiber", PrimitiveGetCurrentFiber); AddPrimitive(primitives, "switch-to-fiber:passing:", PrimitiveSwitchToFiber); */ AddPrimitive(primitives, "callstack-depth", PrimitiveGetCallstackDepth); // The special singleton values. mNil = MakeGlobal("nil"); mTrue = MakeGlobal("true"); mFalse = MakeGlobal("false"); }
void cldb::Database::AddBaseTypePrimitives() { // Create a selection of basic C++ types // TODO: Figure the size of these out based on platform Name parent; AddPrimitive(Type(GetName("void"), parent, 0)); AddPrimitive(Type(GetName("bool"), parent, sizeof(bool))); AddPrimitive(Type(GetName("char"), parent, sizeof(char))); AddPrimitive(Type(GetName("unsigned char"), parent, sizeof(unsigned char))); AddPrimitive(Type(GetName("wchar_t"), parent, sizeof(wchar_t))); AddPrimitive(Type(GetName("short"), parent, sizeof(short))); AddPrimitive(Type(GetName("unsigned short"), parent, sizeof(unsigned short))); AddPrimitive(Type(GetName("int"), parent, sizeof(int))); AddPrimitive(Type(GetName("unsigned int"), parent, sizeof(unsigned int))); AddPrimitive(Type(GetName("long"), parent, sizeof(long))); AddPrimitive(Type(GetName("unsigned long"), parent, sizeof(unsigned long))); AddPrimitive(Type(GetName("float"), parent, sizeof(float))); AddPrimitive(Type(GetName("double"), parent, sizeof(double))); // 64-bit types as clang sees them AddPrimitive(Type(GetName("long long"), parent, sizeof(clcpp::int64))); AddPrimitive(Type(GetName("unsigned long long"), parent, sizeof(clcpp::uint64))); }