- Write C++ program main.cpp, define eight vertex coordinates vertex_list array and vertex ordinal number array index_list of the cube in the program, and realize the line drawing of the cube by glVertex 3FV
#include<GL/glut.h> // Drawing Cubes // Save eight vertices of a cube in an array static const float vertex_list[][3] = { -0.5f, -0.5f, -0.5f, 0.5f, -0.5f, -0.5f, -0.5f, 0.5f, -0.5f, 0.5f, 0.5f, -0.5f, -0.5f, -0.5f, 0.5f, 0.5f, -0.5f, 0.5f, -0.5f, 0.5f, 0.5f, 0.5f, 0.5f, 0.5f, }; // Save the serial number of the vertex to be used in an array static const GLint index_list[][2] = { { 0, 1 }, { 2, 3 }, { 4, 5 }, { 6, 7 }, { 0, 2 }, { 1, 3 }, { 4, 6 }, { 5, 7 }, { 0, 4 }, { 1, 5 }, { 7, 3 }, { 2, 6 } }; // Drawing Cubes void DrawCube(void) { int i, j; glBegin(GL_LINES); for (i = 0; i<12; ++i) // 12 lines { for (j = 0; j<2; ++j) // Two vertices per line segment { glVertex3fv(vertex_list[index_list[i][j]]); } } glEnd(); } static float rotate = 0; static int times = 0; void renderScene(void) { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glPushMatrix(); times++; if (times > 100) { times = 0; } if (times % 5 == 0) { rotate += 1; } glRotatef(rotate, 0, 1, 0); glRotatef(rotate, 1, 0, 0); glRotatef(rotate, 0, 0, 1); DrawCube(); glPopMatrix(); glutSwapBuffers(); } int main(int argc, char **argv) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA); glutInitWindowPosition(100, 100); glutInitWindowSize(500, 500); glutCreateWindow("GLDemo"); glutDisplayFunc(renderScene); glutIdleFunc(renderScene); glutMainLoop(); }
- By commanding EMCC main.cpp-s WASM=1-o index.html to compile, errors are always reported (Fig. 1), errors indicate that functions such as glColor3f are undefined, various information is consulted, and finally emscripten official website finds an explanation (Fig. 2): If you use the old version of desktop OpenGL API, you must add-s LEGACY_GL_EMULATION=1 when linking items.
Figure 1 Misinformation
Figure 2 Solutions
- View the rendered cubic rendering on the browser by commanding emrun --no_browser --port 8080 index.html
The results are as follows: