除了几何图形的渲染,还有两种重要的类型的数据渲染
位图:由0和1组成的矩形数组(采用黑白,0为黑1,为白)
图像数据:每个像素包含好几段数据(RGBA)
当前光栅位置
当前光栅位置就是开始绘制下一副位图或图像的屏幕位置,光栅坐标需要经过模型投影矩阵变换
glRasterPos{234}{sifd} (TYPE x, TYPE y,TYPE z,TYPE w);
当前窗口位置
glWindowPos{23}{sifd} (TYPE x, TYPE y,TYPE z);
设置当前窗口位置,不需要经过模型投影矩阵变换
glOrtho (-20, 80, 0, h, -1.0, 1.0);
左图1:光栅坐标
右图2:窗口坐标
绘制位图
glBitmap (GLsizei width, GLsizei height, GLfloat xorig, GLfloat yorig, GLfloat xmove, GLfloat ymove, const GLubyte *bitmap);
xorig,yorig定义了光栅的原点,它根据当前光栅位置确定的,正值在当前光栅的上面和右面,负值在当前的下面和左面
xmove,ymove表示位图光栅化后光栅位置的增加量
说明:如果位图信息数据包含N个字节,位图高度为M,那么每行所包含的字节数为n/m根据此内容渲染位图0,1 来达到期望的效果
图像
void glReadPixels (GLint x, GLint y, GLsizei width, GLsizei height, GLenum format, GLenum type, GLvoid *pixels);
从帧缓冲区读取一个矩形像素数组,并把数据保存在内存中
glDrawPixels (GLsizei width, GLsizei height, GLenum format, GLenum type, const GLvoid *pixels);
把内存中保存的一个矩形像素数组写入到帧缓冲区中由glRasterPos指定的当前位置
glCopyPixels (GLint x, GLint y, GLsizei width, GLsizei height, GLenum buffer);
从帧缓冲区读取一个矩形像素数组,并把数据保存在内存中
buffer:GL_DEPTH(GL_DEPTH_COMPONENT)
GL_STENCIL(GL_STENCIL_INDEX)
GL_COLOR(GL_RGBA或GL_COLOR_INDEX)
放大,缩小或旋转图像
void glPixelZoom(Glfloat zoomx,Glfloat zoomy)
设置像素的写入操作glDrawPixels,glCopyPixels中x,y方向的放大或缩小因子,默认值都是1.0,如果它们都是2.0,图像每个像素被绘制4个屏幕像素,可以使用负的缩放因子,负的缩放因子根据当前光栅位置对图像进行翻转。正常图片是自底向上,从左到用,如果Y为负数,图像映射到屏幕上就是自顶向下的
glRasterPos2i(0, 0);
//glPixelZoom (2, 2);//1
glDrawPixels(checkImageWidth, checkImageHeight, GL_RGB, GL_UNSIGNED_BYTE, checkImage);
//glPixelZoom (1, 1);//2
glRasterPos2i (250, 0);
glCopyPixels(0,0,checkImageWidth,checkImageHeight,GL_COLOR);
左边的图为glDrawPixels,右边的图是glCopyPixels
将1,2处解开,可见DrawPixels大小放大2*2倍,glCopyPixels在2*2倍 再次放大1*1倍
将1,2处解开,可见DrawPixels大小放大2*2倍,glCopyPixels在2*2倍 再次放大1*1倍
由此可见不显示调用glPixelZoom (1, 1),默认与使用原来放大倍数再次放大
glReadPixels与glDrawPixels所使用的像素格式 |
GL_COLOR |
1个颜色索引值 |
GL_RG |
|
GL_RGB |
|
GL_RGBA |
|
GL_BGR |
|
GL_BGRA |
|
GL_RED |
|
GL_GREEN |
|
GL_BLUE |
|
GL_ALPHA |
|
GL_LUMINANCE |
一个亮度成分 |
GL_LUMINANCE_ALPHA |
一个亮度成分和一个alpha成分 |
GL_STENCIL_INDEX |
一个模板索引值 |
GL_DEPTH_COMPONENT |
一个深度成分 |
GL_DEPTH_STENCIL |
深度成分和模板成分混合 |
glReadPixels与glDrawPixels所使用的数据类型
GL_UNSIGNED_BYTE |
|
GL_BYTE |
|
GL_BITMAP |
|
GL_UNSIGNED_SHORT |
|
GL_SHORT |
|
GL_UNSIGNED_INT |
|
GL_INT |
|
GL_FLOAT |
|
GL_HALF_FLOAT |
|
GL_UNSIGNED_BYTE_3_3_2 |
|
GL_UNSIGNED_BYTE_2_3_3_REV |
REV后缀专门用于微软操作系统 |
GL_UNSIGNED_SHORT_5_6_5 |
|
GL_UNSIGNED_SHORT_5_6_5_REV |
|
GL_UNSIGNED_SHORT_4_4_4_4 |
|
GL_UNSIGNED_SHORT_4_4_4_4_REV |
|
GL_UNSIGNED_SHORT_5_5_5_1 |
|
GL_UNSIGNED_SHORT_1_5_5_5_REV |
|
GL_UNSIGNED_INT_8_8_8_8 |
|
GL_UNSIGNED_INT_8_8_8_8_REV |
|
GL_UNSIGNED_INT_10_10_10_2 |
|
GL_UNSIGNED_INT_2_10_10_10_REV |
|
GL_UNSIGNED_INT_24_8 |
|
像素格式与数据类型匹配关系
GL_UNSIGNED_BYTE_3_3_2 |
GL_RGB |
GL_UNSIGNED_BYTE_2_3_3_REV |
GL_RGB |
GL_UNSIGNED_SHORT_5_6_5 |
GL_RGB |
GL_UNSIGNED_SHORT_5_6_5_REV |
GL_RGB |
GL_UNSIGNED_SHORT_4_4_4_4 |
GL_RGBA,GL_BGRA |
GL_UNSIGNED_SHORT_4_4_4_4_REV |
GL_RGBA,GL_BGRA |
GL_UNSIGNED_SHORT_5_5_5_1 |
GL_RGBA,GL_BGRA |
GL_UNSIGNED_SHORT_1_5_5_5_REV |
GL_RGBA,GL_BGRA |
GL_UNSIGNED_INT_8_8_8_8 |
GL_RGBA,GL_BGRA |
GL_UNSIGNED_INT_8_8_8_8_REV |
GL_RGBA,GL_BGRA |
GL_UNSIGNED_INT_10_10_10_2 |
GL_RGBA,GL_BGRA |
GL_UNSIGNED_INT_2_10_10_10_REV |
GL_RGBA,GL_BGRA |
GL_UNSIGNED_INT_24_8 |
GL_DEPTH_STENCIL |
GL_UNSIGNED_INT_10F_11F_11F |
GL_RGB |
GL_UNSIGNED_INT_5_9_9_9_REV |
GL_RGB |
GL_FLOAT_32_UNSIGNED_INT_24_8_REV |
GL_DEPTH_STENCIL |
#include "header.h"
#include <string.h>
GLubyte space[] =
{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
GLubyte letters[][13] = {
{0x00, 0x00, 0xc3, 0xc3, 0xc3, 0xc3, 0xff, 0xc3, 0xc3, 0xc3, 0x66, 0x3c, 0x18}, //a
{0x00, 0x00, 0xfe, 0xc7, 0xc3, 0xc3, 0xc7, 0xfe, 0xc7, 0xc3, 0xc3, 0xc7, 0xfe}, //b
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c0, 0xfe, 0xc7, 0xc3, 0xc3, 0xc7, 0xfe},
{0x00, 0x00, 0x3f, 0x6e, 0xdf, 0xdb, 0xc3, 0xc3, 0xc3, 0xc3, 0xc3, 0x66, 0x3c},
{0x00, 0x00, 0xc3, 0xc6, 0xcc, 0xd8, 0xf0, 0xfe, 0xc7, 0xc3, 0xc3, 0xc7, 0xfe},
{0x00, 0x00, 0x7e, 0xe7, 0x03, 0x03, 0x07, 0x7e, 0xe0, 0xc0, 0xc0, 0xe7, 0x7e},
{0x00, 0x00, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0xff},
{0x00, 0x00, 0x7e, 0xe7, 0xc3, 0xc3, 0xc3, 0xc3, 0xc3, 0xc3, 0xc3, 0xc3, 0xc3},
{0x00, 0x00, 0x18, 0x3c, 0x3c, 0x66, 0x66, 0xc3, 0xc3, 0xc3, 0xc3, 0xc3, 0xc3},
{0x00, 0x00, 0xc3, 0xe7, 0xff, 0xff, 0xdb, 0xdb, 0xc3, 0xc3, 0xc3, 0xc3, 0xc3},
{0x00, 0x00, 0xc3, 0x66, 0x66, 0x3c, 0x3c, 0x18, 0x3c, 0x3c, 0x66, 0x66, 0xc3},
{0x00, 0x00, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x3c, 0x3c, 0x66, 0x66, 0xc3},
{0x00, 0x00, 0xff, 0xc0, 0xc0, 0x60, 0x30, 0x7e, 0x0c, 0x06, 0x03, 0x03, 0xff}
};
GLuint fontOffset;
void makeRasterFont(void)
{
GLuint i, j;
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
fontOffset = glGenLists (128);
for (i = 0,j = 'A'; i < 26; i++,j++) {
glNewList(fontOffset + j, GL_COMPILE);
glBitmap(8, 13, 0.0, 2.0, 10.0, 0.0, letters[i]);
glEndList();
}
glNewList(fontOffset + ' ', GL_COMPILE);
glBitmap(8, 13, 0.0, 2.0, 10.0, 0.0, space);
glEndList();
}
void init(void)
{
glShadeModel (GL_FLAT);
makeRasterFont();
}
void printString(char *s)
{
glPushAttrib (GL_LIST_BIT);
glListBase(fontOffset);
glCallLists(strlen(s), GL_UNSIGNED_BYTE, (GLubyte *) s);
glPopAttrib ();
}
void display(void)
{
GLfloat white[3] = { 1.0, 1.0, 1.0 };
glClear(GL_COLOR_BUFFER_BIT);
glColor3fv(white);
glRasterPos2i(20, 60);
printString("ABCDEFGHIJKLMNOPQRSTUVWXYZ");
glRasterPos2i(20, 40);
printString("OVER A LAZY DOG");
glFlush ();
}
void reshape(int w, int h)
{
glViewport(0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho (0.0, w, 0.0, h, -1.0, 1.0);
glMatrixMode(GL_MODELVIEW);
}
void keyboard(unsigned char key, int x, int y)
{
switch (key) {
case 27:
exit(0);
}
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(300, 100);
glutInitWindowPosition (100, 100);
glutCreateWindow("位图");
init();
glutReshapeFunc(reshape);
glutKeyboardFunc(keyboard);
glutDisplayFunc(display);
glutMainLoop();
return 0;
}