Created
April 13, 2009 03:10
-
-
Save danopia/94239 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require "G3DRuby" | |
include G3D | |
class Mesh | |
def self.quad(s) | |
vertex = Array_Vector3.new | |
vertex.append(Vector3.new(-s, -s, 0), Vector3.new( s, -s, 0), Vector3.new( s, s, 0), Vector3.new(-s, s, 0)) | |
normal = Array_Vector3.new | |
v = Vector3::unitZ | |
normal.append(v, v, v, v) | |
tex = Array_Vector2.new | |
tex.append(Vector2.new(0, 1), Vector2.new(1, 1), Vector2.new(1, 0), Vector2.new(0, 0)) | |
index = Array_int.new | |
index.append(0, 1, 2) | |
index.append(0, 2, 3) | |
return Mesh.new(index, vertex, normal, tex) | |
end | |
# Takes Array_int, Array_Vector3, Array_Vector3, Array_Vector2 | |
def initialize(index, vertex, normal, tex) | |
@varArea = VARArea.create(1024 * 1024, VARArea::WRITE_ONCE) | |
@indexArray = index | |
binormal = Array_Vector3.new | |
tangent = Array_Vector3.new | |
face = Array_MeshAlg_Face.new | |
tmpEdge = Array_MeshAlg_Edge.new | |
tmpVertex = Array_MeshAlg_Vertex.new | |
MeshAlg::computeAdjacency(vertex, index, face, tmpEdge, tmpVertex) | |
MeshAlg::computeTangentSpaceBasis(vertex, tex, normal, face, tangent, binormal) | |
@dbgVertexArray = vertex | |
@dbgTangentArray = tangent | |
@dbgBinormalArray = binormal | |
@dbgNormalArray = normal | |
@vertexArray = VAR_Vector3.new(vertex, @varArea) | |
@texCoordArray = VAR_Vector2.new(tex, @varArea) | |
@normalArray = VAR_Vector3.new(normal, @varArea) | |
@binormalArray = VAR_Vector3.new(binormal, @varArea) | |
@tangentArray = VAR_Vector3.new(tangent, @varArea) | |
end | |
def render(rd) | |
rd.beginIndexedPrimitives() | |
rd.setVertexArray(@vertexArray) | |
rd.setTexCoordArray(0, @texCoordArray) | |
rd.setTexCoordArray(1, @tangentArray) | |
rd.setTexCoordArray(2, @binormalArray) | |
rd.setNormalArray(@normalArray) | |
rd.sendIndices(RenderDevice::TRIANGLES, @indexArray) | |
rd.endIndexedPrimitives() | |
rd.setShader(nil) | |
Draw::vertexVectors(@dbgVertexArray, @dbgTangentArray, rd, Color4.new(Color3::red())) | |
Draw::vertexVectors(@dbgVertexArray, @dbgBinormalArray, rd, Color4.new(Color3::green())) | |
Draw::vertexVectors(@dbgVertexArray, @dbgNormalArray, rd, Color4.new(Color3::blue())) | |
end | |
end | |
class Entity | |
attr_reader :mesh, :cframe | |
def initialize(mesh, cframe) | |
@mesh = mesh | |
@cframe = cframe | |
end | |
def render(rd) | |
rd.pushState | |
rd.setObjectToWorldMatrix(@cframe) | |
@mesh.render(rd) | |
rd.popState | |
end | |
end | |
class GLSLDemo < GApplet | |
def onInit() | |
$app.debugLog.println("Loading shader and textures.") | |
path = $app.dataDir + "../demos/GLSL_Demo/" | |
@sky = Sky::create($app.renderDevice, $app.dataDir + "sky/") | |
@bumpShader = Shader::fromFiles(path+"bump.vert", path+"bump.frag") | |
@textureMap = Texture::fromFile(path+"rockwall.tga"); | |
normal = GImage.new | |
GImage::computeNormalMap(GImage.new(path+"rockwall-bump.tga"), normal, true, true) | |
@normalBumpMap = Texture::fromGImage("rockwall-bump.tga", normal) | |
G3D::debugAssertGLOk() | |
$app.debugCamera.setPosition(Vector3.new(0, 0, 4)) | |
$app.debugCamera.lookAt(Vector3.new(0, 0, 0)) | |
@entity = Entity.new(Mesh::quad(2.0), CoordinateFrame.new()) | |
@bumpScale = 0.05 | |
end | |
def onCleanup() | |
end | |
def onSimulation(rdt, sdt, idt) | |
t = System::getTick() | |
target = @entity.cframe.translation + Vector3.new(Math.cos(t) * 0.5, -1, -1 + Math.sin(t) * 0.5) | |
@entity.cframe.lookAt(target) | |
end | |
def onUserInput(ui) | |
if ui.keyPressed(SDLK_ESCAPE) | |
# Even when we aren't in debug mode, quit on escape. | |
self.endApplet = true; | |
$app.endProgram = true; | |
end | |
if ui.keyPressed(SDLK_p) | |
@bumpScale = (@bumpScale > 0.0) ? 0.0 : 0.05 | |
end | |
end | |
def onGraphics(rd) | |
lighting = LightingParameters.new(G3D::toSeconds(11, 00, 00, AM)) | |
rd.setProjectionAndCameraMatrix($app.debugCamera) | |
rd.setColorClearValue(Color4.new(Color3.new(0.1, 0.5, 1.0))) | |
rd.clear(false, true, true) | |
@sky.render(lighting) | |
rd.enableLighting() | |
rd.setLight(0, GLight::directional(lighting.lightDirection, lighting.lightColor)) | |
rd.setAmbientLightColor(lighting.ambient) | |
camera = rd.getCameraToWorldMatrix() | |
@bumpShader.args.set("wsLightPos", Vector4.new(lighting.lightDirection, 0)) | |
@bumpShader.args.set("wsEyePos", camera.translation) | |
@bumpShader.args.set("texture", @textureMap) | |
@bumpShader.args.set("normalBumpMap", @normalBumpMap) | |
@bumpShader.args.set("reflectivity", 0.0) # 0.35 | |
@bumpShader.args.set("specularity", 0.0) # 0.4 | |
@bumpShader.args.set("environmentMap", @sky.getEnvironmentMap()) | |
@bumpShader.args.set("bumpScale", @bumpScale) | |
rd.setShader(@bumpShader) | |
G3D::debugAssertGLOk() | |
@entity.render(rd) | |
G3D::debugAssertGLOk() | |
rd.setShader(nil) | |
rd.disableLighting() | |
@sky.renderLensFlare(lighting) | |
end | |
end | |
class App < GApp | |
def main() | |
applet = GLSLDemo.new(self) | |
applet.run | |
end | |
end | |
settings = GApp_Settings.new | |
settings.dataDir = "../../g3d-6_10/data/" | |
settings.useNetwork = false | |
settings.window.fsaaSamples = 4 | |
settings.window.width = 800 | |
settings.window.height = 600 | |
$app = App.new(settings) | |
$app.window.setCaption("GLSL Demo") | |
$app.setDebugMode(true) | |
$app.debugController.setActive(true) | |
$app.run() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment