LoginSignup
4
4

More than 5 years have passed since last update.

DebianにGLFW3とDerelict3をインストール

Last updated at Posted at 2014-04-14

OpenGLを勉強する必要に迫られたが、せっかくなのでこちらも勉強中のD言語でやってみることにした。
DebianだとDerelict3もGLFW3もパッケージがまだ無いため自前で入れる。

追記(4/20):libglfw3入ったようです。
https://packages.debian.org/unstable/main/libglfw3

GLFW3 をインストール

CMakeLists.txtのBUILD_SHARED_LIBS をONにしておく。
BUILD_SHARED_LIBSがOFFのままだと静的ライブラリしか作られないが、Derelictはlibglfw3.soを動的にリンクしようとするので実行時にコケる。

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 743e72d..1c4498c 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -10,7 +10,7 @@ set(GLFW_VERSION "${GLFW_VERSION_MAJOR}.${GLFW_VERSION_MINOR}")
 set(GLFW_VERSION_FULL "${GLFW_VERSION}.${GLFW_VERSION_PATCH}${GLFW_VERSION_EXTRA}")
 set(LIB_SUFFIX "" CACHE STRING "Takes an empty string or 64. Directory where lib will be installed: lib or lib64")

-option(BUILD_SHARED_LIBS "Build shared libraries" OFF)
+option(BUILD_SHARED_LIBS "Build shared libraries" ON)
 option(GLFW_BUILD_EXAMPLES "Build the GLFW example programs" ON)
 option(GLFW_BUILD_TESTS "Build the GLFW test programs" ON)
 option(GLFW_BUILD_DOCS "Build the GLFW documentation" ON)

ビルド

$ git clone https://github.com/glfw/glfw.git
$ cd glfw
$ cmake .
$ make
$ sudo make install

cmakeのときにRandRが無いって怒られたので
$ sudo aptitude install libxrandr-dev
した。

Derelict3 をビルド

Debianにはdmdパッケージはないのでldc(ldmd2)でビルドする。

$ git clone https://github.com/aldacron/Derelict3.git
$ cd Derelict3/build
$ ldmd2 build.d
$ ./build Util GL3 GLFW3

パッケージは
Derelict3/import
ライブラリは
Derelict3/lib/ldc
にそれぞれ作成される。

https://github.com/aldacron/Derelict3
READMEには特に特定のディレクトリへのインストール方法などは記載されていなかったので、今回は上のディレクトリをMakefileに直接指定することにする。

テストコードを書いてみる

http://www.glfw.org/docs/3.0/quick.html
ここを参考に。

エラー処理等は今回は省略

openwindow.d
import derelict.opengl3.gl;
import derelict.opengl3.gl3;
import derelict.glfw3.glfw3;

import std.stdio;

void main() {
  DerelictGL.load();
  DerelictGLFW3.load();

  glfwInit();
  auto window = glfwCreateWindow(800, 600, "Hello, GLFW3!", null, null);
  glfwMakeContextCurrent(window);

  while(!glfwWindowShouldClose(window)){
    int width, height;
    glfwGetFramebufferSize(window, &width, &height);

    immutable ratio = width / cast(float) height;
    glViewport(0, 0, width, height);
    glClear(GL_COLOR_BUFFER_BIT);

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();

    glOrtho(-ratio, ratio, -1., 1., 1., -1.);
    glMatrixMode(GL_MODELVIEW);

    glLoadIdentity();
    glRotatef(cast(float) glfwGetTime() * 50., 0., 0., 1.);

    glBegin(GL_TRIANGLES);
    glColor3f(1., 0., 0.);
    glVertex3f(-0.6, -0.4, 0.);
    glColor3f(0., 1., 0.);
    glVertex3f(0.6, -0.4, 0.);
    glColor3f(0., 0., 1.);
    glVertex3f(0., 0.6, 0.);
    glEnd();

    glfwSwapBuffers(window);
    glfwPollEvents();
  }

  glfwDestroyWindow(window);
  glfwTerminate();

}

以下Makefile
コード内にpragma(lib, "DerelictGL3");と書けば本来は-L-lDerelictGL3のような記述は必要ないはずだが、リンクエラーが出てしまうため記載。

Makefile
P:=openwindow
DC:=ldmd2
SRCS:=openwindow.d
INCLUDES:=$(HOME)/work/Derelict3/import/
LIBS:=-L-L$(HOME)/work/Derelict3/lib/ldc/ -L-lDerelictGL3 -L-lDerelictGLFW3 -L-lDerelictUtil

$(P): $(SRCS)
        $(DC) -of$(P) -I$(INCLUDES) $(LIBS) $(SRCS)

clean:
        -rm -f openwindow.o openwindow
.PHONY: clean

出力はこんな感じ。三角形がくるくる回る。

Screenshot from 2014-04-14 18:50:18.png

4
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
4