Pastebin
API
tools
faq
paste
Login
Sign up
Please fix the following errors:
New Paste
Syntax Highlighting
// include Simple Direct-media Layer #include <SDL/SDL.h> #pragma comment( lib, "SDL.lib" ) #include <math.h> // setup some basic data types typedef signed long long s64; typedef unsigned long long u64; typedef unsigned int u32; typedef signed int s32; typedef signed short s16; typedef unsigned char byte; typedef signed long long fp64; // ---- ----- ---- ---- ---- ---- ---- ---- FRAMEWORK SPECIFICS namespace framework { // the title name for this application const char *app_name = "Raytrace Demo"; // size of the video buffer const int vidSize = 512; // pixel size const int vidScale = 1; // video buffer u32 *video = NULL; // number of pixels in the video buffer const int nPixels = vidSize * vidSize; // calculated real SDL video buffer size const int scrRes = vidSize * vidScale; // the real SDL video buffer SDL_Surface *screen = NULL; // rate onTick is called in FPS const int tickFPS = 45; // mouse position int mousex = 0; int mousey = 0; }; // namespace framework using namespace framework; // ---- ----- ---- ---- ---- ---- ---- ---- MATH HELPER FUNCTIONS namespace mathHelper { static u32 randSeed = 0xBABEFACE; static s64 absi( s64 a ) { if ( a < 0 ) return -a; else return a; } static s64 maxi( s64 a, s64 b ) { if ( a > b ) return a; else return b; } static s64 mini( s64 a, s64 b ) { if ( a < b ) return a; else return b; } inline u32 randi( void ) { randSeed *= 1103515245; randSeed += 12345; return (randSeed >> 15) & 0xFFFF; } inline u32 scale( u32 a, byte scale ) { return (a * scale) >> 8; } }; // namespace mathHelper using namespace mathHelper; // ---- ----- ---- ---- ---- ---- ---- ---- FIXED POINT HELPER FUNCTIONS namespace fixedPointHelper { const fp64 fp_1 = 0x10000; fp64 fp_mul( fp64 a, fp64 b ) { return (a * b) >> 16; } fp64 fp_div( fp64 a, fp64 b ) { return (a << 16) / b; } fp64 fp_ceil( fp64 a ) { return (a | 0xFFFF) + 1; } fp64 fp_floor( fp64 a ) { return a & (~0xFFFF); } fp64 fp_fract( fp64 a ) { return a & 0xFFFF; } fp64 fp_ifract( fp64 a ) { return 0xFFFF - (a & 0xFFFF); } int fp_to_int( fp64 a ) { return (int) (a >> 16); } fp64 int_to_fp( int a ) { return (a << 16); } fp64 fp_rand( int max ) { return ((randi() % max) << 16) | (randi() & 0xFFFF); } }; // namespace fixedPointHelper using namespace fixedPointHelper; // ---- ----- ---- ---- ---- ---- ---- ---- DRAWING HELPER FUNCTIONS namespace videoHelper { static u32 drawColour = 0xFFFFFF; inline void plot( int x, int y ) { bool out = x < 0; out |= y < 0; out |= x >= vidSize; out |= y >= vidSize; if ( out ) return; video[ x + y * vidSize ] = drawColour; } inline u32 readPixel( int x, int y ) { bool out = x < 0; out |= y < 0; out |= x >= vidSize; out |= y >= vidSize; if ( out ) return 0; return video[ x + y * vidSize ]; } void circle( s32 x0, s32 y0, s32 radius ) { s32 error = 1 - radius; s32 errorY = 1; s32 errorX = -2 * radius; s32 x = radius, y = 0; plot(x0, y0 + radius); plot(x0, y0 - radius); plot(x0 + radius, y0); plot(x0 - radius, y0); while(y < x) { if (error > 0) { x--; errorX += 2; error += errorX; } { y++; errorY += 2; error += errorY; } plot(x0 + x, y0 + y); plot(x0 - x, y0 + y); plot(x0 + x, y0 - y); plot(x0 - x, y0 - y); plot(x0 + y, y0 + x); plot(x0 - y, y0 + x); plot(x0 + y, y0 - x); plot(x0 - y, y0 - x); } } void line( s32 X0, s32 Y0, s32 X1, s32 Y1 ) { s32 incrementVal = 0; s32 shortLen = Y1 - Y0; s32 longLen = X1 - X0; bool yLonger = false; s32 decInc = 0; s32 endVal = 0; s32 j = 0; if ( absi( shortLen ) > absi( longLen ) ) { int swap = shortLen; shortLen = longLen; longLen = swap; yLonger = true; } endVal = longLen; if ( longLen < 0 ) { incrementVal = -1; longLen = -longLen; } else incrementVal = 1; if ( longLen == 0 ) decInc = 0; else decInc = (shortLen << 16) / longLen; if ( yLonger ) { for ( s32 i=0; i!=endVal; i+=incrementVal ) { plot( X0 + (j >> 16), Y0 + i ); j += decInc; } } else { for ( s32 i=0; i!=endVal; i+=incrementVal ) { plot( X0 + i, Y0 + (j >> 16) ); j += decInc; } } } }; // namesapce videoHelper using namespace videoHelper; // ---- ----- ---- ---- ---- ---- ---- ---- DEMO CODE ( the good stuff ) namespace demo { struct vec3 { float x, y, z; vec3( ) : x( 0 ), y( 0 ), z( 0 ) { } vec3( float _tx, float _ty, float _tz ) : x( _tx ), y( _ty ), z( _tz ) { } void normalize( void ) { float d = 1.0f / sqrtf( x*x + y*y + z*z ); x *= d; y *= d; z *= d; } vec3 operator - ( const vec3 &a ) { return vec3( x - a.x, y - a.y, z - a.z ); } vec3 operator + ( const vec3 &a ) { return vec3( x + a.x, y + a.y, z + a.z ); } float operator * ( const vec3 &a ) { return x*a.x + y*a.y + z*a.z; } vec3 operator * ( const float s ) { return vec3( x*s, y*s, z*s ); } float lengthSqr( void ) { return x*x + y*y + z*z; } }; struct sSphere { sSphere( ) : origin( 0, 0, 0 ), radius( 0 ) { } sSphere( float x, float y, float z, float r ) : origin( x, y, z ), radius( r ) { } sSphere( float x, float y, float z, float r, u32 c ) : origin( x, y, z ), radius( r ), colour( c ) { } vec3 origin; float radius; u32 colour; }; struct sRay { vec3 origin; vec3 normal; }; u32 randColour( void ) { return ((randi()&0xFF) << 16 ) | ((randi()&0xFF) << 8 ) | ((randi()&0xFF) ); } bool intersect( sRay &ray, sSphere &sphere, vec3 &ipoint ) { vec3 EO = sphere.origin - ray.origin; float v = EO * ray.normal; float disc = sphere.radius * sphere.radius; disc -= (EO*EO) - (v*v); if ( disc < 0.0 ) return false; float d = sqrtf( disc ); ipoint = ray.origin + ray.normal * (v-d); return true; } const int nSpheres = 3; sSphere spheres[ nSpheres ] = { sSphere( 0, 32, 315, 96, randColour( ) ), sSphere( -128, -32, 512, 48, randColour( ) ), sSphere( 0, -64, 256, 64, randColour( ) ) }; void onInit( void ) { /* for ( int i=0; i<nSpheres; i++ ) { sSphere &s = spheres[i]; s.origin.x = (float) ( randi() % 256) - 128; s.origin.y = (float) ( randi() % 256) - 128; s.origin.z = (float) ( randi() % 256) + 128; s.radius = (float) ( randi() % 5) + 8; s.colour = randColour( ); } */ } void onKeyHit( SDLKey key ) { switch ( key ) { case ( SDLK_SPACE ): onInit( ); } } u32 rayShoot( sRay &ray ) { // float min = -1.0f; u32 colour = 0; // for ( int i=0; i<nSpheres; i++ ) { vec3 ipoint( 0, 0, 0 ); if ( intersect( ray, spheres[i], ipoint ) ) { float dst = ipoint.lengthSqr( ); if ( dst < min || min < 0.0f ) { min = dst; vec3 normal = (ipoint - spheres[i].origin); float irad = 1.0f / spheres[i].radius; normal.x *= irad; normal.y *= irad; normal.z *= irad; int r = (int)(127.0 + 127.0 * normal.x); int g = (int)(127.0 + 127.0 * normal.y); int b = (int)(127.0 - 127.0 * normal.z); // colour = spheres[i].colour; colour = (r<<16) | (g<<8) | b; } } } return colour; } void incWrap( float &x, float y ) { const float _2PI = 6.28318530718f; if ( (x+=y) > _2PI ) x -= _2PI; } float t = 0.0; void onTick( void ) { float hvs = vidSize / 2.0; for ( int y=0; y<vidSize; y++ ) { for ( int x=0; x<vidSize; x++ ) { sRay ray = { vec3( ), vec3( x-hvs, y-hvs, hvs*2 ) }; ray.normal.normalize( ); drawColour = rayShoot( ray ); plot( x, y ); } } incWrap( t, 0.01f ); spheres[0].origin.x = sin( (float)t ) * 256.0f; } }; // namespace demo using namespace demo; // ---- ----- ---- ---- ---- ---- ---- ---- FRAMEWORK FUNCIONS namespace framework { static bool app_init( void ) { if ( SDL_Init( SDL_INIT_VIDEO ) != 0 ) return false; SDL_WM_SetCaption( app_name, NULL ); screen = SDL_SetVideoMode( scrRes, scrRes, 32, 0 ); if ( screen == NULL ) return false; if ( vidScale == 1 ) video = (u32*)screen->pixels; else video = new u32[ nPixels ]; return true; } static void blit_2x( void ) { // const int stride = screen->w; u32 *dst = (u32*) screen->pixels; int a = 0; for ( int i=0; i<nPixels; i++ ) { u32 colour = video[ i ]; // x4 pixel write dst[0 ] = colour; dst[1 ] = colour; dst[ stride] = colour; dst[1+stride] = colour; dst += 2; if ( a++ >= vidSize ) { a = 0; // add on ONE stride not two since one length // has already been walked dst += stride; } } } static void app_draw( void ) { if ( vidScale <= 1 ) return; if ( vidScale == 2 ) { blit_2x( ); return; } for ( int i=0; i<nPixels; i++ ) { SDL_Rect rect = { (i % vidSize) * vidScale, (i / vidSize) * vidScale, vidScale, vidScale }; SDL_FillRect( screen, &rect, video[ i ] ); } } static bool app_tick( void ) { static bool active = true; SDL_Event event; while ( SDL_PollEvent( &event ) ) { switch ( event.type ) { case ( SDL_QUIT ): active = false; break; case ( SDL_KEYUP ): { if ( event.key.keysym.sym == SDLK_ESCAPE ) active = false; else onKeyHit( event.key.keysym.sym ); } break; } } return active; } static void app_quit( void ) { // if ( video != NULL ) if ( video != screen->pixels ) delete [] video; video = NULL; // if ( screen != NULL ) SDL_FreeSurface( screen ); // SDL_Quit( ); } }; // namespace framework // windows entry point int __stdcall WinMain( int, int, int, int ) { // start up SDL atexit( app_quit ); if (! app_init( ) ) return 1; // get keyboard access u32 keyCount = 0; byte *keys = SDL_GetKeyState( (int*) &keyCount ); // seed the random randSeed ^= SDL_GetTicks( ); // call the on initialize function onInit( ); // int oldTicks = SDL_GetTicks( ); // work out the tick threshold in milliseconds const int tickThresh = 1000 / tickFPS; // while ( app_tick( ) ) { int diff = SDL_GetTicks() - oldTicks; if ( diff < 10 ) SDL_Delay( 10 ); if ( diff > 500 ) { oldTicks += diff; continue; } while ( diff > tickThresh ) { // grab the mouse state SDL_GetMouseState( &mousex, &mousey ); // tick the application onTick( ); app_draw( ); SDL_Flip( screen ); // progress the tick count oldTicks += tickThresh; diff -= tickThresh; } } return 0; }
Optional Paste Settings
Category:
None
Cryptocurrency
Cybersecurity
Fixit
Food
Gaming
Haiku
Help
History
Housing
Jokes
Legal
Money
Movies
Music
Pets
Photo
Science
Software
Source Code
Spirit
Sports
Travel
TV
Writing
Tags:
Syntax Highlighting:
None
Bash
C
C#
C++
CSS
HTML
JSON
Java
JavaScript
Lua
Markdown (PRO members only)
Objective C
PHP
Perl
Python
Ruby
Swift
4CS
6502 ACME Cross Assembler
6502 Kick Assembler
6502 TASM/64TASS
ABAP
AIMMS
ALGOL 68
APT Sources
ARM
ASM (NASM)
ASP
ActionScript
ActionScript 3
Ada
Apache Log
AppleScript
Arduino
Asymptote
AutoIt
Autohotkey
Avisynth
Awk
BASCOM AVR
BNF
BOO
Bash
Basic4GL
Batch
BibTeX
Blitz Basic
Blitz3D
BlitzMax
BrainFuck
C
C (WinAPI)
C Intermediate Language
C for Macs
C#
C++
C++ (WinAPI)
C++ (with Qt extensions)
C: Loadrunner
CAD DCL
CAD Lisp
CFDG
CMake
COBOL
CSS
Ceylon
ChaiScript
Chapel
Clojure
Clone C
Clone C++
CoffeeScript
ColdFusion
Cuesheet
D
DCL
DCPU-16
DCS
DIV
DOT
Dart
Delphi
Delphi Prism (Oxygene)
Diff
E
ECMAScript
EPC
Easytrieve
Eiffel
Email
Erlang
Euphoria
F#
FO Language
Falcon
Filemaker
Formula One
Fortran
FreeBasic
FreeSWITCH
GAMBAS
GDB
GDScript
Game Maker
Genero
Genie
GetText
Go
Godot GLSL
Groovy
GwBasic
HQ9 Plus
HTML
HTML 5
Haskell
Haxe
HicEst
IDL
INI file
INTERCAL
IO
ISPF Panel Definition
Icon
Inno Script
J
JCL
JSON
Java
Java 5
JavaScript
Julia
KSP (Kontakt Script)
KiXtart
Kotlin
LDIF
LLVM
LOL Code
LScript
Latex
Liberty BASIC
Linden Scripting
Lisp
Loco Basic
Logtalk
Lotus Formulas
Lotus Script
Lua
M68000 Assembler
MIX Assembler
MK-61/52
MPASM
MXML
MagikSF
Make
MapBasic
Markdown (PRO members only)
MatLab
Mercury
MetaPost
Modula 2
Modula 3
Motorola 68000 HiSoft Dev
MySQL
Nagios
NetRexx
Nginx
Nim
NullSoft Installer
OCaml
OCaml Brief
Oberon 2
Objeck Programming Langua
Objective C
Octave
Open Object Rexx
OpenBSD PACKET FILTER
OpenGL Shading
Openoffice BASIC
Oracle 11
Oracle 8
Oz
PARI/GP
PCRE
PHP
PHP Brief
PL/I
PL/SQL
POV-Ray
ParaSail
Pascal
Pawn
Per
Perl
Perl 6
Phix
Pic 16
Pike
Pixel Bender
PostScript
PostgreSQL
PowerBuilder
PowerShell
ProFTPd
Progress
Prolog
Properties
ProvideX
Puppet
PureBasic
PyCon
Python
Python for S60
QBasic
QML
R
RBScript
REBOL
REG
RPM Spec
Racket
Rails
Rexx
Robots
Roff Manpage
Ruby
Ruby Gnuplot
Rust
SAS
SCL
SPARK
SPARQL
SQF
SQL
SSH Config
Scala
Scheme
Scilab
SdlBasic
Smalltalk
Smarty
StandardML
StoneScript
SuperCollider
Swift
SystemVerilog
T-SQL
TCL
TeXgraph
Tera Term
TypeScript
TypoScript
UPC
Unicon
UnrealScript
Urbi
VB.NET
VBScript
VHDL
VIM
Vala
Vedit
VeriLog
Visual Pro Log
VisualBasic
VisualFoxPro
WHOIS
WhiteSpace
Winbatch
XBasic
XML
XPP
Xojo
Xorg Config
YAML
YARA
Z80 Assembler
ZXBasic
autoconf
jQuery
mIRC
newLISP
q/kdb+
thinBasic
Paste Expiration:
Never
Burn after read
10 Minutes
1 Hour
1 Day
1 Week
2 Weeks
1 Month
6 Months
1 Year
Paste Exposure:
Public
Unlisted
Private
Folder:
(members only)
Password
NEW
Enabled
Disabled
Burn after read
NEW
Paste Name / Title:
Create New Paste
Hello
Guest
Sign Up
or
Login
Sign in with Facebook
Sign in with Twitter
Sign in with Google
You are currently not logged in, this means you can not edit or delete anything you paste.
Sign Up
or
Login
Public Pastes
웹 보안 - HTML & JavaScript 기초
6 hours ago | 6.45 KB
Trevor-powershell
6 hours ago | 0.15 KB
Halo Evolutions 21
13 hours ago | 1.25 KB
EU Defense Stocks
1 day ago | 0.06 KB
Halo Evolutions 19
1 day ago | 0.42 KB
Halo Evolutions 17
1 day ago | 0.43 KB
Halo Evolutions 16
1 day ago | 0.20 KB
gen5
1 day ago | 0.75 KB
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the
Cookies Policy
.
OK, I Understand
Not a member of Pastebin yet?
Sign Up
, it unlocks many cool features!