オレオレ描画エンジン(?)を作る : 1日目 その2

2019年2月23日C/C++,GLSL,OpenGL,Programming,TECHNOLOGY,オレオレ描画エンジン(?)を作る

続きです。

 

 

サンプルを参考に適当なコードを書いてみる

examplesにあるsimple.cが参考になりそうです。

とりあえず、適当にこんなコード書いてみました

#include <stdio.h>
#include <stdlib.h>

#include <GLFW/glfw3.h>


static void errorCallback(int error, const char* description){
    fprintf(stderr, "Error: %sn", description);
}

static void keyCallback(GLFWwindow* window, int key, int scancode, int action, int mods){
    if(key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
        glfwSetWindowShouldClose(window, GLFW_TRUE);
}


int main(int argc, char const *argv[]){
    GLFWwindow* window;
    GLuint vertex_buffer, vertex_shader, fragment_shader, program;
    GLint mvp_location, vpos_location, vcol_location;

    glfwSetErrorCallback(errorCallback);

    if(!glfwInit()) exit(EXIT_FAILURE);

    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 2);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0);

    window = glfwCreateWindow(800, 450, "RViz Main Window", NULL, NULL);

    if(!window){
        glfwTerminate(); //Because glfw is already initialized!
        exit(EXIT_FAILURE);
    }

    glfwSetKeyCallback(window, keyCallback);

    glfwMakeContextCurrent(window);
    glfwSwapInterval(1);

    while(!glfwWindowShouldClose(window)){
        glClearColor(0.0, 0.0, 0.0, 0.0);
        glClear(GL_COLOR_BUFFER_BIT);
        glfwSwapBuffers(window);
        glfwPollEvents();
    }

    glfwDestroyWindow(window);

    glfwTerminate();

    return 0;
}

 

良さそうですね。

 

 

ビルドに必要なファイルをコピー

  • RVizのincludeディレクトリにGLFW3のincludeディレクトリの中にあるGLFWディレクトリをコピー
  • GLFW3のsrc内にあるビルドしたライブラリ(libglfw3.a)をRVizのlibディレクトリ内にコピー
  • (ダミーファイルは消します)

 

 

ビルド

srcディレクトリまで移動しておもむろに以下のコマンドを実行

g++ main.cpp -I../include -L../lib -lglfw3

 

main.cpp:44:9: warning: 'glClearColor' is deprecated: first deprecated in macOS 10.14 - OpenGL API deprecated. (Define GL_SILENCE_DEPRECATION to silence these warnings)
      [-Wdeprecated-declarations]
        glClearColor(0.0, 0.0, 0.0, 0.0);
        ^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.14.sdk/System/Library/Frameworks/OpenGL.framework/Headers/gl.h:2396:13: note: 
      'glClearColor' has been explicitly marked deprecated here
extern void glClearColor (GLclampf red, GLclampf green, GLclampf blue, GLclampf alpha) OPENGL_DEPRECATED(10.0, 10.14);
            ^
main.cpp:45:9: warning: 'glClear' is deprecated: first deprecated in macOS 10.14 - OpenGL API deprecated. (Define GL_SILENCE_DEPRECATION to silence these warnings)
      [-Wdeprecated-declarations]
        glClear(GL_COLOR_BUFFER_BIT);
        ^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.14.sdk/System/Library/Frameworks/OpenGL.framework/Headers/gl.h:2394:13: note: 
      'glClear' has been explicitly marked deprecated here
extern void glClear (GLbitfield mask) OPENGL_DEPRECATED(10.0, 10.14);
            ^
2 warnings generated.
Undefined symbols for architecture x86_64:
  "_CFArrayAppendValue", referenced from:
      __glfwInitJoysticksNS in libglfw3.a(cocoa_joystick.m.o)
      _addJoystickElement in libglfw3.a(cocoa_joystick.m.o)
  "_CFArrayApplyFunction", referenced from:
      _matchCallback in libglfw3.a(cocoa_joystick.m.o)
  "_CFArrayCreateMutable", referenced from:
      __glfwInitJoysticksNS in libglfw3.a(cocoa_joystick.m.o)
      _matchCallback in libglfw3.a(cocoa_joystick.m.o)
  "_CFArrayGetCount", referenced from:
      _matchCallback in libglfw3.a(cocoa_joystick.m.o)
....
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

 

はい、ダメですね。

どうみてもリンクエラーが発生してます。

glfw3ライブラリを使ってMacのg++でビルドする場合以下のオプションが必要となります。

-framework Cocoa -framework OpenGL -framework IOKit -framework CoreFoundation -framework CoreVideo

 

なので、

g++ main.cpp -I../include -L../lib -lglfw3 -framework Cocoa -framework OpenGL -framework IOKit -framework CoreFoundation -framework CoreVideo

 

main.cpp:44:9: warning: 'glClearColor' is deprecated: first deprecated in macOS 10.14 - OpenGL API deprecated. (Define GL_SILENCE_DEPRECATION to silence these warnings)
      [-Wdeprecated-declarations]
        glClearColor(0.0, 0.0, 0.0, 0.0);
        ^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.14.sdk/System/Library/Frameworks/OpenGL.framework/Headers/gl.h:2396:13: note: 
      'glClearColor' has been explicitly marked deprecated here
extern void glClearColor (GLclampf red, GLclampf green, GLclampf blue, GLclampf alpha) OPENGL_DEPRECATED(10.0, 10.14);
            ^
main.cpp:45:9: warning: 'glClear' is deprecated: first deprecated in macOS 10.14 - OpenGL API deprecated. (Define GL_SILENCE_DEPRECATION to silence these warnings)
      [-Wdeprecated-declarations]
        glClear(GL_COLOR_BUFFER_BIT);
        ^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.14.sdk/System/Library/Frameworks/OpenGL.framework/Headers/gl.h:2394:13: note: 
      'glClear' has been explicitly marked deprecated here
extern void glClear (GLbitfield mask) OPENGL_DEPRECATED(10.0, 10.14);
            ^
2 warnings generated.

 

警告が出ますが、無事ビルドできました.

(この警告はもうMac OSでOpenGLがdeprecatedになっているからです、まじでOpenGLプログラマーを殺さないでくれ….)

 

 

実行

./a.out

素晴らしいいいいいい

ウィンドウが出せましたね。

 

 

1日目 その2終了

Windowも出せたし、もう全てが最高になった

commitしてpush!

終了です。

 

ゲームプログラミングC++

ゲームプログラミングC++

Sanjay Madhav
5,060円(04/20 08:32時点)
Amazonの情報を掲載しています