顯示具有 40447017S_田蜜 標籤的文章。 顯示所有文章
顯示具有 40447017S_田蜜 標籤的文章。 顯示所有文章

2017年11月23日 星期四

田蜜 Week09

Bump Mapping

1.資源連結:https://www.openprocessing.org/sketch/24945  

2. moodle下載 processing-3.3.6-windows64.zip

3. 解壓縮並執行 processing.exe

4.將Bump Mapping 程式碼貼到processing.exe中
   並在random()中加入 225















5.執行




















6.在同一行多加一個f



















7.執行


















在Processing中匯入圖片


1.將圖片拖曳到編輯器中(會出現"將一個文件添加到速寫本"的訊息)
   並加入程式碼

PImage img=loadImage("圖片檔名");
size(長,寬);
image(img,0,0,長,寬);



















2.讓照片跟著滑鼠移動

PImage img;

void setup()
{
   img=loadImage("pchu.jpg");
   size(500,500);
}

void draw()
{
image(img,mouseX,mouseY);
}



















馬力歐 Processing 程式碼

可用鍵盤控制!


int [][]map={{0,0,0,0,0,0,0,0,0,0,0,0,0,0},
             {0,0,0,0,0,0,0,0,0,0,0,0,0,0},
             {0,0,0,0,0,0,0,0,0,0,0,0,0,0},
             {0,0,0,0,0,0,0,0,0,0,0,0,0,0},
             { 0,0,0,0,0,0,0,0,0,0,0,0,0,0},
             {0,0,0,0,0,0,0,0,0,0,0,0,0,0},
             {0,0,1,1,2,2,0,0,0,0,0,0,0,0},
             {0,0,0,0,0,0,0,0,0,0,0,0,0,0},
             {0,0,0,0,0,0,0,0,0,0,0,0,0,0},
             {0,0,0,0,0,0,0,0,0,0,0,0,0,0},
             {1,1,1,1,1,1,1,1,1,1,1,1,1,1},};
PImage imgMario,imgBrick,imgBrick2;
void setup()
{
  size(800,600);
  imgMario=loadImage("mario.png");
  imgBrick=loadImage("Brick_Block.png");
  imgBrick2=loadImage("5da98730977895279f05af2eb7bd7516.png");
}
float marioX=200, marioY=100,marioVX=0,marioVY=0;
boolean marioOnFloor=false;
void draw()
{
  background(255);
  for(int x=0;x<14;x++)
  {
    for(int y=0;y<11;y++)
    {
      if(map[y][x]==1) image(imgBrick,  x*50,y*50,50,50);
      if(map[y][x]==2) image(imgBrick2, x*50,y*50,50,50);
    }
  }
  image(imgMario,marioX,marioY,100,100);
  marioY+=marioVY; marioX+=marioVX;
  marioVY+=0.98;
  if(marioY>=400){marioY=400;marioVY=0;marioOnFloor=true;}
}
void keyPressed(){
  if(keyCode==UP && marioOnFloor){marioVY=-15;marioOnFloor=false;}
  if(keyCode==RIGHT) marioVX=5;
  if(keyCode==LEFT) marioVX=-5;
}
void keyReleased(){
  if(keyCode==RIGHT || keyCode==LEFT) marioVX=0;
}



2017年11月2日 星期四

田蜜Week08

Texture

1.至http://www.cmlab.csie.ntu.edu.tw/~jsyeh/3dcg10/下載data , win32 , source 及 glut32.gll 檔案
   並將data 和 glut32.gll 放入windows 資料夾中

2.執行 Texture.exe 檔案
















OpenCV專案

1.下載OpenCV-2.1.0-win32-vs2008.exe

2.安裝時記得選擇第三項

















3.安裝完成後開啟codeblocks專案
   file->new->project->console application

4.接著開啟settings->compiler


















5.將main.cpp內容更改為

#include <opencv/highgui.h>

int main()
{
    IplImage * img=cvLoadImage("earth.jpg");
    cvNamedWindow("hello opencv");
    cvShowImage("hello opencv",img);
    cvWaitKey(0);
}

















6.搜尋圖片earth map
   另存圖片至專案資料夾,名稱為earth.jpg


















7.專案上按右鍵->build option

















8.選擇 Search directories
   新增C:\OpenCV2.1\include 至 Compiler
















9.新增C:\OpenCV2.1\lib 至 Linker
















10. 選擇 Linker settings
      新增 lib 資料夾中的 cv210  ,cxcore210 , highgui210  三個檔案

































11. 編譯執行
















用GLUT專案開OpenCV

1.開啟新的GLUT project

2. 加上 #include <opencv/highgui.h>
    並在 main 中加入程式碼

    IplImage * img=cvLoadImage("earth.jpg");
    cvNamedWindow("hello");
    cvShowImage("hello", img);
    cvWaitKey(0);

















3. 將 earth.jpg 放入 freeglut->bin 中

















4. 對新專案再做一次build option的設定
    編譯執行 (按空白鍵叫出左上角的3D圖)
















3D旋轉地球

#include <opencv/highgui.h> ///for cvLoadImage()
#include <opencv/cv.h> ///for cvCvtColor()
#include <GL/glut.h> ///3D glut
#include <stdio.h>
GLUquadric * quad;
GLuint id;
float angle=0;
void display()
{   glEnable(GL_DEPTH_TEST); ///要啟動 Detph Test 深度值的測試,3D顯示才正確
    glClear(GL_COLOR_BUFFER_BIT  | GL_DEPTH_BUFFER_BIT);
    glPushMatrix();///自動轉很帥
        glRotatef(90, 1,0,0);
        glRotatef(angle, 0,0,1);///自動轉很帥
        gluQuadricTexture(quad, 1);
        gluSphere(quad, 1, 30, 30);///glutSolidTeapot(0.3);
    glPopMatrix();///自動轉很帥
    glFlush();
}
void timer(int t)
{   glutTimerFunc(20, timer, 0);/// 1000 msec   50fps:20msec
    angle+=1;///自動轉很帥
    glutPostRedisplay();
}
int myTexture(char *filename)
{
    IplImage * img = cvLoadImage(filename); ///OpenCV讀圖
    cvCvtColor(img,img, CV_BGR2RGB); ///OpenCV轉色彩 (需要cv.h)
    glEnable(GL_TEXTURE_2D); ///1. 開啟貼圖功能
    GLuint id; ///準備一個 unsigned int 整數, 叫 貼圖ID
    glGenTextures(1, &id); /// 產生Generate 貼圖ID
    glBindTexture(GL_TEXTURE_2D, id); ///綁定bind 貼圖ID
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); /// 貼圖參數, 超過包裝的範圖T, 就重覆貼圖
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); /// 貼圖參數, 超過包裝的範圖S, 就重覆貼圖
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); /// 貼圖參數, 放大時的內插, 用最近點
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); /// 貼圖參數, 縮小時的內插, 用最近點
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, img->width, img->height, 0, GL_RGB, GL_UNSIGNED_BYTE, img->imageData);
    return id;
}
void myInit()
{   quad = gluNewQuadric();
    id = myTexture("earth.jpg");
}
int main(int argc, char**argv)
{   glutInit(&argc, argv);
    glutCreateWindow("3D");
    glutDisplayFunc(display); ///顯示
    glutTimerFunc(0, timer, 0);
    myInit(); ///我的 init 初始化 把貼圖準備好 前面OpenCV 2行, 後面 OpenGL 9行
    glutMainLoop();
}










2017年10月26日 星期四

田蜜Week07

 Mouse-Lighting

1.打開glut專案
找到idle函式與main中的lighting相關程式碼



















2.light_position表示光的位址
   glutMotionFunc(motion); 表示滑鼠的改變

   將const  GLfloat light_position[] = { 2.0f, 5.0f, 5.0f, 0.0f }; 前面const 刪去
   即可改變light_position的變數



















3.加上函式

void motion(int x, int y)
{
    light_position[0]=(x-150*2)/150.0*2;  //更變光源座標為滑鼠座標
    light_position[1]=-(y-150)/150.0*2; 
    glLightfv(GL_LIGHT0,GL_POSITION,light_position); //重設光源
    glutPostRedisplay(); //重畫
}

  在main中加上glutMotionFunc(motion);  //mouse motion函式



















4.編譯執行




















完成!


2017年10月19日 星期四

田蜜Week06


下載week5.zip 

從setting -> compiler 取消C++11 即可編譯








打光Lighting

在main裡面glutMainloop();前加入程式碼
    glEnable(GL_LIGHTING);
    glEnable(GL_DEPTH_TEST);
    glEnable(GL_LIGHT0);
    glLightfv(GL_LIGHT0, GL_POSITION, pos);

並在main之前加上
    GLfloat pos[]={0.0,0.0,-1.0,0.0};




編譯執行結果
















加入Keyboard,Mouse

#include<stdio.h>
int rotateX=0,rotateY=0,rotateZ=0;
void keyboard(unsigned char key,int x,int y)
{
printf("now: %c (%d %d)\n",key,x,y);
}

在main中加入
glutMouseFunc(mouse);
glutKeyboardFunc(keyboard);





























編譯執行結果















在void keyboard();中加入
if(key=='1')rotateX++;
if(key=='2')rotateY++;
if(key=='3')rotateZ++;
glutPostRedisplay();

加上
void mouse(int button,int state,int x,int y)
{
    printf("button:%d state:%d (%d %d)\n",button,state,x,y);
}

在display();中加入
glPushMatrix();
    glRotatef(rotateX,1,0,0);
    glRotatef(rotateY,0,1,0);
    drawmodel();
glPopMatrix();

即可用按鍵1,2,3對x,y,z軸做即時旋轉
















Mouse motion

在mouse();中加入
    if(state==GLUT_DOWN)
    {
        oldX=x;
        oldY=y;
    }

加入函式
void motion(int x,int y)
{
    rotateY+= -(x-oldX);
    rotateX+= -(y-oldY);
    oldX=x;
    oldY=y;
    glutPostRedisplay();
}

在main中加入
glutMotionFunc(motion);





























編譯執行結果