2023年3月1日 星期三

week03 mouse

Week03

先到https://jsyeh.org/3dcg10/ 下載

1.data data.zip

2.win32 windows.zip

3.glut32.d11


解壓縮

1.data data.zip

2.win32 windows.zip

3.glut32.d11

把data裡的資料夾放到windows裡

開啟Transformation.exe
作業1

先開GLUT project,從老師上課軟體裡解壓縮檔案freelut

解壓縮後放置桌面,並將檔案複製改名,接著開啟GLUT project 

#include <GL/glut.h>
void display()
{
glutSolidTeapot( 0.3 );
glutSwapBuffers();
}

int main(int argc, char *argv[])
    glutInit(&argc, argv); 
    glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("GLUT Shapes"); 
    glutDisplayFunc(display); 
    glutMainLoop(); 
}
複製week02茶杯的程式
加入2行新的程式


#include <GL/glut.h>
void display()
{
glPushMatrix(); 備份矩陣
        glTranslatef(0.5,0,0);
        glutSolidTeapot( 0.3 );
        glPopMatrix(); 還原矩陣
glutSwapBuffers();
}

int main(int argc, char *argv[])
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("GLUT Shapes");
    glutDisplayFunc(display);
    glutMainLoop();
}


顯示點擊的座標

#include <GL/glut.h>
void display()
{
glPushMatrix();
        glTranslatef(0.5,0,0);
        glutSolidTeapot( 0.3 );
    glPopMatrix();
glutSwapBuffers();
}
#include <stdio.h>
void mouse(int button, int state, int x,int y)
{
    printf("%d %d %d %d\n",button,state,x,y);
}
int main(int argc, char *argv[])
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("GLUT Shapes");
    glutDisplayFunc(display);

    glutMouseFunc(mouse);
    glutMainLoop();
}

讓茶壺跟著鼠標走
#include <GL/glut.h>
float X=0 ,Y=0;
void display()
{
glClear(GL_COLOR_BUFFER_BIT |GL_COLOR_BUFFER_BIT  ); 清背景
glPushMatrix();
        glTranslatef(X,Y,0);
        glutSolidTeapot( 0.3 );
        glPopMatrix();
glutSwapBuffers();
}
#include <stdio.h>
void mouse(int button, int state, int x,int y)
{
    X=(x-150)/150.0;
    Y=-(y-150)/150.0; 口訣:減一半,除一半,y負號
}

int main(int argc, char *argv[])
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("GLUT Shapes");
    glutDisplayFunc(display);

    glutMouseFunc(mouse);
    glutMainLoop();
}



用鼠標幫你畫圖

#include <GL/glut.h>
#include <stdio.h>
void display()
{

    glBegin(GL_POLYGON);
    glVertex2f(-0.268, 0.160);
    glVertex2f(-0.304, 0.204);
    glVertex2f(-0.332, 0.264);
    glVertex2f(-0.352, 0.384);
    glVertex2f(-0.344, 0.484);
    glVertex2f(-0.308, 0.532);
    glVertex2f(-0.236, 0.552);
    glVertex2f(-0.204, 0.496);
    glVertex2f(-0.204, 0.404);
    glVertex2f(-0.204, 0.348);
    glVertex2f(-0.180, 0.280);
    glVertex2f(-0.156, 0.228);
    glVertex2f(-0.156, 0.212);
    glVertex2f(-0.152, 0.188);
    glEnd();

    glBegin(GL_POLYGON);
    glVertex2f(0.040, 0.200);
    glVertex2f(0.032, 0.248);
    glVertex2f(0.028, 0.328);
    glVertex2f(0.028, 0.404);
    glVertex2f(0.040, 0.480);
    glVertex2f(0.064, 0.540);
    glVertex2f(0.096, 0.572);
    glVertex2f(0.152, 0.616);
    glVertex2f(0.208, 0.604);
    glVertex2f(0.260, 0.524);
    glVertex2f(0.240, 0.468);
    glVertex2f(0.192, 0.428);
    glVertex2f(0.164, 0.384);
    glVertex2f(0.152, 0.328);
    glVertex2f(0.148, 0.276);
    glVertex2f(0.148, 0.244);
    glVertex2f(0.140, 0.212);
    glVertex2f(0.140, 0.188);
    glEnd();

    glBegin(GL_POLYGON);
    glVertex2f(-0.196, 0.044);
    glVertex2f(-0.188, 0.068);
    glVertex2f(-0.112, 0.096);
    glVertex2f(-0.060, 0.096);
    glVertex2f(-0.048, 0.068);
    glVertex2f(-0.088, 0.048);
    glVertex2f(-0.140, 0.032);
    glVertex2f(-0.188, 0.032);
    glEnd();

    glBegin(GL_LINE_LOOP);
    glVertex2f(-0.212, 0.152);
    glVertex2f(-0.256, 0.116);
    glVertex2f(-0.272, 0.064);
    glVertex2f(-0.300, -0.084);
    glVertex2f(-0.292, -0.140);
    glVertex2f(-0.220, -0.224);
    glVertex2f(-0.124, -0.252);
    glVertex2f(0.080, -0.260);
    glVertex2f(0.156, -0.256);
    glVertex2f(0.224, -0.224);
    glVertex2f(0.264, -0.160);
    glVertex2f(0.288, -0.152);
    glVertex2f(0.284, 0.004);
    glVertex2f(0.256, 0.092);
    glVertex2f(0.232, 0.164);
    glVertex2f(0.068, 0.180);
    glVertex2f(0.000, 0.192);
    glVertex2f(-0.144, 0.176);
    glEnd();
    glutSwapBuffers();
}
void mouse(int button, int state, int x, int y)
{
    float X = (x-250)/250.0;
    float Y = -(y-250)/250.0;
    if(state==GLUT_DOWN){
        printf("    glVertex2f(%.3f, %.3f);\n", X, Y);
    }
}
int main(int argc, char* argv[] )
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGB|GLUT_DOUBLE|GLUT_DEPTH);
    glutInitWindowSize(500,500);
    glutCreateWindow("week03");

    glutDisplayFunc(display);
    glutMouseFunc(mouse);

    glutMainLoop();
}






沒有留言:

張貼留言