Skip to content

Commit

Permalink
Further optimization of the code
Browse files Browse the repository at this point in the history
  • Loading branch information
ruofeidu committed Dec 17, 2017
1 parent 5738e28 commit c7d5172
Show file tree
Hide file tree
Showing 33 changed files with 764 additions and 417 deletions.
99 changes: 33 additions & 66 deletions DuEngine/DuEngine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ DuEngine::GC DuEngine::gc;

DuEngine::DuEngine() {
camera = new Camera();
window = Window::GetInstance();
m_window = Window::GetInstance();
}

DuEngine* DuEngine::GetInstance() {
Expand Down Expand Up @@ -55,7 +55,7 @@ void g_timer(int id) {
glutPostRedisplay();
}

// https://stackoverflow.com/questions/12619107/opengl-glut-window-very-slow-why
// https://stackoverflow.com/questions/12619107/opengl-glut-m_window-very-slow-why
void g_render() {
Sleep(1);
#if COMPILE_WITH_TIMER
Expand Down Expand Up @@ -87,9 +87,9 @@ void g_reshape(int width, int height) {
void DuEngine::start(int argc, char* argv[]) {
// setup configuration files and the scene name
if (argc > 1) {
configName = std::string(argv[1]);
config = new DuConfig(configName);
m_sceneName = configName.substr(0, configName.size() - 4);
m_configName = std::string(argv[1]);
config = new DuConfig(m_configName);
m_sceneName = m_configName.substr(0, m_configName.size() - 4);
} else {
config = new DuConfig(DuConfig::DefaultName);
m_sceneName = "default";
Expand Down Expand Up @@ -119,11 +119,11 @@ void DuEngine::start(int argc, char* argv[]) {
m_presetsPath = config->GetStringWithDefault("presets_path", m_shadersPath + "presets/");
m_resourcesPath = config->GetStringWithDefault("resources_path", m_presetsPath);

// setup the default window width and height
// setup the default m_window width and height
m_defaultWidth = config->GetIntWithDefault("window_width", m_defaultWidth);
m_defaultHeight = config->GetIntWithDefault("window_height", m_defaultHeight);
string _windowTitle = config->GetStringWithDefault("window_title", "DuRenderer | " + m_sceneName);
window->init(argc, argv, m_defaultWidth, m_defaultHeight, _windowTitle);
m_window->init(argc, argv, m_defaultWidth, m_defaultHeight, _windowTitle);

// setup recording
m_recording = config->GetBoolWithDefault("recording", m_recording);
Expand All @@ -136,7 +136,7 @@ void DuEngine::start(int argc, char* argv[]) {
initScene();

// bind the glut functions
glutReshapeWindow(window->width, window->height);
glutReshapeWindow(m_window->width, m_window->height);
glutDisplayFunc(g_render);
glutMouseFunc(g_mousePress);
glutMotionFunc(g_mouseMove);
Expand All @@ -155,35 +155,6 @@ void DuEngine::keyboard(unsigned char key, int x, int y, bool up) {
case GLUT_KEY_ESC:
exit(EXIT_SUCCESS);
break;
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
break;
case '-':
case '_':
#if COMPILE_WITH_SH
shadertoy->uniforms->iNumBands--;
if (shadertoy->uniforms->iNumBands < 0)
shadertoy->uniforms->iNumBands = 0;
#endif
break;
case '=':
case '+':
#if COMPILE_WITH_SH
shadertoy->uniforms->iNumBands++;
if (shadertoy->uniforms->iNumBands >= NUM_BANDS)
shadertoy->uniforms->iNumBands = NUM_BANDS - 1;
#endif
break;
default:
break;
}
}

Expand All @@ -197,11 +168,8 @@ void DuEngine::special(int key, int x, int y, bool up) {
switch (key) {
case GLUT_KEY_F1:
// Reset the time
this->shadertoy->uniforms->reset(this->shadertoy->geometry);
shadertoy->uniforms->resetTime();
for (const auto& t : videoTextures) {
t->resetTime();
}
m_shadertoy->reset();
m_textureManager->reset();
break;

case GLUT_KEY_F2:
Expand All @@ -210,11 +178,7 @@ void DuEngine::special(int key, int x, int y, bool up) {
break;

case GLUT_KEY_F5:
// Reset and recompile
shadertoy->uniforms->resetTime();
for (const auto& t : videoTextures) {
t->resetTime();
}
m_shadertoy->recompile();
break;
case GLUT_KEY_F6:
// Video Pause
Expand All @@ -223,20 +187,11 @@ void DuEngine::special(int key, int x, int y, bool up) {
}
case GLUT_KEY_F10:
// Debug Mouse
debug(this->shadertoy->uniforms->getMouseString());
debug(ShaderToyUniforms::GetMouseString());
break;

case GLUT_KEY_F11:
// Full screen
m_fullscreen = !m_fullscreen;
if (m_fullscreen) {
m_defaultWidth = window->width;
m_defaultHeight = window->height;
glutFullScreen();
} else {
glutReshapeWindow(m_defaultWidth, m_defaultHeight);
glutPositionWindow(0, 0);
}
this->toggleFullScreen();
break;
}
}
Expand All @@ -253,20 +208,32 @@ void DuEngine::special(int key, int x, int y, bool up) {
void DuEngine::mousePress(int button, int state, int x, int y) {
if (button != GLUT_LEFT_BUTTON) return;
if (state == GLUT_DOWN) {
shadertoy->uniforms->onMouseDown((float)x, (float)y);
ShaderToyUniforms::OnMouseDown((float)x, (float)y);
}
if (state == GLUT_UP) {
shadertoy->uniforms->onMouseUp((float)x, (float)y);
ShaderToyUniforms::OnMouseUp((float)x, (float)y);
}
}

void DuEngine::mouseMove(int x, int y) {
shadertoy->uniforms->onMouseMove((float)x, (float)y);
ShaderToyUniforms::OnMouseMove((float)x, (float)y);
}

void DuEngine::reshape(int width, int height) {
window->reshape(width, height);
shadertoy->reshape(width, height);
m_window->reshape(width, height);
m_shadertoy->reshape(width, height);
}

void DuEngine::toggleFullScreen() {
m_fullscreen = !m_fullscreen;
if (m_fullscreen) {
m_defaultWidth = m_window->width;
m_defaultHeight = m_window->height;
glutFullScreen();
} else {
glutReshapeWindow(m_defaultWidth, m_defaultHeight);
glutPositionWindow(0, 0);
}
}

int DuEngine::getNumFrameFromVideos() {
Expand All @@ -291,11 +258,11 @@ void DuEngine::takeScreenshot(string folderName) {
if (m_video == nullptr) {
m_video = new cv::VideoWriter();
m_video->open(folderName + "/" + getTimeForFileName() + ".avi", cv::VideoWriter::fourcc('M', 'J', 'P', 'G'),
DEFAULT_RENDER_FPS, cv::Size(window->width, window->height));
DEFAULT_RENDER_FPS, cv::Size(m_window->width, m_window->height));
}
}

cv::Mat img(window->height, window->width, CV_8UC3);
cv::Mat img(m_window->height, m_window->width, CV_8UC3);
glPixelStorei(GL_PACK_ALIGNMENT, (img.step & 3) ? 1 : 4);
glPixelStorei(GL_PACK_ROW_LENGTH, (GLint)img.step / (GLint)img.elemSize());
glReadPixels(0, 0, img.cols, img.rows, GL_BGR_EXT, GL_UNSIGNED_BYTE, img.data);
Expand All @@ -308,7 +275,7 @@ void DuEngine::takeScreenshot(string folderName) {
m_video->release();
}
} else {
cv::imwrite(folderName + "/" + this->configName.substr(0, configName.size() - 4) + "_" + to_string(getFrameNumber()) + ".png", img);
cv::imwrite(folderName + "/" + this->m_configName.substr(0, m_configName.size() - 4) + "_" + to_string(getFrameNumber()) + ".png", img);
}
}

10 changes: 7 additions & 3 deletions DuEngine/DuEngine.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ class DuEngine
void initScene();

protected:
TexturesManager* m_textureManager;

vector<TextureVideo*> videoTextures;
TextureKeyboard* keyboardTexture;
Texture* fontTexture;
Expand All @@ -53,10 +55,10 @@ class DuEngine
static DuEngine *s_Instance;
clock_t beginTime;
Camera* camera;
Window* window;
ShaderToy* shadertoy;
Window* m_window;
ShaderToy* m_shadertoy;
DuConfig* config;
string configName;
string m_configName;
string m_sceneName;
bool m_fullscreen = false;
bool m_recording = false;
Expand All @@ -76,6 +78,8 @@ class DuEngine
int m_defaultHeight = 720;

private:
void toggleFullScreen();

int getNumFrameFromVideos();

void printHelp();
Expand Down
11 changes: 11 additions & 0 deletions DuEngine/DuEngine.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -132,11 +132,15 @@
<ClCompile Include="DuEngine.cpp" />
<ClCompile Include="DuScene.cpp" />
<ClCompile Include="DuUtils.cpp" />
<ClCompile Include="FrameBuffer.cpp" />
<ClCompile Include="Geometry.cpp" />
<ClCompile Include="Main.cpp" />
<ClCompile Include="ShaderProgram.cpp" />
<ClCompile Include="ShaderToy.cpp" />
<ClCompile Include="ShaderToyFrameBuffer.cpp" />
<ClCompile Include="ShaderToyGeometry.h" />
<ClCompile Include="ShaderToyGeommetry.cpp" />
<ClCompile Include="ShaderToyScreenBuffer.cpp" />
<ClCompile Include="ShaderToyUniforms.cpp" />
<ClCompile Include="SHUtils.cpp" />
<ClCompile Include="Textuer2D.cpp" />
Expand All @@ -153,6 +157,7 @@
<ClCompile Include="TextureVideoFile.cpp" />
<ClCompile Include="TextureVideoLightField.cpp" />
<ClCompile Include="TextureVideoSequence.cpp" />
<ClCompile Include="Uniforms.cpp" />
<ClCompile Include="Window.cpp" />
</ItemGroup>
<ItemGroup>
Expand All @@ -162,9 +167,14 @@
<ClInclude Include="DuConfig.h" />
<ClInclude Include="DuEngine.h" />
<ClInclude Include="DuUtils.h" />
<ClInclude Include="FrameBuffer.h" />
<ClInclude Include="Geometry.h" />
<ClInclude Include="HashVec2b.h" />
<ClInclude Include="ShaderToyFrameBuffer.h" />
<ClInclude Include="ShaderToyScreenBuffer.h" />
<ClInclude Include="ShaderProgram.h" />
<ClInclude Include="ShaderToy.h" />
<ClInclude Include="ShaderToyUniforms.h" />
<ClInclude Include="SHUtils.h" />
<ClInclude Include="stdafx.h" />
<ClInclude Include="Texture.h" />
Expand All @@ -181,6 +191,7 @@
<ClInclude Include="TextureVideoFile.h" />
<ClInclude Include="TextureVideoLightField.h" />
<ClInclude Include="TextureVideoSequence.h" />
<ClInclude Include="Uniforms.h" />
<ClInclude Include="Window.h" />
</ItemGroup>
<ItemGroup>
Expand Down
67 changes: 59 additions & 8 deletions DuEngine/DuEngine.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,24 @@
<Filter Include="Source Files\Engine">
<UniqueIdentifier>{f68966bc-5adb-41e5-b82e-7d2163c664df}</UniqueIdentifier>
</Filter>
<Filter Include="Header Files\FrameBuffer">
<UniqueIdentifier>{e67326d2-b92f-4f7f-8c55-595248ec1f6e}</UniqueIdentifier>
</Filter>
<Filter Include="Header Files\Geometry">
<UniqueIdentifier>{8ad2ba01-9671-48e6-a1b8-efbf94354473}</UniqueIdentifier>
</Filter>
<Filter Include="Source Files\Geometry">
<UniqueIdentifier>{7f801b85-f8d5-4aff-9a9e-343eef603810}</UniqueIdentifier>
</Filter>
<Filter Include="Header Files\Uniforms">
<UniqueIdentifier>{aab86be6-05ea-4c79-82ab-465d1a732ff6}</UniqueIdentifier>
</Filter>
<Filter Include="Source Files\Uniforms">
<UniqueIdentifier>{44630626-0861-4096-b358-58a7dd1ccf89}</UniqueIdentifier>
</Filter>
<Filter Include="Source Files\FrameBuffer">
<UniqueIdentifier>{c65a8049-f13a-4d9d-90b9-209d6fd95355}</UniqueIdentifier>
</Filter>
</ItemGroup>
<ItemGroup>
<ClCompile Include="SHUtils.cpp">
Expand Down Expand Up @@ -120,23 +138,35 @@
<ClCompile Include="TexturesManager.cpp">
<Filter>Source Files\Textures</Filter>
</ClCompile>
<ClCompile Include="ShaderToyGeometry.h">
<Filter>Header Files\Geometry</Filter>
</ClCompile>
<ClCompile Include="ShaderToyGeommetry.cpp">
<Filter>Source Files\Engine</Filter>
<Filter>Source Files\Geometry</Filter>
</ClCompile>
<ClCompile Include="Geometry.cpp">
<Filter>Source Files\Geometry</Filter>
</ClCompile>
<ClCompile Include="Uniforms.cpp">
<Filter>Source Files\Uniforms</Filter>
</ClCompile>
<ClCompile Include="ShaderToyUniforms.cpp">
<Filter>Source Files\Engine</Filter>
<Filter>Source Files\Uniforms</Filter>
</ClCompile>
<ClCompile Include="ShaderToyFrameBuffer.cpp">
<Filter>Source Files\Engine</Filter>
<Filter>Source Files\FrameBuffer</Filter>
</ClCompile>
<ClCompile Include="FrameBuffer.cpp">
<Filter>Source Files\FrameBuffer</Filter>
</ClCompile>
<ClCompile Include="ShaderToyScreenBuffer.cpp">
<Filter>Source Files\FrameBuffer</Filter>
</ClCompile>
<ClCompile Include="ShaderProgram.cpp">
<Filter>Header Files\Engine</Filter>
<Filter>Source Files\Uniforms</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="HashVec2b.h">
<Filter>Source Files\SH</Filter>
</ClInclude>
<ClInclude Include="Texture.h">
<Filter>Header Files\Textures</Filter>
</ClInclude>
Expand Down Expand Up @@ -209,8 +239,29 @@
<ClInclude Include="TexturesManager.h">
<Filter>Header Files\Textures</Filter>
</ClInclude>
<ClInclude Include="FrameBuffer.h">
<Filter>Header Files\FrameBuffer</Filter>
</ClInclude>
<ClInclude Include="Geometry.h">
<Filter>Header Files\Geometry</Filter>
</ClInclude>
<ClInclude Include="ShaderToyScreenBuffer.h">
<Filter>Header Files\FrameBuffer</Filter>
</ClInclude>
<ClInclude Include="Uniforms.h">
<Filter>Header Files\Uniforms</Filter>
</ClInclude>
<ClInclude Include="ShaderToyUniforms.h">
<Filter>Header Files\Uniforms</Filter>
</ClInclude>
<ClInclude Include="ShaderProgram.h">
<Filter>Source Files\Engine</Filter>
<Filter>Header Files\Uniforms</Filter>
</ClInclude>
<ClInclude Include="HashVec2b.h">
<Filter>Header Files\SH</Filter>
</ClInclude>
<ClInclude Include="ShaderToyFrameBuffer.h">
<Filter>Header Files\FrameBuffer</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
Expand Down
Loading

0 comments on commit c7d5172

Please sign in to comment.