Sunday, September 4, 2011

Social networking

Social networking is the grouping of individuals into specific groups, like small rural communities or a neighborhood subdivision, if you will. Although social networking is possible in person, especially in the workplace, universities, and high schools, it is most popular online.

Cloudy combo Google App Engine and Amazon S3 combo pack:

Cloudy combo Google App Engine and Amazon S3 combo pack:

Google App Engine provides you a ready to use environment to build and host web applications. The hosting environment is secure and made highly available by the google’s cloud computing infrastructure.
While GAE provides you such a strong platform to build and host web applications, it has it’s own limitations as well. While Google’s infrastructure gives you abundant computing power but not the  case with storage which you would expect if you are developing web applications which require a large amount of flexible secondary storage as well, for that even the applications requiring moderate amount of secondary storage to store the files.
Coming to the actual point, GAE has a limitation of allowing applications to store not more than 10MB of a single large file on it’s server. This may not be a problem for some of the web applications. This is a serious issue for many web applications which assume sufficiently large underlying storage, given these days secondary storage is available in abundance at cheap rates. Some of the developers and users are shying away from using GAE as a platform for their web application development and hosting only because of this reason. And to make life hard google does not have a paid service either to work around this issue.
What are these web application developers supposed to do? Switch to something else which offers sufficient storage along with computing power. The other better options available are Amazon EC2 and VPS. Give me a break. Why do we want to run away from the problem? Well, look around. Amazing Amazon is there to help you out. We need to think creatively and be selective when making decisions about technology these days. There are a bunch of technologies lying around. It is just a matter of selecting and integrating the two technologies and using them to satisfy needs. I am not trying to promote any specific cloud here, but to highlight the fact that GAE and Amazon S3 work really well. And it turns out to be a cost effective solution.
GAE provides the development and hosting platform for you web applications at free of cost while Amazon S3 provides you reliable, powerful and abundant storage at cheaper rates. GAE and Amazon S3 together can do wonders

Click here to find out more! Why Cloud Computing is the Future of Mobile

The term "cloud computing" is being bandied about a lot these days, mainly in the context of the "future of the web." But cloud computing's potential doesn't begin and end with the personal computer's transformation into a thin client - the mobile platform is going to be heavily impacted by this technology as well. At least that's the analysis being put forth by ABI Research. Their recent report, Mobile Cloud Computing, theorizes that the cloud will soon become a disruptive force in the mobile world, eventually becoming the dominant way in which mobile applications operate.
You may be wondering: what does the term "mobile cloud computing" really mean? Basically, it refers to an infrastructure where both the data storage and the data processing happen outside of the mobile device. Today, there are already some good examples of mobile cloud computing applications including mobile Gmail, Google Maps, and some navigation apps. However, the majority of applications today still do most of the data storage and processing on the mobile devices themselves and not in the cloud. In a few years, that could change.

Why Mobile Cloud Computing?

With a Western-centric view of the world, it can sometimes be hard to remember that not everyone owns a smartphone. There are still a large number of markets worldwide where the dominant phone is a feature phone. While it's true that smartphones will grow in percentage and feature phones will become more sophisticated in time, these lower-end phones are not going away anytime soon. And it's their very existence which will help drive the mobile cloud computing trend.
Not only is there a broader audience using feature phones in the world, there are also more web developers capable of building mobile web applications than there are developers for any other type of mobile device. Those factors, combined with the fact that feature phones themselves are becoming more capable with smarter built-in web browsers (and more alternative browsers available for download), will have an impact on mobile cloud computing's growth.

How Will Mobile Cloud Computing Become a Disruptive Force?

There are two primary reasons why ABI believes cloud computing will become a disruptive force in the mobile world. The first is simply the number of users the technology has the power to reach: far more than the number of smartphone users alone. The second reason has to do with how applications are distributed today. Currently, mobile applications are tied to a carrier. If you want an iPhone app, for example, you have to first have a relationship with the mobile operator who carries the iPhone. If you want a Blackberry app, the same rule applies. But with mobile clouding computing applications, as long as you have access to the web, you have access to the mobile application.

Moves by PaaS Players Could Change Everything

When you think of Plaform-as-a-Service (PaaS), one of the first companies that springs to mind is probably Salesforce. With their Force.com platform, business applications can be built and run "in the cloud." But Salesforce is not the only major PaaS player out there today - Amazon Web Services (AWS) and Google's App Engine are also two platforms that could have a major impact on this trend.
Currently, AWS is used by over half a million developers and Google's App Engine hosts 45,000 applications. Now imagine if those two companies along with Force.com all of a sudden started aggressively marketing their mobile capabilities. Today, neither AWS nor Google offers this, and Salesforce's mobile offering is limited to smartphones (Windows Mobile, Blackberry, and iPhone). But if the companies decided to make building for the mobile web as easy as building for the web, you could have a mobile revolution on your hands.

But People Like Apps!

Saying that "mobile cloud computing" is the future doesn't mean phones will be filled with links to websites that work in any browser instead of special, downloadable applications, some of which you can even purchase. Instead, mobile applications will exist in both formats. As for the downloadable applications themselves, they will still appear to be your typical mobile app - end users won't even notice a difference. However, there will be a difference - it will just be on the back-end. Mobile applications will begin to store your data in the cloud as opposed to on the mobile device, and the applications will become more powerful as processing power is also offloaded to the cloud.
The first mobile apps powered by the cloud will likely be business-focused mobile productivity applications where collaboration, data sharing, multitasking, and scheduling are key factors. For consumers, though, navigation and mapping applications will be the most obvious examples of the trend. Plus, there are some specialty applications today which already function as mobile cloud apps - for example, Schlage offers a remote keyless entry system which lets you mobilely control your home from a distance. You can let someone into your house, manage your lights, your thermostat, your camera system, etc. There are also a few applications in the iPhone app store that let you remotely manage your PC and your DVR, too.

Potential Problems

Of course, there are some potential issues that could be barriers to this shift in mobile computing. The most notable problem is the lack of speedy mobile Internet access everywhere. Here in the US, for example, 3G coverage is spotty outside urban areas, leading to intermittent connection issues and slow speeds. Other markets may have it even worse.
However, new technologies like HTML5, which does local caching, could help mobile cloud apps get past those sorts of issues. And there's even a chance that the browser could one day be replaced - at least in some markets - with another technology altogether which provides a better way to access the mobile web. ABI Research mentions initiatives like OMA's Smartcard Web Server, essentially a souped-up SIM card that connects directly with the carrier to push applications to mobile phones. There's also TokTok, a technology that allows access to web services like Gmail and Google Calendar by voice. With voice-enabled search like this, mobile apps could talk directly to the service itself which sits on the edge of the network, as opposed to needing the user to launch a web browser and navigate through the mobile web.

When Will Mobile Cloud Computing Really Take Off?

According to ABI, this change is only a few years away. By 2010, we'll see one or all of the major PaaS players marketing their mobile capabilities, they say. But first, API standards from open-source mobile collaboration group BONDI will go into effect. Later, in 2011, we'll see more of HTML5, and the OneAPI standard will come into play. (OneAPI involves standardized apps for networks allowing developers to consistently access parts of network providers' capabilities, such as location services). All these factors combined will help drive the move to the cloud.
The changes will occur with differing speeds depending on the market. Markets with higher Internet participation will obviously lead the way, as will markets with higher subscriber penetration. That includes Western Europe, North America, and parts of Asia. Other markets will then follow. By 2014, mobile cloud computing will become the predominant application development strategy. By that time, our PCs will be more like thin client devices than they are today, and now it seems our phones will too.
from  http://www.readwriteweb.com/archives/why_cloud_computing_is_the_future_of_mobile.php

Saturday, May 14, 2011

10、OpenGL中的帧缓冲

1、颜色缓冲区
通常用于绘图的缓冲区,包含了颜色索引或RGB颜色数据,还可能包含了alpha数据。如果opengl系统支持立体图,则有左右两个缓冲区。双缓冲系统提供了前后缓冲区。每个opengl实现都必须提供一个前左缓冲区。
2、深度缓冲
Z-BUFFER,保存每个像素的深度值,用于保存像素Z方向的数值。深度通常是根据物体和观察点的距离来测量的,较大深度值的像素可能会被较小深度值的像素所覆盖。但这只是一种约定,可以改变这种行为。
3、模板缓冲区
把绘图限制在屏幕中的某个区域,就像用纸板和喷漆实现精确的绘图一样。用于保持屏幕上某些位置图形不变,而其他部分重绘。例如模拟驾驶的程序,可以只绘制车内的仪表和其他物体1,当汽车开动时,只有车窗外的场景需要更新。
4累积缓存
只保存RGBA数据,用于合成图像累积缓冲区通常用于把一系列的图像合成为一副图像。通过这种方法,可以对图像进行超量采样,然后对样本求平均值,并将结果写入到颜色缓冲区中,从何实现场景的抗锯齿处理。我们不能把数据直接写入到累积缓冲区,累积操作总是以进行快块为单位idui数据进行操作,通常是把数据移入或移出颜色缓冲区。
指定清除值:
void glClearColor( GLclampf red, GLclampf green, GLclampf blue, GLclampf alpha );
void glClearIndex( GLfloat index );
void glClearDepth( GLclampf depth );
void glClearStencil( GLint s );
void glClearAccum( GLfloat red, GLfloat green, GLfloat blue, GLfloat alpha );
GLclampf/d 经过截取的GLfloatGLdouble都限定在0.01.0的范围之内。默认的深度缓冲区清楚值是1.0(表示与观察点尽可能远的距离),其他所有缓冲区的清除值都是0.0然后进行清除:
void glClear(GLbitfield mask);
mask: GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT | GL_ACCUM_BUFFER_BIT

9、二次曲面的绘制

首先定义一个GLUquadricObj对象;
其次创建一个曲面对象gluNewQuadric;
再次设置二次曲面的特性(gluQuadricDrawStyle, gluQuadricTexture)
绘制二次曲面(gluCylinder,gluSphere,gluDisk, gluPartialDisk)
关于二次曲面的过程
gluNewQuadric 创建一个新的二次曲面对象
gluDeleteQuadric 删除一个二次曲面对象
gluQuadricDrawStyle 指定要绘制的二次曲面类型
gluQuadricNormal 设置二次曲面的法矢量
gluQuadricOrientation 设置二次曲面是内旋还是外旋转
gluQuadricTexture 设置二次曲面是否使用纹理
二次方程对象 gluQuadricObj
Ax2+BY2+CZ2+DXY+EXZ+FYZ+GX+HY+IZ+J=0
圆柱
gluCylinder( GLUquadricObj qobj, GLdouble baseRadius, GLdouble topRadius, GLdouble height, GLint
slices, GLint stacks )
qobj指定一个二次曲面,baseRadius为所绘制圆柱的底半径,topRadius为所绘制圆柱的上顶面半径,height为圆柱的
高,slices为绕Z轴的分割线数,stacks为沿Z轴的分割线数。
圆环
gluDisk(qobj:GLUquadricObj,innerRadius:GLdouble,outerRadius:GLdouble,slices:GLint,
loops:GLint);
半圆环
gluPartialDisk(qobj:GLUquadricObj,innerRadius:GLdouble,outerRadius:GLdouble,slices:GLint,
loops:GLint,startAngle:GLdouble,sweepAngle:GLdouble);
startAngle,sweepAngle是半圆环的起始角与终止角
球体
function gluSphere(qObj:GLUquadricObj,radius:GLdouble,slices:GLint,stacks:GLint);

8、Opengl动画(编程指南有详细的解释)

动画:通过快速的吧看似连续的画面一幅幅的呈现在人们面前,一旦每秒钟的画面超过24幅,人们就会错以为它是连续的。
假设某动画一共有n副图片,则它的工作步骤是:
显示第1幅图片,然后等待一小段时间,直到下一个1/24
显示第2幅图片,然后等待一小段时间,直到下一个1/24
……
显示第n幅图片,然后等待一小段时间,直到下一个1/24
结束
如果用C语言伪代码来描述这一过程,就是:
for( i=0; i<n; i++ )
{
     DrawScene( i );
     Wait();
}
双缓冲技术
实际的动画是先画好了,播放的时候直接拿出来现世就行,但计算机动画则是画一张,就拿出来一张,再画下一张,再拿出来。如果所绘制的图形简单,那么这样也没什么问题。但一旦图形比较复杂,绘制所需要的时间较长,问题就会变得突出。让我们把计算机想象成一个画图比较快的人,假如他直接在屏幕上画图,而图形比较复杂,则有可能在他只画了某幅图的一半的时候就被观众看到。而后面虽然他把画补全了,但观众的眼睛却又没有反应过来,还停留在原来那个残缺的画面上。也就是说,有时候观众看到完整的图象,有时却又只看到残缺的图象,这样就造成了屏幕的闪烁。
如何解决这一问题呢?我们设想有两块画板,画图的人在旁边画,画好以后把他手里的画板与挂在屏幕上的画板相交换。这样以来,观众就不会看到残缺的画了。这一技术被应用到计算机图形中,称为双缓冲技术。即:在存储器(很有可能是显存)中开辟两块区域,一块作为发送到显示器的数据,一块作为绘画的区域,在适当的时候交换它们。由于交换两块内存区域实际上只需要交换两个指针,这一方法效率非常高,所以被广泛的采用。
要启动双缓冲功能,最简单的办法就是使用GLUT工具包。我们以前在main函数里面写:
glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
其中GLUT_SINGLE表示单缓冲,如果改成GLUT_DOUBLE就是双缓冲了。
当然还有需要更改的地方——每次绘制完成时,我们需要交换两个缓冲区,把绘制好的信息用于屏幕显示(否则无论怎么绘制,还是什么都看不到)。如果使用GLUT工具包,也可以很轻松的完成这一工作,只要在绘制完成时简单的调用glutSwapBuffers函数就可以了

7、Opengl中模型视图 投影 视口

opengl中不设置模型,投影,视口,所绘制的几何图形的坐标只能是-11(X轴向右,Y轴向上,Z轴垂直屏幕)
产生目标场景的过程类似于用照相机进行拍照:
(1)把照相机固定在三角架上,并让他对准场景
   从不同位置观察场景(视图变换)
(2)对场景进行安排,使各个物体在照片中的位置是我们所希望的
   移动,旋转或者放大缩小场景中的物体(模型变换)
(3)选择照相机镜头,并调整放大倍数(调焦)
   显示物体时,可以选择物体是如何投影到屏幕上(投影变换)
(4)确定照片的大小,放大照片还是缩小照片
   把图形画下来,是要占据整个屏幕还是屏幕的一部分(视口变换)
三大变换:(都是通过操作矩阵来实现的)
1、模型视图变换
在进行模型视图变换之前,应先设置当前操作的矩阵为"模型视图矩阵",通过设置glMatrixMode( GL_MODELVIEW ),在进行变换
之前把当前矩阵设置为单位矩阵glLoadIdentity();如果不进行单位化,所有的后续操作都是在当前矩阵的前提下进行的。
2、投影变换
投影变换即定义一个可视空间,可视空间以外的物体不会被绘制到屏幕上(从现在起,坐标不在是-1.01.0)
opengl支持两种投影,透视投影和正投影,通过设置glMatrixMode( GL_PROJECTION )来操作投影矩阵。
透视投影的主要函数
1glFrustum()
2glPerspective( GLdouble fovy, GLdouble aspect, GLdouble near, GLdouble far)
创建一个表示对称透视视图平截头体的矩阵,并把它与当前矩阵相乘。fovyYZ平面上视野的角度,范围【0180】。aspect是这个平截头体的纵横比,也就是宽度除于高度。nearfar值分片是观察点与近侧裁剪平面以及远侧裁剪平面的距离(沿Z轴负方向)这两个值都是正的。
正投影的主要函数
1glOrtho( GLdouble left, GLdouble right, GLdouble bottom, GLdouble top, GLdouble near, GLdouble far)
创建一个表示正交平行视景体,并把它与当前矩阵相乘。
2glOrtho2D( GLdouble left, GLdouble right, GLdouble bottom, GLdouble top)
创建一个表示把二维坐标投影到屏幕上的矩阵,并把当前矩阵与它相乘,裁剪区域为矩形。
把像素绘制到屏幕上glViewport()定义视口,前两个参数最左下方,后两个参数宽度和高度。
3、视口变换
在窗口中定义一个像素矩形,最终的图像将映射到这个矩形中。
glViewport( Glint x, Glint y, GLsizei width, GLsizei height );XY指定了视口的左下角,WH指定了视口的高度和宽度。在默认情况下,视口的初始值是(0,0,winwidth,winheight)
视口的纵横比一般和视景体的纵横比相同。当窗口的大小发生变化时,并不会自动影响视口,应用程序应该检测窗口大小改变事件。

6、Opengl对像素的操作

读取,写入和复制像素数据:
glReadPixels():从帧缓存区读取一个矩形像素数组,并把数据保存在内存中。
glDrawPixels():把内存中所保存的一个矩形像素数组写入到帧缓冲区中由glRasterPos*()所指定的当前位置。
glCopyPixels():把一个矩形像素数组从帧缓冲区的一个部分复制到另一部分。这个函数相当于先Read再Draw,但数据并不会写入内存中(可能)。(Opengl第四版,Page193)
读取(屏幕截图):
假设我们先建立一个1*1大小的24位色BMP,文件名为dummy.bmp,又假设新的BMP文件名称为grab.bmp。则可以编写如下代码:
#include <stdio.h>
#include <stdlib.h>
//截取函数grab,抓取窗口中宽度WindowWidth和WindowHeigh的像素
#define BMP_HEADER_LENGHT 54
void grad( void )
{
FILE * PdUMMYfILE;
FILE * pWritingFile;
GLubyte* pPixelData;
GLubyte BMP_HEADER[BMP_Header_Lenght];
GLint i, j;
GLint PixelDataLength;
//3-》24位色BMP,这个值可以通过位图信息头来获取
//里面有个参数biBitCount指定了颜色要用到的位数
//计算像素数据的实际长度
i = WidhowWidth*3; //得到每一行的像素数据长度
while( i%4!=0) //补充数据,直到是4的倍数
++i; 
PixelDataLenght=i*WindowHeight; //实际的数据量
//分配内存和打开文件
pPixelData = (GLubyte*)malloc(PixelDataLength);
if( pPixelData==0 ) //分配失败
exit(0);
//读取像素
glPixelStorei( GL_UNPACK_ALIGNMENT, 4 );
glReadPixels( 0, 0, WindowWidth, WindowHeight, GL_BGR_EXT, GL_UNSIGNED_BYTE, pPixelData);
//把dummy.bmp的文件头复制为新文件的文件头
fread( BMP_HEADER, sizeof( BMP_HEADER), 1, pDummyFile);
fwrite( BMP_HEADER, sizeof( BMP_HEADER), 1, pWritingFile );
fseek( pWritingFile, 0x0012, SEEK_SET );
i = WindowWidth;
j = WindowHeight;
fwrite( &i, sizeof(i), pWritingFile );
fwrite( &j, sizeof(j), pWritingFile );
//写入像素数据
fseek( pWritingFile, 0, SEEK_END );
fwrite( pPixelData, PixelDataLength, 1, pWritingFile );
//释放内存和关闭文件
fclose( pDummyFile );
fclose( pWritingFile );
free( pPixelData );}
绘制:(完整代码)。
#include <gl/glut.h>
#include <stdio.h>
#include <stdlib.h>
#define FileName "Bliss.bmp"
static GLint ImageWidth;
static GLint ImageHeigth;
static GLint PixelLength;
static GLubyte* PixelData;
void display( void )
{ glClear( GL_COLOR_BUFFER_BIT );
//绘制像素
//宽度,高度,像素数据内容,像素数据在内存中的格式,数据位置
glDrawPixels( ImageWidth,ImageHeigth, GL_BGR_EXT, GL_UNSIGNED_BYTE, PixelData);
//完成绘制
glutSwapBuffers();}
int main( int argc, char* argv[] )
{ // 打开文件
     FILE* pFile = fopen("Bliss.bmp", "rb");
     if( pFile == 0 )
         exit(0);
     // 读取图象的大小信息
     fseek(pFile, 0x0012, SEEK_SET);
     fread(&ImageWidth, sizeof(ImageWidth), 1, pFile);
     fread(&ImageHeigth, sizeof(ImageHeigth), 1, pFile);
     // 计算像素数据长度
     PixelLength = ImageWidth * 3;
     while( PixelLength % 4 != 0 )
         ++PixelLength;
     PixelLength *= ImageHeigth;
     // 读取像素数据
     PixelData = (GLubyte*)malloc(PixelLength);
     if( PixelData == 0 )
         exit(0);
     fseek(pFile, 54, SEEK_SET);
     fread(PixelData, PixelLength, 1, pFile);
     // 关闭文件
     fclose(pFile);
// 初始化GLUT并运行
     glutInit(&argc, argv);
     glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA);
     glutInitWindowPosition(100, 100);
     glutInitWindowSize(ImageWidth, ImageHeigth);
     glutCreateWindow(FileName);
     glutDisplayFunc(&display);
     glutMainLoop();
     // 释放内存
     // 实际上,glutMainLoop函数永远不会返回,这里也永远不会到达
     // 这里写释放内存只是出于一种个人习惯
     // 不用担心内存无法释放。在程序结束时操作系统会自动回收所有内存
     free(PixelData);
     return 0;}
复制像素:
#include <gl/glut.h>
#include <stdio.h>
#include <stdlib.h>
#define WindowWidth   400
#define WindowHeight 400
void display(void)
{   // 清除屏幕
     glClear(GL_COLOR_BUFFER_BIT);
     // 绘制
     glBegin(GL_TRIANGLES);
         glColor3f(1.0f, 0.0f, 0.0f);     glVertex2f(0.0f, 0.0f);
         glColor3f(0.0f, 1.0f, 0.0f);     glVertex2f(1.0f, 0.0f);
         glColor3f(0.0f, 0.0f, 1.0f);     glVertex2f(0.5f, 1.0f);
     glEnd();
     glPixelZoom( -0.5f, -0.5f);
     glRasterPos2i( 0, 0 );
//1、2表示复制像素来源的矩形的左下角坐标
//3、4表示复制像素来源的矩形的高度和宽度
//复制像素的颜色,可以是GL_DEPTH或GL_STENCIL,深度/模板缓存
     glCopyPixels(WindowWidth/2, WindowHeight/2,
        WindowWidth/2, WindowHeight/2, GL_COLOR);
     // 完成绘制,并抓取图象保存为BMP文件
     glutSwapBuffers();
}
int main( int argc, char* argv[] )
{ // 初始化GLUT并运行
     glutInit(&argc, argv);
     glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA);
     glutInitWindowPosition(0, 0);
     glutInitWindowSize(WindowWidth, WindowHeight);
     glutCreateWindow("FileName");
     glutDisplayFunc(display);
     glutMainLoop();
     return 0;}

5、Opengl显示文字

1、显示ASCII:128个字符。
// ASCII字符总共只有0到127,一共128种字符
#define MAX_CHAR       128
void drawString(const char* str) 
{
    static int isFirstCall = 1;
    static GLuint lists;
    if( isFirstCall ) 

// 如果是第一次调用,执行初始化
        // 为每一个ASCII字符产生一个显示列表
        isFirstCall = 0;
        // 申请MAX_CHAR个连续的显示列表编号
        lists = glGenLists(MAX_CHAR);
        // 把每个字符的绘制命令都装到对应的显示列表中
        wglUseFontBitmaps(wglGetCurrentDC(), 0, MAX_CHAR, lists);
    }
    // 调用每个字符对应的显示列表,绘制每个字符
    for(; *str!='\0'; ++str)
        glCallList(lists + *str);}
//Opengl显示Hello World!
void display( void )
{
glClear(GL_COLOR_BUFFER_BIT);
    glColor3f(1.0f, 0.0f, 0.0f);
glLoadIdentity();
    glRasterPos2f(0.0f, 0.0f);
    drawString("Hello, World!");
    glutSwapBuffers();
}
2、指定字体的大小,类型与字符集
//三个参数字体大小,字符集,字体类型
void selectFont( int size, int charset, const char* face )
{
//创建指定字体
HFONT hFont = CreateFontA(size, 0, 0, 0, FW_MEDIUM, 0, 0, 0,
        charset, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS,
        DEFAULT_QUALITY, DEFAULT_PITCH | FF_SWISS, face);
//把创建的字体选入设备上下文,保存旧字体
    HFONT hOldFont = (HFONT)SelectObject(wglGetCurrentDC(), hFont);
}

3、显示中文,Opengl的显示列表空间有限,无法存储庞大的中文库,对显示的中文,直接创建显示列表,显示中文,删除列表。英文字符只占用一个字节,而中文字符则占用两个字节,通过MultiByteToWideChar函数,可以转化为所有的字符都占两个字节。
void drawCNString( const char * str )
{
int len=0, i=0;
wchar_t * wstring;
HDC hDC=wglGetCurrentDC();
GLuint list=glGenLists( 1 );
//计算字符的个数
// 如果是双字节字符的(比如中文字符),两个字节才算一个字符
    // 否则一个字节算一个字符
for( i=0; str[i]!='\0'; i++ )
{
   //判断字符str[i]是否由两个字节组成
   if( IsDBCSLeadByte( str[i] ))
    i++;
   len++;
}
//将混合字符转化为宽字符
wstring=(wchar_t*)malloc((len+1)*sizeof(wchar_t));
MultiByteToWideChar( CP_ACP, MB_PRECOMPOSED, str, -1, wstring, len );
wstring[len]=L'\0';
//逐个输出字符
for( i=0; i<len; i++ )
{
   wglUseFontBitmapsW( hDC, wstring[i], 1, list );
   glCallList( list );
}
//资源回收
free( wstring );
glDeleteLists( list, 1 );
}

Friday, May 13, 2011

3、圆,五角星,正弦曲线的画法 4、Opengl中的RGBA

3、圆,五角星,正弦曲线的画法
2009-07-04 09:40
1、圆的画法:比较耗资源,低效率。
通过正多边的不断细化实现,正4,5,6……n;即近似圆。
for( i=0; i<n; i++ )
glVertex2f( R*cos(2*Pi/n*i), R*sin(2*Pi/n*i) );
glEnd();//计算多边形个点的坐标值即可(n边形)
2、画五角星:已知正5边形的长度,计算五角星的中心到顶点的距离,再根据对称性和正余炫关系计算个点坐标。
GLfloat a=1/(2-2*cos(72*pi/180 ));
GLfloat bx=a*cos(18*pi/180 );
GLfloat by=a*sin(18*pi/180 );
GLfloat cy=-a*cos(36*pi/180 );
GLfloat cx=a*sin(36*pi/180 );
GLfloat
PointA[2] ={ 0, a },
PointB[2] ={ bx, by },
PointC[2] ={ cx, cy },
PointD[2] ={ -cx, cy },
PointE[2] ={ -bx, by };
glBegin( GL_LINE_LOOP );
glVertex2fv( PointA );
glVertex2fv( PointC );
glVertex2fv( PointE );
glVertex2fv( PointB );
glVertex2fv( PointD );
glEnd();
//画正弦函数的图形
glBegin( GL_LINES );
glVertex2f( -1.0f, 0.0f );
glVertex2f( 1.0f, 0.0f );//X轴
glVertex2f( 0.0f, 1.0f );
glVertex2f( 0.0f, -1.0f );//Y轴
glEnd();
glBegin( GL_LINE_STRIP );
for( x=-1.0f/factor; x<1.0f/factor; x+= 0.01f )
{
glVertex2f( x*factor, sin(x)*factor );
}
glEnd();




4、Opengl中的RGBA


glColor3*、glColor4*系列函数,在参数类型不同时,表示“最大”颜色的值也不同。
采用f和d做后缀的函数,以1.0表示最大的使用。
采用b做后缀的函数,以127表示最大的使用。
采用ub做后缀的函数,以255表示最大的使用。
采用s做后缀的函数,以32767表示最大的使用。
采用us做后缀的函数,以65535表示最大的使用。

1、立方体的绘制:顶点,顶点数组 2、关于glEnalbe/glDisalbe和glEanbleClientState的细节

1、立方体的绘制:顶点,顶点数组
2009-07-03 23:29
提高绘制立方体的时效率:
1、直接采用顶点绘制:24个顶点,6个面
glBegin( GL_QUADS );
glColor3f( 1.0, 0.0, 0.0 );
glVertex3f( 0.0, 0.0, 0.0 );
glVertex3f( 0.5, 0.0, 0.0 );
glVertex3f( 0.5, 0.5, 0.0 );
glVertex3f( 0.0, 0.5, 0.0 );
......
glEnd();   
2、采用数组,把数据和代码进行分离:循环实现
// 将立方体的八个顶点保存到一个数组里面
/*static const GLfloat vertex_lists[][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};
// 将要使用的顶点的序号保存到一个数组里面
static const GLint face_lists[][4]=
{
0, 3, 2, 1,
6, 5, 1, 2,
3, 0, 4, 7,
3, 7, 6, 2,
0, 4, 5, 1,
7, 4, 5, 6,
};//注意每个面绘制的顺序,背面采用顺时针方向。
glBegin(GL_QUADS);
for(int i=0; i<6; ++i)         // 有六个面,循环六次
   for(int j=0; j<4; ++j)     // 每个面有四个顶点,循环四次
     glVertex3fv(vertex_lists[face_lists[i][j]]);
glEnd();     

3、采用顶点数组:减少函数的调用次数。
//启用顶点数组
glEnableClientState( GL_VERTEX_ARRAY );
//指定顶点数组的位置1、表示每个顶点由(x,y,z)三个分量构成,2、分量的类型,3、数据之间的间隔,4、顶点数组的位置
glVertexPointer( 3, GL_FLOAT, 0, vertex_lists );
//1、绘制的图形类型2、顶点个数3、序号数组内的数据的类型4、序号数组的位置
glDrawElements( GL_QUADS, 24, GL_UNSIGNED_INT, face_lists );   


2:关于glEnalbe/glDisalbe和glEanbleClientState的细节

glEnable/glDisable函数与glEnableClientState/glDisableClientState函数的区别:Opengl的工作机制  
    OpenGL在设计时,认为可以将整个OpenGL系统分为两部分,一部分是客户端,它负责发送OpenGL命令。一部分是服务端,它负责接收OpenGL命令并执行相应的操作。对于个人计算机来说,可以将CPU、内存等硬件,以及用户编写的OpenGL程序看做客户端,而将OpenGL驱动程序、显示设备等看做服务端。
通常,所有的状态都是保存在服务器端的,便于OpenGL使用。例如,是否启用了纹理,服务器端在绘制时经常需要知道这个状态,而我们编写的客户端OpenGL程序只在很少的时候需要知道这个状态。所以将这个状态放在服务端是比较有利的。
    但顶点数组的状态则不同。我们指定顶点,实际上就是把顶点数据从客户端发送到服务端。是否启用顶点数组,只是控制发送顶点数据的方式而已。服务端只管接收顶点数据,而不必管顶点数据到底是用哪种方式指定的(可以直接使用glBegin/glEnd/glVertex*,也可以使用顶点数组)。所以,服务端不需要知道顶点数组是否开启。因此,顶点数组的状态放在客户端是比较合理的。
    为了表示服务端状态和客户端状态的区别,服务端的状态用glEnable/glDisable,客户端的状态则用glEnableClientState/glDisableClientState

1、glGenTextures(GLsizei n, GLuint *textures)函数说明

glGenTextures(GLsizei n, GLuint *textures)函数说明 
n:       用来生成纹理的数量 
textures:存储纹理索引的 
        glGenTextures函数根据纹理参数返回n个纹理索引。glGenTextures就是用来产生你要操作的纹理对象的索引的,比如你告诉OpenGL,我需要5个纹理对象glGenTextures(5,&textures),它会从没有用到的纹理对象索引里返回5个纹理对象的索引号存放到textures数组中。 
       OpenGL中存在内部状态机,OpenGL内部实际上存储了很多状态,比如当前操作的纹理对象索引号,glBindTexture实际上是改变了OpenGL的这个状态,它告诉OpenGL下面对纹理的任何操作都是对它所绑定的纹理对象的,比如glBindTexture(GL_TEXTURE_2D,1)告诉OpenGL下面代码中对2D纹理的任何设置都是针对索引为1的纹理的。 
       glGenTextures函数根据纹理参数返回n个纹理名称。纹理名称集合不必是一个连续的整数集合。因此,可能在先前调用的glGenTextures的时候没有名称集合被返回。产生纹理函数假定目标纹理的面积是由glBindTexture函数限制的。先前调用glGentTextures产生的纹理名称集不会由后面调用的glGenTextures得到,除非他们首先被glDeleteTextures删除。 
你不可以在显示列表中包含glGenTextures 
      EXP: 
       GLuint   Tex[5]; 
       glGenTextures(5,&Tex); 
       执行以上两行代码,就得到了5个纹理对象的索引号,他们分别存储在Tex[0]~~Tex[4]中。