« 收到 La Fonera 了 解決 CRT 螢幕玩遊戲會閃爍 »

一月16

OpenGL-海龜作圖法

tags , | 1,893 views


海龜作圖法(Turtle Graphics),意思是模仿海龜移動的方式,只能前進及左右轉,而將海龜的尾巴當作畫筆,可以放下及抬起。
本範例為二維空間,使用 OpenGL 搭配 C++ 實做。

程式功能介紹:
1、由 turtle.txt 檔案讀入作圖指令。
2、INIT - 設定海龜初始位置。(x 軸, y 軸, 龜頭角度)
3、PEN - 設定尾巴狀態。(0 抬起 / 1放下)
4、FW - 海龜向前移動。(移動長度)
5、RIGHT - 海龜右轉角度。(角度)
6、LEFT - 海龜左轉角度。(角度)

#include <iostream>
#include <fstream>
#include <cstring>
#include <cmath>
#include <GL/glut.h>
using namespace std;
 
const GLfloat PI = 3.141593;
 
class Turtle {
private:
    
bool penStatus, a;
    
GLfloat x1, y1, x2, y2; //座標
    
GLfloat cAngle; //角度
public:
    
Turtle() //constractor
    
{
        
penStatus = 0;
        
x1 = y1 = x2 = y2 = 0.0;
        
cAngle = 0.0;
        
a = 0;
    
}
 
    
/* 設定海龜位置 */
    
void InitPoint(GLfloat x, GLfloat y, GLfloat angle)
    
{
        
x1 = x; y1 = y; cAngle = angle; a = 1;
        
if (penStatus) //如果 pen down 應該畫出一點
        
{
            
glBegin(GL_POINTS);
                
glVertex2f(x, y);
            
glEnd();
        
}
    
}
    
/* 海龜向前移動 */
    
void forward(GLfloat L)
    
{
        
x2 = x1 + L * cos(cAngle*PI/180);
        
y2 = y1 + L * sin(cAngle*PI/180);
 
        
if (penStatus)
        
{
            
glBegin(GL_LINES);
                
glVertex2f(x1, y1);
                
glVertex2f(x2, y2);
            
glEnd();
        
}
        
        
x1 = x2; y1 = y2;
    
}
    
/* 海龜右轉角度 */
    
void right(GLfloat angle)
    
{
        
cAngle = (a) ? cAngle - angle : angle;
        
a = 1;
    
}
    
/* 海龜左轉角度 */
    
void left(GLfloat angle)
    
{
        
cAngle = (a) ? cAngle + angle : angle;
        
a = 1;
    
}
    
/* 設定畫筆狀態(0 up/1 down) */
    
void setPen(bool value)
    
{
        
penStatus = value;
        
if (value) //pen down 畫出一點
        
{
            
glBegin(GL_POINTS);
                
glVertex2f(x1, y1);
            
glEnd();
        
}
    
}
 
};
 
void display(void)
{
    
glClear(GL_COLOR_BUFFER_BIT)/*clear the window */
 
    
char cmd[10];
    
float x, y, angle;
    
bool p;
    
ifstream fp("turtle.txt");
    
Turtle t;
 
    
while (!fp.eof())
    
{
        
fp >> cmd;
        
if (!strcmp(cmd, "INIT")) {
            
fp >> x >> y >> angle;
            
t.InitPoint(x, y, angle);
        
} else if (!strcmp(cmd, "FW")) {
            
fp >> x;
            
t.forward(x);
        
} else if (!strcmp(cmd, "RIGHT")) {
            
fp >> x;
            
t.right(x);
        
} else if (!strcmp(cmd, "LEFT")) {
            
fp >> x;
            
t.left(x);
        
} else if (!strcmp(cmd, "PEN")) {
            
fp >> p;
            
t.setPen(p);
        
}
    
}
 
    
glFlush(); /* clear buffers */
}
 
int main(int argc, char** argv)
{
 
/* Standard GLUT initialization */
    
glutInit(&argc, argv);
    
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); /* default, not needed */
    
glutInitWindowSize(500, 500); /* 500 x 500 pixel window */
    
glutInitWindowPosition(0, 0); /* place window top left on display */
    
glutCreateWindow("Turtle Graphics"); /* window title */
    
glutDisplayFunc(display); /* display callback invoked when window opened */
 
/* attributes */
    
glClearColor(1.0, 1.0, 1.0, 1.0); /* white background */
    
glColor3f(1.0, 0.0, 0.0); /* draw in red */
/* set up viewing */
/* 500 x 500 window with origin lower left */
    
glMatrixMode(GL_PROJECTION);
    
glLoadIdentity();
    
gluOrtho2D(0.0, 500.0, 0.0, 500.0);
    
glMatrixMode(GL_MODELVIEW);
 
    
glutMainLoop(); /* enter event loop */
 
    
return 0;
}

turtle.txt

Trackback:

One Comment on “OpenGL-海龜作圖法”

  1. http://yrchen.OrangeStudio.org/blog/archives/361

    [...] 憂藍夢境‧部落格 » Blog Archive » OpenGL-海龜作圖法 (tags: Tech Programming OpenGL TurtleGraphics Guide) [...]

Leave a Reply