⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 3dtesting._c

📁 3D LCD for running graphical lCDs
💻 _C
字号:
#include <iom128v.h>
#include "avr.h"
#include "math.h"

#define OFFSETX  63             	
#define OFFSETY  31              
#define OFFSETZ  31

/////////////////////////////////立方体加文字//////////////////////////////////////////////////


//////////////////////////////////////////////////////////////////////////////////////////////

/////////////////////////////////立方体加文字//////////////////////////////////////////////////
const signed int aa[23]={8,-8,-8,8,8,-8,-8,8,8,8,8,8,8,0,4,-4,-8,-8,-8,-8,-8,-8,-8};	// x data for shape vertex
const signed int bb[23]={8,8,-8,-8,8,8,-8,-8,0,-4,4,-2,2,8,8,8,4,4,4,-4,-4,-4,0};       // y data for shape vertex
const signed int cc[23]={-8,-8,-8,-8,8,8,8,8,6,-6,-6,0,0,-6,6,6,-6,0,6,6,0,-6,0};       // z data for shape vertex

const int ff[22]={1,2,3,4,5,6,7,8,1,2,3,4,9,9,12,14,14,17,19,20,21,22};                 // start vertex for lines
const int gg[22]={2,3,4,1,6,7,8,5,5,6,7,8,10,11,13,15,16,19,20,21,18,23};	            // end vertex for lines
//////////////////////////////////////////////////////////////////////////////////////////////

extern unsigned char displaybuffer[8][128];
int sx,sy,ex,ey;                	            // define global vars for calling graphics subroutines
float rotx=0.0;                  							   // starting amount of x rotation
float roty=0.0;                 							   // starting amount of y rotation
float rotz=0.0;                							   // starting amount of z rotation

void cube (float xpos,float ypos,float zpos);     // define the subroutines this one is the actual cube routine

/***************************************
*  FUNCTION NAME: cube
*  DESCRIPTION: 3d-cube显示
/***************************************/
void cube(float xpos,float ypos,float zpos)             		 		 	  	  	 	  // routine to draw and calc 3d cube
{
    int newx[23];                   				   		   	  // translated screen x co-ordinates for vertex
 	int newy[23];                   		           			  // translated screen y co-ordinates for vertex
 	int i,loop;                    				   			      // temp variable for loops
 	int vertex;
 	float xt,yt,zt,x,y,z,sinax,cosax,sinay,cosay,sinaz,cosaz;     // lots of work variables
 	//float xpos=0.0;											      // position for object in 3d space, in x
 	//float ypos=0.0;											      // y
 	//float zpos=0.0;											      // and z values
 	//float rotx=0.0;                  							  // starting amount of x rotation
 	//float roty=0.0;                 							  // starting amount of y rotation
 	//float rotz=0.0;               							      // starting amount of z rotation

  	xpos=xpos+0.0;										   // move the object
    ypos=ypos+0.0;										   // it would wander off screen
  	zpos=zpos+0.0;										   // really quick, so leave it centered

  	rotx=rotx+0.02;                						   // rotate the cube on X axis
  	roty=roty+0.03;                						   // and on its y axis
  	rotz=rotz+0.01;                			 		   // dont bother with z or it gets confusing

  	sinax=sin(rotx);									   // precalculate the sin and cos values
  	cosax=cos(rotx);									   // for the rotation as this saves a 
  
  	sinay=sin(roty);									   // little time when running as we
  	cosay=cos(roty);									   // call sin and cos less often
  
  	sinaz=sin(rotz);									   // they are slow routines
  	cosaz=cos(rotz);									   // and we dont want slow!

   	for (i=0; i<23; i++)           							   // translate 3d vertex position to 2d screen position
    {
        x=aa[i];                							   // get x for vertex i
        y=bb[i];                							   // get y for vertex i
        z=cc[i];                	   						   // get z for vertex i

        yt = y * cosax - z * sinax;							   // rotate around the x axis
        zt = y * sinax + z * cosax;							   // using the Y and Z for the rotation
        y = yt;
        z = zt;

        xt = x * cosay - z * sinay;							   // rotate around the Y axis
        zt = x * sinay + z * cosay;							   // using X and Z
        x = xt;
        z = zt;

        xt = x * cosaz - y * sinaz;						  	   // finaly rotate around the Z axis
        yt = x * sinaz + y * cosaz;						   	   // using X and Y
        x = xt;
        y = yt;

        x=x+xpos;											   // add the object position offset
        y=y+ypos;											   // for both x and y
        z=z+OFFSETZ-zpos;									   // as well as Z

        newx[i]=(x*64/z)+OFFSETX;							   // translate 3d to 2d coordinates for screen
        newy[i]=(y*64/z)+OFFSETY;							   // drawing so we can see the cube
	 }

    for (i=0; i<22; i++)									   // draw the lines that make up the object
    {
       vertex=ff[i]-1;         							   // temp = start vertex for this line
       sx=newx[vertex];        							   // set line start x to vertex[i] x position
       sy=newy[vertex];        							   // set line start y to vertex[i] y position
       vertex=gg[i]-1;         							   // temp = end vertex for this line
       ex=newx[vertex];									   // set line end x to vertex[i+1] x position
       ey=newy[vertex];									   // set line end y to vertex[i+1] y position
	   if(sx>255 | sy>255 | ex>255 | ey>255)
	   {
	       sx=255;
		   sy=255;
		   ex=255;
		   ey=255;
	   }
	   DisplyLine(sx, sy, ex, ey, 1);
    }
	   LcdDsiplayRefresh();
	   GuiDisStringF3(0,7,"MEGA128 16MHz  128X64",1);
	   //Delayms(20);			// delay for a while to allow looking at the cube
       //ClrDisplyBuffer();            	// clear the screen to remove old cube
	for (i=0; i<22; i++)		
    {
       vertex=ff[i]-1;      
       sx=newx[vertex];       
       sy=newy[vertex];  
       vertex=gg[i]-1;      
       ex=newx[vertex];	
       ey=newy[vertex];
	   if(sx>255 | sy>255 | ex>255 | ey>255)
	   {
	       sx=255;
		   sy=255;
		   ex=255;
		   ey=255;
	   }
	   DisplyLine(sx, sy, ex, ey, 0);
    }
 }
 
/***************************************
*  FUNCTION NAME: WriteDot
*  DESCRIPTION:
/***************************************/
void WriteDot(unsigned char x,unsigned char y,unsigned char color)
{
    unsigned char temp1,temp2,a;
	if(x>127 | y>63)
	    return;
	temp1 = y / 8;			  	 			   /* GET VALUE OF WHICH PAGE */
    temp2 = y % 8;                             /* GET VALUE OF WHICH Y-OFFSET */
	a = displaybuffer[temp1][x];			       /* READ NOW SEGMENT VALUE */
	
	if(color == 0)						           /* DOT ON/OFF */
		a&=~(1<<temp2);
	else
	    a|=(1<<temp2);
		
   displaybuffer[temp1][x] = a;                     /* WRITE SEGMENT */
}

/***************************************
*  FUNCTION NAME: DisplyLine
*  DESCRIPTION: 
/***************************************/
void DisplyLine(unsigned char x1,unsigned char y1,unsigned char x2,unsigned char y2,unsigned char color)
{
    unsigned char temp;
	int p;	   		 	 		 	 		 /* p-取值判断因子 */
	unsigned char x;						 /* x-坐标 */
	unsigned char y;						 /* y-坐标 */
	int dx;						             /* x方向差值 */
	int dy;						             /* y方向差值 */
	char signx;	  							 /* x方向标识 */
	char signy;								 /* Y方向标识 */
	if(x1>123 | y1>63 | x2>123 | y2>63)
	    return;
	x = x1;
	y = y1;
	 /* 1.计算X方向的参数 */
	if(x2 > x1)								
	{
	    dx = x2 - x1;	
		signx = 1;					 
	}
	else if(x2 < x1)
	{
	    dx = x1 - x2;	
		signx = -1;					 
	}
	else
	{
	    dx = 0;	
		signx = 0;					 
	}
	 /* 2.计算Y方向的参数 */
	if(y2 > y1)								
	{
	    dy = y2 - y1;	
		signy = 1;					 
	}
	else if(y2 < y1)
	{
	    dy = y1 - y2;	
		signy = -1;					 
	}
	else
	{
	    dy = 0;	
		signy = 0;
	}
	/* 3.计算首个判断因子 */
	p = 2*dy -dx;	
    /* 4a.以X方向增长画图 */				 		 
	if(dx >= dy)								
	{
		for(temp=0; temp<dx; temp++)
		{
	         WriteDot(x,y,color);		/* 画点 */
			if(p >= 0)						/* 根据P来判断画哪一点 */
			{
			    x = x + signx;
				y = y + signy;
				p = p + 2*(dy - dx);		/* dy,dx需要定义定义成有符号的,才能得到正确的值 */
			}
			else
			{
				x = x + signx;
				y = y;
				p = p + 2*dy;
			}
		}
    }
	else								
	{
		for(temp=0; temp<dy; temp++)
		{
	        WriteDot(x,y,color);		/* 画点 */
			if(p >= 0)						/* 根据P来判断画哪一点 */
			{
			    y = y + signy;
				x = x + signx;
				p = p + 2*(dx - dy);
			}
			else
			{
				y = y + signy;
				x = x;
				p = p + 2*dx;
			}
		}
    }
     WriteDot(x2,y2,color);		         /* 画最后一个点 */
    //LcdDsiplayRefresh();
}

/***************************************
*  FUNCTION NAME: ClrDisplyBuffer
*  DESCRIPTION: 
/***************************************/
void ClrDisplyBuffer(void)
{
    unsigned char temp1,temp2;
	for(temp1=0; temp1<8; temp1++)
	{
	    for(temp2=0; temp2<128; temp2++)
		{
		    displaybuffer[temp1][temp2] = 0x00;	
		}
	}
}

/***************************************
*  FUNCTION NAME: Delayms
*  DESCRIPTION:
/***************************************/
 void Delayms(unsigned int time)
{
    unsigned int temp;   
	unsigned int i;
	for(i=0;i<time;i++)
	{     
       for(temp=10; temp>0;temp--)              /* Delay About 1ms */
       {
         NOP;                                 
	   }
	}
}


void action(void)
{
    float temp;
	unsigned char temp1;

    for(temp1=0; temp1<50; temp1++)
		{
		     cube(0.0,0.0,15.0);
		}
		
	    
		for(temp=15.0; temp>0.0; temp=temp-0.5)
		{
	         cube(0.0,0.0,temp);
		}
		

		for(temp1=0; temp1<200; temp1++)
		{
		     cube(0.0,0.0,0.0);
		}
		
        for(temp=0.0; temp>-20.0; temp=temp-0.5)
		{
			 cube(0.0,0.0,temp);
		}
		
		for(temp1=0; temp1<50; temp1++)
		{
		     cube(0.0,0.0,-20.0);
		}  
		
		for(temp=-20.0; temp<0; temp=temp+0.5)
		{
			 cube(0.0,0.0,temp);
		}
		
		for(temp1=0; temp1<200; temp1++)
		{
		     cube(0.0,0.0,0.0);
		}  
		
	    for(temp=0.0; temp>-40.0; temp=temp-0.5)
		{
			 cube(temp,0.0,0.0);
		}
		
	    for(temp=40.0; temp>0.0; temp=temp-0.5)
		{
			 cube(temp,0.0,0.0);
		}
		
	    for(temp1=0; temp1<30; temp1++)
		{
		     cube(0.0,0.0,0.0);
		} 
		
	   for(temp=0.0; temp>-20.0; temp=temp-0.5)
		{
			 cube(0.0,temp,0.0);
		}
		
	    for(temp=20.0; temp>0.0; temp=temp-0.5)
		{
			 cube(0.0,temp,0.0);
		}
		
        for(temp1=0; temp1<80; temp1++)
		{
		     cube(0.0,0.0,0.0);
		} 
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -