TestRender.cpp 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. /*
  2. * Copyright (c) 2021, Leon Albrecht <leon2002.la@gmail.com>.
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibTest/TestCase.h>
  7. #include <AK/Debug.h>
  8. #include <AK/Format.h>
  9. #include <LibGL/GL/gl.h>
  10. #include <LibGL/GLContext.h>
  11. #include <LibGfx/BMPWriter.h>
  12. #include <LibGfx/Bitmap.h>
  13. #include <LibGfx/Font/FontDatabase.h>
  14. #include <fcntl.h>
  15. #include <unistd.h>
  16. #define RENDER_WIDTH 16
  17. #define RENDER_HEIGHT 16
  18. TEST_CASE(simple_triangle)
  19. {
  20. auto bitmap = MUST(Gfx::Bitmap::try_create(Gfx::BitmapFormat::BGRx8888, { RENDER_WIDTH, RENDER_HEIGHT }));
  21. auto context = GL::create_context(*bitmap);
  22. GL::make_context_current(context);
  23. glFrontFace(GL_CCW);
  24. glCullFace(GL_BACK);
  25. glEnable(GL_CULL_FACE);
  26. glEnable(GL_DEPTH_TEST);
  27. glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
  28. glClearDepth(1.0);
  29. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  30. glBegin(GL_TRIANGLES);
  31. glColor4f(1, 1, 1, 1);
  32. glVertex2f(0, 1);
  33. glVertex2f(-1, -1);
  34. glVertex2f(1, -1);
  35. glEnd();
  36. context->present();
  37. EXPECT_EQ(glGetError(), 0u);
  38. // FIXME: Verify that the image is indeed correct
  39. if constexpr (GL_DEBUG) {
  40. // output the image to manually verify that the output is correct
  41. Gfx::BMPWriter writer {};
  42. auto buffer = writer.dump(bitmap);
  43. int fd = open("./picture.bmp", O_CREAT | O_WRONLY, 0755);
  44. EXPECT(fd > 0);
  45. ssize_t nwritten = write(fd, buffer.data(), buffer.size());
  46. EXPECT_EQ((size_t)nwritten, buffer.size());
  47. close(fd);
  48. }
  49. }