Assignment 2 使用OpenGL畫安卓機器人

來源:互聯網
上載者:User

標籤:

一. 實現簡述

Assignment 2 Report

目標:畫一個安卓機器人。

代碼結構:在 glutDisplayFunc(drawRobot)中的參數 drawRobot 函數是實 現畫機器人的最外層函數,其中包括畫臉、畫身體、畫手和畫腳。每個函數又繼 續細分畫的步驟和方法。如所示:

二. 心得體會
這次任務讓我對 OpenGl 有了初步認識。通過閱讀課本和網上的資料,掌握

使用 OpenGl 的準系統,比如如何畫基本的點、線和多邊形。並理解了一些之前沒明白的重要概念,比如 glLoadIdentity 和 glPushMatrix、glPopMatrix 的區別。根據我現在的理解,glLoadIdentity 是將矩陣堆棧中棧頂的矩陣置為單位矩陣,相當於座標原點回到一開始的設定,也就忽略了之前平移、旋轉等導致座標原點位置的改變。而 glPushMaxtrix 和 glPopMatrix 則是用於將當前矩陣壓入棧中,然後執行一些變換,這些變換是基於之前已經變換過的座標,最後再出棧,回到入棧前的座標。

接下來我將繼續學習 OpenGL 更多強大的功能。

 代碼如下:
#include <gl/glut.h>#include <math.h>int i;const GLfloat PI = 3.1415926536f;const int n = 1000;const GLfloat eyeR = 0.025f; //radius of eyeconst GLfloat faceR = 0.3f; //radius of faceconst GLfloat bodyR = 0.05f; //radius of the corner of bodyconst GLfloat limbR = 0.06f; //radius of limb in both endsvoid drawLimb(void) {    //draw a half circle at one end    glBegin(GL_POLYGON);    for (i = 0; i < n; i++) {        glVertex2f(limbR * cos(2 * PI / n * i), limbR * sin(2 * PI / n * i));    }    glEnd();        //draw a rectangle    glTranslatef(-0.06f, 0.0f, 0.0f);    glRectf(0.0f, 0.0f, 0.12f, -0.3f);    glEnd();        //draw a half circle at the other end    glTranslatef(0.06f, -0.3f, 0.0f);    glBegin(GL_POLYGON);    for (i = 0; i < n; i++) {        glVertex2f(limbR * cos(2 * PI / n * i), limbR * sin(2 * PI / n * i));    }    glEnd();    glFlush();}void drawHands(void){    glLoadIdentity();    glTranslatef(-0.38f, 0.2f, 0.0f);    drawLimb();    glLoadIdentity();    glTranslatef(0.38f, 0.2f, 0.0f);    drawLimb();}void drawBody(void){    glLoadIdentity();    //draw a rectangle first    glTranslatef(-0.3f, 0.25f, 0.0f);    glRectf(0.0f, 0.0f, 0.6f, -0.45f);    glEnd();        //draw a half circle at the left bottom    glTranslatef(0.05f, -0.45f, 0.0f);    glBegin(GL_POLYGON);    for (i = 0; i < n; i++) {        if (sin(2 * PI / n * i) < 0) {            glVertex2f(bodyR * cos(2 * PI / n * i), bodyR * sin(2 * PI / n * i));        }    }    glEnd();        //draw a half circle at the right bottom    glTranslatef(0.5f, 0.0f, 0.0f);    glBegin(GL_POLYGON);    for (i = 0; i < n; i++) {        if (sin(2 * PI / n * i) < 0) {            glVertex2f(bodyR * cos(2 * PI / n * i), bodyR * sin(2 * PI / n * i));        }    }    glEnd();        //draw the bottom rectangle    glRectf(0.0f, 0.0f, -0.5f, -0.05f);    glEnd();        glFlush();}void drawLegs(void){    glLoadIdentity();    glTranslatef(-0.11f, -0.08f, 0.0f);    drawLimb();        glLoadIdentity();    glTranslatef(0.11f, -0.08f, 0.0f);    drawLimb();    }void drawLine(void) {    glLineWidth(5.0f);        glBegin(GL_LINES);    glVertex2f(0.0f, 0.0f);    glVertex2f(0.0f, 0.15f);    glEnd();        glFlush();}void drawEye(void) {    glColor3f(1.0f, 1.0f, 1.0f);    glBegin(GL_POLYGON);    for (i = 0; i < n; i++) {        glVertex2f(eyeR * cos(2 * PI / n * i), eyeR * sin(2 * PI / n * i));    }    glEnd();    glColor3ub(164, 202, 57);        glFlush();}void drawFaceShape(void) {    glBegin(GL_POLYGON);    for (i = 0; i < n; i++) {        if (sin(2 * PI / n * i) >= 0) {            glVertex2f(faceR * cos(2 * PI / n * i), faceR * sin(2 * PI / n * i));        }    }    glEnd();    glFlush();}void drawFace(void){    glLoadIdentity();    glTranslatef(0.0f, 0.27f, 0.0f);    drawFaceShape();//draw the overall shape of face        //draw left eye    glLoadIdentity();    glTranslatef(-0.13f, 0.42f, 0.0f);    drawEye();        //draw right eye    glLoadIdentity();    glTranslatef(0.13f, 0.42f, 0.0f);    drawEye();        //draw left line    glLoadIdentity();    glTranslatef(-0.13f, 0.5f, 0.0f);    glRotated(30.0f, 0.0f, 0.0f, 1.0f);    drawLine();        //draw right line    glLoadIdentity();    glTranslatef(0.13f, 0.5f, 0.0f);    glRotated(-30.0f, 0.0f, 0.0f, 1.0f);    drawLine();        }void drawRobot(void){    glClearColor(1.0f, 1.0f, 1.0f, 0.0f);//set background color to white    glClear(GL_COLOR_BUFFER_BIT);    glLoadIdentity();    glColor3ub(164, 202, 57);//color of robot        drawFace();    drawBody();    drawHands();    drawLegs();}int main(int argc, char *argv[]){    glutInit(&argc, argv);    glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);    glutInitWindowPosition(100, 100);    glutInitWindowSize(400, 400);    glutCreateWindow("Android Robot");    glutDisplayFunc(drawRobot);    glutMainLoop();        return 0;}

 

Assignment 2 使用OpenGL畫安卓機器人

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.