实 现:在屏幕上画点功能。(能够很快显示)
综合实例(使用480*320显示)
待 改 进:降低GL_Idle函数的调用频率
编 者:张永辉 2012年11月17日
----------------------------------------------------------------------------------------------------
综合实例:
/***************************************************************************************************
* INCLUDE FILES
****************************************************************************************************/
#include <includes.h>
OS_STK AppStartTaskStk[APP_OS_CFG_START_TASK_STK_SIZE];
OS_STK GLTaskStk[256];
static void App_TaskStart (void);
void GL_TaskStart (void);
/***************************************************************************************************
* INCLUDE FILES
***************************************************************************************************/
int main (void)
{
INT8U err;
OSInit();
OSTaskCreate((void (*)(void *)) App_TaskStart,
(void *) 0,
(OS_STK *)&AppStartTaskStk[APP_OS_CFG_START_TASK_STK_SIZE-1],
(INT16U )(OS_TASK_OPT_STK_CHK | OS_TASK_OPT_STK_CLR));
OSTaskCreate((void (*)(void *)) GL_TaskStart,
(void *) 0,
(OS_STK *)&GLTaskStk[256-1],
(INT16U )10);
#if (OS_TASK_NAME_SIZE >= 5) //Give a name to tasks
OSTaskNameSet(OS_TASK_IDLE_PRIO,"Idle",&err); //默认的Idle任务
#endif
OSStart(); // Start OS
}
/***************************************************************************************************
* 第一个任务 App_TaskStart()
***************************************************************************************************/
void DisTset(void); //将调用的测试函数
void App_TaskStart (void)
{
#if OS_TASK_STAT_EN > 0
OSStatInit(); // Determine CPU capacity
#endif
OSTimeDlyHMSM(0, 0, 1, 0); //需要任务GL_TaskStart申请空间完成后才能DisTset。
while (1)
{
printf("Delay 2 second and print\n");
DisTset();
}
}
/***************************************************************************************************
* 第二个任务 GL_TaskStart
***************************************************************************************************/
//*******************************************.H****************************************************/
#include <GL/glut.h>
#pragma comment(lib, "opengl32.lib")
#pragma comment(lib, "glu32.lib")
#pragma comment(lib, "glut32.lib")
#define X_SIZE 480 //定义屏幕尺寸
#define Y_SIZE 320
void DisPoint(unsigned int x,unsigned int y,unsigned short Color); //描点函数
void DisClear(void); //清屏
//*******************************************.C****************************************************/
static const GLint ImageWidth =X_SIZE; //使用OpenGL的GLint类型,它是32位的。
static const GLint ImageHeight=Y_SIZE;
static GLint ImagelLength; //图像缓冲占的字节长度
static GLubyte* ImageDataP; //图像缓冲区指针更改该区域的值即可改变图像
//--------------------------------------------------------------------------------------------------
//函数功能:性能测试 功能测试函数
void DisTset(void)
{
int x = 0;
int y = 0;
OSTimeDlyHMSM(0, 0, 1, 0);
DisClear();
OSTimeDlyHMSM(0, 0, 1, 0);
//绘制红色区域块
for(x = 0;x < X_SIZE/3; x++)
{
for(y = 0;y <= Y_SIZE;y++)
{
DisPoint(x,y,0x1f<<11);
}
}
//绘制绿色区域块
for(x = X_SIZE/3; x < 2*X_SIZE/3; x++)
{
for(y = 0;y <= Y_SIZE;y++)
{
DisPoint(x,y,0x3f<<5);
}
}
//绘制蓝色区域块
for(x = 2*X_SIZE/3; x <= X_SIZE; x++)
{
for(y = 0;y <= Y_SIZE;y++)
{
DisPoint(x,y,0x1f);
}
}
}
//--------------------------------------------------------------------------------------------------
//函数功能:清屏
void DisClear(void)
{
int x = 0;
int y = 0;
for(x = 0;x < X_SIZE; x++)
{
for(y = 0;y < Y_SIZE;y++)
{
DisPoint(x,y,0xffff);
}
}
}
//--------------------------------------------------------------------------------------------------
//函数功能:在屏幕上画点
// x : x坐标位置 0... X_SIZE-1
// y : y坐标位置 0... Y_SIZE-1
// C : 点的颜色 0... 65535 RGB565模式
//坐 标 系:左上角(0,0) 右下角(X_SIZE,Y_SIZE)
void DisPoint(unsigned int x,unsigned int y,unsigned short Color)
{
GLint post;
GLubyte fr,fg,fb;
if(x >= X_SIZE || y >= Y_SIZE)
{
return;
}
//颜色变换 将565变成24位模式
fr = (GLubyte)((Color >> 8)|(0x07));
fg = (GLubyte)((Color >> 3)|(0x03));
fb = (GLubyte)((Color << 3));
//坐标变换 ImageDataP坐标是:按笛卡尔坐标系的第一象限,x=[0 ... X_SIZE-1] y=[0 ... Y_SIZE-1]
post = (Y_SIZE-1-y)*X_SIZE + x;
post *= 3;
//向缓冲区写入一个点的颜色
ImageDataP[post] = fb; //B
ImageDataP[post+1] = fg; //G
ImageDataP[post+2] = fr; //R
}
//************************************************************************************************************
void static GL_Display(void); //显示处理回调函数
void static GL_Idle(void); //CPU空闲时自动回调用的函数
//------------------------------------------------------------------------------------------------------------
//函数功能:GL任务
void GL_TaskStart (void)
{
#if OS_TASK_STAT_EN > 0
OSStatInit(); // Determine CPU capacity
#endif
//计算图像大小
ImagelLength = ImageWidth * 3; //每个像素3字节
while( ImagelLength % 4 != 0 ) //修正LineLength使其为4的倍数
{
++ImagelLength; //每一行像素数据的长度若不是4的倍数,则填充一些数据使它是4的倍数
}
ImagelLength *= ImageHeight; //PixelLength = ImageWidth*3*ImageHeight
//申请图像缓冲区
ImageDataP = (GLubyte*)malloc(ImagelLength);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE); //修改了参数为GLUT_DOUBLE
glutInitWindowPosition(100, 100);
glutInitWindowSize(X_SIZE, 320);
glutCreateWindow("华丽的显示屏"); //窗口标题
glutDisplayFunc(&GL_Display);
glutIdleFunc(&GL_Idle); //新加入了这句
glutMainLoop();
free(ImageDataP);
while (1)
{
printf("err : GL_TaskStart is backup");
OSTimeDlyHMSM(0, 0, 2, 0);
}
}
void static GL_Display(void)
{
//glClear(GL_COLOR_BUFFER_BIT); //可以不要清屏,因为每次绘制时,画面都覆盖整个屏幕
//绘制像素
glDrawPixels(ImageWidth, ImageHeight,GL_BGR_EXT,GL_UNSIGNED_BYTE,ImageDataP);
//完成绘制
glutSwapBuffers();
}
void static GL_Idle(void)
{
//在CPU空闲时调用. 在这里可以添加程序,使扫描速度降低。
GL_Display();
}