jit.gl.lua で光源・マテリアルを設定して描画:Cycling’74 Max,OpenGL,Jitter

スクリーンショット 2014-01-27 01.30.13


this.autowatch = 1
this.gc = 1

local gl = require("opengl")
local glu = require("opengl.glu")
local GL = gl
local GLU = glu;
local _analogIn = {};

local vertexes = {
 {-0.5,0.5,0.5},
 {-0.5,-0.5,0.5},
 {0.5,-0.5,0.5},
 {0.5,0.5,0.5},
 {-0.5,0.5,-0.5},
 {-0.5,-0.5,-0.5},
 {0.5,-0.5,-0.5},
 {0.5,0.5,-0.5}
};

local lightAmb = {0,0,0,1}
local lightDiff = {1,1,1,1}
local lightSpec = {1,1,1,1}
local lightPos = {1,1,1,0}

local goldAmb = {0.24725,0.1995,0.0745,1}
local goldDiff = {0.75164,0.60648,0.22648,1};
local goldSpec = {0.628281,0.555802,0.366065,1};
local goldShin = 51.2;

local _rot = {0,0,0};
local angle = 0;

function rot(...)

 _rot = {...};
end

function drawCube()

 --top
 gl.Normal(0,1,0);
 gl.Begin(GL.QUADS);
 gl.Vertex(vertexes[1])
 gl.Vertex(vertexes[4])
 gl.Vertex(vertexes[8])
 gl.Vertex(vertexes[5])
 gl.End();

 --buttom
 gl.Normal(0,-1,0)
 gl.Begin(GL.QUADS)
 gl.Vertex(vertexes[6])
 gl.Vertex(vertexes[7]);
 gl.Vertex(vertexes[3]);
 gl.Vertex(vertexes[2]);
 gl.End()

 --front
 gl.Normal(0,0,1)
 gl.Begin(GL.QUADS)
 gl.Vertex(vertexes[1])
 gl.Vertex(vertexes[2])
 gl.Vertex(vertexes[3])
 gl.Vertex(vertexes[4])
 gl.End()

 --back
 gl.Normal(0,0,-1)
 gl.Begin(GL.QUADS)
 gl.Vertex(vertexes[5])
 gl.Vertex(vertexes[8])
 gl.Vertex(vertexes[7])
 gl.Vertex(vertexes[6])
 gl.End()

 --left
 gl.Normal(-1,0,0)
 gl.Begin(GL.QUADS)
 gl.Vertex(vertexes[1])
 gl.Vertex(vertexes[5])
 gl.Vertex(vertexes[6])
 gl.Vertex(vertexes[2])
 gl.End()

 --right
 gl.Normal(1,0,0)
 gl.Begin(GL.QUADS)
 gl.Vertex(vertexes[4])
 gl.Vertex(vertexes[3])
 gl.Vertex(vertexes[7])
 gl.Vertex(vertexes[8])
 gl.End()

end

function scriptload()

 print("scriptload");

end

function draw()

 --init
 gl.ClearColor(1,1,1,1)
 gl.ClearDepth(1)
 gl.Enable(GL.DEPTH_TEST)

 gl.Light(GL.LIGHT0,GL.AMBIENT,lightAmb)
 gl.Light(GL.LIGHT0,GL.DIFFUSE,lightDiff)
 gl.Light(GL.LIGHT0,GL.SPECULAR,lightSpec)

 gl.Enable(GL.LIGHT0)
 gl.Enable(GL.LIGHTING)
 gl.Enable(GL.NORMALIZE)

 gl.Clear(GL.COLOR_BUFFER_BIT,GL.DEPTH_BUFFER_BIT)
 gl.LoadIdentity();
 glu.LookAt({0,0,2},{0,0,0},{0,1,0});

 --light
 gl.Light(GL.LIGHT0,GL.POSITION,lightPos)

 gl.PushMatrix()
 gl.Rotate(_rot)

-- gl.Rotate(angle,0,1,0)
 gl.Material(GL.FRONT_AND_BACK,GL.AMBIENT,goldAmb);
 gl.Material(GL.FRONT_AND_BACK,GL.DIFFUSE,goldDiff);
 gl.Material(GL.FRONT_AND_BACK,GL.SPECULAR,goldSpec)
 gl.Material(GL.FRONT_AND_BACK,GL.SHININESS,goldShin);

 drawCube();

 gl.PopMatrix()

 angle = angle + 1;
 if angle >= 360 then angle = 0 end

end

参考図書

はじめてのOpenGL―ライブラリを使った「3D‐CGプログラミング」の基本  – p.131 からのサンプルを利用しました。MaxのOpenGLとバージョンがあっているため、この書籍は勉強しやすいです。

Leave a Comment