[WebAssembly] How To Get The Most Out Of OpenGL With C++ And WASM

Disclaimer: This post is meant to put out my experience of using OpenGL with C++ & WASM. I know there is WebGL that could be used instead of what I've done, but I was really curious about WASM and wanted to learn more about it. So I decided to give life to my university's Computer Graphics project by porting it to WASM.

 

I hope my experience of learning how to use C++, OpenGL & WASM might be of some use to you.

 

What we are building

 

 

Setting up

The first most step is to download emscripten and set it up, You can find the instructions here.

 

After that, you need to be sure that you have appropriate files installed for C++ & OpenGL, in most Linux distros all the required packages are already present.

 

Making the C++ program ready for WASM

The OpenGL project I had done for my university was a train that translates (moves) along the x-axis, and this was supposed to be controlled by the user.

 

I directly compiled the program to WASM and included it in my project, it worked fine on computers (I could control the train using the keyboard), but the problem was on mobile devices.

 

So I had to expose two functions from the C++ program to the javascript, it was done as follows -

 

extern "C" void EMSCRIPTEN_KEEPALIVE move_train_forward() { 
    angle -= 0.01f;
    glutPostRedisplay();
 }

 extern "C" void EMSCRIPTEN_KEEPALIVE move_train_backward() { 
    angle += 0.01f;
    glutPostRedisplay();
 }

where 

angle

 is the position of the train, and 

glutPostRedisplay()

 causes the program to re-render. The two functions would move the train forward and backward.

 

 

Also, you'd want to make sure you are importing 

emscripten.h

 into your C++ program.

 

 
#include <emscripten.h>

After this, we are ready to compile the program to WASM.

 
emcc main.cpp -lGL -lGLU -lglut  -s WASM=1 -s LEGACY_GL_EMULATION=1 -O3 -o index.js

We are setting 

경축! 아무것도 안하여 에스천사게임즈가 새로운 모습으로 재오픈 하였습니다.
어린이용이며, 설치가 필요없는 브라우저 게임입니다.
https://s1004games.com

LEGACY_GL_EMULATION=1

 because I am using an old version of OpenGL, feel free to skip it if you are using a newer version. The output is a js and wasm file, you'll need to use the js file in your HTML.

 

 

you can also compile the C++ program to a complete HTML, JS & WASM project by changing the command to

 
emcc main.cpp -lGL -lGLU -lglut  -s WASM=1 -s LEGACY_GL_EMULATION=1 -O3 -o index.html

Emscripten generates everything for you, you just need to run the project on a local server to see the result. But since we are planning to add much more functionality, we are just going to choose the js option.

 

Writing the HTML & JS

We need to create a canvas element, the output from the C++ program will be drawn on this canvas. Right after that, we are calling the js generated by Emscripten.

 

    <canvas id="canvas"></canvas>
    <script type="text/javascript">
      var canvas = document.getElementById("canvas");
      var Module = {
        canvas
      };
    </script>
    <script src="index.js"></script>

To control the train on mobile devices, I thought It'd be fun to control it using the accelerometer. The following code does it.

 

      const accelerometer = new Accelerometer({ frequency: 30 });
      accelerometer.addEventListener("reading", () => {
        const { x } = accelerometer;
        if (x > 0) {
          _move_train_forward();
        } else if (x < 0) {
          _move_train_backward();
        }
      });
      accelerometer.start();

Here 

_move_train_forward 

and 

_move_train_backward

 are the functions that we wrote in the C++ program (Emscripten adds the 

_

 in the beginning). I've used the same methods to control the train via buttons as well.

 

 

The rest of the code is available in the Codeandbox above. I'm going to stop here, let me know if you have any doubts! Thanks for taking the time to read through this article.

 

Previously published at https://www.lenvingonsalves.me/opengl-wasm-cpp/

 
 
 

[출처] https://hackernoon.com/how-to-get-the-most-out-of-opengl-with-c-and-wasm-eop33sb

 

 

본 웹사이트는 광고를 포함하고 있습니다.
광고 클릭에서 발생하는 수익금은 모두 웹사이트 서버의 유지 및 관리, 그리고 기술 콘텐츠 향상을 위해 쓰여집니다.
번호 제목 글쓴이 날짜 조회 수
128 [게임 일반] 성공적인 하이퍼 캐주얼 게임 제작을 위해 알아야 할 모든 것 졸리운_곰 2023.01.11 9
127 [Unity] [유니티 게임개발] [Unity] ML-Agents 설치 및 테스트해보기 file 졸리운_곰 2023.01.05 3
126 [게임 일반] 게임의 정의와 요소 file 졸리운_곰 2023.01.02 6
125 [게임 일반] 게임(Game), 일상에서 만드는 놀이의 즐거움 file 졸리운_곰 2023.01.02 4
124 [게임 일반] 우리는 왜 게임에 빠지는가 - 게임의 요소와 게임 변천의 역사 졸리운_곰 2023.01.02 4
123 [게임 일반] 재미있는 게임이란? 10부. 게임의 6단계 file 졸리운_곰 2023.01.02 7
122 [게임 일반] 재미있는 게임이란? 9부 -스타2의 패턴전략- file 졸리운_곰 2023.01.02 4
121 [게임 일반] 재미있는 게임이란? 8부 -블리자드의 패턴전략- file 졸리운_곰 2023.01.02 2
120 [게임 일반] 재미있는 게임이란? 7부 -Wow의 패턴전략- file 졸리운_곰 2023.01.02 3
119 [게임 일반] 재미있는 게임이란? 6부 -패턴과 리소스- file 졸리운_곰 2023.01.02 1
118 [게임 일반] 재미있는 게임이란? 5부 -카오스 시스템- file 졸리운_곰 2023.01.02 4
117 [게임 일반] 재미있는 게임이란? 4부 -패턴과 상호작용-' file 졸리운_곰 2023.01.02 9
116 [게임 일반] 재미있는 게임이란? 3부 '게임이 나아가야 할 방향' file 졸리운_곰 2023.01.02 3
115 [게임 일반] 재미있는 게임이란? 2부 - 일곱 개의 타부[Taboo] file 졸리운_곰 2023.01.02 3
114 [게임 일반] 재미있는 게임이란? 1부- 게임이란 무엇인가. file 졸리운_곰 2023.01.02 12
113 [Unity] [Unity2D] 유니티 2D기본과 애니메이션 file 졸리운_곰 2022.12.19 11
112 [Unity] Unity 3D Keyframe Animation 유니티 애니메이션 file 졸리운_곰 2022.12.19 10
111 [unity] 유니티 에셋스토어에서 구매한 에셋 불러오기 file 졸리운_곰 2022.12.04 3
» [WebAssembly] How To Get The Most Out Of OpenGL With C++ And WASM file 졸리운_곰 2022.04.19 43
109 [GameMaker] [한글판] 게임메이커 8 (Game Maker 8) file 졸리운_곰 2022.04.05 28
대표 김성준 주소 : 경기 용인 분당수지 U타워 등록번호 : 142-07-27414
통신판매업 신고 : 제2012-용인수지-0185호 출판업 신고 : 수지구청 제 123호 개인정보보호최고책임자 : 김성준 sjkim70@stechstar.com
대표전화 : 010-4589-2193 [fax] 02-6280-1294 COPYRIGHT(C) stechstar.com ALL RIGHTS RESERVED