直接用颜色数据绘制到屏幕上,这种方法效率较低。
代码如下:
char* _pixel;
//初始化像素颜色
static void initPixel() {
_pixel = new char[100 * 200 * 4];
for(int i = 0; i < 100 * 200 * 4; ++i) {
_pixel[i] = rand() % 255;
}
}
// 绘制
static void render(GLFWwindow * window) {
glClearColor(0, 0, 0, 1);
glClear(GL_COLOR_BUFFER_BIT);
//指定绘制的位置 x,y
glRasterPos2i(100, 300);
// glDrawPixels (GLsizei width, GLsizei height, GLenum format, GLenum type, const void *pixels);
// width 图片宽度
// height 图片高度
// format 图片颜色值格式
// type rgb每个通道的数据类型 (无符号char)
// pixels 颜色数据指针
glDrawPixels(100, 200, GL_RGBA, GL_UNSIGNED_BYTE, _pixel);
glfwSwapBuffers(window);
glfwPollEvents();
}