コード例 #1
0
ファイル: main.cpp プロジェクト: Zethes/CGUL
int main()
{
    try
    {
        Image image;
        image.Load("resources/jattabox.png");

        WindowStyle style;
        style.title = U8("CGUL - 3D World");
        style.size = UCoord32(640, 480);
        style.backgroundColor = Colors::black;
        style.resizable = true;

        Window window;
        window.Create(style);

        OpenGL::Context context;
        context.Create(&window);

        GL::BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
        GL::Enable(GL_BLEND);
        GL::Enable(GL_ALPHA_TEST);
        GL::Enable(GL_TEXTURE_2D);
        GL::Enable(GL_DEPTH_TEST);
        GL::Enable(GL_CULL_FACE);
        glCullFace(GL_BACK);

        UIntN program = LoadShader(U8("resources/shader.vert"), U8("resources/shader.frag"));

        UIntN box = MakeBox();

        UIntN texture;
        GL::GenTextures(1, &texture);
        GL::BindTexture(GL_TEXTURE_2D, texture);
        GL::TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
        GL::TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
        GL::TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
        GL::TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
        GL::TexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
        GL::PixelStorei(GL_UNPACK_ALIGNMENT, 1);
        GL::TexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, image.GetWidth(), image.GetHeight(), 0, GL_RGBA, GL_UNSIGNED_BYTE, image.GetData< void >());
        GL::BindTexture(GL_TEXTURE_2D, 0);

        Timer timer;
        Float32 hue = 0.0f;
        while (window.IsOpen())
        {
            Float32 deltaTime = timer.GetDeltaTime();
            Timer::Sleep(1);
            hue = Math::Mod(hue + deltaTime * 0.125f, 1.0f);
            context.ClearColor(Color::MakeHSV(hue, 0.156862745098039f, 1.0f));

            Float32 movement = 1 + Math::Cos(hue / 10.0f);
            static Float32 rot = 0;
            rot += deltaTime / 1.0f;

            context.Viewport(0, 0, window.GetWidth(), window.GetHeight());
            context.Clear(GL::COLOR_BUFFER_BIT | GL::DEPTH_BUFFER_BIT);
            GL::UseProgram(program);
            //GL::UniformMatrix4fv(GL::GetUniformLocation(program, "orthoMatrix"), 1, false, Matrix::MakeOrtho2D(0, 1280, 768, 0).GetData());
            MatrixF view = MatrixF::MakeLookAt(Vector3F(Math::Cos(rot) * 50, Math::Cos(rot) * 50, Math::Sin(rot) * 50), Vector3F(0, 0, 0), Vector3F(0, -1, 0));
            MatrixF projection = MatrixF::MakePerspective(45.0, window.GetWidth() / (Float32)window.GetHeight(), 1, 1000);
            MatrixF vp = view * projection;

            MatrixF model;
            model = MatrixF::MakeScaling(Vector3F(20, 20, 20)) * model;

            //Matrix wvp = model * Matrix::MakeOrtho2D(0, 1280, 768, 0);
            MatrixF wvp = model * vp;
            GL::UniformMatrix4fv(GL::GetUniformLocation(program, "matrix"), 1, false, wvp.GetData());
            GL::Uniform1i(GL::GetUniformLocation(program, "texture"), 0);
            GL::ActiveTexture(GL_TEXTURE0);
            GL::BindTexture(GL_TEXTURE_2D, texture);
            GL::BindVertexArray(box);
            GL::DrawArrays(GL_QUADS, 0, 24);
            GL::BindVertexArray(0);
            GL::UseProgram(0);
            context.SwapBuffers();

            Window::Update();
        }
    }
    catch (Exception& e)
    {
        std::cout << e.what() << std::endl;
        std::cout << std::hex << "Error code: 0x" << e.unique << std::dec << " (decimal " << e.unique << ")" << std::endl;
    }
}
コード例 #2
0
ファイル: main.cpp プロジェクト: JoshuaBrookover/Jatta
int main()
{
    try
    {
        ImageLoaders::PNG loader;
        if (!loader.CanLoad("resources/logo.png"))
        {
            throw FatalException("Cannot load PNG images.");
        }
        Image* image = loader.Load("resources/logo.png");

        WindowStyle style;
        style.title = U8("logo.png (") + image->GetWidth() + U8(", ") + image->GetHeight() + U8(")");
        style.width = image->GetWidth();
        style.height = image->GetHeight();
        style.backgroundColor = Colors::black;
        style.resizable = false;

        Window window;
        window.Create(style);

        OpenGL::Context context;
        context.Create(&window);

        GL::BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
        GL::Enable(GL_BLEND);
        GL::Enable(GL_ALPHA_TEST);
        GL::Enable(GL_TEXTURE_2D);

        UInt program = LoadShader(U8("resources/shader.vert"), U8("resources/shader.frag"));

        UInt box = MakeBox();

        UInt texture;
        GL::GenTextures(1, &texture);
        GL::BindTexture(GL_TEXTURE_2D, texture);
        GL::TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
        GL::TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
        GL::TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
        GL::TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
        GL::TexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
        GL::PixelStorei(GL_UNPACK_ALIGNMENT, 1);
        GL::TexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, image->GetWidth(), image->GetHeight(), 0, GL_RGBA, GL_UNSIGNED_BYTE, image->GetData());
        GL::BindTexture(GL_TEXTURE_2D, 0);

        Timer timer;
        Float32 hue = 0.0f;
        while (window.IsOpen())
        {
            Timer::Sleep(1);
            hue = Math::Mod(hue + timer.GetDeltaTime() * 45, 360.0f);
            context.ClearColor(Color::MakeHSL(hue, 40, 255));

            context.Viewport(0, 0, window.GetWidth(), window.GetHeight());
            context.Clear(GL::COLOR_BUFFER_BIT | GL::DEPTH_BUFFER_BIT);
            GL::UseProgram(program);
            GL::UniformMatrix4fv(GL::GetUniformLocation(program, "orthoMatrix"), 1, false, Matrix::MakeOrtho(0, 1, 1, 0).GetData());
            GL::UniformMatrix4fv(GL::GetUniformLocation(program, "modelMatrix"), 1, false, Matrix::Identity().GetData());
            GL::Uniform1i(GL::GetUniformLocation(program, "texture"), 0);
            GL::ActiveTexture(GL_TEXTURE0);
            GL::BindTexture(GL_TEXTURE_2D, texture);
            GL::BindVertexArray(box);
            GL::DrawArrays(GL_QUADS, 0, 4);
            GL::BindVertexArray(0);
            GL::UseProgram(0);
            context.SwapBuffers();

            Window::Update();
        }
    }
    catch (Exception& e)
    {
        std::cout << e.what() << std::endl;
        std::cout << std::hex << "Error code: 0x" << e.unique << std::dec << " (decimal " << e.unique << ")" << std::endl;
    }
}
コード例 #3
0
ファイル: main.cpp プロジェクト: JoshuaBrookover/Jatta
int main()
{
    Jatta::Matrix wee;
    wee[1][2] = 5;
    std::cout << wee << std::endl;
    try
    {
        // Setup the window
        WindowStyle style;
        style.title = "A Quick Brown Fox Jumps Over The Lazy Dog";
        style.width = 800;
        style.height = 350;
        style.backgroundColor = Colors::black;
        style.resizable = false;
        Window window;
        window.Create(style);

        // Create the OpenGL context
        OpenGL::Context context;
        context.Create(&window);

        OpenGL::Program program = LoadShader("Resources/screen.vert", "Resources/screen.frag");

        Font font;
        if (font.Load("Resources/Arial.ttf") == -1)
        { throw std::runtime_error("Failed to load 'Resources/Arial.ttf'."); }
        if (font.Load("Resources/epgyosho.ttf") == -1)
        { throw std::runtime_error("Failed to load 'Resources/epgyosho.ttf'."); }
        if (font.Load("Resources/LastResort.ttf") == -1)
        { throw std::runtime_error("Failed to load 'Resources/LastResort.ttf'."); }
        font.SetStyle(FontStyles::OBLIQUE | FontStyles::UNDERLINED);

        Image image[22];
        OpenGL::Texture texture[22];
        OpenGL::VertexArray box[22];

        for (unsigned int i = 7; i < 27; ++i)
        {
            font.SetSize(i);
            image[i-7] = font.GenerateText(Jatta::Colors::white,"A Quick Brown Fox Jumps Over The Lazy Dog 0123456789");

            texture[i-7].Create(OpenGL::GL::TEXTURE_2D);
            texture[i-7].Bind();
            texture[i-7].SetTextureWrapS(OpenGL::GL::REPEAT);
            texture[i-7].SetTextureWrapT(OpenGL::GL::REPEAT);
            texture[i-7].SetMinFilter(OpenGL::GL::LINEAR);
            texture[i-7].SetMagFilter(OpenGL::GL::LINEAR);
            texture[i-7].Image2D(0, OpenGL::GL::RGBA, image[i-7].GetWidth(), image[i-7].GetHeight(), 0, OpenGL::GL::RGBA, OpenGL::GL::UNSIGNED_BYTE, image[i-7].GetData());
            texture[i-7].Unbind();

            box[i-7] = MakeBox(Float2(image[i-7].GetWidth(), image[i-7].GetHeight()));
        }

        font.SetSize(16);
        font.SetStyle(FontStyles::OVERLINED | FontStyles::UNDERLINED);
        image[21] = font.GenerateText(Jatta::Colors::white,"こんにちわ");

        texture[21].Create(OpenGL::GL::TEXTURE_2D);
        texture[21].Bind();
        texture[21].SetTextureWrapS(OpenGL::GL::REPEAT);
        texture[21].SetTextureWrapT(OpenGL::GL::REPEAT);
        texture[21].SetMinFilter(OpenGL::GL::LINEAR);
        texture[21].SetMagFilter(OpenGL::GL::LINEAR);
        texture[21].Image2D(0, OpenGL::GL::RGBA, image[21].GetWidth(), image[21].GetHeight(), 0, OpenGL::GL::RGBA, OpenGL::GL::UNSIGNED_BYTE, image[21].GetData());
        texture[21].Unbind();

        box[21] = MakeBox(Float2(image[21].GetWidth(), image[21].GetHeight()));

        while (window.IsOpen())
        {
            Window::Update();

            context.Viewport(0, 0, window.GetWidth(), window.GetHeight());
            context.Clear(OpenGL::GL::COLOR_BUFFER_BIT | OpenGL::GL::DEPTH_BUFFER_BIT);

            UInt32 y = 0;

            program.Bind();
            OpenGL::Program::UniformMatrix4f(program.GetUniformLocation("orthoMatrix"), false, Matrix::MakeOrtho(0, window.GetWidth(), window.GetHeight(), 0));
            for (unsigned int i = 7; i < 27; ++i)
            {
                OpenGL::Program::UniformMatrix4f(program.GetUniformLocation("modelMatrix"), false, Matrix::MakeTranslation(Float2(0,y)));
                OpenGL::Program::Uniform1i(program.GetUniformLocation("texture"), texture[i-7].GetID());
                OpenGL::ClearErrors();
                OpenGL::Texture::Active(texture[i-7].GetID());
                texture[i-7].Bind();
                box[i-7].Bind();
                box[i-7].DrawArrays(OpenGL::GL::QUADS, 0, 4);
                box[i-7].Unbind();
                y += image[i-7].GetHeight();
            }

            OpenGL::Program::UniformMatrix4f(program.GetUniformLocation("modelMatrix"), false, Matrix::MakeTranslation(Float2(800-image[21].GetWidth(),0)));
            OpenGL::Program::Uniform1i(program.GetUniformLocation("texture"), texture[21].GetID());
            OpenGL::ClearErrors();
            OpenGL::Texture::Active(texture[21].GetID());
            texture[21].Bind();
            box[21].Bind();
            box[21].DrawArrays(OpenGL::GL::QUADS, 0, 4);
            box[21].Unbind();

            program.Unbind();

            context.SwapBuffers();
        }
    }
    catch (std::exception& e)
    {
        std::cout << e.what() << std::endl;
    }
}