Skip to content

Commit

Permalink
#298: Use OpenGL instead of Cairo in luacmdinterface
Browse files Browse the repository at this point in the history
  • Loading branch information
feragon committed Jun 19, 2020
1 parent 0f69a1b commit 942a34a
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 67 deletions.
35 changes: 3 additions & 32 deletions luacmdinterface/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,13 @@ set (CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")

message("***** Lua command line interface *****")

# Cairo
find_package(Cairo REQUIRED)
include_directories(${CAIRO_INCLUDE_DIRS})

# Pango
find_package(Pango 1.36 REQUIRED)
include_directories(${PANGO_INCLUDE_DIRS})
link_directories(${PANGO_LIBRARY_DIRS})

# Curl
find_package(CURL REQUIRED)
include_directories(${CURL_INCLUDE_DIRS})
link_directories(${CURL_LIBRARY_DIRS})

find_package(glfw3 3.3 REQUIRED)

# Eigen 3
find_package(Eigen3 REQUIRED)
if( CMAKE_COMPILER_IS_GNUCXX)
Expand All @@ -32,24 +25,6 @@ find_package(Boost COMPONENTS program_options filesystem system log date_time th
FIND_PACKAGE ( Threads REQUIRED )
include_directories(${Boost_INCLUDE_DIRS})

# GDK-Pixbuf
find_package(GDK-Pixbuf REQUIRED)
include_directories(${GDK-PIXBUF_INCLUDE_DIRS})

#GDK
find_package(GDK REQUIRED)
include_directories(${GDK_INCLUDE_DIRS})

#GTK
find_package(GTK REQUIRED)
include_directories(${GTK_INCLUDE_DIRS})


#GLib
set(GLIB_FIND_COMPONENTS gobject)
find_package(GLib REQUIRED)
include_directories(${GLIB_INCLUDE_DIRS})

#Lua
find_package(Lua 5.2 REQUIRED)
include_directories(${LUA_INCLUDE_DIR})
Expand Down Expand Up @@ -83,14 +58,10 @@ set(hdrs
add_executable(luacmdinterface ${src} ${hdrs})
target_link_libraries(luacmdinterface
${CMAKE_THREAD_LIBS_INIT}
${CAIRO_LIBRARIES} ${PANGO_LIBRARIES}
${Boost_LIBRARIES}
${CURL_LIBRARIES}
${APR_LIBRARIES}
${GLIB_GOBJECT_LIBRARIES} ${GLIB_LIBRARIES}
${LUA_LIBRARIES}
${GDK-PIXBUF_LIBRARIES}
${GTK_LIBRARIES}
lcluascript lckernel lcluascript lcviewernoqt ${Boost_LIBRARIES}
lcluascript lckernel lcluascript lcviewernoqt ${Boost_LIBRARIES} glfw

)
99 changes: 64 additions & 35 deletions luacmdinterface/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@
#include <fstream>

#include <cad/storage/storagemanagerimpl.h>
#include <cad/operations/entitybuilder.h>
#include <documentcanvas.h>
#include <painters/lccairopainter.tcc>
#include <drawables/gradientbackground.h>
#include <cad/storage/undomanagerimpl.h>
#include <curl/curl.h>
#include <boost/program_options.hpp>
#include <boost/filesystem.hpp>
#include <managers/pluginmanager.h>
#include <managers/luacustomentitymanager.h>
#include <painters/createpainter.h>
#include <GL/glew.h>
#include <GLFW/glfw3.h>


namespace po = boost::program_options;
Expand All @@ -25,7 +25,7 @@ static const int DEFAULT_IMAGE_WIDTH = 400;
static const int DEFAULT_IMAGE_HEIGHT = 400;

static std::string* readBuffer;
static size_t WriteCallback(void* contents, size_t size, size_t nmemb, void* userp) {
static size_t WriteCallback(void* contents, size_t size, size_t nmemb, void*) {
size_t realsize = size * nmemb;
readBuffer->append((char*) contents, realsize);
return realsize;
Expand All @@ -39,7 +39,6 @@ std::string loadFile(const std::string& url) {

if (curl != nullptr) {
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
/* example.com is redirected, so we tell libcurl to follow redirection */
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);

Expand All @@ -61,16 +60,6 @@ std::string loadFile(const std::string& url) {
}
}

std::ofstream* ofile;
cairo_status_t write_func (void* closure, const unsigned char* data, unsigned int length) {

if (ofile->is_open()) {
ofile->write((const char*) data, length);
}

return CAIRO_STATUS_SUCCESS;
}

static FILE* openFileDialog(bool isOpening, const char* description, const char* mode) {
std::string path;

Expand Down Expand Up @@ -102,7 +91,7 @@ int main(int argc, char** argv) {
("height,h", po::value<int>(&height), "(optional) Set output image height, example -h 200")
("ifile,i", po::value<std::string>(&fIn), "(required) Set LUA input file name, example: -i file:myFile.lua")
("ofile,o", po::value<std::string>(&fOut), "(optional) Set output filename, example -o out.png")
("otype,t", po::value<std::string>(&fType), "(optional) output file type, example -t svg");
("otype,t", po::value<std::string>(&fType), "(optional) output file type, example -t tga");

po::variables_map vm;
po::store(po::parse_command_line(argc, argv, desc), vm);
Expand Down Expand Up @@ -130,7 +119,7 @@ int main(int argc, char** argv) {
// Create Librecad document
auto _storageManager = std::make_shared<lc::storage::StorageManagerImpl>();
auto _document = std::make_shared<lc::storage::DocumentImpl>(_storageManager);
auto _canvas = std::make_shared<DocumentCanvas>(_document);
auto _canvas = std::make_shared<lc::viewer::DocumentCanvas>(_document);

// Add background
auto _gradientBackground = std::make_shared<lc::viewer::drawable::GradientBackground>(lc::Color(0x90, 0x90, 0x90),
Expand All @@ -144,24 +133,52 @@ int main(int argc, char** argv) {
}

std::transform(fType.begin(), fType.end(), fType.begin(), ::tolower);
ofile = new std::ofstream;
ofile->open(fOut);

using namespace CairoPainter;
LcPainter* lcPainter;
if(!glfwInit()) {
LOG_ERROR << "Failed to initialize GLFW";
return -1;
}

LcPainter* lcPainter = nullptr;
if (fType == "pdf") {
lcPainter = new LcCairoPainter<backend::PDF>(width, height, &write_func);
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0);
glfwWindowHint(GLFW_OPENGL_DEBUG_CONTEXT, true);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);

GLFWwindow* window;
window = glfwCreateWindow(width, height, "LibreCAD", nullptr, nullptr);
if(window == nullptr) {
const char* description;
glfwGetError(&description);
LOG_ERROR << "Failed opening GLFW: " << description << std::endl;
glfwTerminate();
return -1;
}
else if (fType == "svg") {
lcPainter = new LcCairoPainter<backend::SVG>(width, height, &write_func);

glfwMakeContextCurrent(window);

glewExperimental = GL_TRUE;
GLenum err = glewInit();

if (err != GLEW_OK) {
LOG_ERROR << "GLEW Error: " << glewGetErrorString(err) << std::endl;
exit(1);
}
else {
lcPainter = new LcCairoPainter<backend::SVG>(width, height, nullptr);
if (!GLEW_VERSION_2_1) {
LOG_ERROR << "OpenGL version 2.1 is not available" << std::endl;
exit(1);
}

LOG_INFO << (char*) glGetString(GL_VERSION) << std::endl;

lcPainter = lc::viewer::createOpenGLPainter(nullptr, width, height);

lcPainter->create_resources();
_canvas->setPainter(lcPainter);

// Set device width/height
_canvas->newDeviceSize(width, height);
lcPainter->new_device_size(width, height);

// Render Lua Code
kaguya::State luaState;
Expand Down Expand Up @@ -191,19 +208,31 @@ int main(int argc, char** argv) {
}

_canvas->autoScale(*lcPainter);
_canvas->render(*lcPainter, VIEWER_BACKGROUND);
_canvas->render(*lcPainter, VIEWER_DOCUMENT);
_canvas->render(*lcPainter, VIEWER_FOREGROUND);
lcPainter->clear(0,0,0);

if (fType == "png" || (fType != "pdf" && fType != "svg")) {
dynamic_cast<LcCairoPainter<CairoPainter::backend::Image>*>(lcPainter)->writePNG(fOut);
}
ofile->close();
_canvas->render(*lcPainter, lc::viewer::VIEWER_BACKGROUND);
_canvas->render(*lcPainter, lc::viewer::VIEWER_DOCUMENT);
_canvas->render(*lcPainter, lc::viewer::VIEWER_FOREGROUND);

glfwSwapBuffers(window);

FILE* out = fopen(fOut.c_str(), "wb");
char* pixel_data = new char[3*width*height];
short TGAhead[] = { 0, 2, 0, 0, 0, 0, static_cast<short>(width), static_cast<short>(height), 24 };

glReadBuffer(GL_FRONT);
glReadPixels(0, 0, width, height, GL_BGR, GL_UNSIGNED_BYTE, pixel_data);

fwrite(&TGAhead,sizeof(TGAhead),1,out);
fwrite(pixel_data, 3*width*height, 1, out);
fclose(out);

delete[] pixel_data;

lc::lua::LuaCustomEntityManager::getInstance().removePlugins();
glfwDestroyWindow(window);

delete lcPainter;
delete ofile;
delete readBuffer;
return 0;
}

0 comments on commit 942a34a

Please sign in to comment.