annotate gldragon.cpp @ 55:bf73a2a70ec7

Add OpenGL error checking.
author Matti Hamalainen <ccr@tnsp.org>
date Fri, 06 Dec 2019 23:29:26 +0200
parents 73fa5fb437a0
children 3f635b7ea2cf
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3
be31ff9e5f58 Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents: 2
diff changeset
1 //
22
03b86b9c2f29 Add copyright blurbs and licenses.
Matti Hamalainen <ccr@tnsp.org>
parents: 21
diff changeset
2 // GLDragon - OpenGL PLY model viewer / simple benchmark
03b86b9c2f29 Add copyright blurbs and licenses.
Matti Hamalainen <ccr@tnsp.org>
parents: 21
diff changeset
3 // Programmed and designed by Matti 'ccr' Hämäläinen <ccr@tnsp.org>
3
be31ff9e5f58 Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents: 2
diff changeset
4 // (C) Copyright 2019 Tecnic Software productions (TNSP)
be31ff9e5f58 Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents: 2
diff changeset
5 //
22
03b86b9c2f29 Add copyright blurbs and licenses.
Matti Hamalainen <ccr@tnsp.org>
parents: 21
diff changeset
6 // See file "COPYING" for license information.
0
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
7 //
22
03b86b9c2f29 Add copyright blurbs and licenses.
Matti Hamalainen <ccr@tnsp.org>
parents: 21
diff changeset
8 // Originally based on 'glxdragon' Copyright (c) 2009, Thomas Trummer
3
be31ff9e5f58 Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents: 2
diff changeset
9 //
0
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
10 #include <SDL.h>
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
11 #include <SDL_opengl.h>
3
be31ff9e5f58 Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents: 2
diff changeset
12 #include <GL/glu.h>
0
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
13
21
1404dfcee7b8 More work on scenefile and model loading support.
Matti Hamalainen <ccr@tnsp.org>
parents: 20
diff changeset
14 #include "dmutil.h"
1404dfcee7b8 More work on scenefile and model loading support.
Matti Hamalainen <ccr@tnsp.org>
parents: 20
diff changeset
15 #include "dmmodel.h"
0
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
16
16
c134a186912f Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
17
11
89dc8caeff41 Improve option handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
18 /* Default settings etc. constants
89dc8caeff41 Improve option handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
19 */
18
b1e75c65016d Check for "too large" shader files.
Matti Hamalainen <ccr@tnsp.org>
parents: 17
diff changeset
20 #define SET_DEF_WIDTH 1280
b1e75c65016d Check for "too large" shader files.
Matti Hamalainen <ccr@tnsp.org>
parents: 17
diff changeset
21 #define SET_DEF_HEIGHT 960
b1e75c65016d Check for "too large" shader files.
Matti Hamalainen <ccr@tnsp.org>
parents: 17
diff changeset
22 #define SET_FRAMES (180)
b1e75c65016d Check for "too large" shader files.
Matti Hamalainen <ccr@tnsp.org>
parents: 17
diff changeset
23 #define SET_MAX_SHADER_SIZE (128 * 1024)
3
be31ff9e5f58 Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents: 2
diff changeset
24
be31ff9e5f58 Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents: 2
diff changeset
25
11
89dc8caeff41 Improve option handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
26 /* Options
89dc8caeff41 Improve option handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
27 */
89dc8caeff41 Improve option handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
28 bool optUseShaders = false;
89dc8caeff41 Improve option handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
29 int optWidth = SET_DEF_WIDTH,
13
c1e8057cc4d0 Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents: 12
diff changeset
30 optHeight = SET_DEF_HEIGHT,
c1e8057cc4d0 Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents: 12
diff changeset
31 optVSyncMode = 1;
c1e8057cc4d0 Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents: 12
diff changeset
32
11
89dc8caeff41 Improve option handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
33
89dc8caeff41 Improve option handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
34 /* Globals
89dc8caeff41 Improve option handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
35 */
89dc8caeff41 Improve option handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
36 SDL_Window *dmWindow = NULL;
89dc8caeff41 Improve option handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
37 SDL_GLContext dmGLContext = NULL;
89dc8caeff41 Improve option handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
38
89dc8caeff41 Improve option handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
39
21
1404dfcee7b8 More work on scenefile and model loading support.
Matti Hamalainen <ccr@tnsp.org>
parents: 20
diff changeset
40 /* Helpers
1404dfcee7b8 More work on scenefile and model loading support.
Matti Hamalainen <ccr@tnsp.org>
parents: 20
diff changeset
41 */
55
bf73a2a70ec7 Add OpenGL error checking.
Matti Hamalainen <ccr@tnsp.org>
parents: 54
diff changeset
42 bool dmGLCheckErrors(void)
bf73a2a70ec7 Add OpenGL error checking.
Matti Hamalainen <ccr@tnsp.org>
parents: 54
diff changeset
43 {
bf73a2a70ec7 Add OpenGL error checking.
Matti Hamalainen <ccr@tnsp.org>
parents: 54
diff changeset
44 bool ok = true;
bf73a2a70ec7 Add OpenGL error checking.
Matti Hamalainen <ccr@tnsp.org>
parents: 54
diff changeset
45 GLenum err;
bf73a2a70ec7 Add OpenGL error checking.
Matti Hamalainen <ccr@tnsp.org>
parents: 54
diff changeset
46 while ((err = glGetError()) != GL_NO_ERROR)
bf73a2a70ec7 Add OpenGL error checking.
Matti Hamalainen <ccr@tnsp.org>
parents: 54
diff changeset
47 {
bf73a2a70ec7 Add OpenGL error checking.
Matti Hamalainen <ccr@tnsp.org>
parents: 54
diff changeset
48 dmError("OpenGL error code: 0x%x (%d)\n", err);
bf73a2a70ec7 Add OpenGL error checking.
Matti Hamalainen <ccr@tnsp.org>
parents: 54
diff changeset
49 ok = false;
bf73a2a70ec7 Add OpenGL error checking.
Matti Hamalainen <ccr@tnsp.org>
parents: 54
diff changeset
50 }
bf73a2a70ec7 Add OpenGL error checking.
Matti Hamalainen <ccr@tnsp.org>
parents: 54
diff changeset
51 return ok;
bf73a2a70ec7 Add OpenGL error checking.
Matti Hamalainen <ccr@tnsp.org>
parents: 54
diff changeset
52 }
bf73a2a70ec7 Add OpenGL error checking.
Matti Hamalainen <ccr@tnsp.org>
parents: 54
diff changeset
53
bf73a2a70ec7 Add OpenGL error checking.
Matti Hamalainen <ccr@tnsp.org>
parents: 54
diff changeset
54
42
3c7e1d3fa5a2 Implement OpengL extension handling through new header file dmglexts.h to
Matti Hamalainen <ccr@tnsp.org>
parents: 39
diff changeset
55 #ifdef GL_GLEXT_PROTOTYPES
3c7e1d3fa5a2 Implement OpengL extension handling through new header file dmglexts.h to
Matti Hamalainen <ccr@tnsp.org>
parents: 39
diff changeset
56 #define DM_GLEXT_INIT(extproctype, extprocname) /* stub */
3c7e1d3fa5a2 Implement OpengL extension handling through new header file dmglexts.h to
Matti Hamalainen <ccr@tnsp.org>
parents: 39
diff changeset
57 #else
3c7e1d3fa5a2 Implement OpengL extension handling through new header file dmglexts.h to
Matti Hamalainen <ccr@tnsp.org>
parents: 39
diff changeset
58 #define DM_GLEXT_INIT(extproctype, extprocname) extproctype extprocname = NULL;
3c7e1d3fa5a2 Implement OpengL extension handling through new header file dmglexts.h to
Matti Hamalainen <ccr@tnsp.org>
parents: 39
diff changeset
59 #include "dmglexts.h"
3c7e1d3fa5a2 Implement OpengL extension handling through new header file dmglexts.h to
Matti Hamalainen <ccr@tnsp.org>
parents: 39
diff changeset
60 #undef DM_GLEXT_INIT
3c7e1d3fa5a2 Implement OpengL extension handling through new header file dmglexts.h to
Matti Hamalainen <ccr@tnsp.org>
parents: 39
diff changeset
61
3c7e1d3fa5a2 Implement OpengL extension handling through new header file dmglexts.h to
Matti Hamalainen <ccr@tnsp.org>
parents: 39
diff changeset
62
53
d871e4b24328 Be less verbose when checking for GL extensions, only inform when something
Matti Hamalainen <ccr@tnsp.org>
parents: 52
diff changeset
63 void dmGLCheckExtension(const std::string &name, bool &status)
42
3c7e1d3fa5a2 Implement OpengL extension handling through new header file dmglexts.h to
Matti Hamalainen <ccr@tnsp.org>
parents: 39
diff changeset
64 {
53
d871e4b24328 Be less verbose when checking for GL extensions, only inform when something
Matti Hamalainen <ccr@tnsp.org>
parents: 52
diff changeset
65 if (!SDL_GL_ExtensionSupported(name.c_str()))
d871e4b24328 Be less verbose when checking for GL extensions, only inform when something
Matti Hamalainen <ccr@tnsp.org>
parents: 52
diff changeset
66 {
d871e4b24328 Be less verbose when checking for GL extensions, only inform when something
Matti Hamalainen <ccr@tnsp.org>
parents: 52
diff changeset
67 status = false;
d871e4b24328 Be less verbose when checking for GL extensions, only inform when something
Matti Hamalainen <ccr@tnsp.org>
parents: 52
diff changeset
68 dmMsg(" - '%s' NOT supported.\n",
d871e4b24328 Be less verbose when checking for GL extensions, only inform when something
Matti Hamalainen <ccr@tnsp.org>
parents: 52
diff changeset
69 name.c_str());
d871e4b24328 Be less verbose when checking for GL extensions, only inform when something
Matti Hamalainen <ccr@tnsp.org>
parents: 52
diff changeset
70 }
52
6343c3392a95 After looking at many incorrect examples, the correct method for GL
Matti Hamalainen <ccr@tnsp.org>
parents: 51
diff changeset
71 }
6343c3392a95 After looking at many incorrect examples, the correct method for GL
Matti Hamalainen <ccr@tnsp.org>
parents: 51
diff changeset
72
6343c3392a95 After looking at many incorrect examples, the correct method for GL
Matti Hamalainen <ccr@tnsp.org>
parents: 51
diff changeset
73
6343c3392a95 After looking at many incorrect examples, the correct method for GL
Matti Hamalainen <ccr@tnsp.org>
parents: 51
diff changeset
74 void * dmGLGetProcAddr(const std::string &name)
6343c3392a95 After looking at many incorrect examples, the correct method for GL
Matti Hamalainen <ccr@tnsp.org>
parents: 51
diff changeset
75 {
42
3c7e1d3fa5a2 Implement OpengL extension handling through new header file dmglexts.h to
Matti Hamalainen <ccr@tnsp.org>
parents: 39
diff changeset
76 void *ptr = SDL_GL_GetProcAddress(name.c_str());
3c7e1d3fa5a2 Implement OpengL extension handling through new header file dmglexts.h to
Matti Hamalainen <ccr@tnsp.org>
parents: 39
diff changeset
77
53
d871e4b24328 Be less verbose when checking for GL extensions, only inform when something
Matti Hamalainen <ccr@tnsp.org>
parents: 52
diff changeset
78 if (ptr == NULL)
d871e4b24328 Be less verbose when checking for GL extensions, only inform when something
Matti Hamalainen <ccr@tnsp.org>
parents: 52
diff changeset
79 dmMsg(" - '%s' NOT supported.\n");
42
3c7e1d3fa5a2 Implement OpengL extension handling through new header file dmglexts.h to
Matti Hamalainen <ccr@tnsp.org>
parents: 39
diff changeset
80
52
6343c3392a95 After looking at many incorrect examples, the correct method for GL
Matti Hamalainen <ccr@tnsp.org>
parents: 51
diff changeset
81 return ptr;
6343c3392a95 After looking at many incorrect examples, the correct method for GL
Matti Hamalainen <ccr@tnsp.org>
parents: 51
diff changeset
82 }
45
534eccdd33d4 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 44
diff changeset
83
42
3c7e1d3fa5a2 Implement OpengL extension handling through new header file dmglexts.h to
Matti Hamalainen <ccr@tnsp.org>
parents: 39
diff changeset
84
3c7e1d3fa5a2 Implement OpengL extension handling through new header file dmglexts.h to
Matti Hamalainen <ccr@tnsp.org>
parents: 39
diff changeset
85 void * dmGLExtInit(const std::string &name, bool &status)
3c7e1d3fa5a2 Implement OpengL extension handling through new header file dmglexts.h to
Matti Hamalainen <ccr@tnsp.org>
parents: 39
diff changeset
86 {
3c7e1d3fa5a2 Implement OpengL extension handling through new header file dmglexts.h to
Matti Hamalainen <ccr@tnsp.org>
parents: 39
diff changeset
87 void *ptr;
52
6343c3392a95 After looking at many incorrect examples, the correct method for GL
Matti Hamalainen <ccr@tnsp.org>
parents: 51
diff changeset
88 bool ok =
6343c3392a95 After looking at many incorrect examples, the correct method for GL
Matti Hamalainen <ccr@tnsp.org>
parents: 51
diff changeset
89 (ptr = dmGLGetProcAddr(name)) != NULL ||
6343c3392a95 After looking at many incorrect examples, the correct method for GL
Matti Hamalainen <ccr@tnsp.org>
parents: 51
diff changeset
90 (ptr = dmGLGetProcAddr(name + "EXT")) != NULL ||
6343c3392a95 After looking at many incorrect examples, the correct method for GL
Matti Hamalainen <ccr@tnsp.org>
parents: 51
diff changeset
91 (ptr = dmGLGetProcAddr(name + "ARB")) != NULL;
42
3c7e1d3fa5a2 Implement OpengL extension handling through new header file dmglexts.h to
Matti Hamalainen <ccr@tnsp.org>
parents: 39
diff changeset
92
52
6343c3392a95 After looking at many incorrect examples, the correct method for GL
Matti Hamalainen <ccr@tnsp.org>
parents: 51
diff changeset
93 if (!ok)
6343c3392a95 After looking at many incorrect examples, the correct method for GL
Matti Hamalainen <ccr@tnsp.org>
parents: 51
diff changeset
94 status = false;
6343c3392a95 After looking at many incorrect examples, the correct method for GL
Matti Hamalainen <ccr@tnsp.org>
parents: 51
diff changeset
95
6343c3392a95 After looking at many incorrect examples, the correct method for GL
Matti Hamalainen <ccr@tnsp.org>
parents: 51
diff changeset
96 return ptr;
42
3c7e1d3fa5a2 Implement OpengL extension handling through new header file dmglexts.h to
Matti Hamalainen <ccr@tnsp.org>
parents: 39
diff changeset
97 }
3c7e1d3fa5a2 Implement OpengL extension handling through new header file dmglexts.h to
Matti Hamalainen <ccr@tnsp.org>
parents: 39
diff changeset
98 #endif
3c7e1d3fa5a2 Implement OpengL extension handling through new header file dmglexts.h to
Matti Hamalainen <ccr@tnsp.org>
parents: 39
diff changeset
99
3c7e1d3fa5a2 Implement OpengL extension handling through new header file dmglexts.h to
Matti Hamalainen <ccr@tnsp.org>
parents: 39
diff changeset
100
3c7e1d3fa5a2 Implement OpengL extension handling through new header file dmglexts.h to
Matti Hamalainen <ccr@tnsp.org>
parents: 39
diff changeset
101 bool dmInitGLExtensions(void)
3c7e1d3fa5a2 Implement OpengL extension handling through new header file dmglexts.h to
Matti Hamalainen <ccr@tnsp.org>
parents: 39
diff changeset
102 {
3c7e1d3fa5a2 Implement OpengL extension handling through new header file dmglexts.h to
Matti Hamalainen <ccr@tnsp.org>
parents: 39
diff changeset
103 bool status = true;
52
6343c3392a95 After looking at many incorrect examples, the correct method for GL
Matti Hamalainen <ccr@tnsp.org>
parents: 51
diff changeset
104 dmMsg("Checking for required OpenGL extensions ..\n");
6343c3392a95 After looking at many incorrect examples, the correct method for GL
Matti Hamalainen <ccr@tnsp.org>
parents: 51
diff changeset
105
53
d871e4b24328 Be less verbose when checking for GL extensions, only inform when something
Matti Hamalainen <ccr@tnsp.org>
parents: 52
diff changeset
106 #ifndef GL_GLEXT_PROTOTYPES
d871e4b24328 Be less verbose when checking for GL extensions, only inform when something
Matti Hamalainen <ccr@tnsp.org>
parents: 52
diff changeset
107 dmGLCheckExtension("GL_ARB_shader_objects", status);
d871e4b24328 Be less verbose when checking for GL extensions, only inform when something
Matti Hamalainen <ccr@tnsp.org>
parents: 52
diff changeset
108 dmGLCheckExtension("GL_ARB_shading_language_100", status);
d871e4b24328 Be less verbose when checking for GL extensions, only inform when something
Matti Hamalainen <ccr@tnsp.org>
parents: 52
diff changeset
109 dmGLCheckExtension("GL_ARB_vertex_shader", status);
d871e4b24328 Be less verbose when checking for GL extensions, only inform when something
Matti Hamalainen <ccr@tnsp.org>
parents: 52
diff changeset
110 dmGLCheckExtension("GL_ARB_fragment_shader", status);
d871e4b24328 Be less verbose when checking for GL extensions, only inform when something
Matti Hamalainen <ccr@tnsp.org>
parents: 52
diff changeset
111 if (!status)
52
6343c3392a95 After looking at many incorrect examples, the correct method for GL
Matti Hamalainen <ccr@tnsp.org>
parents: 51
diff changeset
112 {
53
d871e4b24328 Be less verbose when checking for GL extensions, only inform when something
Matti Hamalainen <ccr@tnsp.org>
parents: 52
diff changeset
113 dmError("One or more of the required OpenGL extensions not supported.\n");
52
6343c3392a95 After looking at many incorrect examples, the correct method for GL
Matti Hamalainen <ccr@tnsp.org>
parents: 51
diff changeset
114 return false;
6343c3392a95 After looking at many incorrect examples, the correct method for GL
Matti Hamalainen <ccr@tnsp.org>
parents: 51
diff changeset
115 }
6343c3392a95 After looking at many incorrect examples, the correct method for GL
Matti Hamalainen <ccr@tnsp.org>
parents: 51
diff changeset
116
51
9c6654647608 Cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 50
diff changeset
117 #define DM_GLEXT_INIT(extproctype, extprocname) \
9c6654647608 Cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 50
diff changeset
118 extprocname = (extproctype) dmGLExtInit(#extprocname, status);
42
3c7e1d3fa5a2 Implement OpengL extension handling through new header file dmglexts.h to
Matti Hamalainen <ccr@tnsp.org>
parents: 39
diff changeset
119 #include "dmglexts.h"
53
d871e4b24328 Be less verbose when checking for GL extensions, only inform when something
Matti Hamalainen <ccr@tnsp.org>
parents: 52
diff changeset
120 #endif
d871e4b24328 Be less verbose when checking for GL extensions, only inform when something
Matti Hamalainen <ccr@tnsp.org>
parents: 52
diff changeset
121
42
3c7e1d3fa5a2 Implement OpengL extension handling through new header file dmglexts.h to
Matti Hamalainen <ccr@tnsp.org>
parents: 39
diff changeset
122 return status;
3c7e1d3fa5a2 Implement OpengL extension handling through new header file dmglexts.h to
Matti Hamalainen <ccr@tnsp.org>
parents: 39
diff changeset
123 }
3c7e1d3fa5a2 Implement OpengL extension handling through new header file dmglexts.h to
Matti Hamalainen <ccr@tnsp.org>
parents: 39
diff changeset
124
3c7e1d3fa5a2 Implement OpengL extension handling through new header file dmglexts.h to
Matti Hamalainen <ccr@tnsp.org>
parents: 39
diff changeset
125
5
5dcae4dddcd9 Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 3
diff changeset
126 bool dmInitSDLGL(const int width, const int height, const char *title)
3
be31ff9e5f58 Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents: 2
diff changeset
127 {
13
c1e8057cc4d0 Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents: 12
diff changeset
128 int ret;
c1e8057cc4d0 Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents: 12
diff changeset
129 std::string msg;
c1e8057cc4d0 Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents: 12
diff changeset
130
6
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
131 // Attempt to initialize libSDL
3
be31ff9e5f58 Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents: 2
diff changeset
132 if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER | SDL_INIT_EVENTS) != 0)
be31ff9e5f58 Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents: 2
diff changeset
133 {
47
9909014498f0 Add helper functions dmError() and dmMsg() and use them.
Matti Hamalainen <ccr@tnsp.org>
parents: 46
diff changeset
134 dmError("Unable to initialize SDL: %s\n",
3
be31ff9e5f58 Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents: 2
diff changeset
135 SDL_GetError());
be31ff9e5f58 Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents: 2
diff changeset
136 return false;
be31ff9e5f58 Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents: 2
diff changeset
137 }
0
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
138
44
d93b1c2690f8 Set SDL_GL_* attributes after initializing SDL, but before creating window or OpenGL context.
Matti Hamalainen <ccr@tnsp.org>
parents: 42
diff changeset
139 // Set GL attributes
46
0c75c5f5c6b6 Downgrade GL version to 2.1.
Matti Hamalainen <ccr@tnsp.org>
parents: 45
diff changeset
140 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2);
0c75c5f5c6b6 Downgrade GL version to 2.1.
Matti Hamalainen <ccr@tnsp.org>
parents: 45
diff changeset
141 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1);
44
d93b1c2690f8 Set SDL_GL_* attributes after initializing SDL, but before creating window or OpenGL context.
Matti Hamalainen <ccr@tnsp.org>
parents: 42
diff changeset
142 SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
d93b1c2690f8 Set SDL_GL_* attributes after initializing SDL, but before creating window or OpenGL context.
Matti Hamalainen <ccr@tnsp.org>
parents: 42
diff changeset
143
d93b1c2690f8 Set SDL_GL_* attributes after initializing SDL, but before creating window or OpenGL context.
Matti Hamalainen <ccr@tnsp.org>
parents: 42
diff changeset
144 SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
d93b1c2690f8 Set SDL_GL_* attributes after initializing SDL, but before creating window or OpenGL context.
Matti Hamalainen <ccr@tnsp.org>
parents: 42
diff changeset
145 SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
d93b1c2690f8 Set SDL_GL_* attributes after initializing SDL, but before creating window or OpenGL context.
Matti Hamalainen <ccr@tnsp.org>
parents: 42
diff changeset
146 SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
d93b1c2690f8 Set SDL_GL_* attributes after initializing SDL, but before creating window or OpenGL context.
Matti Hamalainen <ccr@tnsp.org>
parents: 42
diff changeset
147 SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 8);
d93b1c2690f8 Set SDL_GL_* attributes after initializing SDL, but before creating window or OpenGL context.
Matti Hamalainen <ccr@tnsp.org>
parents: 42
diff changeset
148
d93b1c2690f8 Set SDL_GL_* attributes after initializing SDL, but before creating window or OpenGL context.
Matti Hamalainen <ccr@tnsp.org>
parents: 42
diff changeset
149 SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);
d93b1c2690f8 Set SDL_GL_* attributes after initializing SDL, but before creating window or OpenGL context.
Matti Hamalainen <ccr@tnsp.org>
parents: 42
diff changeset
150 //SDL_GL_SetAttribute(SDL_GL_BUFFER_SIZE, 32);
d93b1c2690f8 Set SDL_GL_* attributes after initializing SDL, but before creating window or OpenGL context.
Matti Hamalainen <ccr@tnsp.org>
parents: 42
diff changeset
151 SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
d93b1c2690f8 Set SDL_GL_* attributes after initializing SDL, but before creating window or OpenGL context.
Matti Hamalainen <ccr@tnsp.org>
parents: 42
diff changeset
152
6
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
153 // Attempt to create a window
8
628c42e94149 Clean up variable names.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
154 if ((dmWindow = SDL_CreateWindow(title,
3
be31ff9e5f58 Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents: 2
diff changeset
155 SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
be31ff9e5f58 Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents: 2
diff changeset
156 width, height,
be31ff9e5f58 Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents: 2
diff changeset
157 SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE)) == NULL)
2
b45d8958e5a6 Cosmetic indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
158 {
47
9909014498f0 Add helper functions dmError() and dmMsg() and use them.
Matti Hamalainen <ccr@tnsp.org>
parents: 46
diff changeset
159 dmError("Could not create SDL window: %s",
3
be31ff9e5f58 Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents: 2
diff changeset
160 SDL_GetError());
be31ff9e5f58 Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents: 2
diff changeset
161 return false;
2
b45d8958e5a6 Cosmetic indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
162 }
0
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
163
8
628c42e94149 Clean up variable names.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
164 if ((dmGLContext = SDL_GL_CreateContext(dmWindow)) == NULL)
3
be31ff9e5f58 Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents: 2
diff changeset
165 {
47
9909014498f0 Add helper functions dmError() and dmMsg() and use them.
Matti Hamalainen <ccr@tnsp.org>
parents: 46
diff changeset
166 dmError("Unable to create SDL OpenGL context: %s\n",
3
be31ff9e5f58 Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents: 2
diff changeset
167 SDL_GetError());
be31ff9e5f58 Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents: 2
diff changeset
168 return false;
be31ff9e5f58 Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents: 2
diff changeset
169 }
0
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
170
13
c1e8057cc4d0 Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents: 12
diff changeset
171 // Check if we want to attempt to use vsync
c1e8057cc4d0 Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents: 12
diff changeset
172 switch (optVSyncMode)
c1e8057cc4d0 Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents: 12
diff changeset
173 {
c1e8057cc4d0 Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents: 12
diff changeset
174 case 3:
c1e8057cc4d0 Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents: 12
diff changeset
175 ret = SDL_GL_SetSwapInterval(-1);
c1e8057cc4d0 Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents: 12
diff changeset
176 msg = "adaptive vsync";
c1e8057cc4d0 Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents: 12
diff changeset
177 break;
c1e8057cc4d0 Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents: 12
diff changeset
178
c1e8057cc4d0 Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents: 12
diff changeset
179 case 2:
c1e8057cc4d0 Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents: 12
diff changeset
180 ret = SDL_GL_SetSwapInterval(1);
c1e8057cc4d0 Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents: 12
diff changeset
181 msg = "synchronized (vsync)";
c1e8057cc4d0 Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents: 12
diff changeset
182 break;
c1e8057cc4d0 Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents: 12
diff changeset
183
c1e8057cc4d0 Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents: 12
diff changeset
184 case 1:
c1e8057cc4d0 Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents: 12
diff changeset
185 ret = SDL_GL_SetSwapInterval(0);
c1e8057cc4d0 Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents: 12
diff changeset
186 msg = "immediate (no vsync)";
c1e8057cc4d0 Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents: 12
diff changeset
187 break;
c1e8057cc4d0 Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents: 12
diff changeset
188
38
372fa249ce26 Add vsync option 0 (disable attempts of setting vsync mode.)
Matti Hamalainen <ccr@tnsp.org>
parents: 37
diff changeset
189 case 0:
372fa249ce26 Add vsync option 0 (disable attempts of setting vsync mode.)
Matti Hamalainen <ccr@tnsp.org>
parents: 37
diff changeset
190 msg = "vsync handling disabled";
372fa249ce26 Add vsync option 0 (disable attempts of setting vsync mode.)
Matti Hamalainen <ccr@tnsp.org>
parents: 37
diff changeset
191 ret = 0;
372fa249ce26 Add vsync option 0 (disable attempts of setting vsync mode.)
Matti Hamalainen <ccr@tnsp.org>
parents: 37
diff changeset
192 break;
372fa249ce26 Add vsync option 0 (disable attempts of setting vsync mode.)
Matti Hamalainen <ccr@tnsp.org>
parents: 37
diff changeset
193
13
c1e8057cc4d0 Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents: 12
diff changeset
194 default:
c1e8057cc4d0 Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents: 12
diff changeset
195 ret = -1;
c1e8057cc4d0 Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents: 12
diff changeset
196 msg = "INVALID VSYNC MODE";
c1e8057cc4d0 Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents: 12
diff changeset
197 break;
c1e8057cc4d0 Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents: 12
diff changeset
198 }
c1e8057cc4d0 Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents: 12
diff changeset
199
c1e8057cc4d0 Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents: 12
diff changeset
200 if (ret != 0)
c1e8057cc4d0 Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents: 12
diff changeset
201 {
47
9909014498f0 Add helper functions dmError() and dmMsg() and use them.
Matti Hamalainen <ccr@tnsp.org>
parents: 46
diff changeset
202 dmError("Could not set vsync mode to %s.\n",
13
c1e8057cc4d0 Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents: 12
diff changeset
203 msg.c_str());
c1e8057cc4d0 Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents: 12
diff changeset
204 return false;
c1e8057cc4d0 Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents: 12
diff changeset
205 }
c1e8057cc4d0 Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents: 12
diff changeset
206
42
3c7e1d3fa5a2 Implement OpengL extension handling through new header file dmglexts.h to
Matti Hamalainen <ccr@tnsp.org>
parents: 39
diff changeset
207 // Get/initialize OpenGL extension function pointers
3c7e1d3fa5a2 Implement OpengL extension handling through new header file dmglexts.h to
Matti Hamalainen <ccr@tnsp.org>
parents: 39
diff changeset
208 if (optUseShaders && !dmInitGLExtensions())
3c7e1d3fa5a2 Implement OpengL extension handling through new header file dmglexts.h to
Matti Hamalainen <ccr@tnsp.org>
parents: 39
diff changeset
209 return false;
3c7e1d3fa5a2 Implement OpengL extension handling through new header file dmglexts.h to
Matti Hamalainen <ccr@tnsp.org>
parents: 39
diff changeset
210
6
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
211 // Dump some information
47
9909014498f0 Add helper functions dmError() and dmMsg() and use them.
Matti Hamalainen <ccr@tnsp.org>
parents: 46
diff changeset
212 dmMsg("GL_VENDOR : %s\n", glGetString(GL_VENDOR));
9909014498f0 Add helper functions dmError() and dmMsg() and use them.
Matti Hamalainen <ccr@tnsp.org>
parents: 46
diff changeset
213 dmMsg("GL_RENDERER : %s\n", glGetString(GL_RENDERER));
9909014498f0 Add helper functions dmError() and dmMsg() and use them.
Matti Hamalainen <ccr@tnsp.org>
parents: 46
diff changeset
214 dmMsg("GL_VERSION : %s\n", glGetString(GL_VERSION));
9909014498f0 Add helper functions dmError() and dmMsg() and use them.
Matti Hamalainen <ccr@tnsp.org>
parents: 46
diff changeset
215 dmMsg("VSync mode : %s\n", msg.c_str());
0
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
216
55
bf73a2a70ec7 Add OpenGL error checking.
Matti Hamalainen <ccr@tnsp.org>
parents: 54
diff changeset
217 if (!dmGLCheckErrors())
bf73a2a70ec7 Add OpenGL error checking.
Matti Hamalainen <ccr@tnsp.org>
parents: 54
diff changeset
218 return false;
bf73a2a70ec7 Add OpenGL error checking.
Matti Hamalainen <ccr@tnsp.org>
parents: 54
diff changeset
219
2
b45d8958e5a6 Cosmetic indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
220 // Setup the window and view port
3
be31ff9e5f58 Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents: 2
diff changeset
221 glViewport(0, 0, width, height);
0
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
222
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
223 glMatrixMode(GL_PROJECTION);
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
224 glLoadIdentity();
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
225
3
be31ff9e5f58 Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents: 2
diff changeset
226 gluPerspective(45.0f, GLfloat(width) / GLfloat(height), 0.1f, 1000.0f);
0
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
227
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
228 glMatrixMode(GL_MODELVIEW);
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
229 glLoadIdentity();
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
230
2
b45d8958e5a6 Cosmetic indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
231 // Enable back face culling
b45d8958e5a6 Cosmetic indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
232 glEnable(GL_CULL_FACE);
0
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
233
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
234 // Enable smooth shading
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
235 glShadeModel(GL_SMOOTH);
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
236
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
237 // Enable the depth buffer
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
238 glEnable(GL_DEPTH_TEST);
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
239
21
1404dfcee7b8 More work on scenefile and model loading support.
Matti Hamalainen <ccr@tnsp.org>
parents: 20
diff changeset
240 // Enable normal rescaling
1404dfcee7b8 More work on scenefile and model loading support.
Matti Hamalainen <ccr@tnsp.org>
parents: 20
diff changeset
241 glEnable(GL_RESCALE_NORMAL);
1404dfcee7b8 More work on scenefile and model loading support.
Matti Hamalainen <ccr@tnsp.org>
parents: 20
diff changeset
242
0
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
243 // Setup depth buffer
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
244 glClearDepth(1.0f);
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
245
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
246 // Set the depth buffer function
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
247 glDepthFunc(GL_LEQUAL);
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
248
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
249 // Enable vertex and and normal arrays
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
250 glEnableClientState(GL_VERTEX_ARRAY);
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
251 glEnableClientState(GL_NORMAL_ARRAY);
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
252
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
253 // Set correct perspective correction
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
254 glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
3
be31ff9e5f58 Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents: 2
diff changeset
255
55
bf73a2a70ec7 Add OpenGL error checking.
Matti Hamalainen <ccr@tnsp.org>
parents: 54
diff changeset
256 return dmGLCheckErrors();
0
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
257 }
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
258
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
259
37
73a785323e8a Rendering cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 36
diff changeset
260 void dmDrawModel(const DMSimpleScene &scene, const DMModel &model)
0
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
261 {
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
262 int maxIndices;
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
263
37
73a785323e8a Rendering cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 36
diff changeset
264 if (optUseShaders)
73a785323e8a Rendering cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 36
diff changeset
265 {
73a785323e8a Rendering cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 36
diff changeset
266 // Enable shader program
73a785323e8a Rendering cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 36
diff changeset
267 glUseProgram(model.id_prog);
73a785323e8a Rendering cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 36
diff changeset
268 glUniform1i(glGetUniformLocation(model.id_prog, "nlights"), scene.lights.size());
73a785323e8a Rendering cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 36
diff changeset
269 }
73a785323e8a Rendering cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 36
diff changeset
270
73a785323e8a Rendering cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 36
diff changeset
271 // Set the material of the model
54
73fa5fb437a0 Fumbling around.
Matti Hamalainen <ccr@tnsp.org>
parents: 53
diff changeset
272 glEnable(GL_COLOR_MATERIAL);
32
1215fdd0a8ab Add support for specifying specular and shininess values per model.
Matti Hamalainen <ccr@tnsp.org>
parents: 31
diff changeset
273 glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE);
48
0ae1ff609626 Implement diffuse setting for models in scenefile. Also refactor
Matti Hamalainen <ccr@tnsp.org>
parents: 47
diff changeset
274 glMateriali(GL_FRONT, GL_SHININESS, model.material.shininess);
0ae1ff609626 Implement diffuse setting for models in scenefile. Also refactor
Matti Hamalainen <ccr@tnsp.org>
parents: 47
diff changeset
275 glMaterialfv(GL_FRONT, GL_SPECULAR, model.material.specular.values);
0ae1ff609626 Implement diffuse setting for models in scenefile. Also refactor
Matti Hamalainen <ccr@tnsp.org>
parents: 47
diff changeset
276 glColor4fv(model.material.diffuse.values);
32
1215fdd0a8ab Add support for specifying specular and shininess values per model.
Matti Hamalainen <ccr@tnsp.org>
parents: 31
diff changeset
277
17
0fa9302e324d Cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 16
diff changeset
278 // Render the model
2
b45d8958e5a6 Cosmetic indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
279 glGetIntegerv(GL_MAX_ELEMENTS_INDICES, &maxIndices);
0
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
280
21
1404dfcee7b8 More work on scenefile and model loading support.
Matti Hamalainen <ccr@tnsp.org>
parents: 20
diff changeset
281 // Add transforms
36
d640f2a34031 Only scale/translate/rotate the model if those attributes have been set in the scenefile.
Matti Hamalainen <ccr@tnsp.org>
parents: 32
diff changeset
282 if (model.scaleSet)
d640f2a34031 Only scale/translate/rotate the model if those attributes have been set in the scenefile.
Matti Hamalainen <ccr@tnsp.org>
parents: 32
diff changeset
283 glScalef(model.scale.x, model.scale.y, model.scale.z);
d640f2a34031 Only scale/translate/rotate the model if those attributes have been set in the scenefile.
Matti Hamalainen <ccr@tnsp.org>
parents: 32
diff changeset
284
d640f2a34031 Only scale/translate/rotate the model if those attributes have been set in the scenefile.
Matti Hamalainen <ccr@tnsp.org>
parents: 32
diff changeset
285 if (model.translateSet)
d640f2a34031 Only scale/translate/rotate the model if those attributes have been set in the scenefile.
Matti Hamalainen <ccr@tnsp.org>
parents: 32
diff changeset
286 glTranslatef(model.translate.x, model.translate.y, model.translate.z);
d640f2a34031 Only scale/translate/rotate the model if those attributes have been set in the scenefile.
Matti Hamalainen <ccr@tnsp.org>
parents: 32
diff changeset
287
d640f2a34031 Only scale/translate/rotate the model if those attributes have been set in the scenefile.
Matti Hamalainen <ccr@tnsp.org>
parents: 32
diff changeset
288 if (model.rotateSet)
d640f2a34031 Only scale/translate/rotate the model if those attributes have been set in the scenefile.
Matti Hamalainen <ccr@tnsp.org>
parents: 32
diff changeset
289 {
d640f2a34031 Only scale/translate/rotate the model if those attributes have been set in the scenefile.
Matti Hamalainen <ccr@tnsp.org>
parents: 32
diff changeset
290 glRotatef(model.rotate.x, 1.0f, 0.0f, 0.0f);
d640f2a34031 Only scale/translate/rotate the model if those attributes have been set in the scenefile.
Matti Hamalainen <ccr@tnsp.org>
parents: 32
diff changeset
291 glRotatef(model.rotate.y, 0.0f, 1.0f, 0.0f);
d640f2a34031 Only scale/translate/rotate the model if those attributes have been set in the scenefile.
Matti Hamalainen <ccr@tnsp.org>
parents: 32
diff changeset
292 glRotatef(model.rotate.z, 0.0f, 0.0f, 1.0f);
d640f2a34031 Only scale/translate/rotate the model if those attributes have been set in the scenefile.
Matti Hamalainen <ccr@tnsp.org>
parents: 32
diff changeset
293 }
0
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
294
21
1404dfcee7b8 More work on scenefile and model loading support.
Matti Hamalainen <ccr@tnsp.org>
parents: 20
diff changeset
295 glVertexPointer(3, GL_FLOAT, 0, &model.vertices[0]);
1404dfcee7b8 More work on scenefile and model loading support.
Matti Hamalainen <ccr@tnsp.org>
parents: 20
diff changeset
296 glNormalPointer( GL_FLOAT, 0, &model.normals[0]);
1404dfcee7b8 More work on scenefile and model loading support.
Matti Hamalainen <ccr@tnsp.org>
parents: 20
diff changeset
297
1404dfcee7b8 More work on scenefile and model loading support.
Matti Hamalainen <ccr@tnsp.org>
parents: 20
diff changeset
298 for (int n = 0; n < model.nfaces; n += maxIndices)
2
b45d8958e5a6 Cosmetic indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
299 {
21
1404dfcee7b8 More work on scenefile and model loading support.
Matti Hamalainen <ccr@tnsp.org>
parents: 20
diff changeset
300 const int count = std::min(maxIndices, model.nfaces - n);
1404dfcee7b8 More work on scenefile and model loading support.
Matti Hamalainen <ccr@tnsp.org>
parents: 20
diff changeset
301 glDrawElements(GL_TRIANGLES, count * 3, GL_UNSIGNED_INT, &model.faces[n * 3]);
2
b45d8958e5a6 Cosmetic indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
302 }
37
73a785323e8a Rendering cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 36
diff changeset
303
73a785323e8a Rendering cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 36
diff changeset
304 // Restore
73a785323e8a Rendering cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 36
diff changeset
305 if (optUseShaders)
73a785323e8a Rendering cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 36
diff changeset
306 {
73a785323e8a Rendering cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 36
diff changeset
307 glUseProgram(0);
73a785323e8a Rendering cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 36
diff changeset
308 }
0
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
309 }
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
310
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
311
21
1404dfcee7b8 More work on scenefile and model loading support.
Matti Hamalainen <ccr@tnsp.org>
parents: 20
diff changeset
312 void dmDrawScene(const DMSimpleScene &scene)
0
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
313 {
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
314 glClear(GL_DEPTH_BUFFER_BIT);
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
315
2
b45d8958e5a6 Cosmetic indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
316 glMatrixMode(GL_PROJECTION);
b45d8958e5a6 Cosmetic indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
317 glPushMatrix();
b45d8958e5a6 Cosmetic indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
318 glLoadIdentity();
0
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
319
2
b45d8958e5a6 Cosmetic indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
320 glOrtho(0.0, 1.0, 0.0, 1.0, -1, 1);
0
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
321
2
b45d8958e5a6 Cosmetic indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
322 glMatrixMode(GL_MODELVIEW);
b45d8958e5a6 Cosmetic indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
323 glPushMatrix();
b45d8958e5a6 Cosmetic indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
324 glLoadIdentity();
0
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
325
17
0fa9302e324d Cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 16
diff changeset
326 // Draw the background gradient
0
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
327 glDisable(GL_DEPTH_TEST);
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
328 glDisable(GL_LIGHTING);
2
b45d8958e5a6 Cosmetic indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
329 glBegin(GL_QUADS);
b45d8958e5a6 Cosmetic indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
330 {
b45d8958e5a6 Cosmetic indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
331 glColor3ub(0x3B, 0x3B, 0x75);
b45d8958e5a6 Cosmetic indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
332 glVertex2f(0.0f, 0.0f);
b45d8958e5a6 Cosmetic indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
333 glVertex2f(1.0f, 0.0f);
0
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
334
2
b45d8958e5a6 Cosmetic indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
335 glColor3ub(0x00, 0x00, 0x00);
b45d8958e5a6 Cosmetic indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
336 glVertex2f(1.0f, 1.0f);
b45d8958e5a6 Cosmetic indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
337 glVertex2f(0.0f, 1.0f);
b45d8958e5a6 Cosmetic indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
338 }
b45d8958e5a6 Cosmetic indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
339 glEnd();
0
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
340
2
b45d8958e5a6 Cosmetic indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
341 // Restore the 3D projection
b45d8958e5a6 Cosmetic indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
342 glMatrixMode(GL_PROJECTION);
b45d8958e5a6 Cosmetic indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
343 glPopMatrix();
0
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
344
2
b45d8958e5a6 Cosmetic indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
345 glMatrixMode(GL_MODELVIEW);
b45d8958e5a6 Cosmetic indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
346 glPopMatrix();
0
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
347
3
be31ff9e5f58 Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents: 2
diff changeset
348 glEnable(GL_DEPTH_TEST);
37
73a785323e8a Rendering cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 36
diff changeset
349 glEnable(GL_LIGHTING);
0
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
350
21
1404dfcee7b8 More work on scenefile and model loading support.
Matti Hamalainen <ccr@tnsp.org>
parents: 20
diff changeset
351 // Draw models
1404dfcee7b8 More work on scenefile and model loading support.
Matti Hamalainen <ccr@tnsp.org>
parents: 20
diff changeset
352 for (const DMModel &model : scene.models)
27
097184bd34a8 Implement number of lights uniform for the shaders, clean up light setup and
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
353 {
097184bd34a8 Implement number of lights uniform for the shaders, clean up light setup and
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
354 glPushMatrix();
37
73a785323e8a Rendering cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 36
diff changeset
355 dmDrawModel(scene, model);
27
097184bd34a8 Implement number of lights uniform for the shaders, clean up light setup and
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
356 glPopMatrix();
097184bd34a8 Implement number of lights uniform for the shaders, clean up light setup and
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
357 }
6
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
358 }
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
359
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
360
24
c1897cfc8463 Add shader compilation error handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 23
diff changeset
361 bool dmCompileShader(const GLenum stype, const std::string &src, GLuint &shader)
6
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
362 {
24
c1897cfc8463 Add shader compilation error handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 23
diff changeset
363 GLint status;
6
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
364 const char *tmp = src.c_str();
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
365
24
c1897cfc8463 Add shader compilation error handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 23
diff changeset
366 shader = glCreateShader(stype);
6
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
367 glShaderSource(shader, 1, &tmp, 0);
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
368 glCompileShader(shader);
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
369
24
c1897cfc8463 Add shader compilation error handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 23
diff changeset
370 glGetShaderiv(shader, GL_COMPILE_STATUS, &status);
c1897cfc8463 Add shader compilation error handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 23
diff changeset
371 if (status == GL_TRUE)
c1897cfc8463 Add shader compilation error handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 23
diff changeset
372 return true;
c1897cfc8463 Add shader compilation error handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 23
diff changeset
373 else
c1897cfc8463 Add shader compilation error handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 23
diff changeset
374 {
26
67647ed860f0 Improve shader compilation error reporting.
Matti Hamalainen <ccr@tnsp.org>
parents: 25
diff changeset
375 GLint bufLen = 0;
67647ed860f0 Improve shader compilation error reporting.
Matti Hamalainen <ccr@tnsp.org>
parents: 25
diff changeset
376 glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &bufLen);
67647ed860f0 Improve shader compilation error reporting.
Matti Hamalainen <ccr@tnsp.org>
parents: 25
diff changeset
377
67647ed860f0 Improve shader compilation error reporting.
Matti Hamalainen <ccr@tnsp.org>
parents: 25
diff changeset
378 if (bufLen > 0)
67647ed860f0 Improve shader compilation error reporting.
Matti Hamalainen <ccr@tnsp.org>
parents: 25
diff changeset
379 {
67647ed860f0 Improve shader compilation error reporting.
Matti Hamalainen <ccr@tnsp.org>
parents: 25
diff changeset
380 char *buf = new char[bufLen];
67647ed860f0 Improve shader compilation error reporting.
Matti Hamalainen <ccr@tnsp.org>
parents: 25
diff changeset
381 glGetShaderInfoLog(shader, bufLen, NULL, buf);
47
9909014498f0 Add helper functions dmError() and dmMsg() and use them.
Matti Hamalainen <ccr@tnsp.org>
parents: 46
diff changeset
382 dmError("Shader compliation error:\n%s\n",
26
67647ed860f0 Improve shader compilation error reporting.
Matti Hamalainen <ccr@tnsp.org>
parents: 25
diff changeset
383 buf);
67647ed860f0 Improve shader compilation error reporting.
Matti Hamalainen <ccr@tnsp.org>
parents: 25
diff changeset
384 delete buf;
67647ed860f0 Improve shader compilation error reporting.
Matti Hamalainen <ccr@tnsp.org>
parents: 25
diff changeset
385 }
67647ed860f0 Improve shader compilation error reporting.
Matti Hamalainen <ccr@tnsp.org>
parents: 25
diff changeset
386 else
67647ed860f0 Improve shader compilation error reporting.
Matti Hamalainen <ccr@tnsp.org>
parents: 25
diff changeset
387 {
47
9909014498f0 Add helper functions dmError() and dmMsg() and use them.
Matti Hamalainen <ccr@tnsp.org>
parents: 46
diff changeset
388 dmError("Shader compilation error occured, but no error information got.\n");
26
67647ed860f0 Improve shader compilation error reporting.
Matti Hamalainen <ccr@tnsp.org>
parents: 25
diff changeset
389 }
24
c1897cfc8463 Add shader compilation error handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 23
diff changeset
390 return false;
c1897cfc8463 Add shader compilation error handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 23
diff changeset
391 }
6
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
392 }
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
393
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
394
21
1404dfcee7b8 More work on scenefile and model loading support.
Matti Hamalainen <ccr@tnsp.org>
parents: 20
diff changeset
395 void dmLinkModelShaders(DMModel &model)
6
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
396 {
21
1404dfcee7b8 More work on scenefile and model loading support.
Matti Hamalainen <ccr@tnsp.org>
parents: 20
diff changeset
397 model.id_prog = glCreateProgram();
1404dfcee7b8 More work on scenefile and model loading support.
Matti Hamalainen <ccr@tnsp.org>
parents: 20
diff changeset
398 glAttachShader(model.id_prog, model.id_fs);
1404dfcee7b8 More work on scenefile and model loading support.
Matti Hamalainen <ccr@tnsp.org>
parents: 20
diff changeset
399 glAttachShader(model.id_prog, model.id_vs);
1404dfcee7b8 More work on scenefile and model loading support.
Matti Hamalainen <ccr@tnsp.org>
parents: 20
diff changeset
400 glLinkProgram(model.id_prog);
0
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
401 }
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
402
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
403
25
2403030a0352 "Finish" implementing multiple lights support in scene files.
Matti Hamalainen <ccr@tnsp.org>
parents: 24
diff changeset
404 void dmSetupLight(const int n, const DMLight &light)
2403030a0352 "Finish" implementing multiple lights support in scene files.
Matti Hamalainen <ccr@tnsp.org>
parents: 24
diff changeset
405 {
2403030a0352 "Finish" implementing multiple lights support in scene files.
Matti Hamalainen <ccr@tnsp.org>
parents: 24
diff changeset
406 glEnable(GL_LIGHT0 + n);
48
0ae1ff609626 Implement diffuse setting for models in scenefile. Also refactor
Matti Hamalainen <ccr@tnsp.org>
parents: 47
diff changeset
407 glLightfv(GL_LIGHT0 + n, GL_AMBIENT, light.color.ambient.values);
0ae1ff609626 Implement diffuse setting for models in scenefile. Also refactor
Matti Hamalainen <ccr@tnsp.org>
parents: 47
diff changeset
408 glLightfv(GL_LIGHT0 + n, GL_DIFFUSE, light.color.diffuse.values);
0ae1ff609626 Implement diffuse setting for models in scenefile. Also refactor
Matti Hamalainen <ccr@tnsp.org>
parents: 47
diff changeset
409 glLightfv(GL_LIGHT0 + n, GL_SPECULAR, light.color.specular.values);
25
2403030a0352 "Finish" implementing multiple lights support in scene files.
Matti Hamalainen <ccr@tnsp.org>
parents: 24
diff changeset
410 glLightfv(GL_LIGHT0 + n, GL_POSITION, light.position.values);
2403030a0352 "Finish" implementing multiple lights support in scene files.
Matti Hamalainen <ccr@tnsp.org>
parents: 24
diff changeset
411 }
2403030a0352 "Finish" implementing multiple lights support in scene files.
Matti Hamalainen <ccr@tnsp.org>
parents: 24
diff changeset
412
2403030a0352 "Finish" implementing multiple lights support in scene files.
Matti Hamalainen <ccr@tnsp.org>
parents: 24
diff changeset
413
6
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
414 int main(int argc, char *argv[])
0
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
415 {
13
c1e8057cc4d0 Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents: 12
diff changeset
416 int startTime, cycleStart, cycleFrames = 0, totalFrames = 0;
c1e8057cc4d0 Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents: 12
diff changeset
417 double totalTime;
21
1404dfcee7b8 More work on scenefile and model loading support.
Matti Hamalainen <ccr@tnsp.org>
parents: 20
diff changeset
418 bool
1404dfcee7b8 More work on scenefile and model loading support.
Matti Hamalainen <ccr@tnsp.org>
parents: 20
diff changeset
419 exitFlag = false,
1404dfcee7b8 More work on scenefile and model loading support.
Matti Hamalainen <ccr@tnsp.org>
parents: 20
diff changeset
420 optShowHelp = false,
1404dfcee7b8 More work on scenefile and model loading support.
Matti Hamalainen <ccr@tnsp.org>
parents: 20
diff changeset
421 optSetInputFilename = false;
1404dfcee7b8 More work on scenefile and model loading support.
Matti Hamalainen <ccr@tnsp.org>
parents: 20
diff changeset
422 std::string optInputFilename = "dragon.scene", basePath;
1404dfcee7b8 More work on scenefile and model loading support.
Matti Hamalainen <ccr@tnsp.org>
parents: 20
diff changeset
423 DMSimpleScene scene;
6
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
424
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
425 // Check commandline argument for enabling shaders
7
95dd5417e7de Add simple help.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
426 for (int narg = 1; narg < argc; narg++)
95dd5417e7de Add simple help.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
427 {
95dd5417e7de Add simple help.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
428 char *arg = argv[narg];
11
89dc8caeff41 Improve option handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
429 if (arg[0] == '-')
89dc8caeff41 Improve option handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
430 {
89dc8caeff41 Improve option handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
431 char *opt = arg + 1;
89dc8caeff41 Improve option handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
432
89dc8caeff41 Improve option handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
433 if ((opt[0] == '-' && opt[1] == 'h' && opt[2] == 'e') ||
12
52a586c344f4 Add option -m<filename prefix> to specify mesh/shader filename prefix.
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
434 opt[0] == '?' || (opt[0] == '-' && opt[1] == '?'))
11
89dc8caeff41 Improve option handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
435 {
89dc8caeff41 Improve option handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
436 optShowHelp = true;
89dc8caeff41 Improve option handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
437 break;
89dc8caeff41 Improve option handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
438 }
89dc8caeff41 Improve option handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
439 else
89dc8caeff41 Improve option handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
440 if (opt[0] == '-')
89dc8caeff41 Improve option handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
441 opt++;
89dc8caeff41 Improve option handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
442
13
c1e8057cc4d0 Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents: 12
diff changeset
443 switch (opt[0])
12
52a586c344f4 Add option -m<filename prefix> to specify mesh/shader filename prefix.
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
444 {
55
bf73a2a70ec7 Add OpenGL error checking.
Matti Hamalainen <ccr@tnsp.org>
parents: 54
diff changeset
445 case 'g':
bf73a2a70ec7 Add OpenGL error checking.
Matti Hamalainen <ccr@tnsp.org>
parents: 54
diff changeset
446 optUseShaders = true;
bf73a2a70ec7 Add OpenGL error checking.
Matti Hamalainen <ccr@tnsp.org>
parents: 54
diff changeset
447 break;
bf73a2a70ec7 Add OpenGL error checking.
Matti Hamalainen <ccr@tnsp.org>
parents: 54
diff changeset
448
13
c1e8057cc4d0 Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents: 12
diff changeset
449 case 'w':
c1e8057cc4d0 Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents: 12
diff changeset
450 case 'h':
c1e8057cc4d0 Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents: 12
diff changeset
451 case 'm':
c1e8057cc4d0 Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents: 12
diff changeset
452 case 'v':
c1e8057cc4d0 Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents: 12
diff changeset
453 if (opt[1] == 0)
c1e8057cc4d0 Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents: 12
diff changeset
454 {
c1e8057cc4d0 Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents: 12
diff changeset
455 printf("Option '%s' requires an argument.\n", opt);
c1e8057cc4d0 Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents: 12
diff changeset
456 goto exit;
c1e8057cc4d0 Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents: 12
diff changeset
457 }
c1e8057cc4d0 Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents: 12
diff changeset
458
c1e8057cc4d0 Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents: 12
diff changeset
459 switch (opt[0])
c1e8057cc4d0 Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents: 12
diff changeset
460 {
c1e8057cc4d0 Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents: 12
diff changeset
461 case 'w': optWidth = atoi(opt + 1); break;
c1e8057cc4d0 Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents: 12
diff changeset
462 case 'h': optHeight = atoi(opt + 1); break;
c1e8057cc4d0 Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents: 12
diff changeset
463 case 'v': optVSyncMode = atoi(opt + 1); break;
c1e8057cc4d0 Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents: 12
diff changeset
464 }
c1e8057cc4d0 Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents: 12
diff changeset
465 break;
c1e8057cc4d0 Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents: 12
diff changeset
466
c1e8057cc4d0 Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents: 12
diff changeset
467 default:
c1e8057cc4d0 Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents: 12
diff changeset
468 printf("Unknown option '%s'.\n", opt);
12
52a586c344f4 Add option -m<filename prefix> to specify mesh/shader filename prefix.
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
469 goto exit;
52a586c344f4 Add option -m<filename prefix> to specify mesh/shader filename prefix.
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
470 }
11
89dc8caeff41 Improve option handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
471 }
21
1404dfcee7b8 More work on scenefile and model loading support.
Matti Hamalainen <ccr@tnsp.org>
parents: 20
diff changeset
472 else
1404dfcee7b8 More work on scenefile and model loading support.
Matti Hamalainen <ccr@tnsp.org>
parents: 20
diff changeset
473 {
1404dfcee7b8 More work on scenefile and model loading support.
Matti Hamalainen <ccr@tnsp.org>
parents: 20
diff changeset
474 if (optSetInputFilename)
1404dfcee7b8 More work on scenefile and model loading support.
Matti Hamalainen <ccr@tnsp.org>
parents: 20
diff changeset
475 {
47
9909014498f0 Add helper functions dmError() and dmMsg() and use them.
Matti Hamalainen <ccr@tnsp.org>
parents: 46
diff changeset
476 dmError("Please specify only one scene file.\n");
21
1404dfcee7b8 More work on scenefile and model loading support.
Matti Hamalainen <ccr@tnsp.org>
parents: 20
diff changeset
477 goto exit;
1404dfcee7b8 More work on scenefile and model loading support.
Matti Hamalainen <ccr@tnsp.org>
parents: 20
diff changeset
478 }
1404dfcee7b8 More work on scenefile and model loading support.
Matti Hamalainen <ccr@tnsp.org>
parents: 20
diff changeset
479
1404dfcee7b8 More work on scenefile and model loading support.
Matti Hamalainen <ccr@tnsp.org>
parents: 20
diff changeset
480 optSetInputFilename = true;
1404dfcee7b8 More work on scenefile and model loading support.
Matti Hamalainen <ccr@tnsp.org>
parents: 20
diff changeset
481 optInputFilename = std::string(arg);
1404dfcee7b8 More work on scenefile and model loading support.
Matti Hamalainen <ccr@tnsp.org>
parents: 20
diff changeset
482 if (optInputFilename.empty())
1404dfcee7b8 More work on scenefile and model loading support.
Matti Hamalainen <ccr@tnsp.org>
parents: 20
diff changeset
483 {
47
9909014498f0 Add helper functions dmError() and dmMsg() and use them.
Matti Hamalainen <ccr@tnsp.org>
parents: 46
diff changeset
484 dmError("Invalid input filename.\n");
21
1404dfcee7b8 More work on scenefile and model loading support.
Matti Hamalainen <ccr@tnsp.org>
parents: 20
diff changeset
485 goto exit;
1404dfcee7b8 More work on scenefile and model loading support.
Matti Hamalainen <ccr@tnsp.org>
parents: 20
diff changeset
486 }
1404dfcee7b8 More work on scenefile and model loading support.
Matti Hamalainen <ccr@tnsp.org>
parents: 20
diff changeset
487
1404dfcee7b8 More work on scenefile and model loading support.
Matti Hamalainen <ccr@tnsp.org>
parents: 20
diff changeset
488 }
7
95dd5417e7de Add simple help.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
489 }
95dd5417e7de Add simple help.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
490
8
628c42e94149 Clean up variable names.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
491 if (optShowHelp)
7
95dd5417e7de Add simple help.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
492 {
95dd5417e7de Add simple help.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
493 printf(
21
1404dfcee7b8 More work on scenefile and model loading support.
Matti Hamalainen <ccr@tnsp.org>
parents: 20
diff changeset
494 "Usage: %s [options] [<scenefile.scene>]\n"
12
52a586c344f4 Add option -m<filename prefix> to specify mesh/shader filename prefix.
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
495 "-? Show this help\n"
52a586c344f4 Add option -m<filename prefix> to specify mesh/shader filename prefix.
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
496 "-g Use GLSL shader instead of basic OpenGL lighting\n"
52a586c344f4 Add option -m<filename prefix> to specify mesh/shader filename prefix.
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
497 "-w<width> Window width (default %d)\n"
52a586c344f4 Add option -m<filename prefix> to specify mesh/shader filename prefix.
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
498 "-h<height> Window height (default %d)\n"
38
372fa249ce26 Add vsync option 0 (disable attempts of setting vsync mode.)
Matti Hamalainen <ccr@tnsp.org>
parents: 37
diff changeset
499 "-v<0-3> Set vsync mode: 0 = do not attempt to set vsync mode\n"
50
d4232c89145f Clarify the help for -v option.
Matti Hamalainen <ccr@tnsp.org>
parents: 49
diff changeset
500 " (may be required for software rendering backends),\n"
d4232c89145f Clarify the help for -v option.
Matti Hamalainen <ccr@tnsp.org>
parents: 49
diff changeset
501 " 1 = no vsync, 2 = vsync, 3 = adaptive.\n"
d4232c89145f Clarify the help for -v option.
Matti Hamalainen <ccr@tnsp.org>
parents: 49
diff changeset
502 " Using vsync (2) will result in FPS being approximately\n"
d4232c89145f Clarify the help for -v option.
Matti Hamalainen <ccr@tnsp.org>
parents: 49
diff changeset
503 " whatever your monitor refresh rate is. The default\n"
d4232c89145f Clarify the help for -v option.
Matti Hamalainen <ccr@tnsp.org>
parents: 49
diff changeset
504 " value is 1 (no vsync).\n"
11
89dc8caeff41 Improve option handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
505 "\n",
89dc8caeff41 Improve option handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
506 argv[0],
89dc8caeff41 Improve option handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
507 SET_DEF_WIDTH, SET_DEF_HEIGHT
89dc8caeff41 Improve option handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
508 );
89dc8caeff41 Improve option handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
509
7
95dd5417e7de Add simple help.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
510 goto exit;
95dd5417e7de Add simple help.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
511 }
6
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
512
13
c1e8057cc4d0 Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents: 12
diff changeset
513 if (optWidth < 100 || optWidth > 8192 || optHeight < 100 || optHeight > 8192)
c1e8057cc4d0 Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents: 12
diff changeset
514 {
47
9909014498f0 Add helper functions dmError() and dmMsg() and use them.
Matti Hamalainen <ccr@tnsp.org>
parents: 46
diff changeset
515 dmError("Invalid window width or height (%d x %d).\n",
13
c1e8057cc4d0 Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents: 12
diff changeset
516 optWidth, optHeight);
c1e8057cc4d0 Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents: 12
diff changeset
517 goto exit;
c1e8057cc4d0 Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents: 12
diff changeset
518 }
c1e8057cc4d0 Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents: 12
diff changeset
519
21
1404dfcee7b8 More work on scenefile and model loading support.
Matti Hamalainen <ccr@tnsp.org>
parents: 20
diff changeset
520 // Load the scene
1404dfcee7b8 More work on scenefile and model loading support.
Matti Hamalainen <ccr@tnsp.org>
parents: 20
diff changeset
521 if (!scene.loadInfo(optInputFilename))
1404dfcee7b8 More work on scenefile and model loading support.
Matti Hamalainen <ccr@tnsp.org>
parents: 20
diff changeset
522 goto exit;
1404dfcee7b8 More work on scenefile and model loading support.
Matti Hamalainen <ccr@tnsp.org>
parents: 20
diff changeset
523
1404dfcee7b8 More work on scenefile and model loading support.
Matti Hamalainen <ccr@tnsp.org>
parents: 20
diff changeset
524 if (scene.models.size() == 0)
12
52a586c344f4 Add option -m<filename prefix> to specify mesh/shader filename prefix.
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
525 {
47
9909014498f0 Add helper functions dmError() and dmMsg() and use them.
Matti Hamalainen <ccr@tnsp.org>
parents: 46
diff changeset
526 dmError("Scenefile '%s' contains no models.\n",
21
1404dfcee7b8 More work on scenefile and model loading support.
Matti Hamalainen <ccr@tnsp.org>
parents: 20
diff changeset
527 optInputFilename.c_str());
12
52a586c344f4 Add option -m<filename prefix> to specify mesh/shader filename prefix.
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
528 goto exit;
52a586c344f4 Add option -m<filename prefix> to specify mesh/shader filename prefix.
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
529 }
52a586c344f4 Add option -m<filename prefix> to specify mesh/shader filename prefix.
Matti Hamalainen <ccr@tnsp.org>
parents: 11
diff changeset
530
31
6847715b46cd Move piece of code a bit as a cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
531 // Define a default light if none defined in scene file
6847715b46cd Move piece of code a bit as a cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
532 if (scene.lights.size() == 0)
6847715b46cd Move piece of code a bit as a cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
533 {
6847715b46cd Move piece of code a bit as a cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
534 DMLight light; // Default light
6847715b46cd Move piece of code a bit as a cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
535 scene.lights.push_back(light);
6847715b46cd Move piece of code a bit as a cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
536 }
6847715b46cd Move piece of code a bit as a cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
537
47
9909014498f0 Add helper functions dmError() and dmMsg() and use them.
Matti Hamalainen <ccr@tnsp.org>
parents: 46
diff changeset
538 dmMsg("Loading %ld model(s) ..\n",
21
1404dfcee7b8 More work on scenefile and model loading support.
Matti Hamalainen <ccr@tnsp.org>
parents: 20
diff changeset
539 scene.models.size());
6
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
540
21
1404dfcee7b8 More work on scenefile and model loading support.
Matti Hamalainen <ccr@tnsp.org>
parents: 20
diff changeset
541 basePath = dmGetPath(optInputFilename);
47
9909014498f0 Add helper functions dmError() and dmMsg() and use them.
Matti Hamalainen <ccr@tnsp.org>
parents: 46
diff changeset
542 dmMsg("Scene base path '%s'\n", basePath.c_str());
21
1404dfcee7b8 More work on scenefile and model loading support.
Matti Hamalainen <ccr@tnsp.org>
parents: 20
diff changeset
543
1404dfcee7b8 More work on scenefile and model loading support.
Matti Hamalainen <ccr@tnsp.org>
parents: 20
diff changeset
544 for (DMModel &model : scene.models)
2
b45d8958e5a6 Cosmetic indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
545 {
21
1404dfcee7b8 More work on scenefile and model loading support.
Matti Hamalainen <ccr@tnsp.org>
parents: 20
diff changeset
546 if (!model.loadFromPLY(basePath + model.modelFile))
6
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
547 goto exit;
21
1404dfcee7b8 More work on scenefile and model loading support.
Matti Hamalainen <ccr@tnsp.org>
parents: 20
diff changeset
548
1404dfcee7b8 More work on scenefile and model loading support.
Matti Hamalainen <ccr@tnsp.org>
parents: 20
diff changeset
549 if (optUseShaders)
1404dfcee7b8 More work on scenefile and model loading support.
Matti Hamalainen <ccr@tnsp.org>
parents: 20
diff changeset
550 {
1404dfcee7b8 More work on scenefile and model loading support.
Matti Hamalainen <ccr@tnsp.org>
parents: 20
diff changeset
551 std::string
1404dfcee7b8 More work on scenefile and model loading support.
Matti Hamalainen <ccr@tnsp.org>
parents: 20
diff changeset
552 fragFile = model.fragShaderFile.empty() ? "shader.frag" : basePath + model.fragShaderFile,
1404dfcee7b8 More work on scenefile and model loading support.
Matti Hamalainen <ccr@tnsp.org>
parents: 20
diff changeset
553 vertFile = model.vertShaderFile.empty() ? "shader.vert" : basePath + model.vertShaderFile;
1404dfcee7b8 More work on scenefile and model loading support.
Matti Hamalainen <ccr@tnsp.org>
parents: 20
diff changeset
554
1404dfcee7b8 More work on scenefile and model loading support.
Matti Hamalainen <ccr@tnsp.org>
parents: 20
diff changeset
555 if (!dmReadText(fragFile, model.fragShaderStr, SET_MAX_SHADER_SIZE) ||
1404dfcee7b8 More work on scenefile and model loading support.
Matti Hamalainen <ccr@tnsp.org>
parents: 20
diff changeset
556 !dmReadText(vertFile, model.vertShaderStr, SET_MAX_SHADER_SIZE))
1404dfcee7b8 More work on scenefile and model loading support.
Matti Hamalainen <ccr@tnsp.org>
parents: 20
diff changeset
557 goto exit;
1404dfcee7b8 More work on scenefile and model loading support.
Matti Hamalainen <ccr@tnsp.org>
parents: 20
diff changeset
558 }
6
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
559 }
0
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
560
6
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
561 // Initialize SDL + OpenGL
21
1404dfcee7b8 More work on scenefile and model loading support.
Matti Hamalainen <ccr@tnsp.org>
parents: 20
diff changeset
562 if (!dmInitSDLGL(optWidth, optHeight, "GLDragon"))
6
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
563 goto exit;
3
be31ff9e5f58 Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents: 2
diff changeset
564
6
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
565 // According to our mode ..
8
628c42e94149 Clean up variable names.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
566 if (optUseShaders)
6
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
567 {
21
1404dfcee7b8 More work on scenefile and model loading support.
Matti Hamalainen <ccr@tnsp.org>
parents: 20
diff changeset
568 for (DMModel &model : scene.models)
1404dfcee7b8 More work on scenefile and model loading support.
Matti Hamalainen <ccr@tnsp.org>
parents: 20
diff changeset
569 {
24
c1897cfc8463 Add shader compilation error handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 23
diff changeset
570 if (!dmCompileShader(GL_FRAGMENT_SHADER, model.fragShaderStr, model.id_fs) ||
c1897cfc8463 Add shader compilation error handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 23
diff changeset
571 !dmCompileShader(GL_VERTEX_SHADER, model.vertShaderStr, model.id_vs))
c1897cfc8463 Add shader compilation error handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 23
diff changeset
572 goto exit;
c1897cfc8463 Add shader compilation error handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 23
diff changeset
573
21
1404dfcee7b8 More work on scenefile and model loading support.
Matti Hamalainen <ccr@tnsp.org>
parents: 20
diff changeset
574 dmLinkModelShaders(model);
1404dfcee7b8 More work on scenefile and model loading support.
Matti Hamalainen <ccr@tnsp.org>
parents: 20
diff changeset
575 }
6
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
576 }
25
2403030a0352 "Finish" implementing multiple lights support in scene files.
Matti Hamalainen <ccr@tnsp.org>
parents: 24
diff changeset
577
27
097184bd34a8 Implement number of lights uniform for the shaders, clean up light setup and
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
578 // Setup lights
097184bd34a8 Implement number of lights uniform for the shaders, clean up light setup and
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
579 for (size_t n = 0; n < scene.lights.size(); n++)
097184bd34a8 Implement number of lights uniform for the shaders, clean up light setup and
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
580 dmSetupLight(n, scene.lights[n]);
6
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
581
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
582 // Define the camera
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
583 gluLookAt(0, 0.12, 0.24, 0, 0.12, 0, 0, 1, 0);
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
584
3
be31ff9e5f58 Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents: 2
diff changeset
585
6
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
586 // Main loop starts
13
c1e8057cc4d0 Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents: 12
diff changeset
587 startTime = cycleStart = SDL_GetTicks();
6
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
588
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
589 while (!exitFlag)
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
590 {
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
591 SDL_Event event;
3
be31ff9e5f58 Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents: 2
diff changeset
592
6
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
593 // Check for quit events
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
594 while (SDL_PollEvent(&event))
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
595 switch (event.type)
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
596 {
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
597 case SDL_QUIT:
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
598 exitFlag = true;
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
599 break;
3
be31ff9e5f58 Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents: 2
diff changeset
600
6
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
601 case SDL_KEYDOWN:
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
602 switch (event.key.keysym.sym)
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
603 {
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
604 case SDLK_ESCAPE:
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
605 case SDLK_q:
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
606 exitFlag = true;
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
607 break;
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
608 }
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
609 }
3
be31ff9e5f58 Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents: 2
diff changeset
610
6
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
611 // Render the next frame
21
1404dfcee7b8 More work on scenefile and model loading support.
Matti Hamalainen <ccr@tnsp.org>
parents: 20
diff changeset
612 dmDrawScene(scene);
6
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
613
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
614 // Draw the current frame
8
628c42e94149 Clean up variable names.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
615 SDL_GL_SwapWindow(dmWindow);
3
be31ff9e5f58 Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents: 2
diff changeset
616
6
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
617 // Rotate for 2 degrees
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
618 glRotatef(2.0f, 0, 1, 0);
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
619
55
bf73a2a70ec7 Add OpenGL error checking.
Matti Hamalainen <ccr@tnsp.org>
parents: 54
diff changeset
620 // Check for errors
bf73a2a70ec7 Add OpenGL error checking.
Matti Hamalainen <ccr@tnsp.org>
parents: 54
diff changeset
621 dmGLCheckErrors();
bf73a2a70ec7 Add OpenGL error checking.
Matti Hamalainen <ccr@tnsp.org>
parents: 54
diff changeset
622
6
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
623 // Return true if a full rotation was done
15
2d2aadfa3df3 Fix calculation of totalFrames.
Matti Hamalainen <ccr@tnsp.org>
parents: 14
diff changeset
624 totalFrames++;
13
c1e8057cc4d0 Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents: 12
diff changeset
625 if (cycleFrames++ == SET_FRAMES)
6
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
626 {
13
c1e8057cc4d0 Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents: 12
diff changeset
627 // Reset cycleFrames
c1e8057cc4d0 Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents: 12
diff changeset
628 cycleFrames = 0;
3
be31ff9e5f58 Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents: 2
diff changeset
629
6
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
630 // Get the time it took to render a full turn
13
c1e8057cc4d0 Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents: 12
diff changeset
631 int cycleEnd = SDL_GetTicks();
c1e8057cc4d0 Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents: 12
diff changeset
632 double cycleTime = cycleEnd - cycleStart;
10
c6c419861101 Use SDL_GetTicks() instead of std::clock().
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
633
c6c419861101 Use SDL_GetTicks() instead of std::clock().
Matti Hamalainen <ccr@tnsp.org>
parents: 8
diff changeset
634 // Restart the timer
13
c1e8057cc4d0 Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents: 12
diff changeset
635 cycleStart = SDL_GetTicks();
3
be31ff9e5f58 Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents: 2
diff changeset
636
6
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
637 // Print the current frames per second
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
638 printf("%.1lf ms for %d frames = %.1lf FPS\n",
13
c1e8057cc4d0 Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents: 12
diff changeset
639 cycleTime, SET_FRAMES, (SET_FRAMES * 1000.0f) / cycleTime);
3
be31ff9e5f58 Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents: 2
diff changeset
640 }
2
b45d8958e5a6 Cosmetic indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
641 }
6
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
642
13
c1e8057cc4d0 Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents: 12
diff changeset
643 // Show totals
c1e8057cc4d0 Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents: 12
diff changeset
644 totalTime = SDL_GetTicks() - startTime;
c1e8057cc4d0 Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents: 12
diff changeset
645 printf("%.1lf ms total for %d total frames = %.2lf FPS average\n",
c1e8057cc4d0 Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents: 12
diff changeset
646 totalTime, totalFrames, (totalFrames * 1000.0f) / totalTime);
c1e8057cc4d0 Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents: 12
diff changeset
647
6
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
648 exit:
8
628c42e94149 Clean up variable names.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
649 if (dmGLContext != NULL)
628c42e94149 Clean up variable names.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
650 SDL_GL_DeleteContext(dmGLContext);
5
5dcae4dddcd9 Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 3
diff changeset
651
8
628c42e94149 Clean up variable names.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
652 if (dmWindow != NULL)
628c42e94149 Clean up variable names.
Matti Hamalainen <ccr@tnsp.org>
parents: 7
diff changeset
653 SDL_DestroyWindow(dmWindow);
6
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
654
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
655 SDL_Quit();
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
656
4d6fec8f0c64 Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
657 return 0;
0
3d74a9dd96e4 Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff changeset
658 }