2024年3月11日 星期一

Dubi week04

 Week04

glScalef(x,y,z) ///縮放




## week04_mouse_glscalef 

1.安裝freeglut,同時把lib\libfreeglut.a 複製成 libglut32.a

2.flie-new-project, glut 專案

3.程式碼

#include <GL/glut.h>

void display()

 {

    glClearColor(1,1,0.9,1); ///顏色

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glColor3f(0,1,0); 

    glutSolidTeapot( 0.3 );

    glutSwapBuffers();

 }

int main(int argc, char *argv[])

 {

    glutInit(&argc, argv);

    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);

    glutCreateWindow("week04_mouse_glScalef");

    glutDisplayFunc(display);

    glutMainLoop();

 }









##week04_translate_rotate_scale
  1.  file_new_ project, GLUT專案  week04_translate_rotate_scale









##week04_rotate_translate_scale (茶壺轉圈圈) => 逆時針 

1.程式碼

#include <GL/glut.h>

float teapotX = 0.5,teapotY = 0;
float angle = 0;
float s = 0.3;
void display()
{///設定 顏色
    glClearColor(1,1,0.9,1);///顏色R,G,B,A
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glColor3f(0,1,0);
    glPushMatrix();///備份矩陣
        glRotatef(angle++,0,0,1);
        glTranslatef(teapotX,teapotY,0);
        glScalef(s,s,s);
        glutSolidTeapot( 0.3 );
    glPopMatrix();
    glutSwapBuffers();
}
void motion(int x,int y)
{
    s = 1 + (x-150)/150.0;
    display();
}
int main(int argc, char *argv[])
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("week04_mouse_glScalef");
    glutIdleFunc(display);
    glutDisplayFunc(display);
    glutMotionFunc(motion);
    glutMainLoop();
}





1. 按滑鼠自動貼上茶壺
2. 程式碼

#include <GL/glut.h>
#include <stdio.h>
int N = 0;
float teapotX[1000],teapotY[1000];
void display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    for(int i=0; i<N; i++){
        glPushMatrix();
            glTranslatef(teapotX[i],teapotY[i],0);
            glutSolidTeapot( 0.1 );
        glPopMatrix();
    }
    glutSwapBuffers();
}
void keyboard(unsigned char key,int x,int y){
    printf("key:%c  x:%d  y:%d\n",key,x,y);
}
void mouse(int button,int state,int x,int y){
    teapotX[N] = (x-150)/150.0;
    teapotY[N] = -(y-150)/150.0;
    N++;
}
int main(int argc, char *argv[])
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("week04_keyboard_mouse_motion");
    glutDisplayFunc(display);
    glutKeyboardFunc(keyboard);
    glutMouseFunc(mouse);
    glutMainLoop();
}









沒有留言:

張貼留言