chapter 1:输出一张图片
内容简介
第一章内容就是选用一种工具来显示图片,之前浏览过师兄的博客,这里同样也使用glut
#include <stdlib.h>
#include <glut.h>
#define Width 1200
#define Height 600
unsigned char *Pixels;
void Draw(void) {
glClearColor(0.0, 0.0, 0.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT);
glDrawPixels(Width, Height, GL_RGBA, GL_UNSIGNED_BYTE, Pixels);
glFlush();
}
int main(int argc, char* argv[])
{
Pixels = (unsigned char *)malloc(Width * Height * sizeof(unsigned char) * 4);
for (int i = 0; i < Width; i++)
{
for (int j = 0; j < Height; j++)
{
int offset = (Width * j + i) * 4;
Pixels[offset + 0] = 255;
Pixels[offset + 1] = 0;
Pixels[offset + 2] = 0;
Pixels[offset + 3] = 0;
}
}
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGBA);
glutInitWindowSize(Width, Height);
glutInitWindowPosition(600, 400);
glutCreateWindow("Ray Tracing");
glutDisplayFunc(Draw);
glutMainLoop();
}
显示结果
这一部分熟悉如何设置像素点颜色,如何把图片显示出来就可以,也可以像书中一样写ppm文件