顯示具有 40447031S_張晉維 標籤的文章。 顯示所有文章
顯示具有 40447031S_張晉維 標籤的文章。 顯示所有文章

2017年12月21日 星期四

[耍廢]WEEK13

主題1:VR 體驗
GoPro 運動攝影機
OMNI 六台GoPro
Ricoh theta 





主題2:色彩系統、multi-Texture ( TextureID )、畫面等速 ( Timer )

 void timer(int t)
    {
        /******code******/ //執行你要定速的程式碼
        glutTimerFunc(100, timer, t+1); //過了100ms後,再呼叫一次Timer副程式
        glutPostRedisplay();
    }

int main(){
      /******code******/
      glutTimerFunc(500, timer, 0); //過了500ms後,呼叫Timer副程式
      /******code******/
    }
如此一來,timer裡的code會隨著設定的時間執行,適合背景物品。
   

[耍廢]WEEK12

myGL_Frustrum221編譯且執行
1. 至moodle 下載myGL_Frustrum221壓縮檔,解壓縮

2. 建立GLUT專案檔
    解壓縮後資料夾裡的程式碼 複製至專案檔中

3. 出現問題 : freeglut ERROR: Function called without first calling 'glutInit'
    解決方法 :
A.int main(int argc,char *argv[]) 補上
                      B. glutInit( & argc, argv );這行指令加在main裡面)

[耍廢]WEEK10

用 processing寫 android app

1. 將 processing 右上角的 java改成app mode
(要下載 android模式)
自動安裝 Android SDK NDK)
可以與手機互動
2.
2.偵測碰撞遊戲:

void setup(){
size(500,500);
}
float ballX=250,ballY=450, ballVX=1, ballVY=10;
float ball2X=250, ball2Y=50;
void draw(){
if(ballX>450||ballX<50) //牆壁
    ballVX=-ballVX;
if(ballY>450 || ballY<50) //牆壁
    ballVY=-ballVY;
if( dist(ballX, ballY, ball2X, ball2Y)<100 ){
float cx=(ballX+ball2X)/2, cy=(ballY+ball2Y)/2;
float nx=ballX-ball2X, ny=ballY-ball2Y;
PVector N=new PVector(nx,ny);
N.normalize();
PVector v0=new PVector(ballVX, ballVY);
float len= -N.dot(v0);
v0.add(N.mult(len*2));
ellipse(cx, cy, 10,10);
stroke(0); line(ballX, ballY, ball2X, ball2Y);
stroke(255,128,0); line(cx,cy, cx+5*ballVX, cy+5*ballVY);
stroke(255,128,0); line(cx,cy, cx+5*v0.x, cy+5*v0.y);
stroke(255,0,0); line(cx,cy, cx+N.x, cy+N.y);
//return;
ballVX=v0.x; ballVY=v0.y;
}
background(255);
ballX+=ballVX; ballY+=ballVY;
ellipse(ballX, ballY, 100,100);
ellipse(ball2X, ball2Y, 100,100);
}


[耍廢]WEEK09

1.p語言
至moodle下載processing-3.3.6-windows64.zip
解壓縮後執行如下





Bump Mapping 凹凸貼圖 

https://www.openprocessing.org/sketch/249457
code來源

// Processing.jsでバンプマッピング

BumpMappedBox myBox;
void setup() {
  size(200, 200, P3D);

  myBox = new BumpMappedBox(100,
    createTexture(),
    createHeightMapImage());
}

void draw() {
  background(0xff);
  camera();
  noStroke();
  pushMatrix();
    translate(.5 * width, .5 * height);

    float baseAngle = radians(millis() * 0.015);
    rotateX(baseAngle);
    rotateZ(baseAngle);
    rotateY(baseAngle);
   
    myBox.render();  
  popMatrix();
}

PImage createTexture() {
  
  int step = 0x20;
  PImage tex = createImage(0x80, 0x80, RGB);
  tex.loadPixels();
  
  for(int i = 0; i < tex.width; i++) {
    for(int j = 0; j < tex.height; j++) {
      tex.set(i, j, 
        (floor(i / step) + floor(j / step)) % 2 == 0 ?
          color(0xcc) : color(0xff));
    }
  }
  tex.updatePixels();
  return tex;
}

PImage createHeightMapImage() {
  int step = 0x20;
  PImage tex = createImage(0x80, 0x80, RGB);
  tex.loadPixels();
  
  for(int i = 0; i < tex.pixels.length; i++) {
    tex.pixels[i] = color(0xfff - random(255));//與原code不同處
  }
  for(int i = step; i < tex.width -1; i += step) {
    for(int j = 0; j < tex.height; j++) {
      tex.pixels[i + j * tex.width] = 0xff << 0x16;
    }
  }
  for(int i = 0; i < tex.width; i++) {
    for(int j = step; j < tex.height; j += step) {
      tex.pixels[i + j * tex.width] = 0xff << 0x16;
    }
  }
  tex.updatePixels();
  return tex;
}

// ----------------------------------------

interface Renderable {
  void render();
}

// ----------------------------------------

abstract class RenderHelper implements Renderable {
  private final int NUM_TEXTURE_COORDS_DEFAULT = 4;

  protected final int DIFFUSE_COLOR   = 0x7f;
  protected final PVector LIGHT_DIRECTION = new PVector(0, 0, -1);
  protected final PVector ZERO_VECTOR     = new PVector(0, 0, 0);

  protected PVector eyePoint = new PVector(.5 * width, 
                                           .5 * height, 
                                           .5 * height / tan(PI / 6.0));
  protected PVector _origin = new PVector();
  protected PVector _normal = new PVector();
  protected PImage  _originalTexture;

  protected PVector[] _worldVertices;
  protected PVector[] _localVertices;
  protected PVector[] _originalTextureCoords;

  private PImage  _texture;
  private boolean _isBack = false;
  
  public abstract void render();
  
  public void setEyePoint(PVector p) {
    if(p != null) {
      eyePoint.set(p);
    } else {
      setEyePoint(0, 0, 0);
    }
  }
  public void setEyePoint(float x, float y, float z) {
    eyePoint.set(x, y, z);
  }
  
  protected void initVertices(int numVertices) {
    if(numVertices < 1) return;
    
    _worldVertices = new PVector[numVertices];
    _localVertices = new PVector[numVertices];
    
    for(int i = 0; i < numVertices; i++) {
      _worldVertices[i] = new PVector();
      _localVertices[i] = new PVector();
    }
  }
  
  protected void initTextureCoords() {
    _originalTextureCoords = new PVector[NUM_TEXTURE_COORDS_DEFAULT];
    for(int i = 0; i < _originalTextureCoords.length; i++) {
      _originalTextureCoords[i] = new PVector(i < NUM_TEXTURE_COORDS_DEFAULT / 2        ? 0 : 1, 
                                      i % (NUM_TEXTURE_COORDS_DEFAULT - 1) == 0 ? 0 : 1);
    }
  }
  
  protected void initTextureCoords(int numVertices) {
    _originalTextureCoords = new PVector[numVertices];
    
    for(int i = 0; i < numVertices; i++) {
      _originalTextureCoords[i] = new PVector();
    }
  }
  
  protected PVector[] updateWorldVertices(PVector[] localVertices, PVector[] destWorldVertices) {
    for(int i = 0; i < destWorldVertices.length; i++) {
      updateWorldVertex(localVertices[i], destWorldVertices[i]);
    }
    return destWorldVertices;
  }
  
  protected PVector[] updateWorldVertices() {
    return updateWorldVertices(_localVertices, _worldVertices);
  }
  
  protected PVector updateWorldVertex(PVector localVertex, PVector destWorldVertex) {
    destWorldVertex.set(
      modelX(localVertex.x, localVertex.y, localVertex.z), 
      modelY(localVertex.x, localVertex.y, localVertex.z),
      modelZ(localVertex.x, localVertex.y, localVertex.z));      
    return destWorldVertex;
  }
  
  protected PVector updateWorldVertex(PVector localVertex) {
    return new PVector (
      modelX(localVertex.x, localVertex.y, localVertex.z), 
      modelY(localVertex.x, localVertex.y, localVertex.z),
      modelZ(localVertex.x, localVertex.y, localVertex.z));      
  }
  
  protected PVector updateOrigin(PVector center) {
    updateWorldVertex(center, _origin);
    return _origin;
  }
  
  protected PVector updateOrigin() {
    return updateOrigin(ZERO_VECTOR);
  }
  
  protected PVector updateNormalVector(PVector v1, PVector v2) {
    _normal.set(v1.cross(v2));
    _normal.normalize();
    return _normal;
  }
  
  private PVector _ = new PVector();
  protected int getDefaultTintColor() {
    subVec(_origin, eyePoint, _);
    _isBack = 0 < _normal.dot(_);
    float arg = (_isBack ? 1.0 : -1.0) * _normal.dot(LIGHT_DIRECTION);
    return (int)(arg * (0xff - DIFFUSE_COLOR) + DIFFUSE_COLOR);
  }
  
  protected void subVec(PVector p1, PVector p2, PVector dest) {
    dest.set(p1.x - p2.x, p1.y - p2.y, p1.z - p2.z);
  }
  

  protected void renderWithoutShade(PVector[] worldVertices, PVector[] textureCoords) {
    if(!checkArgsBeforeRender(_originalTexture, 
                              worldVertices, 
                              textureCoords)) return;
    getDefaultTintColor();
    if(_isBack) return;
    
    if(!setupRenderTexture()) return;
    _texture.resize(_texture.width, _texture.height);
    renderMain(0xff, _texture, worldVertices, textureCoords);
  }
  
  protected void renderWithShade(PVector[] worldVertices, PVector[] textureCoords) {
    if(!checkArgsBeforeRender(_originalTexture, 
                              worldVertices, 
                              textureCoords)) return;

    int tintColor = getDefaultTintColor();
    if(_isBack) return;
    renderMain(tintColor, _originalTexture, worldVertices, textureCoords);
  }

  private boolean checkArgsBeforeRender(PImage originalTexture, PVector[] worldVertices, PVector[] textureCoords) {
    if(_originalTexture == null || worldVertices == null || textureCoords == null) return false;
    if(textureCoords.length < worldVertices.length) return false;
    if(_originalTexture.get(0, 0) == 0) return false;
    return true;
  }
  
  private boolean setupRenderTexture() {
    // 毎回 createImage しないとうまくレンダリングされない...    
    _texture = createImage(_originalTexture.width, _originalTexture.height, RGB);
    
    updateTexture(_originalTexture, _texture);
    return !(_texture == null || _texture.get(0, 0) == 0);
  }
  
  protected void updateTexture(PImage originalTexture, PImage destUpdatedTexture) {
    destUpdatedTexture.loadPixels();
    for(int i = 0; i < originalTexture.pixels.length; i++) {
      destUpdatedTexture.pixels[i] = originalTexture.pixels[i];
    }
    destUpdatedTexture.updatePixels();
  }
  
  private void renderMain(int tintColor, PImage textureImage, PVector[] worldVertices, PVector[] textureCoords) {
    pushMatrix();
      resetMatrix();
      camera();
      noLights();
      beginShape();
        textureMode(NORMAL);
        texture(textureImage);
        tint(tintColor);
        for(int i = 0; i < worldVertices.length; i++) {
          vertex(worldVertices[i].x, worldVertices[i].y, worldVertices[i].z, 
                 textureCoords[i].x, textureCoords[i].y);
        }
      endShape(CLOSE);
    popMatrix();    
  }
  
  protected void renderWithShade() {
    renderWithShade(_worldVertices, _originalTextureCoords);
  }
  
  protected void renderWithoutShade() {
    renderWithoutShade(_worldVertices, _originalTextureCoords);
  }
}

// ----------------------------------------

class BoxWithTexture extends RenderHelper {
  protected final int NUM_VERTICES    = 8;
  protected final int NUM_PLANES      = 6;
  protected final int NUM_SIDE_PLANES = 4;
  protected final int NUM_VERTICES_PER_PLANE = 4;
  
  protected float _width;
  protected float _height;
  protected float _depth;
  
  protected PVector[]   _centers = new PVector[NUM_PLANES];
  protected PVector[][] _surface = new PVector[NUM_PLANES][NUM_VERTICES_PER_PLANE];  
  
  public BoxWithTexture(float size, PImage tex) {
    this(size, size, size, tex);
  }
  
  public BoxWithTexture(float w, float h, float d, PImage tex) {
    _width   = w;
    _height  = h;
    _depth   = d;
    _originalTexture = tex;  

    setupSurface();
  }
  
  private void setupSurface() {
    float halfWidth  = .5 * _width;
    float halfHeight = .5 * _height;
    float halfDepth  = .5 * _depth;
    
    initTextureCoords();
    initVertices(NUM_VERTICES);
    for(int i = 0; i < NUM_VERTICES; i++) {
      int j = i < NUM_VERTICES_PER_PLANE ? 
                i : i - NUM_VERTICES_PER_PLANE;
                
      _localVertices[i].set(j % 3 == 0 ? -halfWidth  : halfWidth, 
                            i < 4      ? -halfHeight : halfHeight,
                            i % 4 < 2  ? -halfDepth  : halfDepth);
    }
    
    setupSide(halfWidth, halfDepth);
    setupTopAndBottom(halfHeight);

  }
  
  private void setupSide(float halfWidth, float halfDepth) {
    for(int i = 0; i < NUM_SIDE_PLANES; i++) {
      float _x = halfWidth * (i % 2 == 0 ? 0 : i < 2 ?  1 : -1);
      float _z = halfDepth * (i % 2 != 0 ? 0 : i < 2 ? -1 :  1);
      _centers[i] = new PVector(_x, 0, _z);

      for(int j = 0; j < NUM_VERTICES_PER_PLANE; j++) {        
        int surplus = j % (NUM_VERTICES_PER_PLANE - 1);
        int index = surplus != 0 ? 
                    (i + surplus + (NUM_VERTICES_PER_PLANE - 1)) % 
                      NUM_VERTICES_PER_PLANE + 
                        NUM_VERTICES_PER_PLANE :
                    (i + (j < NUM_VERTICES_PER_PLANE / 2 ? 0 : 1)) % 
                      NUM_VERTICES_PER_PLANE;
        _surface[i][j] = _worldVertices[index];
      }
    }
  }
  
  private void setupTopAndBottom(float halfHeight) {
    for(int i = NUM_SIDE_PLANES; i < _centers.length; i++) {
      float _y = halfHeight * (i == NUM_SIDE_PLANES ? -1 : 1);
      _centers[i] = new PVector(0, _y, 0);
      
      for(int j = 0; j < NUM_VERTICES_PER_PLANE; j++) {        
        int index = i == NUM_SIDE_PLANES ? j : NUM_VERTICES - (j + 1);
        _surface[i][j] = _worldVertices[index];
      } 
    }
  }
  
  public void render() {
    updateWorldVertices();
    for(int i = 0; i < _surface.length; i++) {
      updateOrigin(_centers[i]);       
      updateNormalVector(PVector.sub(_surface[i][1], _surface[i][0]), 
                         PVector.sub(_surface[i][3], _surface[i][0]));
      renderWithShade(_surface[i], _originalTextureCoords);
    }
  }
}

// ----------------------------------------

class BumpMappedPlane extends RenderHelper { 
  private final int NUM_VERTICES = 4;

  private float  _width;
  private float  _height;
  private PImage _heightMap;
  private PVector[][] _normalMap; // 法線マップ
  
  public BumpMappedPlane(float w, float h, PImage tex, PImage hMap) {
    _width   = w;
    _height  = h;
    
    _originalTexture = tex;  
    _heightMap       = hMap;

    float halfWidth  = .5 * _width;
    float halfHeight = .5 * _height;
    
    initTextureCoords();
    initVertices(NUM_VERTICES);
    for(int i = 0; i < NUM_VERTICES; i++) {
      _localVertices[i].set(i < 2      ? -halfWidth  : halfWidth, 
                            i % 3 == 0 ? -halfHeight : halfHeight, 0);
    }
  }
  
  PVector _worldNormal = new PVector();
  PImage  _dummyTexture;
  
  public void render() {
    if(_originalTexture.get(0, 0) != 0) {
      if (_normalMap == null) {
        _normalMap = createNormalMap(_heightMap);
      }
    }

    updateOrigin();
    updateWorldVertices();
    updateNormalVector(PVector.sub(_worldVertices[1], _worldVertices[0]), 
                       PVector.sub(_worldVertices[3], _worldVertices[0]));
    renderWithoutShade();
  }
  
  protected PVector zeroVec = new PVector();
  
  protected void updateTexture(PImage originalTexture, PImage destUpdatedTexture) {
    destUpdatedTexture.loadPixels();
    int imgWidth = destUpdatedTexture.width;

    for(int i = 0; i < _normalMap.length; i++) {
      for(int j = 0; j < _normalMap[0].length; j++) {
        updateWorldVertex(_normalMap[i][j], _worldNormal);
        _worldNormal.sub(_origin);
        float arg = _worldNormal.dot(LIGHT_DIRECTION);
        
        float coef = (arg * (0xff - DIFFUSE_COLOR) / 0xff) + (DIFFUSE_COLOR / 0xff);
        int originalColor = originalTexture.pixels[j * imgWidth + i];
        int b = (int)(coef * (originalColor         & 0xff));
        int g = (int)(coef * (originalColor >>  0x8 & 0xff));
        int r = (int)(coef * (originalColor >> 0x10 & 0xff));
        
        destUpdatedTexture.pixels[j * imgWidth + i] = 0xff << 0x18 | r << 0x10 | g << 0x8 | b;
      }
    }
    destUpdatedTexture.updatePixels();
  }
  
  private float dU(PImage heightMap, int i, int j) {
    if(i < 1) return dU(heightMap, 1, j);
    if(!(i < heightMap.width-1)) return dU(heightMap, heightMap.width - 2, j);

    return .5 * (getHeight(heightMap, i+1,j) - getHeight(heightMap, i-1,j));
  }
  
  private float dV(PImage heightMap, int i, int j) {
    if(j < 1) return dV(heightMap, i, 1);
    if(!(j < heightMap.height-1)) return dV(heightMap, i, heightMap.height - 2);

    return .5 * (getHeight(heightMap, i, j+1) - getHeight(heightMap, i,j-1));
  }
  
  private int getHeight(PImage heightMap, int i, int j) {
    return 0xff & heightMap.pixels[i + j * heightMap.width];
  }
  
  private PVector[][] createNormalMap(PImage heightMapImage) {
    int mapWidth  = heightMapImage.width;
    int mapHeight = heightMapImage.height;
    
    PVector[][] normalMap = new PVector[mapWidth][mapHeight];
    PVector v1 = new PVector();
    PVector v2 = new PVector();
    
    for(int i = 0; i < mapWidth; ++i) {
      for(int j = 0; j < mapHeight; ++j) {
                 
        v1.set(1.0, 0, dU(heightMapImage, i, j));
        v1.normalize();
        v2.set(0, 1.0, dV(heightMapImage, i, j));
        v2.normalize();

        normalMap[i][j] = v1.cross(v2);
        normalMap[i][j].normalize();
      }
    }
    return normalMap;
  }
}

// ----------------------------------------

class BumpMappedBox implements Renderable {
  private BumpMappedPlane[] _surface = new BumpMappedPlane[6];
  private float _size;
  
  BumpMappedBox(float boxSize, PImage tex, PImage hMap) {
    _size = boxSize;
    for(int i = 0; i < 6; i++) {
      _surface[i] = new BumpMappedPlane( _size, _size, tex, hMap);
    }
  }
 
  void render() {
    pushMatrix();
    for(int i = 0; i < 4; i++) {
      rotateY(i * HALF_PI);
      
      pushMatrix();
      translate(0, 0, -.5 * _size);
      _surface[i].render();
      popMatrix();
    }
    popMatrix();

    pushMatrix();
      rotateX(HALF_PI);
      for(int i = 4; i < 6; i++) {
        rotateX(i * PI);   
        pushMatrix();
          translate(0, 0, -.5 * _size);
          _surface[i].render();
        popMatrix();
      }
    popMatrix();
  }
}

執行結果




Environment Mapping 環境貼圖

下載一張圖片並匯入processing 3中
PImage img;

void setup(){
  // setting window size
  size(1280, 800);
  
  // processing image load a image
  img = loadImage("a.PNG");
}

void draw(){
  // show image ( image, x, y, width, height)
  image(img, mouseX, mouseY);//圖隨滑鼠移動
}
結果

3.簡易馬力歐遊戲


找瑪莉歐,問號方塊和普通磚塊的圖,存為mario, brick1, brick2
並複製以下程式碼貼上
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,1,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.png");
  imgBrick2=loadImage("brick2.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, 50,50);
  marioY += marioVY; marioX += marioVX;
  marioVY += 0.98;
  if(marioY>=500-50)
  {marioY=500-50; 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

一、
1.至http://www.cmlab.csie.ntu.edu.tw/~jsyeh/3dcg10/
下載data32  win32  glut32.dll 放在桌面新增的一個資料中,解壓縮後執行Texture.exe
2.執行結果


二、在codeblock設定opencv(練習設定)
1.至moodle下載安裝codeblock_MinGW跟OpenCV-2.1.0-win32-vs2008.exe
2.先安裝opencv再裝codeblock(記得opencv安裝時要用選將路徑加入環境數PATH)

3.開始建立codeblock的專案
4.File->New->Project-->選console application-->GO-->之後命名專案跟選擇專案存檔位置
5.將main.cpp中的code取代如下
#include <opencv/highgui.h> int main() { IplImage * img=cvLoadImage("a.jpg"); cvNamedWindow("hello"); cvShowImage("hello", img); cvWaitKey(0); return 0; }

6.會發現無法執行
7.按照下列步驟設定






8.隨便下載個jpg圖片並將圖片改為a.jpg
9.將圖片放到專案的資料夾中
10.執行結果



三、在glut專案中使用opencv
1.新建一個glut專案(方法請見之前的筆記)
2.用上面的方法設定此專案
3.更改一下code

4.把檔名為a.jpg的圖片放到freeglut\bin的資料夾中
5.執行結果

 按下任意鍵之後
四、使用教授給的專案檔來學opengl的texture

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;





























2017年10月26日 星期四

[耍廢]WEEK7

光源改變
1.
const  GLfloat light_position[] = { 2.0f, 5.0f, 5.0f, 0.0f };
void motion(int x,int y)
{
    light_position[0]=(x-150)/150*2;
    light_position[1]=-(y-150)/150*2;  //150是正中間的位置
    glLightfv(GL_LIGHT0,GL_POSITION,light_position);
    glutPostRedisplay();
}

glutPassiveMotionFunc(motion);

根據滑鼠位置(不用按)就可以改變光源位置


[耍費]WEEK6

1.至moodle下載codeblockMinGw、freeglut、week5.zip
2.安裝及解壓縮,把codeblock的Compiler的c++11的勾拿掉
3.編譯並執行week5.cbp之後,應該如下

4.至http://www.cmlab.csie.ntu.edu.tw/~jsyeh/3dcg10/  下載soure.zip檔並解壓縮
5.用notepad++開啟transformatiom.c
6.用ctrl+F開啟搜尋
7.找到
    glEnable(GL_LIGHT0);

    glEnable(GL_LIGHTING);

    glLightfv(GL_LIGHT0, GL_POSITION, pos);
//第一個參數是指定要改變光源屬性的光源 
//在Windows下有八個光源可用,分別是GL_LIGHT0~GL_LIGHT7
//第二個參數指定要修改的是3種光
//環境光(Ambient Light)、散射光(Diffuse Light)、反射光(Specular Light) 的哪一種 
//第三個參數放入一個陣列的指標(就是陣列的名字) 
//陣列中有4個數,代表RGBA顏色值 
    glEnable(GL_DEPTH_TEST);
//OpenGL在繪製的時候就會檢查,當前像素前面是否有別的像素
//如果別的像素擋道了它,那它就不會繪製,也就是說,OpenGL就只繪製最前面的一層。

放到
    glutDisplayFunc(display);

    glutMainLoop();
之間
再將GLfloat pos[] = { 0.0, 0.0, -1.0, 0.0 };放到全域變數//光源的位置

8.執行結果





9.glutMouseFunc(mouse);
    glutKeyboardFunc(keyboard);
    glutMotionFunc(motion);

2017年10月12日 星期四

1.開啟教授的網頁 http://www.cmlab.csie.ntu.edu.tw/~jsyeh/3dcg10/
2.下載紅圈內的三個檔案

3.將三個檔案放在同個資料夾,把所有壓縮檔解壓縮至此

4.開啟Transformation.exe,用滑鼠拖曳綠色數字,可以了解opengl這三個函式的意義




5.開啟Projection.exe


按右鍵可以選gluPerspective、glOrtho、glFrustum
gluPerspective
(視野,y/x比例,螢幕畫面遠近,背景畫面遠近)




glOrtho
()































2017年10月5日 星期四

[耍廢]WEEK4

1.執行並建立GLUT專案
2.

函式說明:
「右手方向 opengl」的圖片搜尋結果
glRotated(旋轉角度,旋轉軸XYZ方向向量),角度可以隨時間變化,旋轉軸會通過物體中心
glTranslated(移動物體的XYZ長度值),移動物體(但旋轉軸不會被移動)
glScale(物體的XYZ方向的大小變換比例),1是維持,大於1是放大,小於1是縮小


2017年9月28日 星期四

[耍廢]WEEK3

第一堂課
1.新建一個plut專案(WEEK1的內容)
2.將裡面的code用學姐的code取代



第二堂課
1.下載3d exploration
http://www.freespacemods.net/e107_plugins/download/download.php?view.129
2.到這個網站  http://www.cmlab.csie.ntu.edu.tw/~jsyeh/3dcg10/  
下載05/04旁的EXAMPLE的DATA連結,解壓縮後有很多3D模型
3.用3d exploration開啟剛剛解壓縮的模型(我用的是soccer)

4.將soccer另存新檔
5.選擇.cpp存檔
6.按下Remender me ,  later兩個鍵


7.選擇smaple app然後存檔


8.開新的一個glut專案
9.把剛剛存的cpp的code取代專案的原本的sample
10.把有問題的code註解掉

11.執行




2017年9月21日 星期四

[耍廢]WEE2的紀錄


1.開始使用GLUT

#include <GL/glut.h>
int main(int argc, char *argv[])
{
       glutInit(&argc, argv);
       glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
       glutCreateWindow("GLUT Shapes");//建立視窗," "裡是視窗上面那條要顯示的名稱
       glutDisplayFunc(display);//開始顯示圖形
       glutMainLoop();//loop

       return 0;
}
//基本的GLUT程式

2.顯示出一個茶壺
#include <GL/glut.h>

static void display(void)
{
    glClearColor(1,1,1,0);
     //設定背景色(R,G,B,A/*透明度*/)避免畫面殘留, 0到1,
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
     //清掉背景的顏色改為剛剛設定的顏色
    glColor3d(1,0,0);
     //類似筆刷,設定下次顯示的顏色
    glutSolidTeapot(0.3);
     //顯示茶壺,數字設定圖片大小(比例)
    glutSwapBuffers();
     //變更顯示的緩衝區,將新的畫面從緩衝區放到畫面上
}
int main(int argc, char *argv[])
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);

    glutCreateWindow(argv[0]);
    glutDisplayFunc(display);

    glutMainLoop();

    return EXIT_SUCCESS;
}




3.建立一個三角形

#include <GL/glut.h>

static void display(void)
{
    glClearColor(1,1,1,0);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glColor3d(1,0,0);
    glBegin(GL_POLYGON);//開始建立多邊型
    glVertex3f(1,-1,0);//設定三個座標,3f的3表示三個點,f表示float
    glVertex3f(-1,-1,0);
    glVertex3f(0,1,0);
    glEnd();//結束建立多邊形
    glutSwapBuffers();
}
int main(int argc, char *argv[])
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);

    glutCreateWindow(argv[0]);
    glutDisplayFunc(display);

    glutMainLoop();

    return EXIT_SUCCESS;
}