Exemple #1
0
void App::gui() {
	ImGuiIO& io = ImGui::GetIO();

	if (ImGui::BeginMainMenuBar()) {
		if (ImGui::BeginMenu("File")) {
			if (ImGui::MenuItem("New", io.OSXBehaviors ? "Cmd+N" : "Ctrl+N")) {
				newShader();
			}
			ImGui::Separator();
			if (ImGui::MenuItem("Open...", io.OSXBehaviors ? "Cmd+O" : "Ctrl+O")) {
				openShaderDialog();
			}
			if (ImGui::BeginMenu("Open Recent", !!recently_used_filepaths[most_recently_used_index])) {
				for (int i = 0; i < (int)ARRAY_COUNT(recently_used_filepaths); i++) {
					int index = (most_recently_used_index+i) % ARRAY_COUNT(recently_used_filepaths);
					if (recently_used_filepaths[index]) {
						char *menuitem_label = recently_used_filepaths[index];
						// we might include libgen.h and use basename(3) but there is no windows support...
						// TODO: test this on windows
						char *basename = strrchr(menuitem_label, '/');
						if (basename) menuitem_label = basename+1;
						if (ImGui::MenuItem(menuitem_label)) {
							loadShader(recently_used_filepaths[index]);
						}
						if (basename && ImGui::IsItemHovered()) {
							ImGui::SetTooltip("%s", recently_used_filepaths[index]);
						}
					} else break;
				}
				ImGui::Separator();
				if (ImGui::MenuItem("Clear Items")) {
					clearRecentlyUsedFilepaths();
				}
				ImGui::EndMenu();
			}
			// TODO: move this to settings pane eventually
			if (ImGui::MenuItem("Autoreload", nullptr, shader_file_autoreload)) {
				shader_file_autoreload = !shader_file_autoreload;
				writePreferences();
			}
			if (ImGui::MenuItem("Save", io.OSXBehaviors ? "Cmd+S" : "Ctrl+S", false, !!shader_filepath)) {
				saveShader();
			}
			if (ImGui::IsItemHovered() && shader_filepath) {
				ImGui::SetTooltip("%s", shader_filepath);
			}
			if (ImGui::MenuItem("Save As...", io.OSXBehaviors ? "Cmd+Shift+S" : "Ctrl+Shift+S", false, !!src_edit_buffer[0])) {
				saveShaderDialog();
			}
			ImGui::Separator();
			if (ImGui::MenuItem("Quit", io.OSXBehaviors ? "Cmd+Q" : "Ctrl+Q")) {
				quit = true;
			}
 			ImGui::EndMenu();
		}
		if (ImGui::BeginMenu("View")) {
			if (ImGui::MenuItem("Fullscreen", io.OSXBehaviors ? "Cmd+F" : "Ctrl+F", windowIsFullscreen())) {
				windowToggleFullscreen();
			}
			if (ImGui::MenuItem("Hide Controls", io.OSXBehaviors ? "Cmd+Shift+H" : "Ctrl+H", hide_gui)) {
				hide_gui = !hide_gui;
			}
			ImGui::Separator();
			if (ImGui::MenuItem(anim_play ? "Pause Animation" : "Play Animation", "Space")) {
				toggleAnimation();
			}
			if (ImGui::MenuItem("Reset Animation")) {
				frame_count = 0;
			}
			if (ImGui::MenuItem("Reset Camera")) {
				resetCamera();
			}
			ImGui::EndMenu();
		}
if (ImGui::BeginMenu("Tools")) {
	if (ImGui::MenuItem("Recompile Shader", io.OSXBehaviors ? "Cmd+B" : "Ctrl+B", false, !!src_edit_buffer[0])) {
		recompileShader();
	}
	ImGui::EndMenu();
}
if (ImGui::BeginMenu("Window")) {
	if (ImGui::MenuItem("Uniforms", io.OSXBehaviors ? "Cmd+1" : "Ctrl+1", show_uniforms_window)) {
		show_uniforms_window = !show_uniforms_window;
	}
	if (ImGui::MenuItem("Textures", io.OSXBehaviors ? "Cmd+2" : "Ctrl+2", show_textures_window)) {
		show_textures_window = !show_textures_window;
	}
	ImGui::Separator();
	if (ImGui::MenuItem("Camera", io.OSXBehaviors ? "Cmd+3" : "Ctrl+3", show_textures_window)) {
		show_camera_window = !show_camera_window;
	}
	ImGui::Separator();
	if (ImGui::MenuItem("Source editor", io.OSXBehaviors ? "Cmd+4" : "Ctrl+4", show_src_edit_window)) {
		show_src_edit_window = !show_src_edit_window;
	}
	ImGui::EndMenu();
}
ImGui::EndMainMenuBar();
	}

	if (show_uniforms_window) {
		if (ImGui::Begin("Uniforms", &show_uniforms_window)) {
			if (ImGui::CollapsingHeader("Built-in uniform names")) {
				ImGui::InputText("Time", u_time_name, sizeof(u_time_name));
				ImGui::InputText("Resolution", u_resolution_name, sizeof(u_resolution_name));
				ImGui::InputText("View to World Matrix", u_view_to_world_name, sizeof(u_view_to_world_name));
				ImGui::InputText("World to View Matrix", u_world_to_view_name, sizeof(u_world_to_view_name));
			}
			ImGui::Separator();

			if (!compile_error_log) {
				ImGui::AlignFirstTextHeightToWidgets();
				ImGui::Text("Data"); ImGui::SameLine();
				if (ImGui::Button("Clear")) {
					if (uniform_data) {
						memset(uniform_data, 0, uniform_data_size);
					}
				} ImGui::SameLine();
				if (ImGui::Button("Save")) {
					writeUniformData();
				} ImGui::SameLine();
				if (ImGui::Button("Load")) {
					readUniformData();
				}
				for (int i = 0; i < uniform_count; i++) {
					// skip builtin uniforms
					if (!strcmp(u_time_name, uniforms[i].name)) continue;
					if (!strcmp(u_resolution_name, uniforms[i].name)) continue;
					if (!strcmp(u_view_to_world_name, uniforms[i].name)) continue;
					if (!strcmp(u_world_to_view_name, uniforms[i].name)) continue;
					uniforms[i].gui();
				}
			}
		}
		ImGui::End();
	}

	if (show_textures_window) {
		if (ImGui::Begin("Textures", &show_textures_window)) {
			ImGui::Columns(2);
			for (int tsi = 0; tsi < (int)ARRAY_COUNT(texture_slots); tsi++) {
				TextureSlot *texture_slot = texture_slots + tsi;

				ImGui::BeginGroup();
				ImGui::PushID(tsi);
				ImGui::Text("%d:", tsi);
				ImGui::SameLine();
				ImGui::PushStyleColor(ImGuiCol_Button, (ImVec4)ImColor::HSV(0.0f, 0.6f, 0.6f));
				ImGui::PushStyleColor(ImGuiCol_ButtonHovered, (ImVec4)ImColor::HSV(0.0f, 0.7f, 0.7f));
				ImGui::PushStyleColor(ImGuiCol_ButtonActive, (ImVec4)ImColor::HSV(0.0f, 0.8f, 0.8f));
				if (ImGui::SmallButton("x")) texture_slot->clear();
				ImGui::PopStyleColor(3);
				if (ImGui::Button(" 2D ")) openImageDialog(texture_slot);
				if (ImGui::Button("Cube")) openImageDialog(texture_slot, /*load_cube_cross*/true);
				ImGui::PopID();
				ImGui::EndGroup();

				ImGui::SameLine();
				//ImTextureID im_tex_id = (ImTextureID)(intptr_t)texture_slot->texture;
				ImGui::Image((void*)texture_slot, ImVec2(64, 64));
				if (ImGui::IsItemHovered() && texture_slot->image_filepath) {
					ImGui::SetTooltip("%s\n%dx%d", texture_slot->image_filepath,
						texture_slot->image_width, texture_slot->image_height);
				}

				if ((tsi & 1) && tsi + 1 != ARRAY_COUNT(texture_slots)) {
					ImGui::Separator();
				}
				else {
					ImGui::SameLine(); ImGui::Spacing();
				}
				ImGui::NextColumn();
			}
			ImGui::Columns(1);
		}
		ImGui::End();
	}

	if (show_camera_window) {
		if (ImGui::Begin("Camera", &show_camera_window)) {
			ImGui::DragFloat3("Location", camera_location.e);
			ImGui::SliderAngle("Pitch", &camera_euler_angles.x);
			ImGui::SliderAngle("Yaw", &camera_euler_angles.y);
			ImGui::SliderAngle("Roll", &camera_euler_angles.z);
			if (ImGui::Button("Reset")) resetCamera();
			ImGui::End();
		}
	}

	if (show_src_edit_window) {
		if (ImGui::Begin("Source editor", &show_src_edit_window)) {
			// TODO: add horizontal scrollbar
			ImGui::InputTextMultiline("##Text buffer", src_edit_buffer, sizeof(src_edit_buffer)-1,
				/*fullwidth, fullheight*/ImVec2(-1.0f, -1.0f), ImGuiInputTextFlags_AllowTabInput);
		}
		ImGui::End();
	}

	// overlay messages
	int overlay_flags = ImGuiWindowFlags_NoTitleBar
		| ImGuiWindowFlags_NoResize | ImGuiWindowFlags_AlwaysAutoResize
		| ImGuiWindowFlags_NoBringToFrontOnFocus
		| ImGuiWindowFlags_NoMove
		| ImGuiWindowFlags_NoSavedSettings;
	if (!shader_filepath && !src_edit_buffer[0]) {
		ImGui::SetNextWindowPosCenter();
		ImGui::Begin("Overlay", nullptr, ImVec2(0, 0), 0.3f, overlay_flags);
		ImGui::AlignFirstTextHeightToWidgets(); // valign text to button
		ImGui::Text("No fragment shader");
		ImGui::SameLine();
		if (ImGui::Button("Open")) openShaderDialog();
		ImGui::SameLine();
		if (ImGui::Button("New")) newShader();
		ImGui::End();
	} else if (compile_error_log) {
		ImGui::SetNextWindowPosCenter();
		ImGui::Begin("Overlay", nullptr, ImVec2(0, 0), 0.3f, overlay_flags);
		ImGui::TextUnformatted(compile_error_log);
		ImGui::End();
	}
}
Exemple #2
0
void App::update(float delta_time) {
	if (anim_play) frame_count++;

	// builtin uniform names
	static char u_time_name[64]       = "u_time";
	static char u_resolution_name[64] = "u_resolution";
	static char u_view_mat_name[64]   = "u_view_mat";

	if (ImGui::BeginMainMenuBar()) {
		if (ImGui::BeginMenu("File")) {
			if (ImGui::MenuItem("Open fragment shader"/*, "Ctrl+O"*/)) {
				openShaderDialog();
			}
			if (ImGui::IsItemHovered() && file_path) {
				ImGui::SetTooltip("%s", file_path);
			}
			if (ImGui::MenuItem("Autoreload", nullptr, file_autoreload)) {
				file_autoreload = !file_autoreload;
			}
			ImGui::EndMenu();
		}
		if (ImGui::BeginMenu("Animation")) {
			if (ImGui::MenuItem(anim_play ? "Pause" : "Play")) {
				anim_play = !anim_play;
			}
			if (ImGui::MenuItem("Reset")) {
				frame_count = 0;
			}
			ImGui::EndMenu();
		}
		if (ImGui::BeginMenu("Window")) {
			if (ImGui::MenuItem("Uniforms", nullptr, show_uniforms_window)) {
				show_uniforms_window = !show_uniforms_window;
			}
			if (ImGui::MenuItem("Textures", nullptr, show_textures_window)) {
				show_textures_window = !show_textures_window;
			}
			ImGui::EndMenu();
		}
		ImGui::EndMainMenuBar();
	}

	if (show_uniforms_window) {
		if (ImGui::Begin("Uniforms", &show_uniforms_window)) {
			if (ImGui::CollapsingHeader("Built-in uniform names")) {
				ImGui::InputText("Time", u_time_name, sizeof(u_time_name));
				ImGui::InputText("Resolution", u_resolution_name, sizeof(u_resolution_name));
				ImGui::InputText("View Matrix", u_view_mat_name, sizeof(u_view_mat_name));
			}
			ImGui::Separator();
	
			if (!compile_error_log) {
				ImGui::AlignFirstTextHeightToWidgets();
				ImGui::Text("Data"); ImGui::SameLine();
				if (ImGui::Button("Clear")) {
					if (uniform_data) {
						memset(uniform_data, 0, uniform_data_size);
					}
				} ImGui::SameLine();
				if (ImGui::Button("Save")) {
					writeUniformData();
				} ImGui::SameLine();
				if (ImGui::Button("Load")) {
					readUniformData();
				}
				for (int i = 0; i < uniform_count; i++) {
					// skip builtin uniforms
					if (!strcmp(u_time_name,       uniforms[i].name)) continue;
					if (!strcmp(u_resolution_name, uniforms[i].name)) continue;
					if (!strcmp(u_view_mat_name,   uniforms[i].name)) continue;
					uniforms[i].gui();
				}
			}
		}
		ImGui::End();
	}

	if (show_textures_window) {
		if (ImGui::Begin("Textures", &show_textures_window, ImGuiWindowFlags_NoResize|ImGuiWindowFlags_AlwaysAutoResize)) {
			ImGui::Columns(2);
			for (int tsi = 0; tsi < ARRAY_COUNT(texture_slots); tsi++) {
				TextureSlot *texture_slot = texture_slots+tsi;
				
				ImGui::BeginGroup();
				ImGui::PushID(tsi);
				ImGui::Text("%d:", tsi);
				ImGui::SameLine();
				ImGui::PushStyleColor(ImGuiCol_Button, ImColor::HSV(0.0f, 0.6f, 0.6f));
				ImGui::PushStyleColor(ImGuiCol_ButtonHovered, ImColor::HSV(0.0f, 0.7f, 0.7f));
				ImGui::PushStyleColor(ImGuiCol_ButtonActive, ImColor::HSV(0.0f, 0.8f, 0.8f));
				if (ImGui::SmallButton("x")) texture_slot->clear();
				ImGui::PopStyleColor(3);
				if (ImGui::Button(" 2D ")) openImageDialog(texture_slot);
				if (ImGui::Button("Cube")) openImageDialog(texture_slot, /*load_cube_cross*/true);
				ImGui::PopID();
				ImGui::EndGroup();

				ImGui::SameLine();
				//ImTextureID im_tex_id = (ImTextureID)(intptr_t)texture_slot->texture;
				ImGui::Image((void*)texture_slot, ImVec2(64, 64));
				if (ImGui::IsItemHovered() && texture_slot->image_file_path) {
					ImGui::SetTooltip("%s\n%dx%d", texture_slot->image_file_path,
						texture_slot->image_width, texture_slot->image_height);
				}

				if ((tsi&1) && tsi+1 != ARRAY_COUNT(texture_slots)) {
					ImGui::Separator();
				} else {
					ImGui::SameLine(); ImGui::Spacing();
				}
				ImGui::NextColumn();
			}
			ImGui::Columns(1);
		}
		ImGui::End();
	}

	//ImGui::ShowTestWindow();

	// autoreload (every 60 frames)
	if (file_path && file_autoreload && (frame_count % 60) == 0) {
		struct stat attr;
		if (!stat(file_path, &attr)) { // file exists
			if (attr.st_mtime > file_mod_time) { // file has been modified
				file_mod_time = attr.st_mtime;
				loadShader(file_path, /*initial*/false);
			}
		}
	}

	// overlay messages
	int overlay_flags = ImGuiWindowFlags_NoTitleBar
		| ImGuiWindowFlags_NoResize | ImGuiWindowFlags_AlwaysAutoResize
		| ImGuiWindowFlags_NoBringToFrontOnFocus
		| ImGuiWindowFlags_NoMove
		| ImGuiWindowFlags_NoSavedSettings;
	if (!file_path) {
		ImGui::SetNextWindowPosCenter();
		ImGui::Begin("Overlay", nullptr, ImVec2(0, 0), 0.3f, overlay_flags);
		ImGui::AlignFirstTextHeightToWidgets(); // valign text to button
		ImGui::Text("No fragment shader");
		ImGui::SameLine();
		if (ImGui::Button("Open")) {
			openShaderDialog();
		}
		ImGui::End();
	} else if (compile_error_log) {
		ImGui::SetNextWindowPosCenter();
		ImGui::Begin("Overlay", nullptr, ImVec2(0, 0), 0.3f, overlay_flags);
		ImGui::TextUnformatted(compile_error_log);
		ImGui::End();
	}

	// update camera
	camera.euler_angles += 2.0f*delta_time
		* v3(movement_command.rotate.x, movement_command.rotate.y, 0.0f);
	mat3 rot_y = rotationMatrix(v3(0.0f, 0.0f, 1.0f), -camera.euler_angles.y);
	camera.location += rot_y * (8.0f*delta_time*movement_command.move);
	camera.updateViewMatrix();

	// bind textures
	for (int tsi = 0; tsi < ARRAY_COUNT(texture_slots); tsi++) {
		glActiveTexture(GL_TEXTURE0+tsi);
		glBindTexture(texture_slots[tsi].target, texture_slots[tsi].texture);
	}

	// draw two triangles
	glClearColor(0.2, 0.21, 0.22, 1.0);
	glClear(GL_COLOR_BUFFER_BIT);
	{ BindShader bind_shader(shader);
		if (!compile_error_log) {
			for (int i = 0; i < uniform_count; i++) {
				uniforms[i].apply();
			}
		}

		// apply builtin uniforms
		u_time = (float)frame_count / 60.0f;
		glUniform1f(shader.getUniformLocation(u_time_name), u_time);
		vec2 u_resolution = v2(video.pixel_scale*video.width, video.pixel_scale*video.height);
		glUniform2fv(shader.getUniformLocation(u_resolution_name), 1, u_resolution.e);
		mat4 u_inv_view_mat = camera.makeInverseViewMatrix();
		int u_view_mat_loc = shader.getUniformLocation(u_view_mat_name);
		glUniformMatrix4fv(u_view_mat_loc, 1, GL_FALSE, u_inv_view_mat.e);

		{ BindArrayBuffer bind_array_buffer(two_triangles_vbo);
			glEnableVertexAttribArray(VAT_POSITION);
			glVertexAttribPointer(VAT_POSITION, 2, GL_FLOAT, GL_FALSE, 0, (GLvoid*)0);
			glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
			glDisableVertexAttribArray(VAT_POSITION);
		}
	}
}