2022年5月17日 星期二

Kuo's Graphic_Note_Week13

Step 1 : Using CodeBlocks to make Rectangle

    1-1. Coding (Make a white rectangle)

    1-2. Build & Run

Code:

#include <GL/glut.h>

void display(){
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glColor3f(1,1,1);
    glRectf(0.5, 0.5, -0.5, -0.5);
    glutSwapBuffers();
}
int main(int ac,char**av,char**ev){
    glutInit(&ac,av);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    glutInitWindowSize(600,600);
    glutCreateWindow("Week13 Rect");
    glutDisplayFunc(display);
    glutMainLoop();
}


    1-3. Coding



    1-4. Build & Run

Code:

#include <GL/glut.h>

void display(){
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glColor3f(1,1,1);///white
    glRectf(0.5, 0.5, -0.5, -0.5);///body
    glPushMatrix();
      //  glTranslatef(0.5,0.5,0);
      //  glRotatef(angle,0,0,1);
      // glTranslatef(-0.5,-0.4,0);
        glColor3f(1,0,0);///red arms
        glRectf(0.5,0.5,1.0,0.3);///arms
    glPopMatrix();
    glutSwapBuffers();
}
int main(int ac,char**av,char**ev){
    glutInit(&ac,av);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    glutInitWindowSize(600,600);
    glutCreateWindow("Week13 Rect");
    glutDisplayFunc(display);
    glutMainLoop();
}


     1-5. Coding



    1-6. Build & Run



     1-7. How to move


     1-8. Coordinate


Code:

#include <GL/glut.h>

void display(){
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glColor3f(1,1,1);///white
    glRectf(0.5, 0.5, -0.5, -0.5);///body
    glPushMatrix();
      //  glTranslatef(0.5,0.5,0);
      //  glRotatef(angle,0,0,1);
        glTranslatef(-0.5,-0.4,0);///Translate Rotating point to O(0,0)
        glColor3f(1,0,0);///red arms
        glRectf(0.5,0.5,1.0,0.3);///arms
    glPopMatrix();
    glutSwapBuffers();
}
int main(int ac,char**av,char**ev){
    glutInit(&ac,av);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    glutInitWindowSize(600,600);
    glutCreateWindow("Week13 Rect");
    glutDisplayFunc(display);
    glutMainLoop();
}

     1-9. Coding


     1-10. Build & Run




     1-11. Coordinate

Code:

#include <GL/glut.h>
float angle=45;
void display(){
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glColor3f(1,1,1);///white
    glRectf(0.5, 0.5, -0.5, -0.5);///body
    glPushMatrix();
        glTranslatef(0.5,0.5,0);///hanging on (0.5,0.5)
        glRotatef(angle,0,0,1);///Rotate
        glTranslatef(-0.5,-0.4,0);///Translate Rotating point to O(0,0)
        glColor3f(1,0,0);///red arms
        glRectf(0.5,0.5,1.0,0.3);///arms
    glPopMatrix();
    glutSwapBuffers();
}
int main(int ac,char**av,char**ev){
    glutInit(&ac,av);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    glutInitWindowSize(600,600);
    glutCreateWindow("Week13 Rect");
    glutDisplayFunc(display);
    glutMainLoop();
}

     1-12. Coding


     1-13. Build & Run and Coordinate

Code:
#include <GL/glut.h>
float angle=45;
void display(){
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glColor3f(1,1,1);///white
    glRectf(0.5, 0.5, -0.5, -0.5);///body
    glPushMatrix();
        glTranslatef(0.5,0.5,0);///hanging on (0.5,0.5)
        glRotatef(angle,0,0,1);///Rotate
        glTranslatef(-0.5,-0.4,0);///Translate Rotating point to O(0,0)
        glColor3f(1,0,0);///red arms
        glRectf(0.5,0.5,1.0,0.3);///arms
    glPopMatrix();
    glutSwapBuffers();
}
int main(int ac,char**av,char**ev){
    glutInit(&ac,av);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    glutInitWindowSize(600,600);
    glutCreateWindow("Week13 Rect");
    glutDisplayFunc(display);
    glutMainLoop();
}
     

    1-14. Coding



    1-15. Build & Run



Step 2 : Using CodeBlocks to build a model

    2-1. Coding



    2-2. Build & Run

Code:

#include <GL/glut.h>
float angle=45,oldX=0;

void mouse(int button,int state, int x, int y){
    oldX=x;
}
void motion(int x,int y){
    angle+=(x-oldX);
    oldX=x;
    glutPostRedisplay();
}
void display(){
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glColor3f(1,1,1);///white
    glRectf(0.3, 0.5, -0.3, -0.2);///body
    glPushMatrix();
        glTranslatef(0.3,0.5,0);///hanging on (0.5,0.5)
        glRotatef(angle,0,0,1);///Rotate
        glTranslatef(-0.3,-0.4,0);///Translate Rotating point to O(0,0)
        glColor3f(1,0,0);///red arms
        glRectf(0.3,0.5,0.8,0.3);///arms
    glPopMatrix();

    glutSwapBuffers();
}
int main(int ac,char**av,char**ev){
    glutInit(&ac,av);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    glutInitWindowSize(600,600);
    glutCreateWindow("Week13 Rect");
    glutMouseFunc(mouse);
    glutMotionFunc(motion);
    glutDisplayFunc(display);
    glutMainLoop();
}

    2-3. Coding


    2-4. Build & Run

Code:

#include <GL/glut.h>
float angle=0,oldX=0;

void mouse(int button,int state, int x, int y){
    oldX=x;
}
void motion(int x,int y){
    angle+=(x-oldX);
    oldX=x;
    glutPostRedisplay();
}
void display(){
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glColor3f(1,1,1);///white
    glRectf(0.3, 0.5, -0.3, -0.2);///body
    glPushMatrix();
        glTranslatef(0.3,0.5,0);///hanging on (0.5,0.5)
      //  glRotatef(angle,0,0,1);///Rotate
        glTranslatef(-0.3,-0.4,0);///Translate Rotating point to O(0,0)
        glColor3f(1,0,0);///red arms
        glRectf(0.3,0.5,0.8,0.3);///arms
        glPushMatrix();
            glColor3f(0,1,0);///green
            glRectf(0.8,0.5,1.1,0.3);
        glPopMatrix();
    glPopMatrix();

    glutSwapBuffers();
}
int main(int ac,char**av,char**ev){
    glutInit(&ac,av);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    glutInitWindowSize(600,600);
    glutCreateWindow("Week13 Rect");
    glutMouseFunc(mouse);
    glutMotionFunc(motion);
    glutDisplayFunc(display);
    glutMainLoop();
}

    2-5. Coding


    2-6. Build & Run


Code:
#include <GL/glut.h>
float angle=0,oldX=0;

void mouse(int button,int state, int x, int y){
    oldX=x;
}
void motion(int x,int y){
    angle+=(x-oldX);
    oldX=x;
    glutPostRedisplay();
}
void display(){
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glColor3f(1,1,1);///white
    glRectf(0.3, 0.5, -0.3, -0.2);///body
    glPushMatrix();
        glTranslatef(0.3,0.5,0);///hanging on (0.5,0.5)
      //  glRotatef(angle,0,0,1);///Rotate
        glTranslatef(-0.3,-0.4,0);///Translate Rotating point to O(0,0)
        glColor3f(1,0,0);///red arms
        glRectf(0.3,0.5,0.8,0.3);///arms
        glPushMatrix();
            glTranslatef(0.8,0.4,0);
            glRotatef(angle,0,0,1);
            glTranslatef(-0.8,-0.4,0);
            glColor3f(0,1,0);///green
            glRectf(0.8,0.5,1.1,0.3);
        glPopMatrix();
    glPopMatrix();

    glutSwapBuffers();
}
int main(int ac,char**av,char**ev){
    glutInit(&ac,av);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    glutInitWindowSize(600,600);
    glutCreateWindow("Week13 Rect");
    glutMouseFunc(mouse);
    glutMotionFunc(motion);
    glutDisplayFunc(display);
    glutMainLoop();
}

    2-7. Coding



    2-8. Build & Run

Final Code:

#include <GL/glut.h>
float angle=0,oldX=0;

void mouse(int button,int state, int x, int y){
    oldX=x;
}
void motion(int x,int y){
    angle+=(x-oldX);
    oldX=x;
    glutPostRedisplay();
}
void display(){
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glColor3f(1,1,1);///white
    glRectf(0.3, 0.5, -0.3, -0.2);///body
    glPushMatrix();
        glTranslatef(0.3,0.5,0);///hanging on (0.5,0.5)
        glRotatef(angle,0,0,1);///Rotate
        glTranslatef(-0.3,-0.4,0);///Translate Rotating point to O(0,0)
        glColor3f(1,0,0);///red arms
        glRectf(0.3,0.5,0.8,0.3);///arms
        glPushMatrix();
            glTranslatef(0.8,0.4,0);
            glRotatef(angle,0,0,1);
            glTranslatef(-0.8,-0.4,0);
            glColor3f(0,1,0);///green
            glRectf(0.8,0.5,1.1,0.3);
        glPopMatrix();
    glPopMatrix();

    glutSwapBuffers();
}
int main(int ac,char**av,char**ev){
    glutInit(&ac,av);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    glutInitWindowSize(600,600);
    glutCreateWindow("Week13 Rect");
    glutMouseFunc(mouse);
    glutMotionFunc(motion);
    glutDisplayFunc(display);
    glutMainLoop();
}

Step 3 : Using CodeBlocks to make Rectangle


    3-1. Coding



    3-2. Build & Run

Code:

#include <GL/glut.h>
float angle=0,oldX=0;

void mouse(int button,int state, int x, int y){
    oldX=x;
}
void motion(int x,int y){
    angle+=(x-oldX);
    oldX=x;
    glutPostRedisplay();
}
void display(){
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glColor3f(1,1,1);///white
    glRectf(0.3, 0.5, -0.3, -0.2);///body
    glPushMatrix();
        glTranslatef(0.3,0.5,0);///hanging on (0.5,0.5)
        glRotatef(angle,0,0,1);///Rotate
        glTranslatef(-0.3,-0.4,0);///Translate Rotating point to O(0,0)
        glColor3f(1,0,0);///red arms
        glRectf(0.3,0.5,0.8,0.3);///arms
        glPushMatrix();
            glTranslatef(0.8,0.4,0);
            glRotatef(angle,0,0,1);
            glTranslatef(-0.8,-0.4,0);
            glColor3f(0,1,0);///green
            glRectf(0.8,0.5,1.1,0.3);
        glPopMatrix();
    glPopMatrix();
    glPushMatrix();
        glTranslatef(-0.3,0.5,0);///hanging on (0.5,0.5)
        glRotatef(angle,0,0,1);///Rotate
        glTranslatef(0.3,-0.4,0);///Translate Rotating point to O(0,0)
        glColor3f(1,0,0);///red arms
        glRectf(-0.3,0.5,-0.8,0.3);///arms
        glPushMatrix();
            glTranslatef(-0.8,0.4,0);
            glRotatef(angle,0,0,1);
            glTranslatef(0.8,-0.4,0);
            glColor3f(0,1,0);///green
            glRectf(-0.8,0.5,-1.1,0.3);
        glPopMatrix();
    glPopMatrix();

    glutSwapBuffers();
}
int main(int ac,char**av,char**ev){
    glutInit(&ac,av);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    glutInitWindowSize(600,600);
    glutCreateWindow("Week13 Rect");
    glutMouseFunc(mouse);
    glutMotionFunc(motion);
    glutDisplayFunc(display);
    glutMainLoop();
}

    3-3. Coding


    3-4. Build & Run

Final Code:

#include <GL/glut.h>
float angle[20],oldX=0;
int angleID=0;
void keyboard(unsigned char key,int x,int y){
    if(key=='0')angleID=0;
    if(key=='1')angleID=1;
    if(key=='2')angleID=2;
    if(key=='3')angleID=3;
}
void mouse(int button,int state, int x, int y){
    oldX=x;
}
void motion(int x,int y){
    angle[angleID]+=(x-oldX);
    oldX=x;
    glutPostRedisplay();
}
void display(){
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glColor3f(1,1,1);///white
    glRectf(0.3, 0.5, -0.3, -0.2);///body
    glPushMatrix();
        glTranslatef(0.3,0.5,0);///hanging on (0.5,0.5)
        glRotatef(angle[0],0,0,1);///Rotate
        glTranslatef(-0.3,-0.4,0);///Translate Rotating point to O(0,0)
        glColor3f(1,0,0);///red arms
        glRectf(0.3,0.5,0.8,0.3);///arms
        glPushMatrix();
            glTranslatef(0.8,0.4,0);
            glRotatef(angle[1],0,0,1);
            glTranslatef(-0.8,-0.4,0);
            glColor3f(0,1,0);///green
            glRectf(0.8,0.5,1.1,0.3);
        glPopMatrix();
    glPopMatrix();
    glPushMatrix();
        glTranslatef(-0.3,0.5,0);///hanging on (0.5,0.5)
        glRotatef(angle[2],0,0,1);///Rotate
        glTranslatef(0.3,-0.4,0);///Translate Rotating point to O(0,0)
        glColor3f(1,0,0);///red arms
        glRectf(-0.3,0.5,-0.8,0.3);///arms
        glPushMatrix();
            glTranslatef(-0.8,0.4,0);
            glRotatef(angle[3],0,0,1);
            glTranslatef(0.8,-0.4,0);
            glColor3f(0,1,0);///green
            glRectf(-0.8,0.5,-1.1,0.3);
        glPopMatrix();
    glPopMatrix();

    glutSwapBuffers();
}
int main(int ac,char**av,char**ev){
    glutInit(&ac,av);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    glutInitWindowSize(600,600);
    glutCreateWindow("Week13 Rect");
    glutKeyboardFunc(keyboard);
    glutMouseFunc(mouse);
    glutMotionFunc(motion);
    glutDisplayFunc(display);
    glutMainLoop();
}




















沒有留言:

張貼留言