Merge pull request #1072 from erwincoumans/master

TinyRenderer, apply backface culling
This commit is contained in:
erwincoumans 2017-04-17 18:15:28 +00:00 committed by GitHub
commit 381eace157
15 changed files with 11824 additions and 10 deletions

View File

@ -158,11 +158,15 @@ end
description = "Double precision version of Bullet"
}
newoption
{
trigger = "audio",
description = "Enable audio"
}
if _OPTIONS["double"] then
defines {"BT_USE_DOUBLE_PRECISION"}
end
configurations {"Release", "Debug"}
configuration "Release"
flags { "Optimize", "EnableSSE2","StaticRuntime", "NoMinimalRebuild", "FloatFast"}
@ -251,6 +255,10 @@ end
language "C++"
if _OPTIONS["audio"] then
include "../examples/TinyAudio"
end
if not _OPTIONS["no-demos"] then
include "../examples/ExampleBrowser"
include "../examples/RobotSimulator"

View File

@ -46,6 +46,7 @@ static GLInstanceGraphicsShape* LoadMeshFromSTL(const char* relativeFileName)
int expectedBinaryFileSize = numTriangles* 50 + 84;
if (expectedBinaryFileSize != size)
{
delete[] memoryBuffer;
return 0;
}

View File

@ -376,7 +376,8 @@ struct MyBroadphaseCallback : public btBroadphaseAabbCallback
if (bodyUniqueId >= 0)
{
m_bodyUniqueIds.push_back(bodyUniqueId);
m_links.push_back(mbl->m_link);
//it is not a multibody, so use -1 otherwise
m_links.push_back(-1);
}
return true;
}

View File

@ -35,7 +35,7 @@ int main(int argc, char* argv[])
CommonExampleInterface* example = StandaloneExampleCreateFunc(options);
example->initPhysics();
for (int i = 0; i < 1000; i++)
for (int i = 0; i < 100000; i++)
{
printf("Simulating step %d\n", i);
example->stepSimulation(1.f / 60.f);

10228
examples/TinyAudio/RtAudio.cpp Normal file

File diff suppressed because it is too large Load Diff

1163
examples/TinyAudio/RtAudio.h Normal file

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,102 @@
#include "TinyAudioExample.h"
#include "../CommonInterfaces/CommonExampleInterface.h"
#include "../CommonInterfaces/CommonGUIHelperInterface.h"
#include "RtAudio.h"
#include "b3AudioListener.h"
#include "b3SoundSource.h"
class TinyAudioExample : public CommonExampleInterface
{
b3AudioListener m_listener;
b3SoundSource m_soundA;
RtAudio m_dac;
GUIHelperInterface* m_guiHelper;
int m_soundIndexA;
public:
TinyAudioExample(struct GUIHelperInterface* helper)
:m_guiHelper(helper)
{
}
virtual ~TinyAudioExample()
{
}
virtual void initPhysics()
{
m_soundIndexA = m_listener.addSoundSource(&m_soundA);
RtAudioFormat format = ( sizeof(double) == 8 ) ? RTAUDIO_FLOAT64 : RTAUDIO_FLOAT32;
RtAudio::StreamParameters parameters;
parameters.deviceId = 1;// dac.getDefaultOutputDevice();
parameters.nChannels = 2;
// The default real-time audio input and output buffer size. If
// clicks are occuring in the input and/or output sound stream, a
// larger buffer size may help. Larger buffer sizes, however, produce
// more latency.
const unsigned int RT_BUFFER_SIZE = 512;
unsigned int bufferFrames = RT_BUFFER_SIZE;
int sampleRate = m_listener.getSampleRate();
m_dac.openStream( &parameters, NULL, format, (unsigned int)sampleRate, &bufferFrames, &b3AudioListener::tick,
(void *)m_listener.getTickData());
// Install an interrupt handler function.
// (void) signal( SIGINT, finish );
m_dac.startStream();
}
virtual void exitPhysics()
{
m_dac.closeStream();
m_listener.removeSoundSource(m_soundIndexA);
}
virtual void renderScene()
{
}
virtual void stepSimulation(float deltaTime)
{
}
virtual void physicsDebugDraw(int debugFlags)
{
}
virtual bool mouseMoveCallback(float x,float y)
{
return false;
}
virtual bool mouseButtonCallback(int button, int state, float x, float y)
{
return false;
}
virtual bool keyboardCallback(int key, int state)
{
return false;
}
void resetCamera()
{
float dist = 4;
float pitch = 52;
float yaw = 35;
float targetPos[3]={0,0,0};
m_guiHelper->resetCamera(dist,pitch,yaw,targetPos[0],targetPos[1],targetPos[2]);
}
};
CommonExampleInterface* TinyAudioExampleCreateFunc(CommonExampleOptions& options)
{
return new TinyAudioExample(options.m_guiHelper);
}
B3_STANDALONE_EXAMPLE(TinyAudioExampleCreateFunc)

View File

@ -0,0 +1,7 @@
#ifndef TINY_AUDIO_EXAMPLE_H
#define TINY_AUDIO_EXAMPLE_H
class CommonExampleInterface* TinyAudioExampleCreateFunc(struct CommonExampleOptions& options);
#endif //TINY_AUDIO_EXAMPLE_H

View File

@ -0,0 +1,132 @@
#include "b3AudioListener.h"
#include "b3SoundSource.h"
template <class T>
inline const T& MyMin(const T& a, const T& b)
{
return a < b ? a : b ;
}
#define MAX_SOUND_SOURCES 128
struct b3AudioListenerInternalData
{
int m_numControlTicks;
double m_sampleRate;
b3SoundSource* m_soundSources[MAX_SOUND_SOURCES];
b3AudioListenerInternalData()
:m_numControlTicks(64),
m_sampleRate(48000)
{
for (int i=0;i<MAX_SOUND_SOURCES;i++)
{
m_soundSources[i] = 0;
}
}
};
b3AudioListener::b3AudioListener()
{
m_data = new b3AudioListenerInternalData();
}
b3AudioListener::~b3AudioListener()
{
delete m_data;
}
int b3AudioListener::addSoundSource(b3SoundSource* source)
{
int soundIndex = -1;
for (int i=0;i<MAX_SOUND_SOURCES;i++)
{
if (m_data->m_soundSources[i]==0)
{
m_data->m_soundSources[i] = source;
soundIndex = i;
break;
}
}
return soundIndex;
}
void b3AudioListener::removeSoundSource(int soundSourceIndex)
{
if (soundSourceIndex >=0 && soundSourceIndex<MAX_SOUND_SOURCES)
{
m_data->m_soundSources[soundSourceIndex] = 0;
}
}
b3AudioListenerInternalData* b3AudioListener::getTickData()
{
return m_data;
}
const b3AudioListenerInternalData* b3AudioListener::getTickData() const
{
return m_data;
}
double b3AudioListener::getSampleRate() const
{
return m_data->m_sampleRate;
}
int b3AudioListener::tick(void *outputBuffer,void *inputBuffer1,unsigned int nBufferFrames,
double streamTime,unsigned int status,void *dataPointer)
{
b3AudioListenerInternalData *data = (b3AudioListenerInternalData *)dataPointer;
register double outs[2],*samples = (double *)outputBuffer;
register double tempOuts[2];
int counter,nTicks = (int)nBufferFrames;
bool done = false;
while(nTicks > 0 && !done)
{
counter = MyMin(nTicks,data->m_numControlTicks);
bool newsynth = true;
if(newsynth)
{
for(int i = 0; i < counter; i++)
{
outs[0] = 0.;
outs[1] = 0.;
//make_sound_double(outs,1);
float numActiveSources = 0;
for (int i=0;i<MAX_SOUND_SOURCES;i++)
{
if (data->m_soundSources[i])
{
if (data->m_soundSources[i]->computeSamples(tempOuts,1, data->m_sampleRate))
{
numActiveSources++;
//simple mixer
outs[0] += tempOuts[0];
outs[1] += tempOuts[1];
}
}
}
//simple mixer
if (numActiveSources)
{
outs[0] *= 1./numActiveSources;
outs[1] *= 1./numActiveSources;
}
*samples++ = outs[0];
*samples++ = outs[1];
}
nTicks -= counter;
}
if(nTicks == 0)
break;
}
return 0;
}

View File

@ -0,0 +1,28 @@
#ifndef B3_AUDIO_LISTENER_H
#define B3_AUDIO_LISTENER_H
class b3SoundSource;
class b3AudioListener
{
struct b3AudioListenerInternalData* m_data;
public:
b3AudioListener();
virtual ~b3AudioListener();
static int tick(void *outputBuffer, void *inputBuffer1, unsigned int nBufferFrames,
double streamTime, unsigned int status, void *dataPointer);
int addSoundSource(b3SoundSource* source);
void removeSoundSource(int soundSourceIndex);
b3AudioListenerInternalData* getTickData();
const b3AudioListenerInternalData* getTickData() const;
double getSampleRate() const;
};
#endif //B3_AUDIO_LISTENER_H

View File

@ -0,0 +1,66 @@
#include "b3SoundSource.h"
#define MY2PI (2.*3.14159265)
#include <math.h>
struct b3SoundOscillator
{
int m_type;
double m_amplitude;
double m_phase;
double m_frequency;
double sampleWaveForm(double sampleRate)
{
while (m_phase >= MY2PI)
m_phase -= MY2PI;
double z = sinf(m_phase);
double sample = m_amplitude*z;
m_phase += MY2PI * (1./sampleRate) * m_frequency;
return sample;
}
b3SoundOscillator()
:m_phase(0),
m_amplitude(0.8),
m_frequency(442.)
{
}
};
struct b3SoundSourceInternalData
{
b3SoundOscillator m_oscillator;
};
b3SoundSource::b3SoundSource()
{
m_data = new b3SoundSourceInternalData();
}
b3SoundSource::~b3SoundSource()
{
delete m_data;
}
bool b3SoundSource::computeSamples(double* sampleBuffer, int numSamples, double sampleRate)
{
double* outputSamples = sampleBuffer;
for (int i=0;i<numSamples;i++)
{
double sample = m_data->m_oscillator.sampleWaveForm(sampleRate);
double sampleLeft = sample;
double sampleRight = sample;
*outputSamples++ = sampleLeft;
*outputSamples++ = sampleRight;
}
return true;
// return false;
}

View File

@ -0,0 +1,16 @@
#ifndef B3_SOUND_SOURCE_H
#define B3_SOUND_SOURCE_H
class b3SoundSource
{
struct b3SoundSourceInternalData* m_data;
public:
b3SoundSource();
virtual ~b3SoundSource();
virtual bool computeSamples(double *sampleBuffer, int numSamples, double sampleRate);
};
#endif //B3_SOUND_SOURCE_H

View File

@ -0,0 +1,38 @@
project "App_TinyAudioExample"
language "C++"
kind "ConsoleApp"
includedirs {
".",
"../../src",
}
defines {"B3_USE_STANDALONE_EXAMPLE", "__STK_REALTIME__"}
files {
"**.cpp",
"**.h",
"../StandaloneMain/main_console_single_example.cpp",
}
if os.is("Windows") then
links {"winmm","Wsock32","dsound"}
defines {"WIN32","__WINDOWS_MM__","__LITTLE_ENDIAN__","__WINDOWS_DS__"}
end
if os.is("Linux") then initX11()
defines {"__OS_LINUX__","__LINUX_ALSA__","__LITTLE_ENDIAN__"}
links {"asound","pthread"}
end
if os.is("MacOSX") then
links{"Cocoa.framework"}
links{"CoreAudio.framework", "coreMIDI.framework", "Cocoa.framework"}
defines {"__OS_MACOSX__","__LITTLE_ENDIAN__"}
end

View File

@ -424,6 +424,16 @@ static void clipEdge(const mat<4,3,float>& triangleIn, int vertexIndexA, int ver
static bool clipTriangleAgainstNearplane(const mat<4,3,float>& triangleIn, b3AlignedObjectArray<mat<4,3,float> >& clippedTrianglesOut)
{
float orientation = (triangleIn[0][1] - triangleIn[0][0]) * (triangleIn[1][2] - triangleIn[1][0])
- (triangleIn[1][1] - triangleIn[1][0]) * (triangleIn[0][2] - triangleIn[0][0]);
if (orientation < 0.0)
{
return true;
}
//discard triangle if all vertices are behind near-plane
if (triangleIn[3][0]<0 && triangleIn[3][1] <0 && triangleIn[3][2] <0)
{

View File

@ -21,10 +21,26 @@ CXX_FLAGS += '-DGWEN_COMPILE_STATIC '
CXX_FLAGS += '-DBT_USE_DOUBLE_PRECISION '
CXX_FLAGS += '-DBT_ENABLE_ENET '
CXX_FLAGS += '-DBT_ENABLE_CLSOCKET '
CXX_FLAGS += '-DB3_DUMP_PYTHON_VERSION '
# libraries += [current_python]
libraries = []
include_dirs = []
try:
import numpy
NP_DIRS = [numpy.get_include()]
except:
print("numpy is disabled. getCameraImage maybe slower.")
else:
print("numpy is enabled.")
CXX_FLAGS += '-DPYBULLET_USE_NUMPY '
for d in NP_DIRS:
print("numpy_include_dirs = %s" % d)
include_dirs += NP_DIRS
sources = ["examples/pybullet/pybullet.c"]\
+["examples/ExampleBrowser/InProcessExampleBrowser.cpp"]\
@ -365,20 +381,18 @@ if _platform == "linux" or _platform == "linux2":
CXX_FLAGS += '-DDYNAMIC_LOAD_X11_FUNCTIONS '
CXX_FLAGS += '-DHAS_SOCKLEN_T '
CXX_FLAGS += '-fno-inline-functions-called-once'
sources = ["examples/ThirdPartyLibs/enet/unix.c"]\
sources = sources + ["examples/ThirdPartyLibs/enet/unix.c"]\
+["examples/OpenGLWindow/X11OpenGLWindow.cpp"]\
+["examples/ThirdPartyLibs/Glew/glew.c"]\
+ sources
+["examples/ThirdPartyLibs/Glew/glew.c"]
elif _platform == "win32":
print("win32!")
libraries = ['Ws2_32','Winmm','User32','Opengl32','kernel32','glu32','Gdi32','Comdlg32']
CXX_FLAGS += '-DWIN32 '
CXX_FLAGS += '-DGLEW_STATIC '
sources = ["examples/ThirdPartyLibs/enet/win32.c"]\
sources = sources + ["examples/ThirdPartyLibs/enet/win32.c"]\
+["examples/OpenGLWindow/Win32Window.cpp"]\
+["examples/OpenGLWindow/Win32OpenGLWindow.cpp"]\
+["examples/ThirdPartyLibs/Glew/glew.c"]\
+sources
+["examples/ThirdPartyLibs/Glew/glew.c"]
elif _platform == "darwin":
print("darwin!")
os.environ['LDFLAGS'] = '-framework Cocoa -framework OpenGL'
@ -404,7 +418,7 @@ setup(
sources = sources,
libraries = libraries,
extra_compile_args=CXX_FLAGS.split(),
include_dirs = ["src","examples/ThirdPartyLibs","examples/ThirdPartyLibs/Glew", "examples/ThirdPartyLibs/enet/include","examples/ThirdPartyLibs/clsocket/src"]
include_dirs = include_dirs + ["src","examples/ThirdPartyLibs","examples/ThirdPartyLibs/Glew", "examples/ThirdPartyLibs/enet/include","examples/ThirdPartyLibs/clsocket/src"]
) ],
classifiers=['Development Status :: 4 - Beta',
'License :: OSI Approved :: zlib/libpng License',