0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

2.1. 点を描く

Last updated at Posted at 2015-12-11

3Dの基本的な知識を理解したくて、『3次元CGの基礎と応用[新訂版]( http://goo.gl/GKoQ4y )』という書籍を購入しました。薄くて読みやすそうな本なのですがサンプルソースがないので、勉強がてらサンプルを作りながら読み進めようと思います。誰でもすぐに追試ができるよう、HTML + Javascriptでサンプルを作っていきます。

完全なサンプルはこちら
https://github.com/nakaken0629/3dstudy

2.1. 点を描く

この節では、ラスターグラフィックスで点を描く話です。フレームバッファをHTML5のCanvasを使って実現しています。ただし1dot = 2pxとすることで少し見やすくなるようにしています。

session2_1.png

サンプルコード

session2_1.htmlより
/* 共通処理 */

var canvas;
var ctx;

window.onload = function() {
    canvas = document.getElementById("myCanvas");
    ctx = canvas.getContext("2d");
    main();
};

var createRgb = function(red, green, blue) {
    return "#"
        + ("0" + red.toString(16)).substr(-2)
        + ("0" + green.toString(16)).substr(-2)
        + ("0" + blue.toString(16)).substr(-2)
    ;
};

var getRandomInt = function(min, max) {
      return Math.floor( Math.random() * (max - min + 1) ) + min;
};

/* 個別処理 */

var main = function() {
    for(var i = 0; i < 100; i++) {
        x = getRandomInt(0, 319);
        y = getRandomInt(0, 199);
        red = getRandomInt(0, 255);
        green = getRandomInt(0, 255);
        blue = getRandomInt(0, 255);
        pset(x, y, createRgb(red, green, blue));
    }
}

var pset = function(x, y, rgb) {
    /* 1pxでは見づらいため、2pxとしている */
    ctx.fillStyle = rgb;
    ctx.fillRect(x * 2, y * 2, 2, 2);
};
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?