Skip to content

Commit

Permalink
added dynamic scaling support to MD2 models via model.setScale(float)
Browse files Browse the repository at this point in the history
Also refactored and moved some stuff around.
  • Loading branch information
sinisterchipmunk committed Oct 29, 2010
1 parent 4756b8a commit 94fa869
Show file tree
Hide file tree
Showing 33 changed files with 127 additions and 10 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ gem 'json'
# gem 'jsbuilder'

group :development, :test do
gem 'sqlite3-ruby'
gem "rspec-rails", ">= 2.0.0.beta.22"
gem "webrat"
gem "database_cleaner"
Expand Down
2 changes: 2 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ GEM
rspec (~> 2.0.0)
sizes (1.1.1)
spork (0.8.4)
sqlite3-ruby (1.3.1)
term-ansicolor (1.0.5)
thor (0.14.3)
treetop (1.4.8)
Expand All @@ -119,4 +120,5 @@ DEPENDENCIES
rails (= 3.0.0)
rspec-rails (>= 2.0.0.beta.22)
spork
sqlite3-ruby
webrat
6 changes: 5 additions & 1 deletion app/controllers/engine_tests_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,11 @@ def camera
end

def interface
dependencies << 'engine/heightmap'
dependencies << 'engine/heightmap' << "objects/md2"
# Creature.all.each do |creature|
# creature.scale = 0.25
# world << creature
# end
end

def picking
Expand Down
16 changes: 16 additions & 0 deletions app/views/engine_tests/md2.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@
// Find the model's skin, load it and then bind it to the model.
model.addTexture(new Texture("/images/textures/"+$("model").value+".png"));
updateScale();
// init the model facing right (positive X axis)
model.orientation.orient([0,0,1],[0,1,0],[-1,0,0]);
// model.orientation.orient([1,0,0], [0,1,0], [0,0,1]);
Expand Down Expand Up @@ -93,6 +95,15 @@
$('use-lighting').focus();
}
function updateScale()
{
var scale = parseFloat($("scale").value);
if (model && !isNaN(scale))
{
model.setScale(scale);
}
}
function updateLoading()
{
logger.attempt("load update", function()
Expand Down Expand Up @@ -199,6 +210,11 @@
</p>
</td>
</tr>
<tr>
<td colspan='2'>
Scale: <input type='text' id='scale' value='1' onkeyup='updateScale();' />
</td>
</tr>
</table>
<h2>About the Demo</h2>
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion config/application.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
require File.expand_path('../boot', __FILE__)

#require 'rails/all'
#require "active_record/railtie"
require "active_record/railtie"
require "action_controller/railtie"
require "action_mailer/railtie"
require "active_resource/railtie"
Expand Down
13 changes: 13 additions & 0 deletions db/migrate/20101029142639_create_textures.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class CreateTextures < ActiveRecord::Migration
def self.up
create_table :textures do |t|
t.string :path

t.timestamps
end
end

def self.down
drop_table :textures
end
end
14 changes: 14 additions & 0 deletions db/migrate/20101029165040_create_model_textures.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class CreateModelTextures < ActiveRecord::Migration
def self.up
create_table :model_textures do |t|
t.references :creature
t.references :texture

t.timestamps
end
end

def self.down
drop_table :model_textures
end
end
14 changes: 14 additions & 0 deletions db/migrate/20101029165109_create_creatures.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class CreateCreatures < ActiveRecord::Migration
def self.up
create_table :creatures do |t|
t.string :name
t.string :model_name

t.timestamps
end
end

def self.down
drop_table :creatures
end
end
22 changes: 21 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,21 @@
#
# It's strongly recommended to check this file into your version control system.

ActiveRecord::Schema.define(:version => 20100923214142) do
ActiveRecord::Schema.define(:version => 20101029165109) do

create_table "creatures", :force => true do |t|
t.string "name"
t.string "model_name"
t.datetime "created_at"
t.datetime "updated_at"
end

create_table "model_textures", :force => true do |t|
t.integer "creature_id"
t.integer "texture_id"
t.datetime "created_at"
t.datetime "updated_at"
end

create_table "passwords", :force => true do |t|
t.string "secret"
Expand All @@ -24,6 +38,12 @@
t.datetime "updated_at"
end

create_table "textures", :force => true do |t|
t.string "path"
t.datetime "created_at"
t.datetime "updated_at"
end

create_table "users", :force => true do |t|
t.string "email"
t.datetime "created_at"
Expand Down
10 changes: 10 additions & 0 deletions db/seeds.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,13 @@
#
# cities = City.create([{ :name => 'Chicago' }, { :name => 'Copenhagen' }])
# Mayor.create(:name => 'Daley', :city => cities.first)
crafty_texture = Texture.create(:path => "/images/textures/crafty.png");
ogro_texture = Texture.create(:path => "/images/textures/ogro.png");
laalaa_texture = Texture.create(:path => "/images/textures/laalaa.png");
pknight_texture = Texture.create(:path => "/images/textures/pknight.png");

Creature.create([{:name => "Demo - Crafty", :model_name => "crafty", :textures => [crafty_texture]},
{:name => "Demo - Laalaa", :model_name => "laalaa", :textures => [laalaa_texture]},
{:name => "Demo - Ogro", :model_name => "ogro", :textures => [ogro_texture]},
{:name => "Demo - Pknight", :model_name => "pknight",:textures => [pknight_texture]},
])
2 changes: 1 addition & 1 deletion lib/web_gl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ module WebGL
autoload :World, 'web_gl/world'

def data_path
@data_path ||= File.join(Rails.root, "data/models")
@data_path ||= File.join(Rails.root, "app/webgl/models")
end

def data_path=(path)
Expand Down
3 changes: 2 additions & 1 deletion lib/web_gl/world.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ def to_js
if camera.changed?
js.concat "#{camera.js_orient('world.camera')};"
end
objects.each { |obj| js.concat "world.addObject(#{obj.to_js});" }
#objects.each { |obj| js.concat "world.addObject(#{obj.to_js});" }
objects.each { |obj| js.concat obj.to_js("function(obj) { world.addObject(obj); }") }
js.concat "return world;"
js.concat "})()"

Expand Down
32 changes: 27 additions & 5 deletions public/javascripts/objects/md2.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ var MD2 = function() {
vindex = triangle.vertex_indices[j];
var tindex = triangle.texcoord_indices[j];

for (k = 0; k < 3; k++) md2.snapshot.vertices[counter*3+k] = (frame.vertices[vindex*3+k]);
for (k = 0; k < 3; k++) md2.snapshot.vertices[counter*3+k] = (frame.vertices[vindex*3+k]) * md2.scale;
for (k = 0; k < 2; k++) md2.snapshot.textureCoords[counter*2+k] = (md2.model_data.texcoords[tindex][k]);
for (k = 0; k < 3; k++) md2.snapshot.normals[counter*3+k] = (MD2.normals[frame.normal_indices[vindex]][k]);

Expand Down Expand Up @@ -63,11 +63,11 @@ var MD2 = function() {
{
// figure out vertex interpolation
for (i = 0; i < currentFrame.vertices.length; i += 3)
md2.interpolation.vertices[i+j] = targetFrame.vertices[i+j] - currentFrame.vertices[i+j];
md2.interpolation.vertices[i+j] = (targetFrame.vertices[i+j] - currentFrame.vertices[i+j]);

// figure out normal interpolation
for (i = 0; i < currentFrame.normal_indices.length; i++)
md2.interpolation.normals[i*3+j] = MD2.normals[targetFrame.normal_indices[i]][j] - MD2.normals[currentFrame.normal_indices[i]][j];
md2.interpolation.normals[i*3+j] = (MD2.normals[targetFrame.normal_indices[i]][j] - MD2.normals[currentFrame.normal_indices[i]][j]);
}

// reset the timer and mark the interpolation as valid
Expand Down Expand Up @@ -143,8 +143,8 @@ var MD2 = function() {

for (k = 0; k < 3; k++)
{
md2.snapshot.vertices[counter] += md2.interpolation.vertices[vindex*3+k] * percentage;
md2.snapshot.normals[counter] += md2.interpolation.normals[vindex*3+k] * percentage;
md2.snapshot.vertices[counter] += md2.interpolation.vertices[vindex*3+k] * percentage * md2.scale;
md2.snapshot.normals[counter] += md2.interpolation.normals[vindex*3+k] * percentage * md2.scale;
counter++;
}
}
Expand All @@ -168,6 +168,7 @@ var MD2 = function() {
md2.callbacks.anim_complete = function(md2) { ... }
*/
initialize: function($super, json) {
this.scale = 1.0;
this.use_interpolation = true;
this.model_data = json;
this.snapshot = {vertices:[],normals:[],textureCoords:[]};
Expand Down Expand Up @@ -244,6 +245,27 @@ var MD2 = function() {
});
},

setScale: function(scale)
{
// update the current snapshot to reflect the new scale
if (this.snapshot) {
// handle divide-by-zero
if (this.scale == 0)
{
this.scale = scale;
initSnapshot(this);
}
else
{
var i;
for (i = 0; i < this.snapshot.vertices.length; i++)
this.snapshot.vertices[i] = this.snapshot.vertices[i] / this.scale * scale;
this.scale = scale;
}
updateBuffers(this);
}
},

init: function(vertices, colors, textureCoords, normals, indices) {
this.draw_mode = GL_TRIANGLES;
this.fps = 10;
Expand Down
Binary file added vendor/cache/sqlite3-ruby-1.3.1.gem
Binary file not shown.

0 comments on commit 94fa869

Please sign in to comment.