2022年3月28日 星期一

09163033_week06

 week 06_01:  Watch the inventor of computer sketchpad

https://www.youtube.com/watch?v=6orsmFndx_o

week 06_01: Go to jsyeh.org/3dcg10 to download windows.zip and data.zip

week 06_01:

#include <stdio.h>

#include <GL/glut.h>


void display()

{

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

        glColor3f(1,1,0);

        glutSolidTeapot(0.3);

    glutSwapBuffers();

}


void keyboard(unsigned char key, int x, int y)

{

    printf("現在安下:%c 座標在: %d %d\n", key, x, y);

}

int main(int argc, char**argv)

{

    glutInit(&argc, argv);

    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);

    glutCreateWindow("week 06 keyboard");


    glutDisplayFunc(display);

    glutKeyboardFunc(keyboard);


    glutMainLoop();

}




week 06_02 Keyboard and mouse

#include <stdio.h>
#include <GL/glut.h>

void display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
        glColor3f(1,1,0);
        glutSolidTeapot(0.3);
    glutSwapBuffers();
}

void keyboard(unsigned char key, int x, int y)
{
    printf("現在安下:%c 座標在: %d %d\n", key, x, y);
}


void mouse(int button, int state, int x, int y)
{
    
}
void motion(int x, int y)
{
    
}
int main(int argc, char**argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("week 06 keyboard");

    glutDisplayFunc(display);
    glutKeyboardFunc(keyboard);

    glutMainLoop();
}


week 06 step 2_02

#include <stdio.h>
#include <GL/glut.h>

float x=0, y=0, z=0, oldX, oldY;

void display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glPushMatrix();
        glTranslatef( (x-150)/150.0 , -(y-150)/150.0 , z );
        glColor3f(1,1,0);
        glutSolidTeapot(0.3);
    glPopMatrix();
    glutSwapBuffers();
}

void keyboard(unsigned char key, int mouseX, int mouseY)
{
    printf("現在安下:%c 座標在: %d %d\n", key, mouseX, mouseY);
}
void mouse(int button, int state, int mouseX, int mouseY)
{
    oldX = mouseX; oldY = mouseY;
}
void motion(int mouseX, int mouseY)
{
    x += (mouseX - oldX);
    y += (mouseY-oldY);
    oldX = mouseX; oldY = mouseY;
    display();
}
int main(int argc, char**argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("week 06 keyboard mouse motion");

    glutDisplayFunc(display);
    glutKeyboardFunc(keyboard);
    glutMouseFunc(mouse);
    glutMotionFunc(motion);

    glutMainLoop();
}


week 06 step 02_03

#include <stdio.h>
#include <GL/glut.h>

float x=0, y=0, z=0, oldX, oldY, scale=1.0;

void display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glPushMatrix();
        //glTranslatef( (x-150)/150.0 , -(y-150)/150.0 , z );
        glScalef(scale, scale, scale);
        glColor3f(1,1,0);
        glutSolidTeapot(0.3);
    glPopMatrix();
    glutSwapBuffers();
}

void keyboard(unsigned char key, int mouseX, int mouseY)
{
    printf("現在安下:%c 座標在: %d %d\n", key, mouseX, mouseY);
}
void mouse(int button, int state, int mouseX, int mouseY)
{
    oldX = mouseX; oldY = mouseY;
}
void motion(int mouseX, int mouseY)
{
    //x += (mouseX - oldX);
    //y += (mouseY-oldY);
    if(mouseX>oldX) scale = scale * 1.01;
    if(mouseX<oldX) scale = scale * 0.99;
    oldX = mouseX; oldY = mouseY;
    display();
}
int main(int argc, char**argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("week 06 keyboard mouse motion");

    glutDisplayFunc(display);
    glutKeyboardFunc(keyboard);
    glutMouseFunc(mouse);
    glutMotionFunc(motion);

    glutMainLoop();
}


week 06 step 3-1

#include <stdio.h>
#include <GL/glut.h>

float x=0, y=0, z=0, oldX, oldY, scale=1.0, angle = 0.0;
int now=1;

void display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glPushMatrix();
        glTranslatef( (x-150)/150.0 , -(y-150)/150.0 , z );
        glRotatef( angle, 0,0,1);
        glScalef(scale, scale, scale);
        glColor3f(1,1,0);
        glutSolidTeapot(0.3);
    glPopMatrix();
    glutSwapBuffers();
}

void keyboard(unsigned char key, int mouseX, int mouseY)
{
    //printf("現在安下:%c 座標在: %d %d\n", key, mouseX, mouseY);
    if(key=='w' || key=='W') now=1;
    if(key=='e' || key=='E') now=2;
    if(key=='r' || key=='R') now=3;
}
void mouse(int button, int state, int mouseX, int mouseY)
{
    oldX = mouseX; oldY = mouseY;
}
void motion(int mouseX, int mouseY)
{
    if(now==1) {
    x += (mouseX - oldX);
    y += (mouseY-oldY); }

    else if(now==3) {
    if(mouseX>oldX) scale = scale * 1.01;
    if(mouseX<oldX) scale = scale * 0.99; }
    oldX = mouseX; oldY = mouseY;
    display();
}
int main(int argc, char**argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("week 06 keyboard mouse motion");

    glutDisplayFunc(display);
    glutKeyboardFunc(keyboard);
    glutMouseFunc(mouse);
    glutMotionFunc(motion);

    glutMainLoop();
}






沒有留言:

張貼留言