OpenGL+WebGL——绘制球体

  • 编写C++程序main.cpp,包括构建点的数据结构、计算球体的参数、绘制球面、初始化设置、绘图函数与主函数调用
#include 
#include 

#define PI 3.14159265358979323846
#define PI2 6.28318530717958647692

GLsizei width = 600, height = 600;

int uStepsNum = 50, vStepNum = 50;
static float rotate = 0;
static int times = 0;

class Point
{
	public:
		Point() {};
		Point(float a, float b, float c) :x(a), y(b), z(c) {};
	public:
		float x;
		float y;
		float z;
};

Point getPoint(float u, float v)
{
	float x = sin(PI*v)*cos(PI2*u);
	float y = sin(PI*v)*sin(PI2*u);
	float z = cos(PI*v);
	return Point(x, y, z);
}


void drawWire()
{
	float ustep = 1 / (float)uStepsNum, vstep = 1 / (float)vStepNum;
	float u = 0, v = 0;
	//绘制下端三角形组
	for (int i = 0;i 100)
		times = 0;

	if (times % 5 == 0)
		rotate += 1;

	glRotatef(rotate, 0, 1, 0);//设置自转方式
	glRotatef(rotate, 1, 0, 0);
	glRotatef(rotate, 0, 0, 1);

	drawWire();
	glutSwapBuffers();
}

int main(int argc, char* argv[])
{
	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
	glutInitWindowPosition(100, 100);
	glutInitWindowSize(width, height);
	glutCreateWindow("Sphere");

	init();
	glutDisplayFunc(displayFunc);
	glutIdleFunc(displayFunc);//回调函数,产生动画
	glutMainLoop();
}
  • 通过命令emcc main.cpp -s WASM=1 -s LEGACY_GL_EMULATION=1 -o index.html生成.js.wasm.html文件
  • 通过命令emrun --no_browser --port 8080 index.html在浏览器上查看绘制的球体效果图

运行结果如下:

OpenGL+WebGL——绘制球体_第1张图片

你可能感兴趣的:(web,assembly+OpenGL)