var world, camera, camera2, gamePaused, jump, boxes, shady, mD, cF, carControl, currentKey, groundControl,progresstext; var unitsToMeters = 0.66 var pause = function () { gamePaused = !gamePaused } window.setup = function () { setGL("agl"); gamePaused = false var agl = new aexolGL(); world = new Scene(); camera = new Camera(1) camera2 = new Camera(1) PATH = '/media/California/' PATH_OBJ = '/media/California/' TRACK_PATH = '/media/California/' galaktyka = basicShader({useSky: true}) airportShader = basicShader({useLights: true, useDiffuse: true, useTiling: true, useBump: true}) lakier2 = basicShader({useLights: true, useReflection: true}) lakier = basicShader({useLights: true}) szyba = basicShader({useLights: true, useAlpha: true, useReflection: true}) grill = basicShader({useLights: true, useDiffuse: true}) reflektory = basicShader({useLights: true, useDiffuse: true}) lights = new Light([ { lightPosition: new Vector(0.0, 150.0, 0.0), attenuation: 400.0, intensity: 0.4, color: [1.0, 0.9, 0.9] }, { lightPosition: new Vector(0.0, 55.0, 100.0), attenuation: 400.0, intensity: 0.4, color: [1.0, 0.9, 0.9] }, { lightPosition: new Vector(0.0, 77.0, -100.0), attenuation: 400.0, intensity: 0.4, color: [1.0, 0.9, 0.9] }, { lightPosition: new Vector(100.0, 66.0, 0.0), attenuation: 400.0, intensity: 0.4, color: [1.0, 0.9, 0.9] }, { lightPosition: new Vector(-100.0, 77.0, 0.0), attenuation: 400.0, intensity: 0.4, color: [1.0, 0.9, 0.9] }, { intensity: 0.2, lightType: 2.0, color: [0.1, 0.2, 0.6] } ]) texGalaxy = Texture.fromCube([PATH + "cube5.png", PATH + "cube3.png", PATH + "cube4.png", PATH + "cube1.png", PATH + "cube2.png", PATH + "cube6.png"], {}) //texGalaxy2 = Texture.fromCube([PATH+"img/galaxy.png",PATH+"img/galaxy.png",PATH+"img/galaxy.png",PATH+"img/galaxy.png",PATH+"img/galaxy.png",PATH+"img/galaxy.png"],{}) boxMesh = Mesh.cube().scale(0.1, 0.1, 0.1) // shady = new Shadow(world,new Vector(3,6,10.0),1) // lights.setShadow(shady) lakierMat = new Material({ color: [1.0, 1.0, 1.0] }) matsSfera = new Material({ color: [0.04, 0.04, 0.08], cube: texGalaxy, reflectionWeight: 0.01 }) sky = new GameObject(world, { mesh: Mesh.sphere(30).scaleUniform(5000.0), shader: galaktyka, material: matsSfera }) car = {} // airport = new GameObject(world, { // shader: airportShader, // material: new Material({color: [0.2, 0.2, 0.34], diffuse: Texture.fromImage(PATH + "roadseamless.jpg", { // wrap: gl.REPEAT // }), bump: Texture.fromImage(PATH + "roadseamless.jpg", { // wrap: gl.REPEAT // })}), // light: lights, // mesh: Mesh.plane().rotate(-90, 0, 0).scaleUniform(5000.0) // }) // airport.uniforms.tiling = [4000.0, 4000.0] // airport.move(0, -0.76, 0) carControl = {} carControl.loaded = false carTextures = { grille1:Texture.fromImage(PATH + "grille1.png"), grille2:Texture.fromImage(PATH + "grille2.png"), lights:Texture.fromImage(PATH + "lights.png"), tire:Texture.fromImage(PATH + "tireA0.png"), nm:Texture.fromImage(PATH + "nodamage.png") } Mesh.obj(PATH_OBJ + "california.obj", function (e) { for (var i in e) { var newMesh = e[i] var shaderAdd = lakier var materialAdd = lakierMat if (i == "glass") { shaderAdd = szyba materialAdd = new Material({color: [0.1, 0.1, 0.1], alpha: 0.2, cube: texGalaxy, reflectionWeight: 0.6}) } if (i == "mirror") { shaderAdd = szyba materialAdd = new Material({color: [0.0, 0.0, 0.0], alpha: 0.9, cube: texGalaxy, reflectionWeight: 0.99}) } else if (i == "black") { materialAdd = new Material({color: [0.01, 0.01, 0.01]}) } else if (i == "badge") { materialAdd = new Material({color: [0.1, 0.5, 0.5]}) } else if (i == "undercarriage") { materialAdd = new Material({color: [0.3, 0.3, 0.22]}) } else if (i == "grille2") { shaderAdd = grill materialAdd = new Material({color: [1.0, 1.0, 1.0], diffuse: carTextures.grille2}) } else if (i == "grille1") { shaderAdd = grill materialAdd = new Material({color: [1.0, 1.0, 1.0], diffuse: carTextures.grille1}) } else if (i == "lights0" || i == "lights1") { shaderAdd = reflektory materialAdd = new Material({color: [1.0, 1.0, 1.0], diffuse: carTextures.lights}) } else if (i == "inner_rim" || i == "inner_rim0" || i == "inner_rim1" || i == "inner_rim2") { shaderAdd = lakier2 materialAdd = new Material({color: [0.0, 0.0, 0.0], cube: texGalaxy, reflectionWeight: 0.2}) } else if (i == "tire" || i == "tire0" || i == "tire1" || i == "tire2") { shaderAdd = reflektory materialAdd = new Material({color: [0.0, 0.0, 0.0], diffuse: carTextures.tire, specularWeight: 0.3, shininess: 10.0}) } else if (i == "Material__80" || i == "Material__800" || i == "Material__801" || i == "Material__802") { shaderAdd = lakier2 materialAdd = new Material({color: [0.0, 0.0, 0.0], cube: texGalaxy, reflectionWeight: 0.8, specularWeight: 1.0, shininess: 3.0}) } else if (i == "c0") { shaderAdd = lakier2 materialAdd = new Material({color: [1.0, 0.0, 0.0], cube: texGalaxy, reflectionWeight: 0.3, specularWeight: 1.0, shininess: 95.0}) } else if (i == "nodemg") { shaderAdd = grill materialAdd = new Material({color: [1.0, 0.0, 1.0], diffuse: carTextures.nm}) } else { } car[i] = new GameObject(world, { shader: shaderAdd, material: materialAdd, light: lights, mesh: newMesh.rotate(-90, 0, 0) }) car[i].aex.centerPivot() } var circ = car.tire.mesh.getAABB() carControl.wheelCircuit = Math.abs(circ.min.z - circ.max.z) * Math.PI carControl["wheel_fl_speed"] = new Pivot() carControl["wheel_fl_speed"].add(car.tire).add(car.inner_rim).add(car.Material__80).setPivot(car.tire.position) carControl["wheel_fl"] = new Pivot() carControl["wheel_fl"].add(carControl["wheel_fl_speed"]).setPivotToCenter() carControl["wheel_rl_speed"] = new Pivot() carControl["wheel_rl_speed"].add(car.tire0).add(car.inner_rim0).add(car.Material__800).setPivot(car.tire0.position) carControl["wheel_rl"] = new Pivot() carControl["wheel_rl"].add(carControl["wheel_rl_speed"]).setPivotToCenter() carControl["wheel_fr_speed"] = new Pivot(), carControl["wheel_fr_speed"].add(car.tire1).add(car.inner_rim1).add(car.Material__801).setPivot(car.tire1.position) carControl["wheel_fr"] = new Pivot(), carControl["wheel_fr"].add(carControl["wheel_fr_speed"]).setPivotToCenter() carControl["wheel_rr_speed"] = new Pivot() carControl["wheel_rr_speed"].add(car.tire2).add(car.inner_rim2).add(car.Material__802).setPivot(car.tire2.position) carControl["wheel_rr"] = new Pivot() carControl["wheel_rr"].add(carControl["wheel_rr_speed"]).setPivotToCenter() carControl["front_axis"] = new Pivot() carControl["front_axis"].add(carControl["wheel_fl"]).add(carControl["wheel_fr"]).setPivotToCenter() carControl["rear_axis"] = new Pivot() carControl["rear_axis"].add(carControl["wheel_rl"]).add(carControl["wheel_rr"]).setPivotToCenter() carControl["body"] = new Pivot() carControl["body"].add(car.nodmg).add(car.c0).add(car.grille1).add(car.grille2).add(car.lights0).add(car.lights1).add(car.undercarriage).add(car.black).add(car.badge).add(car.mirror).add(car.glass).setPivotToCenter() carControl["car"] = new Pivot() carControl["car"].add(carControl["rear_axis"]).add(carControl["front_axis"]).add(carControl["body"]).setPivotToCenter() carControl.loaded = true }) groundControl = {} groundControl.friction = 0.9 carControl.speed = 3.0 * unitsToMeters carControl.brakeForce = 0.0 carControl.maxWheelRotation = 45.0 carControl.maxTurnReductionSpeed = 200.0 * unitsToMeters carControl.turnStep = 1.4 carControl.speedAccStep = 0.01 carControl.speedDccStep = 0.3 carControl.maxSpeed = 300.0 * unitsToMeters carControl.maxSpeedReverse = 60.0 * unitsToMeters carControl.niceWheelRotationSpeed = 50 carControl.startAcceleration = 45.0 camera.position = new Vector(-3.1343522144750655, 2.633754589780208, 4.034481985756767); camera.rotation = new Vector(20.791638911008675, 44.76376777776517, 0); // camera.on(0.1) mD = false cF = 0 var last currentKey = {} gl.canvas.addEventListener("mousemove", function (e) { if (mD) { cF = (last.x - e.x) / 10.0 last = {x: e.x, y: e} } }) gl.canvas.addEventListener('mousedown', function (e) { mD = true last = {x: e.x, y: e} }, false); gl.canvas.addEventListener('mouseup', function () { mD = false }, false); document.addEventListener("keydown", function (e) { currentKey[e.keyCode] = 1 }) document.addEventListener("keyup", function (e) { currentKey[e.keyCode] = 0 console.log("SPEED :", carControl.speed) }) agl.init(); } function brake() { carControl.brakeForce += 0.0005 carControl.speed *= 1.0 - carControl.brakeForce } function checkKey(which) { var mtr = carControl.maxTurnReductionSpeed var turnFriction = Math.sin(((carControl.speed > mtr ? mtr : carControl.speed) / mtr) * Math.PI / 2.0) var ms = carControl.maxSpeed var acceleration = Math.cos(((carControl.speed > ms ? ms : carControl.speed) / ms) * Math.PI / 2.0) * carControl.startAcceleration //LEFT if (which == 37) { var rotY = carControl.wheel_fl.rotation.y + carControl.turnStep * (1.1 - turnFriction) rotY = rotY <= carControl.maxWheelRotation ? rotY : carControl.maxWheelRotation; carControl.wheel_fl.rotY = rotY carControl.wheel_fr.rotY = rotY } //RIGHT if (which == 39) { var rotY = carControl.wheel_fl.rotation.y - carControl.turnStep * (1.1 - turnFriction) var mwr = carControl.maxWheelRotation rotY = rotY <= mwr ? (rotY >= -mwr ? rotY : -mwr) : mwr; carControl.wheel_fl.rotY = rotY carControl.wheel_fr.rotY = rotY } //UP if (which == 38) { if (carControl.speed < -carControl.speedDccStep) { //brake brake() } else { //accelerate carControl.brakeForce = 0.0 var ns = carControl.speed + carControl.speedAccStep * acceleration ns = ns > carControl.maxSpeed ? carControl.maxSpeed : ns carControl.speed = ns } } //DOWN if (which == 40) { if (carControl.speed > carControl.speedDccStep) { //brake brake() } else { //accelerate reverse carControl.brakeForce = 0.0 var ns = carControl.speed - carControl.speedAccStep * acceleration ns = ns < -carControl.maxSpeedReverse ? -carControl.maxSpeedReverse : ns carControl.speed = ns } } } window.logic = function () { // Camera camera.rotation.y += cF cmY = camera.rotation.y cF *= 0.9 //Wheel rotation if (carControl.loaded) { var realFriction = 1.0 - Math.sin(Math.abs(Math.degToRad * carControl.wheel_fl.rotY) + 0.05) * groundControl.friction; camera.position.x = carControl.car.x - Math.sin(cmY * Math.degToRad) * 7 camera.position.z = carControl.car.z + Math.cos(cmY * Math.degToRad) * 7 var carSpeed = ((((carControl.speed / 3.6) / carControl.wheelCircuit) * 360.0) / 60) var maxR = carControl.niceWheelRotationSpeed carSpeed = carSpeed < maxR ? (carSpeed > -maxR ? carSpeed : -maxR) : maxR; carControl.wheel_fl_speed.rotX += carSpeed carControl.wheel_fr_speed.rotX += carSpeed carControl.wheel_rl_speed.rotX += carSpeed carControl.wheel_rr_speed.rotX += carSpeed activeKeys = {} for (var key in currentKey) { if (currentKey[key] == 1) { checkKey(key) activeKeys[key] = 1 } } // Decceleration if (!( 38 in activeKeys || 40 in activeKeys)) { if (carControl.speed > carControl.speedDccStep) { carControl.speed -= carControl.speedDccStep / 6.0 + carControl.speedDccStep * carControl.speed / carControl.maxSpeed } else if (carControl.speed < -carControl.speedDccStep) { carControl.speed += carControl.speedDccStep / 6.0 + carControl.speedDccStep * carControl.speed / carControl.maxSpeed } else { carControl.speed = 0.0 } } var ry = carControl.wheel_fl.rotY carControl.car.rotY += (ry * carControl.speed) * 0.002 var crY = carControl.car.rotY * Math.degToRad var addSpeed = (carControl.speed / 3.6) / 60.0 * realFriction carControl.car.z += addSpeed * Math.cos(crY) carControl.car.x += addSpeed * Math.sin(crY) } } window.draw = function () { if (!gamePaused) { world.draw(camera) } } glStart(window.setup);
RUN