88 lines
No EOL
2.7 KiB
TypeScript
88 lines
No EOL
2.7 KiB
TypeScript
/// <reference path="./glsl.d.ts" />
|
|
import vert_src from "./vertex.vert";
|
|
import frag_src from "./fragment.frag";
|
|
|
|
import {createProgramFromSource, ProgramError, ShaderError} from "./gl_util";
|
|
import { Camera } from "./camera";
|
|
import * as G from "./glWrapper";
|
|
import { Drawable, RenderState } from "./drawable";
|
|
import { Model } from "./model";
|
|
import { mat4, vec3 } from "gl-matrix";
|
|
|
|
export interface DirectionalLight{
|
|
color : vec3;
|
|
ambientIntensity : number;
|
|
direction : vec3;
|
|
diffuseIntensity : number;
|
|
}
|
|
export interface Material{
|
|
specularIntensity: number;
|
|
shininess: number;
|
|
}
|
|
|
|
export class PhongProgram extends G.GLProgram{
|
|
gl: WebGL2RenderingContext;
|
|
constructor(gl: WebGL2RenderingContext){
|
|
super(createProgramFromSource(gl,vert_src,frag_src));
|
|
this.gl = gl;
|
|
this._viewMat = mat4.create();
|
|
this._projectionMat = mat4.create();
|
|
this._modelMat = mat4.create();
|
|
}
|
|
|
|
private _viewMat : mat4;
|
|
public get viewMat() : mat4 {
|
|
return this._viewMat;
|
|
}
|
|
public set viewMat(v : mat4) {
|
|
if(!mat4.equals(v,this._viewMat)){
|
|
this.setUniformMat4f(this.gl,"viewMat",v);
|
|
this._viewMat = v;
|
|
}
|
|
}
|
|
|
|
private _projectionMat : mat4;
|
|
public get projectionMat() : mat4 {
|
|
return this._projectionMat;
|
|
}
|
|
public set projectionMat(v : mat4) {
|
|
if(!mat4.equals(v,this._projectionMat)){
|
|
this.setUniformMat4f(this.gl,"projectionMat",v);
|
|
this._projectionMat = v;
|
|
}
|
|
}
|
|
|
|
private _modelMat : mat4;
|
|
public get modelMat() : mat4 {
|
|
return this._modelMat;
|
|
}
|
|
public set modelMat(v : mat4) {
|
|
if(!mat4.equals(v,this._modelMat)){
|
|
this.setUniformMat4f(this.gl,"modelMat",v);
|
|
this._modelMat = v;
|
|
}
|
|
}
|
|
|
|
|
|
private _colorTextureID : number;
|
|
public get colorTextureID() : number {
|
|
return this._colorTextureID;
|
|
}
|
|
public set colorTextureID(v : number) {
|
|
if(v !== this._colorTextureID){
|
|
this.setUniform1i(this.gl,"material.colorTexture",v);
|
|
this._colorTextureID = v;
|
|
}
|
|
}
|
|
|
|
updateDirectionalLight(light: DirectionalLight){
|
|
this.setUniform3fv(this.gl,"light.color",light.color);
|
|
this.setUniform1f(this.gl,"light.ambientIntensity",light.ambientIntensity);
|
|
this.setUniform3fv(this.gl,"light.direction",light.direction);
|
|
this.setUniform1f(this.gl,"light.diffuseIntensity",light.diffuseIntensity);
|
|
}
|
|
updateMaterial(material: Material){
|
|
this.setUniform1f(this.gl,"material.shininess",material.shininess);
|
|
this.setUniform1f(this.gl,"material.specularIntensity",material.specularIntensity);
|
|
}
|
|
} |