浏览代码

Merge pull request #3036 from malortie/sample-simpleopengl-minor-improvements

Minor fixes and improvements in sample SimpleOpenGL.
Kim Kulling 5 年之前
父节点
当前提交
5feb3628e3
共有 2 个文件被更改,包括 84 次插入18 次删除
  1. 12 6
      samples/SimpleOpenGL/CMakeLists.txt
  2. 72 12
      samples/SimpleOpenGL/Sample_SimpleOpenGL.c

+ 12 - 6
samples/SimpleOpenGL/CMakeLists.txt

@@ -1,3 +1,5 @@
+SET(SAMPLE_PROJECT_NAME assimp_simpleogl)
+
 FIND_PACKAGE(OpenGL)
 FIND_PACKAGE(GLUT)
 IF ( MSVC )
@@ -16,6 +18,10 @@ IF ( NOT GLUT_FOUND )
   ENDIF ()
 ENDIF ()
 
+# Used for usage and error messages in the program.
+ADD_COMPILE_DEFINITIONS(ASSIMP_VERSION="${ASSIMP_VERSION}")
+ADD_COMPILE_DEFINITIONS(PROJECT_NAME="${SAMPLE_PROJECT_NAME}")
+
 if ( MSVC )
   ADD_DEFINITIONS( -D_SCL_SECURE_NO_WARNINGS )
   ADD_DEFINITIONS( -D_CRT_SECURE_NO_WARNINGS )
@@ -34,17 +40,17 @@ LINK_DIRECTORIES(
   ${Assimp_BINARY_DIR}/lib
 )
 
-ADD_EXECUTABLE( assimp_simpleogl
+ADD_EXECUTABLE( ${SAMPLE_PROJECT_NAME}
   Sample_SimpleOpenGL.c
 )
 
-SET_PROPERTY(TARGET assimp_simpleogl PROPERTY DEBUG_POSTFIX ${CMAKE_DEBUG_POSTFIX})
+SET_PROPERTY(TARGET ${SAMPLE_PROJECT_NAME} PROPERTY DEBUG_POSTFIX ${CMAKE_DEBUG_POSTFIX})
 
-TARGET_LINK_LIBRARIES( assimp_simpleogl assimp ${OPENGL_LIBRARIES} ${GLUT_LIBRARIES} ${M_LIB} )
-SET_TARGET_PROPERTIES( assimp_simpleogl PROPERTIES
-  OUTPUT_NAME assimp_simpleogl
+TARGET_LINK_LIBRARIES( ${SAMPLE_PROJECT_NAME} assimp ${OPENGL_LIBRARIES} ${GLUT_LIBRARIES} ${M_LIB} )
+SET_TARGET_PROPERTIES( ${SAMPLE_PROJECT_NAME} PROPERTIES
+  OUTPUT_NAME ${SAMPLE_PROJECT_NAME}
 )
 
-INSTALL( TARGETS assimp_simpleogl
+INSTALL( TARGETS ${SAMPLE_PROJECT_NAME}
   DESTINATION "${ASSIMP_BIN_INSTALL_DIR}" COMPONENT assimp-dev
 )

+ 72 - 12
samples/SimpleOpenGL/Sample_SimpleOpenGL.c

@@ -15,9 +15,9 @@
 #include <stdio.h>
 
 #ifdef __APPLE__
-#include <glut.h>
+#include <freeglut.h>
 #else
-#include <GL/glut.h>
+#include <GL/freeglut.h>
 #endif
 
 /* assimp include files. These three are usually needed. */
@@ -25,6 +25,39 @@
 #include <assimp/scene.h>
 #include <assimp/postprocess.h>
 
+#define COMMAND_USAGE "--usage"
+
+/* ---------------------------------------------------------------------------- */
+inline static void print_run_command(const char* command_name) {
+	printf("Run '%s %s' for more information.\n", 
+		PROJECT_NAME, command_name);
+}
+
+/* ---------------------------------------------------------------------------- */
+inline static void print_error(const char* msg) {
+	printf("ERROR: %s\n", msg);
+}
+
+#define NEW_LINE "\n"
+#define DOUBLE_NEW_LINE NEW_LINE NEW_LINE
+
+/* ---------------------------------------------------------------------------- */
+inline static void print_usage() {
+	static const char* usage_format = 
+		"Usage: "
+		PROJECT_NAME
+		" <file>"	 DOUBLE_NEW_LINE
+		"where:"	 DOUBLE_NEW_LINE
+		"  %-10s %s" DOUBLE_NEW_LINE
+		"options:"	 DOUBLE_NEW_LINE
+		"  %-10s %s" DOUBLE_NEW_LINE;
+	printf(usage_format,
+		// where
+		"file", "The input model file to load.",
+		// options
+		COMMAND_USAGE, "Display usage.");
+}
+
 /* the global Assimp scene object */
 const C_STRUCT aiScene* scene = NULL;
 GLuint scene_list = 0;
@@ -245,7 +278,7 @@ void do_motion (void)
 	static int frames = 0;
 
 	int time = glutGet(GLUT_ELAPSED_TIME);
-	angle += (time-prev_time)*0.01f;
+	angle += static_cast<float>((time-prev_time)*0.01);
 	prev_time = time;
 
 	frames += 1;
@@ -324,12 +357,42 @@ int loadasset (const char* path)
 /* ---------------------------------------------------------------------------- */
 int main(int argc, char **argv)
 {
+	const char* model_file = NULL;
 	C_STRUCT aiLogStream stream;
 
+	if (argc < 2) {
+		print_error("No input model file specifed.");
+		print_run_command(COMMAND_USAGE);
+		return EXIT_FAILURE;
+	}
+
+	// Find and execute available commands entered by the user.
+	for (int i = 1; i < argc; ++i) {
+		if (!strncmp(argv[i], COMMAND_USAGE, strlen(COMMAND_USAGE))) {
+			print_usage();
+			return EXIT_SUCCESS;
+		}
+	}
+
+	// Check and validate the specified model file extension.
+	model_file = argv[1];
+	const char* extension = strchr(model_file, '.');
+	if (!extension) {
+		print_error("Please provide a file with a valid extension.");
+		return EXIT_FAILURE;
+	}
+
+	if (AI_FALSE == aiIsExtensionSupported(extension)) {
+		print_error("The specified model file extension is currently "
+			"unsupported in Assimp " ASSIMP_VERSION ".");
+		return EXIT_FAILURE;
+	}
+
 	glutInitWindowSize(900,600);
 	glutInitWindowPosition(100,100);
 	glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
 	glutInit(&argc, argv);
+	glutSetOption(GLUT_ACTION_ON_WINDOW_CLOSE, GLUT_ACTION_GLUTMAINLOOP_RETURNS);
 
 	glutCreateWindow("Assimp - Very simple OpenGL sample");
 	glutDisplayFunc(display);
@@ -346,14 +409,11 @@ int main(int argc, char **argv)
 	stream = aiGetPredefinedLogStream(aiDefaultLogStream_FILE,"assimp_log.txt");
 	aiAttachLogStream(&stream);
 
-	/* the model name can be specified on the command line. If none
-	  is specified, we try to locate one of the more expressive test
-	  models from the repository (/models-nonbsd may be missing in
-	  some distributions so we need a fallback from /models!). */
-	if( 0 != loadasset( argc >= 2 ? argv[1] : "../../test/models-nonbsd/X/dwarf.x")) {
-		if( argc != 1 || (0 != loadasset( "../../../../test/models-nonbsd/X/dwarf.x") && 0 != loadasset( "../../test/models/X/Testwuson.X"))) {
-			return -1;
-		}
+	// Load the model file.
+	if(0 != loadasset(model_file)) {
+		print_error("Failed to load model. Please ensure that the specified file exists.");
+		aiDetachAllLogStreams();
+		return EXIT_FAILURE;
 	}
 
 	glClearColor(0.1f,0.1f,0.1f,1.f);
@@ -384,5 +444,5 @@ int main(int argc, char **argv)
 	   again. This will definitely release the last resources allocated
 	   by Assimp.*/
 	aiDetachAllLogStreams();
-	return 0;
+	return EXIT_SUCCESS;
 }