OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include <assert.h> | 5 #include <assert.h> |
6 #include <math.h> | 6 #include <math.h> |
7 #include <ppapi/c/ppb_input_event.h> | 7 #include <ppapi/c/ppb_input_event.h> |
8 #include <ppapi/cpp/input_event.h> | 8 #include <ppapi/cpp/input_event.h> |
9 #include <ppapi/cpp/var.h> | 9 #include <ppapi/cpp/var.h> |
10 #include <ppapi/cpp/var_array.h> | 10 #include <ppapi/cpp/var_array.h> |
(...skipping 365 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
376 return ps_context_->data + x + y * ps_context_->stride / sizeof(uint32_t); | 376 return ps_context_->data + x + y * ps_context_->stride / sizeof(uint32_t); |
377 } | 377 } |
378 | 378 |
379 // This is the meat of the ray tracer. Given a pixel span (x0, x1) on | 379 // This is the meat of the ray tracer. Given a pixel span (x0, x1) on |
380 // scanline y, shoot rays into the scene and render what they hit. Use | 380 // scanline y, shoot rays into the scene and render what they hit. Use |
381 // scanline coherence to do a few optimizations | 381 // scanline coherence to do a few optimizations |
382 void Planet::wRenderPixelSpan(int x0, int x1, int y) { | 382 void Planet::wRenderPixelSpan(int x0, int x1, int y) { |
383 if (!base_tex_ || !night_tex_) | 383 if (!base_tex_ || !night_tex_) |
384 return; | 384 return; |
385 const int kColorBlack = MakeRGBA(0, 0, 0, 0xFF); | 385 const int kColorBlack = MakeRGBA(0, 0, 0, 0xFF); |
| 386 float width = ps_context_->width; |
| 387 float height = ps_context_->height; |
| 388 float min_dim = width < height ? width : height; |
| 389 float offset_x = width < height ? 0 : (width - min_dim) * 0.5f; |
| 390 float offset_y = width < height ? (height - min_dim) * 0.5f : 0; |
386 float y0 = eye_y_; | 391 float y0 = eye_y_; |
387 float z0 = eye_z_; | 392 float z0 = eye_z_; |
388 float y1 = (static_cast<float>(y) / ps_context_->height) * 2.0f - 1.0f; | 393 float y1 = (static_cast<float>(y - offset_y) / min_dim) * 2.0f - 1.0f; |
389 float z1 = 0.0f; | 394 float z1 = 0.0f; |
390 float dy = (y1 - y0); | 395 float dy = (y1 - y0); |
391 float dz = (z1 - z0); | 396 float dz = (z1 - z0); |
392 float dy_dy_dz_dz = dy * dy + dz * dz; | 397 float dy_dy_dz_dz = dy * dy + dz * dz; |
393 float two_dy_y0_y_two_dz_z0_z = 2.0f * dy * (y0 - planet_y_) + | 398 float two_dy_y0_y_two_dz_z0_z = 2.0f * dy * (y0 - planet_y_) + |
394 2.0f * dz * (z0 - planet_z_); | 399 2.0f * dz * (z0 - planet_z_); |
395 float planet_xyz_eye_xyz = planet_xyz_ + eye_xyz_; | 400 float planet_xyz_eye_xyz = planet_xyz_ + eye_xyz_; |
396 float y_y0_z_z0 = planet_y_ * y0 + planet_z_ * z0; | 401 float y_y0_z_z0 = planet_y_ * y0 + planet_z_ * z0; |
397 float oowidth = 1.0f / ps_context_->width; | 402 float oowidth = 1.0f / min_dim; |
398 uint32_t* pixels = this->wGetAddr(x0, y); | 403 uint32_t* pixels = this->wGetAddr(x0, y); |
399 for (int x = x0; x <= x1; ++x) { | 404 for (int x = x0; x <= x1; ++x) { |
400 // scan normalized screen -1..1 | 405 // scan normalized screen -1..1 |
401 float x1 = (static_cast<float>(x) * oowidth) * 2.0f - 1.0f; | 406 float x1 = (static_cast<float>(x - offset_x) * oowidth) * 2.0f - 1.0f; |
402 // eye | 407 // eye |
403 float x0 = eye_x_; | 408 float x0 = eye_x_; |
404 // delta from screen to eye | 409 // delta from screen to eye |
405 float dx = (x1 - x0); | 410 float dx = (x1 - x0); |
406 // build a, b, c | 411 // build a, b, c |
407 float a = dx * dx + dy_dy_dz_dz; | 412 float a = dx * dx + dy_dy_dz_dz; |
408 float b = 2.0f * dx * (x0 - planet_x_) + two_dy_y0_y_two_dz_z0_z; | 413 float b = 2.0f * dx * (x0 - planet_x_) + two_dy_y0_y_two_dz_z0_z; |
409 float c = planet_xyz_eye_xyz + | 414 float c = planet_xyz_eye_xyz + |
410 -2.0f * (planet_x_ * x0 + y_y0_z_z0) - (planet_radius2_); | 415 -2.0f * (planet_x_ * x0 + y_y0_z_z0) - (planet_radius2_); |
411 // calculate discriminant | 416 // calculate discriminant |
(...skipping 372 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
784 // Do simulation, render and present. | 789 // Do simulation, render and present. |
785 earth.Update(); | 790 earth.Update(); |
786 } | 791 } |
787 | 792 |
788 return 0; | 793 return 0; |
789 } | 794 } |
790 | 795 |
791 // Register the function to call once the Instance Object is initialized. | 796 // Register the function to call once the Instance Object is initialized. |
792 // see: pappi_simple/ps_main.h | 797 // see: pappi_simple/ps_main.h |
793 PPAPI_SIMPLE_REGISTER_MAIN(example_main); | 798 PPAPI_SIMPLE_REGISTER_MAIN(example_main); |
OLD | NEW |