static void onMouseDown(GLFWwindow* window, int button, int action, int mods) { // Rotation mode if (button == GLFW_MOUSE_BUTTON_1){ if (action == GLFW_PRESS){ s_camera.setMode(glfwGetKey(window, GLFW_KEY_LEFT_ALT) == GLFW_PRESS ? OrbitCamera::Mode::ARC : OrbitCamera::Mode::PAN); } else if (action == GLFW_RELEASE){ s_camera.setMode(OrbitCamera::Mode::NONE); } } // Enable mouse move if (button == GLFW_MOUSE_BUTTON_1) { if (action == GLFW_PRESS){ double x, y; glfwGetCursorPos(window, &x, &y); previousMousePos = glm::vec2(x, y); glfwSetCursorPosCallback(window, onMouseMove); } else{ glfwSetCursorPosCallback(window, NULL); } } }
void update(int delta, SDL_Event& evt) { camera.update(evt); if(camera.isMoving()) { SDL_SetCursor(hand); } else { SDL_SetCursor(arrow); } }
OrbitCamera * OrbitCamera::create(float t, float radius, float deltaRadius, float angleZ, float deltaAngleZ, float angleX, float deltaAngleX) { OrbitCamera * obitCamera = new (std::nothrow) OrbitCamera(); if(obitCamera->initWithDuration(t, radius, deltaRadius, angleZ, deltaAngleZ, angleX, deltaAngleX)) { obitCamera->autorelease(); return obitCamera; } CC_SAFE_DELETE(obitCamera); return nullptr; }
OrbitCamera * OrbitCamera::create(float t, float radius, float deltaRadius, float angleZ, float deltaAngleZ, float angleX, float deltaAngleX) { OrbitCamera * pRet = new OrbitCamera(); if(pRet->initWithDuration(t, radius, deltaRadius, angleZ, deltaAngleZ, angleX, deltaAngleX)) { pRet->autorelease(); return pRet; } CC_SAFE_DELETE(pRet); return NULL; }
static void update(double elapsedTime, GLFWwindow* window){ // Update camera position if (s_cameraIndex == 0){ if (s_camera.getMode() == OrbitCamera::Mode::NONE){ s_camera.rotate(100 * elapsedTime, 0); } } else{ s_camera2.update((float) elapsedTime); } // TODO: Perform particle movement //s_particleSystem->update(); }
void InitApp(UINT dpi) { StartXAudio2(); CHECK_HR(LoadSoundFiles()); CHECK_HR(gpXAudio2->CreateSourceVoice(&gpSourceThunder, &gWfxThunder)); CHECK_HR(gpXAudio2->CreateSourceVoice(&gpSourceCollectible, &gWfxCollectible)); LARGE_INTEGER performanceFrequency, firstFrameTicks; CHECK_WIN32(QueryPerformanceFrequency(&performanceFrequency)); CHECK_WIN32(QueryPerformanceCounter(&firstFrameTicks)); gPerformanceFrequency = performanceFrequency.QuadPart; gLastFrameTicks = firstFrameTicks.QuadPart; gAccumulatedFrameTicks = 0; gpRenderer = std::make_unique<Renderer>(gpDevice.Get(), gpDeviceContext.Get(), gpDxgiDevice.Get(), dpi); gpRenderer->Init(); #define SIM_ORBIT_RADIUS 50.f #define SIM_DISC_RADIUS 12.f auto center = DirectX::XMVectorSet(0.0f, 0.4f*SIM_DISC_RADIUS, 0.0f, 0.0f); auto radius = 35.0f; auto minRadius = SIM_ORBIT_RADIUS - 3.25f * SIM_DISC_RADIUS; auto maxRadius = SIM_ORBIT_RADIUS + 3.0f * SIM_DISC_RADIUS; auto longAngle = 1.50f; auto latAngle = 0.75f; gCamera.View(center, radius, minRadius, maxRadius, longAngle, latAngle); }
void ResizeApp(int width, int height) { CHECK_HR(gpSwapChain->ResizeBuffers(kSwapChainBufferCount, width, height, kSwapChainFormat, 0)); gpRenderer->Resize(width, height); float aspect = (float)gRenderWidth / gRenderHeight; gCamera.Projection(DirectX::XM_PIDIV2 * 0.8f * 3 / 2, aspect); }
static void onMouseMove(GLFWwindow* window, double xpos, double ypos) { glm::vec2 dpos = glm::vec2(xpos - previousMousePos.x, previousMousePos.y - ypos); // Camera control if (s_cameraIndex == 0){ if (s_camera.getMode() == OrbitCamera::Mode::ARC){ s_camera.rotate(dpos); } else if (s_camera.getMode() == OrbitCamera::Mode::PAN){ s_camera.pan(dpos); } } else{ s_camera2.rotate(dpos); } previousMousePos = glm::vec2(xpos, ypos); }
static void draw(double elapsed_time, GLFWwindow* window) { // Clear the main buffer glBindFramebuffer(GL_FRAMEBUFFER, mainBuffer); glClearColor(0.390625f, 0.582031f, 0.925781f, 1.0f); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Get view from camera glm::vec3 cameraPosition = s_camera.getPosition(); glm::mat4 view = (s_cameraIndex == 0 ? s_camera.getView() : s_camera2.getView()); // Draw backgound scene s_skybox->draw(view, s_proj); s_plane->draw(view, s_proj); s_dwarf->Draw(view, s_proj); // Pre-process fluid s_particleSystem->preProcessPass(view, s_proj); // Render background glBindFramebuffer(GL_FRAMEBUFFER, 0); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glUseProgram(postProcessShader); glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, mainBufferTexture); glUniform1i(glGetUniformLocation(postProcessShader, "source"), 0); glActiveTexture(GL_TEXTURE1); glBindTexture(GL_TEXTURE_2D, mainBufferDepth); glUniform1i(glGetUniformLocation(postProcessShader, "depth"), 1); glDepthFunc(GL_LEQUAL); drawQuad(); // Post-process fluid s_particleSystem->postProcessPass( mainBufferTexture, view, s_proj); }
void render(int delta) { rndr->renderToTexture(renderTex, mat4ToFloat16(camera.getViewMatrix())); glClearColor(1, 1, 1, 1); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glBindTexture(GL_TEXTURE_2D, renderTex); glLoadIdentity(); glBegin(GL_QUADS); glTexCoord2f(0, 1); glVertex3f(-1, -1, -1); glTexCoord2f(0, 0); glVertex3f(-1, 1, -1); glTexCoord2f(1, 0); glVertex3f(1, 1, -1); glTexCoord2f(1, 1); glVertex3f(1, -1, -1); glEnd(); glBindTexture(GL_TEXTURE_2D, 0); #ifdef RENDER_LIGHTS glMatrixMode(GL_PROJECTION); glLoadIdentity(); glFrustum(-0.5, 0.5, -0.5, 0.5, 0.5, 100.0); glPushAttrib(GL_CURRENT_BIT); glPointSize(5.0); glBegin(GL_POINTS); for(auto i = lights.begin(); i != lights.end(); i++) { GLfloat c[3] = {1.0, 1.0, 1.0}; glColor3fv(c); glVertex3fv((GLfloat*)&i->position); } glEnd(); glPopAttrib(); glLoadIdentity(); glMatrixMode(GL_MODELVIEW); #endif }
void UpdateApp() { gCamera.ProcessInertia(); LARGE_INTEGER currFrameTicks; CHECK_WIN32(QueryPerformanceCounter(&currFrameTicks)); UINT64 deltaTicks = currFrameTicks.QuadPart - gLastFrameTicks; gAccumulatedFrameTicks += deltaTicks; const UINT64 kMillisecondsPerUpdate = 1000 / kUpdateFrequency; const UINT64 kTicksPerMillisecond = gPerformanceFrequency / 1000; const UINT64 kTicksPerUpdate = kMillisecondsPerUpdate * kTicksPerMillisecond; while (gAccumulatedFrameTicks >= kTicksPerUpdate) { gpRenderer->Update(kMillisecondsPerUpdate); gAccumulatedFrameTicks -= kTicksPerUpdate; } gLastFrameTicks = currFrameTicks.QuadPart; }
void Asteroids::Render(float frameTime, const OrbitCamera& camera, const Settings& settings) { ProfileBeginFrame(0); ProfileBeginRender(); // Frame data ProfileBeginSimUpdate(); mAsteroids->Update(frameTime, camera.Eye(), settings); auto staticAsteroidData = mAsteroids->StaticData(); auto dynamicAsteroidData = mAsteroids->DynamicData(); ProfileEndSimUpdate(); // Clear the render target float clearcol[4] = { 0.0f, 0.0f, 0.0f, 0.0f }; mDeviceCtxt->ClearRenderTargetView(mRenderTargetView, clearcol); mDeviceCtxt->ClearDepthStencilView(mDepthStencilView, D3D11_CLEAR_DEPTH, 0.0f, 0); { ID3D11Buffer* ia_buffers[] = { mVertexBuffer }; UINT ia_strides[] = { sizeof(Vertex) }; UINT ia_offsets[] = { 0 }; mDeviceCtxt->IASetInputLayout(mInputLayout); mDeviceCtxt->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST); mDeviceCtxt->IASetVertexBuffers(0, 1, ia_buffers, ia_strides, ia_offsets); mDeviceCtxt->IASetIndexBuffer(mIndexBuffer, DXGI_FORMAT_R16_UINT, 0); } mDeviceCtxt->VSSetShader(mVertexShader, nullptr, 0); mDeviceCtxt->VSSetConstantBuffers(0, 1, &mDrawConstantBuffer); mDeviceCtxt->RSSetViewports(1, &mViewPort); mDeviceCtxt->RSSetScissorRects(1, &mScissorRect); mDeviceCtxt->PSSetShader(mPixelShader, nullptr, 0); mDeviceCtxt->PSSetSamplers(0, 1, &mSamplerState); mDeviceCtxt->OMSetRenderTargets(1, &mRenderTargetView, mDepthStencilView); mDeviceCtxt->OMSetDepthStencilState(mDepthStencilState, 0); mDeviceCtxt->OMSetBlendState(mBlendState, nullptr, 0xFFFFFFFF); ProfileBeginRenderSubset(); auto viewProjection = camera.ViewProjection(); for (UINT drawIdx = 0; drawIdx < NUM_ASTEROIDS; ++drawIdx) { auto staticData = &staticAsteroidData[drawIdx]; auto dynamicData = &dynamicAsteroidData[drawIdx]; D3D11_MAPPED_SUBRESOURCE mapped = {}; ThrowIfFailed(mDeviceCtxt->Map(mDrawConstantBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &mapped)); auto drawConstants = (DrawConstantBuffer*) mapped.pData; XMStoreFloat4x4(&drawConstants->mWorld, dynamicData->world); XMStoreFloat4x4(&drawConstants->mViewProjection, viewProjection); drawConstants->mSurfaceColor = staticData->surfaceColor; drawConstants->mDeepColor = staticData->deepColor; mDeviceCtxt->Unmap(mDrawConstantBuffer, 0); mDeviceCtxt->PSSetShaderResources(0, 1, &mTextureSRVs[staticData->textureIndex]); mDeviceCtxt->DrawIndexedInstanced(dynamicData->indexCount, 1, dynamicData->indexStart, staticData->vertexStart, 0); } ProfileEndRenderSubset(); // Draw skybox { D3D11_MAPPED_SUBRESOURCE mapped = {}; ThrowIfFailed(mDeviceCtxt->Map(mSkyboxConstantBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &mapped)); auto skyboxConstants = (SkyboxConstantBuffer*) mapped.pData; XMStoreFloat4x4(&skyboxConstants->mViewProjection, camera.ViewProjection()); mDeviceCtxt->Unmap(mSkyboxConstantBuffer, 0); ID3D11Buffer* ia_buffers[] = { mSkyboxVertexBuffer }; UINT ia_strides[] = { sizeof(SkyboxVertex) }; UINT ia_offsets[] = { 0 }; mDeviceCtxt->IASetInputLayout(mSkyboxInputLayout); mDeviceCtxt->IASetVertexBuffers(0, 1, ia_buffers, ia_strides, ia_offsets); mDeviceCtxt->VSSetShader(mSkyboxVertexShader, nullptr, 0); mDeviceCtxt->VSSetConstantBuffers(0, 1, &mSkyboxConstantBuffer); mDeviceCtxt->PSSetShader(mSkyboxPixelShader, nullptr, 0); mDeviceCtxt->PSSetSamplers(0, 1, &mSamplerState); mDeviceCtxt->PSSetShaderResources(0, 1, &mSkyboxSRV); mDeviceCtxt->Draw(6*6, 0); } mDeviceCtxt->OMSetRenderTargets(1, &mRenderTargetView, 0); // No more depth buffer // Draw sprites and fonts { // Fill in vertices (TODO: could move this vector to be a member - not a big deal) std::vector<UINT> controlVertices; controlVertices.reserve(mGUI->size()); { D3D11_MAPPED_SUBRESOURCE mapped = {}; ThrowIfFailed(mDeviceCtxt->Map(mSpriteVertexBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &mapped)); auto vertexBase = (SpriteVertex*)mapped.pData; auto vertexEnd = vertexBase; for (size_t i = 0; i < mGUI->size(); ++i) { auto control = (*mGUI)[i]; controlVertices.push_back((UINT)(control->Draw(mViewPort.Width, mViewPort.Height, vertexEnd) - vertexEnd)); vertexEnd += controlVertices.back(); } mDeviceCtxt->Unmap(mSpriteVertexBuffer, 0); } ID3D11Buffer* ia_buffers[] = { mSpriteVertexBuffer }; UINT ia_strides[] = { sizeof(SpriteVertex) }; UINT ia_offsets[] = { 0 }; mDeviceCtxt->IASetInputLayout(mSpriteInputLayout); mDeviceCtxt->IASetVertexBuffers(0, 1, ia_buffers, ia_strides, ia_offsets); mDeviceCtxt->VSSetShader(mSpriteVertexShader, 0, 0); mDeviceCtxt->OMSetBlendState(mSpriteBlendState, nullptr, 0xFFFFFFFF); // Draw UINT vertexStart = 0; for (size_t i = 0; i < mGUI->size(); ++i) { auto control = (*mGUI)[i]; if (control->Visible()) { if (control->TextureFile().length() == 0) { // Font mDeviceCtxt->PSSetShader(mFontPixelShader, 0, 0); mDeviceCtxt->PSSetShaderResources(0, 1, &mFontTextureSRV); } else { // Sprite auto textureSRV = mSpriteTextures[control->TextureFile()]; mDeviceCtxt->PSSetShader(mSpritePixelShader, 0, 0); mDeviceCtxt->PSSetShaderResources(0, 1, &textureSRV); } mDeviceCtxt->Draw(controlVertices[i], vertexStart); } vertexStart += controlVertices[i]; } } ProfileEndRender(); ProfileBeginPresent(); mSwapChain->Present(settings.vsync ? 1 : 0, 0); ProfileEndPresent(); ProfileEndFrame(); }
static void onMouseScroll(GLFWwindow* window, double xoffset, double yoffset) { if (s_cameraIndex == 0){ s_camera.zoom((float) yoffset); } }
// Event handler LRESULT CALLBACK MyWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { gpRenderer->HandleEvent(message, wParam, lParam); switch (message) { case WM_CLOSE: gShouldClose = true; return 0; case WM_SIZE: { UINT ww = LOWORD(lParam); UINT wh = HIWORD(lParam); // Ignore resizing to minimized if (ww == 0 || wh == 0) return 0; gWindowWidth = (int)ww; gWindowHeight = (int)wh; gRenderWidth = (UINT)(double(gWindowWidth) * gRenderScale); gRenderHeight = (UINT)(double(gWindowHeight) * gRenderScale); // Update camera projection float aspect = (float)gRenderWidth / (float)gRenderHeight; gCamera.Projection(DirectX::XM_PIDIV2 * 0.8f * 3 / 2, aspect); ResizeApp(gWindowWidth, gWindowHeight); return 0; } case WM_MOUSEWHEEL: { auto delta = GET_WHEEL_DELTA_WPARAM(wParam); gCamera.ZoomRadius(-0.07f * delta); return 0; } case WM_POINTERDOWN: case WM_POINTERUPDATE: case WM_POINTERUP: { auto pointerId = GET_POINTERID_WPARAM(wParam); POINTER_INFO pointerInfo; if (GetPointerInfo(pointerId, &pointerInfo)) { if (message == WM_POINTERDOWN) { // Compute pointer position in render units POINT p = pointerInfo.ptPixelLocation; ScreenToClient(hWnd, &p); RECT clientRect; GetClientRect(hWnd, &clientRect); p.x = p.x * gRenderWidth / (clientRect.right - clientRect.left); p.y = p.y * gRenderHeight / (clientRect.bottom - clientRect.top); gCamera.AddPointer(pointerId); } // Otherwise send it to the camera controls gCamera.ProcessPointerFrames(pointerId, &pointerInfo); if (message == WM_POINTERUP) gCamera.RemovePointer(pointerId); } return 0; } default: return DefWindowProc(hWnd, message, wParam, lParam); } }