summaryrefslogtreecommitdiff
path: root/Tests/LibGL/TestRender.cpp
blob: 0beabdaf3ac5cbcbc8a5a3a69bb65155d3800773 (plain)
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
/*
 * Copyright (c) 2021, Leon Albrecht <leon2002.la@gmail.com>.
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#include <LibTest/TestCase.h>

#include <AK/Debug.h>
#include <AK/Format.h>
#include <LibGL/GL/gl.h>
#include <LibGL/GLContext.h>
#include <LibGfx/BMPWriter.h>
#include <LibGfx/Bitmap.h>
#include <LibGfx/FontDatabase.h>
#include <fcntl.h>
#include <unistd.h>

#define RENDER_WIDTH 16
#define RENDER_HEIGHT 16

TEST_CASE(simple_triangle)
{
    auto bitmap = MUST(Gfx::Bitmap::try_create(Gfx::BitmapFormat::BGRx8888, { RENDER_WIDTH, RENDER_HEIGHT }));
    auto context = GL::create_context(*bitmap);

    GL::make_context_current(context);

    glFrontFace(GL_CCW);
    glCullFace(GL_BACK);
    glEnable(GL_CULL_FACE);
    glEnable(GL_DEPTH_TEST);

    glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
    glClearDepth(1.0);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glBegin(GL_TRIANGLES);
    glColor4f(1, 1, 1, 1);
    glVertex2f(0, 1);
    glVertex2f(-1, -1);
    glVertex2f(1, -1);
    glEnd();

    context->present();

    EXPECT_EQ(glGetError(), 0u);
    // FIXME: Verify that the image is indeed correct

    if constexpr (GL_DEBUG) {
        // output the image to manually verify that the output is correct
        Gfx::BMPWriter writer {};
        auto buffer = writer.dump(bitmap);
        int fd = open("./picture.bmp", O_CREAT | O_WRONLY, 0755);
        EXPECT(fd > 0);
        ssize_t nwritten = write(fd, buffer.data(), buffer.size());
        EXPECT_EQ((size_t)nwritten, buffer.size());
        close(fd);
    }
}