Mercurial > hg > forks > gldragon
annotate gldragon.cpp @ 27:097184bd34a8
Implement number of lights uniform for the shaders, clean up light setup and
scene rendering setup.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Fri, 22 Nov 2019 08:39:27 +0200 |
parents | 67647ed860f0 |
children | 6847715b46cd |
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> |
6
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
13 #include <GL/glext.h> |
0
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
14 |
21
1404dfcee7b8
More work on scenefile and model loading support.
Matti Hamalainen <ccr@tnsp.org>
parents:
20
diff
changeset
|
15 #include "dmutil.h" |
1404dfcee7b8
More work on scenefile and model loading support.
Matti Hamalainen <ccr@tnsp.org>
parents:
20
diff
changeset
|
16 #include "dmmodel.h" |
0
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
17 |
16 | 18 |
11
89dc8caeff41
Improve option handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
19 /* Default settings etc. constants |
89dc8caeff41
Improve option handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
20 */ |
18
b1e75c65016d
Check for "too large" shader files.
Matti Hamalainen <ccr@tnsp.org>
parents:
17
diff
changeset
|
21 #define SET_DEF_WIDTH 1280 |
b1e75c65016d
Check for "too large" shader files.
Matti Hamalainen <ccr@tnsp.org>
parents:
17
diff
changeset
|
22 #define SET_DEF_HEIGHT 960 |
b1e75c65016d
Check for "too large" shader files.
Matti Hamalainen <ccr@tnsp.org>
parents:
17
diff
changeset
|
23 #define SET_FRAMES (180) |
b1e75c65016d
Check for "too large" shader files.
Matti Hamalainen <ccr@tnsp.org>
parents:
17
diff
changeset
|
24 #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
|
25 |
be31ff9e5f58
Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
26 |
11
89dc8caeff41
Improve option handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
27 /* Options |
89dc8caeff41
Improve option handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
28 */ |
89dc8caeff41
Improve option handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
29 bool optUseShaders = false; |
89dc8caeff41
Improve option handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
30 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
|
31 optHeight = SET_DEF_HEIGHT, |
c1e8057cc4d0
Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
32 optVSyncMode = 1; |
c1e8057cc4d0
Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
33 |
11
89dc8caeff41
Improve option handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
34 |
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 /* Globals |
89dc8caeff41
Improve option handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
37 */ |
89dc8caeff41
Improve option handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
38 SDL_Window *dmWindow = NULL; |
89dc8caeff41
Improve option handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
39 SDL_GLContext dmGLContext = NULL; |
89dc8caeff41
Improve option handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
40 |
89dc8caeff41
Improve option handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
41 |
21
1404dfcee7b8
More work on scenefile and model loading support.
Matti Hamalainen <ccr@tnsp.org>
parents:
20
diff
changeset
|
42 /* Helpers |
1404dfcee7b8
More work on scenefile and model loading support.
Matti Hamalainen <ccr@tnsp.org>
parents:
20
diff
changeset
|
43 */ |
5 | 44 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
|
45 { |
13
c1e8057cc4d0
Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
46 int ret; |
c1e8057cc4d0
Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
47 std::string msg; |
c1e8057cc4d0
Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
48 |
6
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
49 // Set GL attributes |
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
50 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2); |
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
51 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1); |
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
52 SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE); |
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
53 |
2 | 54 SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24); |
55 SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); | |
0
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
56 |
6
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
57 // 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
|
58 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
|
59 { |
be31ff9e5f58
Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
60 printf("ERROR: Unable to initialize SDL: %s\n", |
be31ff9e5f58
Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
61 SDL_GetError()); |
be31ff9e5f58
Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
62 return false; |
be31ff9e5f58
Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
63 } |
0
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
64 |
6
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
65 // Attempt to create a window |
8 | 66 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
|
67 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
|
68 width, height, |
be31ff9e5f58
Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
69 SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE)) == NULL) |
2 | 70 { |
3
be31ff9e5f58
Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
71 printf("ERROR: Could not create SDL window: %s", |
be31ff9e5f58
Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
72 SDL_GetError()); |
be31ff9e5f58
Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
73 return false; |
2 | 74 } |
0
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
75 |
8 | 76 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
|
77 { |
be31ff9e5f58
Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
78 printf("ERROR: Unable to create SDL OpenGL context: %s\n", |
be31ff9e5f58
Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
79 SDL_GetError()); |
be31ff9e5f58
Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
80 return false; |
be31ff9e5f58
Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
81 } |
0
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
82 |
13
c1e8057cc4d0
Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
83 // 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
|
84 switch (optVSyncMode) |
c1e8057cc4d0
Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
85 { |
c1e8057cc4d0
Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
86 case 3: |
c1e8057cc4d0
Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
87 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
|
88 msg = "adaptive vsync"; |
c1e8057cc4d0
Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
89 break; |
c1e8057cc4d0
Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
90 |
c1e8057cc4d0
Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
91 case 2: |
c1e8057cc4d0
Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
92 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
|
93 msg = "synchronized (vsync)"; |
c1e8057cc4d0
Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
94 break; |
c1e8057cc4d0
Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
95 |
c1e8057cc4d0
Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
96 case 1: |
c1e8057cc4d0
Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
97 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
|
98 msg = "immediate (no vsync)"; |
c1e8057cc4d0
Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
99 break; |
c1e8057cc4d0
Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
100 |
c1e8057cc4d0
Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
101 default: |
c1e8057cc4d0
Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
102 ret = -1; |
c1e8057cc4d0
Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
103 msg = "INVALID VSYNC MODE"; |
c1e8057cc4d0
Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
104 break; |
c1e8057cc4d0
Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
105 } |
c1e8057cc4d0
Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
106 |
c1e8057cc4d0
Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
107 if (ret != 0) |
c1e8057cc4d0
Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
108 { |
c1e8057cc4d0
Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
109 printf("ERROR: Could not set vsync mode to %s.\n", |
c1e8057cc4d0
Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
110 msg.c_str()); |
c1e8057cc4d0
Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
111 return false; |
c1e8057cc4d0
Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
112 } |
c1e8057cc4d0
Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
113 |
6
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
114 // Dump some information |
3
be31ff9e5f58
Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
115 printf( |
be31ff9e5f58
Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
116 "GL_VENDOR : %s\n" |
be31ff9e5f58
Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
117 "GL_RENDERER : %s\n" |
13
c1e8057cc4d0
Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
118 "GL_VERSION : %s\n" |
c1e8057cc4d0
Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
119 "VSync mode : %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
|
120 glGetString(GL_VENDOR), |
be31ff9e5f58
Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
121 glGetString(GL_RENDERER), |
13
c1e8057cc4d0
Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
122 glGetString(GL_VERSION), |
c1e8057cc4d0
Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
123 msg.c_str()); |
0
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
124 |
2 | 125 // 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
|
126 glViewport(0, 0, width, height); |
0
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
127 |
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
128 glMatrixMode(GL_PROJECTION); |
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
129 glLoadIdentity(); |
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
130 |
3
be31ff9e5f58
Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
131 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
|
132 |
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
133 glMatrixMode(GL_MODELVIEW); |
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
134 glLoadIdentity(); |
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
135 |
2 | 136 // Enable back face culling |
137 glEnable(GL_CULL_FACE); | |
0
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
138 |
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
139 // Enable smooth shading |
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
140 glShadeModel(GL_SMOOTH); |
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
141 |
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
142 // Enable the depth buffer |
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
143 glEnable(GL_DEPTH_TEST); |
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
144 |
21
1404dfcee7b8
More work on scenefile and model loading support.
Matti Hamalainen <ccr@tnsp.org>
parents:
20
diff
changeset
|
145 // Enable normal rescaling |
1404dfcee7b8
More work on scenefile and model loading support.
Matti Hamalainen <ccr@tnsp.org>
parents:
20
diff
changeset
|
146 glEnable(GL_RESCALE_NORMAL); |
1404dfcee7b8
More work on scenefile and model loading support.
Matti Hamalainen <ccr@tnsp.org>
parents:
20
diff
changeset
|
147 |
0
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
148 // Setup depth buffer |
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
149 glClearDepth(1.0f); |
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
150 |
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
151 // Set the depth buffer function |
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
152 glDepthFunc(GL_LEQUAL); |
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
153 |
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
154 // Enable vertex and and normal arrays |
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
155 glEnableClientState(GL_VERTEX_ARRAY); |
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
156 glEnableClientState(GL_NORMAL_ARRAY); |
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
157 |
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
158 // Set correct perspective correction |
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
159 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
|
160 |
be31ff9e5f58
Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
161 return true; |
0
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
162 } |
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
163 |
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
164 |
21
1404dfcee7b8
More work on scenefile and model loading support.
Matti Hamalainen <ccr@tnsp.org>
parents:
20
diff
changeset
|
165 void dmDrawModel(const DMModel &model) |
0
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
166 { |
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
167 int maxIndices; |
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
168 |
17 | 169 // Render the model |
2 | 170 glGetIntegerv(GL_MAX_ELEMENTS_INDICES, &maxIndices); |
0
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
171 |
21
1404dfcee7b8
More work on scenefile and model loading support.
Matti Hamalainen <ccr@tnsp.org>
parents:
20
diff
changeset
|
172 // Add transforms |
1404dfcee7b8
More work on scenefile and model loading support.
Matti Hamalainen <ccr@tnsp.org>
parents:
20
diff
changeset
|
173 glScalef(model.scale.x, model.scale.y, model.scale.z); |
25
2403030a0352
"Finish" implementing multiple lights support in scene files.
Matti Hamalainen <ccr@tnsp.org>
parents:
24
diff
changeset
|
174 glTranslatef(model.translate.x, model.translate.y, model.translate.z); |
21
1404dfcee7b8
More work on scenefile and model loading support.
Matti Hamalainen <ccr@tnsp.org>
parents:
20
diff
changeset
|
175 glRotatef(model.rotate.x, 1.0f, 0.0f, 0.0f); |
1404dfcee7b8
More work on scenefile and model loading support.
Matti Hamalainen <ccr@tnsp.org>
parents:
20
diff
changeset
|
176 glRotatef(model.rotate.y, 0.0f, 1.0f, 0.0f); |
1404dfcee7b8
More work on scenefile and model loading support.
Matti Hamalainen <ccr@tnsp.org>
parents:
20
diff
changeset
|
177 glRotatef(model.rotate.z, 0.0f, 0.0f, 1.0f); |
0
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
178 |
21
1404dfcee7b8
More work on scenefile and model loading support.
Matti Hamalainen <ccr@tnsp.org>
parents:
20
diff
changeset
|
179 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
|
180 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
|
181 |
1404dfcee7b8
More work on scenefile and model loading support.
Matti Hamalainen <ccr@tnsp.org>
parents:
20
diff
changeset
|
182 for (int n = 0; n < model.nfaces; n += maxIndices) |
2 | 183 { |
21
1404dfcee7b8
More work on scenefile and model loading support.
Matti Hamalainen <ccr@tnsp.org>
parents:
20
diff
changeset
|
184 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
|
185 glDrawElements(GL_TRIANGLES, count * 3, GL_UNSIGNED_INT, &model.faces[n * 3]); |
2 | 186 } |
0
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
187 } |
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
188 |
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
189 |
21
1404dfcee7b8
More work on scenefile and model loading support.
Matti Hamalainen <ccr@tnsp.org>
parents:
20
diff
changeset
|
190 void dmDrawScene(const DMSimpleScene &scene) |
0
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
191 { |
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
192 glClear(GL_DEPTH_BUFFER_BIT); |
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
193 |
2 | 194 glMatrixMode(GL_PROJECTION); |
195 glPushMatrix(); | |
196 glLoadIdentity(); | |
0
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
197 |
2 | 198 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
|
199 |
2 | 200 glMatrixMode(GL_MODELVIEW); |
201 glPushMatrix(); | |
202 glLoadIdentity(); | |
0
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
203 |
17 | 204 // Draw the background gradient |
0
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
205 glDisable(GL_DEPTH_TEST); |
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
206 glDisable(GL_LIGHTING); |
2 | 207 glBegin(GL_QUADS); |
208 { | |
209 glColor3ub(0x3B, 0x3B, 0x75); | |
210 glVertex2f(0.0f, 0.0f); | |
211 glVertex2f(1.0f, 0.0f); | |
0
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
212 |
2 | 213 glColor3ub(0x00, 0x00, 0x00); |
214 glVertex2f(1.0f, 1.0f); | |
215 glVertex2f(0.0f, 1.0f); | |
216 } | |
217 glEnd(); | |
0
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
218 |
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
219 |
2 | 220 // Restore the 3D projection |
221 glMatrixMode(GL_PROJECTION); | |
222 glPopMatrix(); | |
0
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
223 |
2 | 224 glMatrixMode(GL_MODELVIEW); |
225 glPopMatrix(); | |
0
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
226 |
3
be31ff9e5f58
Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
227 glEnable(GL_DEPTH_TEST); |
0
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
228 |
21
1404dfcee7b8
More work on scenefile and model loading support.
Matti Hamalainen <ccr@tnsp.org>
parents:
20
diff
changeset
|
229 // Draw models |
1404dfcee7b8
More work on scenefile and model loading support.
Matti Hamalainen <ccr@tnsp.org>
parents:
20
diff
changeset
|
230 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
|
231 { |
097184bd34a8
Implement number of lights uniform for the shaders, clean up light setup and
Matti Hamalainen <ccr@tnsp.org>
parents:
26
diff
changeset
|
232 if (optUseShaders) |
097184bd34a8
Implement number of lights uniform for the shaders, clean up light setup and
Matti Hamalainen <ccr@tnsp.org>
parents:
26
diff
changeset
|
233 { |
097184bd34a8
Implement number of lights uniform for the shaders, clean up light setup and
Matti Hamalainen <ccr@tnsp.org>
parents:
26
diff
changeset
|
234 // Enable shader program |
097184bd34a8
Implement number of lights uniform for the shaders, clean up light setup and
Matti Hamalainen <ccr@tnsp.org>
parents:
26
diff
changeset
|
235 glUseProgram(model.id_prog); |
097184bd34a8
Implement number of lights uniform for the shaders, clean up light setup and
Matti Hamalainen <ccr@tnsp.org>
parents:
26
diff
changeset
|
236 glUniform1i(glGetUniformLocation(model.id_prog, "nlights"), scene.lights.size()); |
097184bd34a8
Implement number of lights uniform for the shaders, clean up light setup and
Matti Hamalainen <ccr@tnsp.org>
parents:
26
diff
changeset
|
237 } |
097184bd34a8
Implement number of lights uniform for the shaders, clean up light setup and
Matti Hamalainen <ccr@tnsp.org>
parents:
26
diff
changeset
|
238 else |
097184bd34a8
Implement number of lights uniform for the shaders, clean up light setup and
Matti Hamalainen <ccr@tnsp.org>
parents:
26
diff
changeset
|
239 { |
097184bd34a8
Implement number of lights uniform for the shaders, clean up light setup and
Matti Hamalainen <ccr@tnsp.org>
parents:
26
diff
changeset
|
240 // Set the color of the model |
097184bd34a8
Implement number of lights uniform for the shaders, clean up light setup and
Matti Hamalainen <ccr@tnsp.org>
parents:
26
diff
changeset
|
241 glEnable(GL_LIGHTING); |
097184bd34a8
Implement number of lights uniform for the shaders, clean up light setup and
Matti Hamalainen <ccr@tnsp.org>
parents:
26
diff
changeset
|
242 glColor3ub(0x90, 0x80, 0x90); |
097184bd34a8
Implement number of lights uniform for the shaders, clean up light setup and
Matti Hamalainen <ccr@tnsp.org>
parents:
26
diff
changeset
|
243 } |
097184bd34a8
Implement number of lights uniform for the shaders, clean up light setup and
Matti Hamalainen <ccr@tnsp.org>
parents:
26
diff
changeset
|
244 |
097184bd34a8
Implement number of lights uniform for the shaders, clean up light setup and
Matti Hamalainen <ccr@tnsp.org>
parents:
26
diff
changeset
|
245 glPushMatrix(); |
21
1404dfcee7b8
More work on scenefile and model loading support.
Matti Hamalainen <ccr@tnsp.org>
parents:
20
diff
changeset
|
246 dmDrawModel(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
|
247 glPopMatrix(); |
097184bd34a8
Implement number of lights uniform for the shaders, clean up light setup and
Matti Hamalainen <ccr@tnsp.org>
parents:
26
diff
changeset
|
248 |
097184bd34a8
Implement number of lights uniform for the shaders, clean up light setup and
Matti Hamalainen <ccr@tnsp.org>
parents:
26
diff
changeset
|
249 // Restore |
097184bd34a8
Implement number of lights uniform for the shaders, clean up light setup and
Matti Hamalainen <ccr@tnsp.org>
parents:
26
diff
changeset
|
250 if (optUseShaders) |
097184bd34a8
Implement number of lights uniform for the shaders, clean up light setup and
Matti Hamalainen <ccr@tnsp.org>
parents:
26
diff
changeset
|
251 { |
097184bd34a8
Implement number of lights uniform for the shaders, clean up light setup and
Matti Hamalainen <ccr@tnsp.org>
parents:
26
diff
changeset
|
252 glUseProgram(0); |
097184bd34a8
Implement number of lights uniform for the shaders, clean up light setup and
Matti Hamalainen <ccr@tnsp.org>
parents:
26
diff
changeset
|
253 } |
097184bd34a8
Implement number of lights uniform for the shaders, clean up light setup and
Matti Hamalainen <ccr@tnsp.org>
parents:
26
diff
changeset
|
254 } |
6
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
255 } |
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
256 |
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
257 |
24
c1897cfc8463
Add shader compilation error handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
23
diff
changeset
|
258 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
|
259 { |
24
c1897cfc8463
Add shader compilation error handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
23
diff
changeset
|
260 GLint status; |
6
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
261 const char *tmp = src.c_str(); |
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
262 |
24
c1897cfc8463
Add shader compilation error handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
23
diff
changeset
|
263 shader = glCreateShader(stype); |
6
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
264 glShaderSource(shader, 1, &tmp, 0); |
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
265 glCompileShader(shader); |
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
266 |
24
c1897cfc8463
Add shader compilation error handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
23
diff
changeset
|
267 glGetShaderiv(shader, GL_COMPILE_STATUS, &status); |
c1897cfc8463
Add shader compilation error handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
23
diff
changeset
|
268 if (status == GL_TRUE) |
c1897cfc8463
Add shader compilation error handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
23
diff
changeset
|
269 return true; |
c1897cfc8463
Add shader compilation error handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
23
diff
changeset
|
270 else |
c1897cfc8463
Add shader compilation error handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
23
diff
changeset
|
271 { |
26
67647ed860f0
Improve shader compilation error reporting.
Matti Hamalainen <ccr@tnsp.org>
parents:
25
diff
changeset
|
272 GLint bufLen = 0; |
67647ed860f0
Improve shader compilation error reporting.
Matti Hamalainen <ccr@tnsp.org>
parents:
25
diff
changeset
|
273 glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &bufLen); |
67647ed860f0
Improve shader compilation error reporting.
Matti Hamalainen <ccr@tnsp.org>
parents:
25
diff
changeset
|
274 |
67647ed860f0
Improve shader compilation error reporting.
Matti Hamalainen <ccr@tnsp.org>
parents:
25
diff
changeset
|
275 if (bufLen > 0) |
67647ed860f0
Improve shader compilation error reporting.
Matti Hamalainen <ccr@tnsp.org>
parents:
25
diff
changeset
|
276 { |
67647ed860f0
Improve shader compilation error reporting.
Matti Hamalainen <ccr@tnsp.org>
parents:
25
diff
changeset
|
277 char *buf = new char[bufLen]; |
67647ed860f0
Improve shader compilation error reporting.
Matti Hamalainen <ccr@tnsp.org>
parents:
25
diff
changeset
|
278 glGetShaderInfoLog(shader, bufLen, NULL, buf); |
67647ed860f0
Improve shader compilation error reporting.
Matti Hamalainen <ccr@tnsp.org>
parents:
25
diff
changeset
|
279 printf("ERROR: Shader compliation error:\n%s\n", |
67647ed860f0
Improve shader compilation error reporting.
Matti Hamalainen <ccr@tnsp.org>
parents:
25
diff
changeset
|
280 buf); |
67647ed860f0
Improve shader compilation error reporting.
Matti Hamalainen <ccr@tnsp.org>
parents:
25
diff
changeset
|
281 delete buf; |
67647ed860f0
Improve shader compilation error reporting.
Matti Hamalainen <ccr@tnsp.org>
parents:
25
diff
changeset
|
282 } |
67647ed860f0
Improve shader compilation error reporting.
Matti Hamalainen <ccr@tnsp.org>
parents:
25
diff
changeset
|
283 else |
67647ed860f0
Improve shader compilation error reporting.
Matti Hamalainen <ccr@tnsp.org>
parents:
25
diff
changeset
|
284 { |
67647ed860f0
Improve shader compilation error reporting.
Matti Hamalainen <ccr@tnsp.org>
parents:
25
diff
changeset
|
285 printf("ERROR: Shader compilation error occured, but no error information got.\n"); |
67647ed860f0
Improve shader compilation error reporting.
Matti Hamalainen <ccr@tnsp.org>
parents:
25
diff
changeset
|
286 } |
24
c1897cfc8463
Add shader compilation error handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
23
diff
changeset
|
287 return false; |
c1897cfc8463
Add shader compilation error handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
23
diff
changeset
|
288 } |
6
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
289 } |
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
290 |
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
291 |
21
1404dfcee7b8
More work on scenefile and model loading support.
Matti Hamalainen <ccr@tnsp.org>
parents:
20
diff
changeset
|
292 void dmLinkModelShaders(DMModel &model) |
6
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
293 { |
21
1404dfcee7b8
More work on scenefile and model loading support.
Matti Hamalainen <ccr@tnsp.org>
parents:
20
diff
changeset
|
294 model.id_prog = glCreateProgram(); |
1404dfcee7b8
More work on scenefile and model loading support.
Matti Hamalainen <ccr@tnsp.org>
parents:
20
diff
changeset
|
295 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
|
296 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
|
297 glLinkProgram(model.id_prog); |
0
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
298 } |
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
299 |
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
300 |
25
2403030a0352
"Finish" implementing multiple lights support in scene files.
Matti Hamalainen <ccr@tnsp.org>
parents:
24
diff
changeset
|
301 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
|
302 { |
2403030a0352
"Finish" implementing multiple lights support in scene files.
Matti Hamalainen <ccr@tnsp.org>
parents:
24
diff
changeset
|
303 glEnable(GL_LIGHT0 + n); |
2403030a0352
"Finish" implementing multiple lights support in scene files.
Matti Hamalainen <ccr@tnsp.org>
parents:
24
diff
changeset
|
304 glLightfv(GL_LIGHT0 + n, GL_AMBIENT, light.ambient.values); |
2403030a0352
"Finish" implementing multiple lights support in scene files.
Matti Hamalainen <ccr@tnsp.org>
parents:
24
diff
changeset
|
305 glLightfv(GL_LIGHT0 + n, GL_DIFFUSE, light.diffuse.values); |
2403030a0352
"Finish" implementing multiple lights support in scene files.
Matti Hamalainen <ccr@tnsp.org>
parents:
24
diff
changeset
|
306 glLightfv(GL_LIGHT0 + n, GL_SPECULAR, light.specular.values); |
2403030a0352
"Finish" implementing multiple lights support in scene files.
Matti Hamalainen <ccr@tnsp.org>
parents:
24
diff
changeset
|
307 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
|
308 } |
2403030a0352
"Finish" implementing multiple lights support in scene files.
Matti Hamalainen <ccr@tnsp.org>
parents:
24
diff
changeset
|
309 |
2403030a0352
"Finish" implementing multiple lights support in scene files.
Matti Hamalainen <ccr@tnsp.org>
parents:
24
diff
changeset
|
310 |
6
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
311 int main(int argc, char *argv[]) |
0
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
312 { |
13
c1e8057cc4d0
Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
313 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
|
314 double totalTime; |
21
1404dfcee7b8
More work on scenefile and model loading support.
Matti Hamalainen <ccr@tnsp.org>
parents:
20
diff
changeset
|
315 bool |
1404dfcee7b8
More work on scenefile and model loading support.
Matti Hamalainen <ccr@tnsp.org>
parents:
20
diff
changeset
|
316 exitFlag = false, |
1404dfcee7b8
More work on scenefile and model loading support.
Matti Hamalainen <ccr@tnsp.org>
parents:
20
diff
changeset
|
317 optShowHelp = false, |
1404dfcee7b8
More work on scenefile and model loading support.
Matti Hamalainen <ccr@tnsp.org>
parents:
20
diff
changeset
|
318 optSetInputFilename = false; |
1404dfcee7b8
More work on scenefile and model loading support.
Matti Hamalainen <ccr@tnsp.org>
parents:
20
diff
changeset
|
319 std::string optInputFilename = "dragon.scene", basePath; |
1404dfcee7b8
More work on scenefile and model loading support.
Matti Hamalainen <ccr@tnsp.org>
parents:
20
diff
changeset
|
320 DMSimpleScene scene; |
6
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
321 |
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
322 // Check commandline argument for enabling shaders |
7 | 323 for (int narg = 1; narg < argc; narg++) |
324 { | |
325 char *arg = argv[narg]; | |
11
89dc8caeff41
Improve option handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
326 if (arg[0] == '-') |
89dc8caeff41
Improve option handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
327 { |
89dc8caeff41
Improve option handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
328 char *opt = arg + 1; |
89dc8caeff41
Improve option handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
329 |
89dc8caeff41
Improve option handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
330 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
|
331 opt[0] == '?' || (opt[0] == '-' && opt[1] == '?')) |
11
89dc8caeff41
Improve option handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
332 { |
89dc8caeff41
Improve option handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
333 optShowHelp = true; |
89dc8caeff41
Improve option handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
334 break; |
89dc8caeff41
Improve option handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
335 } |
89dc8caeff41
Improve option handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
336 else |
89dc8caeff41
Improve option handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
337 if (opt[0] == '-') |
89dc8caeff41
Improve option handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
338 opt++; |
89dc8caeff41
Improve option handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
339 |
89dc8caeff41
Improve option handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
340 if (opt[0] == 'g') |
89dc8caeff41
Improve option handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
341 optUseShaders = true; |
89dc8caeff41
Improve option handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
342 else |
13
c1e8057cc4d0
Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
343 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
|
344 { |
13
c1e8057cc4d0
Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
345 case 'w': |
c1e8057cc4d0
Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
346 case 'h': |
c1e8057cc4d0
Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
347 case 'm': |
c1e8057cc4d0
Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
348 case 'v': |
c1e8057cc4d0
Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
349 if (opt[1] == 0) |
c1e8057cc4d0
Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
350 { |
c1e8057cc4d0
Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
351 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
|
352 goto exit; |
c1e8057cc4d0
Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
353 } |
c1e8057cc4d0
Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
354 |
c1e8057cc4d0
Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
355 switch (opt[0]) |
c1e8057cc4d0
Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
356 { |
c1e8057cc4d0
Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
357 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
|
358 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
|
359 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
|
360 } |
c1e8057cc4d0
Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
361 break; |
c1e8057cc4d0
Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
362 |
c1e8057cc4d0
Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
363 default: |
c1e8057cc4d0
Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
364 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
|
365 goto exit; |
52a586c344f4
Add option -m<filename prefix> to specify mesh/shader filename prefix.
Matti Hamalainen <ccr@tnsp.org>
parents:
11
diff
changeset
|
366 } |
11
89dc8caeff41
Improve option handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
367 } |
21
1404dfcee7b8
More work on scenefile and model loading support.
Matti Hamalainen <ccr@tnsp.org>
parents:
20
diff
changeset
|
368 else |
1404dfcee7b8
More work on scenefile and model loading support.
Matti Hamalainen <ccr@tnsp.org>
parents:
20
diff
changeset
|
369 { |
1404dfcee7b8
More work on scenefile and model loading support.
Matti Hamalainen <ccr@tnsp.org>
parents:
20
diff
changeset
|
370 if (optSetInputFilename) |
1404dfcee7b8
More work on scenefile and model loading support.
Matti Hamalainen <ccr@tnsp.org>
parents:
20
diff
changeset
|
371 { |
1404dfcee7b8
More work on scenefile and model loading support.
Matti Hamalainen <ccr@tnsp.org>
parents:
20
diff
changeset
|
372 printf("ERROR: Please specify only one scene file.\n"); |
1404dfcee7b8
More work on scenefile and model loading support.
Matti Hamalainen <ccr@tnsp.org>
parents:
20
diff
changeset
|
373 goto exit; |
1404dfcee7b8
More work on scenefile and model loading support.
Matti Hamalainen <ccr@tnsp.org>
parents:
20
diff
changeset
|
374 } |
1404dfcee7b8
More work on scenefile and model loading support.
Matti Hamalainen <ccr@tnsp.org>
parents:
20
diff
changeset
|
375 |
1404dfcee7b8
More work on scenefile and model loading support.
Matti Hamalainen <ccr@tnsp.org>
parents:
20
diff
changeset
|
376 optSetInputFilename = true; |
1404dfcee7b8
More work on scenefile and model loading support.
Matti Hamalainen <ccr@tnsp.org>
parents:
20
diff
changeset
|
377 optInputFilename = std::string(arg); |
1404dfcee7b8
More work on scenefile and model loading support.
Matti Hamalainen <ccr@tnsp.org>
parents:
20
diff
changeset
|
378 if (optInputFilename.empty()) |
1404dfcee7b8
More work on scenefile and model loading support.
Matti Hamalainen <ccr@tnsp.org>
parents:
20
diff
changeset
|
379 { |
1404dfcee7b8
More work on scenefile and model loading support.
Matti Hamalainen <ccr@tnsp.org>
parents:
20
diff
changeset
|
380 printf("ERROR: Invalid input filename.\n"); |
1404dfcee7b8
More work on scenefile and model loading support.
Matti Hamalainen <ccr@tnsp.org>
parents:
20
diff
changeset
|
381 goto exit; |
1404dfcee7b8
More work on scenefile and model loading support.
Matti Hamalainen <ccr@tnsp.org>
parents:
20
diff
changeset
|
382 } |
1404dfcee7b8
More work on scenefile and model loading support.
Matti Hamalainen <ccr@tnsp.org>
parents:
20
diff
changeset
|
383 |
1404dfcee7b8
More work on scenefile and model loading support.
Matti Hamalainen <ccr@tnsp.org>
parents:
20
diff
changeset
|
384 } |
7 | 385 } |
386 | |
8 | 387 if (optShowHelp) |
7 | 388 { |
389 printf( | |
21
1404dfcee7b8
More work on scenefile and model loading support.
Matti Hamalainen <ccr@tnsp.org>
parents:
20
diff
changeset
|
390 "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
|
391 "-? 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
|
392 "-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
|
393 "-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
|
394 "-h<height> Window height (default %d)\n" |
13
c1e8057cc4d0
Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
395 "-v<1-3> Set vsync mode: 1 = no vsync, 2 = vsync, 3 = adaptive\n" |
21
1404dfcee7b8
More work on scenefile and model loading support.
Matti Hamalainen <ccr@tnsp.org>
parents:
20
diff
changeset
|
396 " Default is no vsync. Using vsync will result in FPS being\n" |
1404dfcee7b8
More work on scenefile and model loading support.
Matti Hamalainen <ccr@tnsp.org>
parents:
20
diff
changeset
|
397 " approx whatever your monitor refresh rate is.\n" |
11
89dc8caeff41
Improve option handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
398 "\n", |
89dc8caeff41
Improve option handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
399 argv[0], |
89dc8caeff41
Improve option handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
400 SET_DEF_WIDTH, SET_DEF_HEIGHT |
89dc8caeff41
Improve option handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
401 ); |
89dc8caeff41
Improve option handling a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
402 |
7 | 403 goto exit; |
404 } | |
6
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
405 |
13
c1e8057cc4d0
Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
406 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
|
407 { |
c1e8057cc4d0
Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
408 printf("ERROR: Invalid window width or height (%d x %d).\n", |
c1e8057cc4d0
Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
409 optWidth, optHeight); |
c1e8057cc4d0
Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
410 goto exit; |
c1e8057cc4d0
Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
411 } |
c1e8057cc4d0
Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
412 |
21
1404dfcee7b8
More work on scenefile and model loading support.
Matti Hamalainen <ccr@tnsp.org>
parents:
20
diff
changeset
|
413 // Load the scene |
1404dfcee7b8
More work on scenefile and model loading support.
Matti Hamalainen <ccr@tnsp.org>
parents:
20
diff
changeset
|
414 if (!scene.loadInfo(optInputFilename)) |
1404dfcee7b8
More work on scenefile and model loading support.
Matti Hamalainen <ccr@tnsp.org>
parents:
20
diff
changeset
|
415 goto exit; |
1404dfcee7b8
More work on scenefile and model loading support.
Matti Hamalainen <ccr@tnsp.org>
parents:
20
diff
changeset
|
416 |
1404dfcee7b8
More work on scenefile and model loading support.
Matti Hamalainen <ccr@tnsp.org>
parents:
20
diff
changeset
|
417 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
|
418 { |
21
1404dfcee7b8
More work on scenefile and model loading support.
Matti Hamalainen <ccr@tnsp.org>
parents:
20
diff
changeset
|
419 printf("ERROR: Scenefile '%s' contains no models.\n", |
1404dfcee7b8
More work on scenefile and model loading support.
Matti Hamalainen <ccr@tnsp.org>
parents:
20
diff
changeset
|
420 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
|
421 goto exit; |
52a586c344f4
Add option -m<filename prefix> to specify mesh/shader filename prefix.
Matti Hamalainen <ccr@tnsp.org>
parents:
11
diff
changeset
|
422 } |
52a586c344f4
Add option -m<filename prefix> to specify mesh/shader filename prefix.
Matti Hamalainen <ccr@tnsp.org>
parents:
11
diff
changeset
|
423 |
21
1404dfcee7b8
More work on scenefile and model loading support.
Matti Hamalainen <ccr@tnsp.org>
parents:
20
diff
changeset
|
424 printf("INFO: Loading %ld model(s) ..\n", |
1404dfcee7b8
More work on scenefile and model loading support.
Matti Hamalainen <ccr@tnsp.org>
parents:
20
diff
changeset
|
425 scene.models.size()); |
6
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
426 |
21
1404dfcee7b8
More work on scenefile and model loading support.
Matti Hamalainen <ccr@tnsp.org>
parents:
20
diff
changeset
|
427 basePath = dmGetPath(optInputFilename); |
1404dfcee7b8
More work on scenefile and model loading support.
Matti Hamalainen <ccr@tnsp.org>
parents:
20
diff
changeset
|
428 printf("INFO: Model base path '%s'\n", basePath.c_str()); |
1404dfcee7b8
More work on scenefile and model loading support.
Matti Hamalainen <ccr@tnsp.org>
parents:
20
diff
changeset
|
429 |
1404dfcee7b8
More work on scenefile and model loading support.
Matti Hamalainen <ccr@tnsp.org>
parents:
20
diff
changeset
|
430 for (DMModel &model : scene.models) |
2 | 431 { |
21
1404dfcee7b8
More work on scenefile and model loading support.
Matti Hamalainen <ccr@tnsp.org>
parents:
20
diff
changeset
|
432 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
|
433 goto exit; |
21
1404dfcee7b8
More work on scenefile and model loading support.
Matti Hamalainen <ccr@tnsp.org>
parents:
20
diff
changeset
|
434 |
1404dfcee7b8
More work on scenefile and model loading support.
Matti Hamalainen <ccr@tnsp.org>
parents:
20
diff
changeset
|
435 if (optUseShaders) |
1404dfcee7b8
More work on scenefile and model loading support.
Matti Hamalainen <ccr@tnsp.org>
parents:
20
diff
changeset
|
436 { |
1404dfcee7b8
More work on scenefile and model loading support.
Matti Hamalainen <ccr@tnsp.org>
parents:
20
diff
changeset
|
437 std::string |
1404dfcee7b8
More work on scenefile and model loading support.
Matti Hamalainen <ccr@tnsp.org>
parents:
20
diff
changeset
|
438 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
|
439 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
|
440 |
1404dfcee7b8
More work on scenefile and model loading support.
Matti Hamalainen <ccr@tnsp.org>
parents:
20
diff
changeset
|
441 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
|
442 !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
|
443 goto exit; |
1404dfcee7b8
More work on scenefile and model loading support.
Matti Hamalainen <ccr@tnsp.org>
parents:
20
diff
changeset
|
444 } |
6
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
445 } |
0
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
446 |
27
097184bd34a8
Implement number of lights uniform for the shaders, clean up light setup and
Matti Hamalainen <ccr@tnsp.org>
parents:
26
diff
changeset
|
447 // Define a default light if none defined in scene file |
097184bd34a8
Implement number of lights uniform for the shaders, clean up light setup and
Matti Hamalainen <ccr@tnsp.org>
parents:
26
diff
changeset
|
448 if (scene.lights.size() == 0) |
097184bd34a8
Implement number of lights uniform for the shaders, clean up light setup and
Matti Hamalainen <ccr@tnsp.org>
parents:
26
diff
changeset
|
449 { |
097184bd34a8
Implement number of lights uniform for the shaders, clean up light setup and
Matti Hamalainen <ccr@tnsp.org>
parents:
26
diff
changeset
|
450 DMLight light; // Default light |
097184bd34a8
Implement number of lights uniform for the shaders, clean up light setup and
Matti Hamalainen <ccr@tnsp.org>
parents:
26
diff
changeset
|
451 scene.lights.push_back(light); |
097184bd34a8
Implement number of lights uniform for the shaders, clean up light setup and
Matti Hamalainen <ccr@tnsp.org>
parents:
26
diff
changeset
|
452 } |
097184bd34a8
Implement number of lights uniform for the shaders, clean up light setup and
Matti Hamalainen <ccr@tnsp.org>
parents:
26
diff
changeset
|
453 |
6
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
454 // Initialize SDL + OpenGL |
21
1404dfcee7b8
More work on scenefile and model loading support.
Matti Hamalainen <ccr@tnsp.org>
parents:
20
diff
changeset
|
455 if (!dmInitSDLGL(optWidth, optHeight, "GLDragon")) |
6
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
456 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
|
457 |
6
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
458 // According to our mode .. |
8 | 459 if (optUseShaders) |
6
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
460 { |
21
1404dfcee7b8
More work on scenefile and model loading support.
Matti Hamalainen <ccr@tnsp.org>
parents:
20
diff
changeset
|
461 for (DMModel &model : scene.models) |
1404dfcee7b8
More work on scenefile and model loading support.
Matti Hamalainen <ccr@tnsp.org>
parents:
20
diff
changeset
|
462 { |
24
c1897cfc8463
Add shader compilation error handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
23
diff
changeset
|
463 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
|
464 !dmCompileShader(GL_VERTEX_SHADER, model.vertShaderStr, model.id_vs)) |
c1897cfc8463
Add shader compilation error handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
23
diff
changeset
|
465 goto exit; |
c1897cfc8463
Add shader compilation error handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
23
diff
changeset
|
466 |
21
1404dfcee7b8
More work on scenefile and model loading support.
Matti Hamalainen <ccr@tnsp.org>
parents:
20
diff
changeset
|
467 dmLinkModelShaders(model); |
1404dfcee7b8
More work on scenefile and model loading support.
Matti Hamalainen <ccr@tnsp.org>
parents:
20
diff
changeset
|
468 } |
6
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
469 } |
25
2403030a0352
"Finish" implementing multiple lights support in scene files.
Matti Hamalainen <ccr@tnsp.org>
parents:
24
diff
changeset
|
470 |
27
097184bd34a8
Implement number of lights uniform for the shaders, clean up light setup and
Matti Hamalainen <ccr@tnsp.org>
parents:
26
diff
changeset
|
471 // ... |
6
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
472 { |
27
097184bd34a8
Implement number of lights uniform for the shaders, clean up light setup and
Matti Hamalainen <ccr@tnsp.org>
parents:
26
diff
changeset
|
473 float specReflection[] = { 0.8f, 0.8f, 0.8f, 1.0f }; |
097184bd34a8
Implement number of lights uniform for the shaders, clean up light setup and
Matti Hamalainen <ccr@tnsp.org>
parents:
26
diff
changeset
|
474 glEnable(GL_COLOR_MATERIAL); |
097184bd34a8
Implement number of lights uniform for the shaders, clean up light setup and
Matti Hamalainen <ccr@tnsp.org>
parents:
26
diff
changeset
|
475 glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE); |
097184bd34a8
Implement number of lights uniform for the shaders, clean up light setup and
Matti Hamalainen <ccr@tnsp.org>
parents:
26
diff
changeset
|
476 glMateriali(GL_FRONT, GL_SHININESS, 96); |
097184bd34a8
Implement number of lights uniform for the shaders, clean up light setup and
Matti Hamalainen <ccr@tnsp.org>
parents:
26
diff
changeset
|
477 glMaterialfv(GL_FRONT, GL_SPECULAR, specReflection); |
25
2403030a0352
"Finish" implementing multiple lights support in scene files.
Matti Hamalainen <ccr@tnsp.org>
parents:
24
diff
changeset
|
478 } |
6
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
479 |
27
097184bd34a8
Implement number of lights uniform for the shaders, clean up light setup and
Matti Hamalainen <ccr@tnsp.org>
parents:
26
diff
changeset
|
480 // 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
|
481 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
|
482 dmSetupLight(n, scene.lights[n]); |
6
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
483 |
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
484 // Define the camera |
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
485 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
|
486 |
3
be31ff9e5f58
Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
487 |
6
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
488 // Main loop starts |
13
c1e8057cc4d0
Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
489 startTime = cycleStart = SDL_GetTicks(); |
6
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
490 |
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
491 while (!exitFlag) |
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
492 { |
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
493 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
|
494 |
6
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
495 // Check for quit events |
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
496 while (SDL_PollEvent(&event)) |
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
497 switch (event.type) |
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
498 { |
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
499 case SDL_QUIT: |
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
500 exitFlag = true; |
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
501 break; |
3
be31ff9e5f58
Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
502 |
6
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
503 case SDL_KEYDOWN: |
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
504 switch (event.key.keysym.sym) |
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
505 { |
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
506 case SDLK_ESCAPE: |
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
507 case SDLK_q: |
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
508 exitFlag = true; |
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
509 break; |
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
510 } |
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
511 } |
3
be31ff9e5f58
Port to libSDL2, clean up the code and some de-C++-ifications.
Matti Hamalainen <ccr@tnsp.org>
parents:
2
diff
changeset
|
512 |
6
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
513 // Render the next frame |
21
1404dfcee7b8
More work on scenefile and model loading support.
Matti Hamalainen <ccr@tnsp.org>
parents:
20
diff
changeset
|
514 dmDrawScene(scene); |
6
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
515 |
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
516 // Draw the current frame |
8 | 517 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
|
518 |
6
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
519 // Rotate for 2 degrees |
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
520 glRotatef(2.0f, 0, 1, 0); |
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
521 |
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
522 // Return true if a full rotation was done |
15
2d2aadfa3df3
Fix calculation of totalFrames.
Matti Hamalainen <ccr@tnsp.org>
parents:
14
diff
changeset
|
523 totalFrames++; |
13
c1e8057cc4d0
Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
524 if (cycleFrames++ == SET_FRAMES) |
6
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
525 { |
13
c1e8057cc4d0
Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
526 // Reset cycleFrames |
c1e8057cc4d0
Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
527 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
|
528 |
6
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
529 // 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
|
530 int cycleEnd = SDL_GetTicks(); |
c1e8057cc4d0
Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
531 double cycleTime = cycleEnd - cycleStart; |
10
c6c419861101
Use SDL_GetTicks() instead of std::clock().
Matti Hamalainen <ccr@tnsp.org>
parents:
8
diff
changeset
|
532 |
c6c419861101
Use SDL_GetTicks() instead of std::clock().
Matti Hamalainen <ccr@tnsp.org>
parents:
8
diff
changeset
|
533 // Restart the timer |
13
c1e8057cc4d0
Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
534 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
|
535 |
6
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
536 // Print the current frames per second |
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
537 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
|
538 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
|
539 } |
2 | 540 } |
6
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
541 |
13
c1e8057cc4d0
Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
542 // Show totals |
c1e8057cc4d0
Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
543 totalTime = SDL_GetTicks() - startTime; |
c1e8057cc4d0
Improvements to option handling, add option for setting preferred vsync
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
544 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
|
545 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
|
546 |
6
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
547 exit: |
8 | 548 if (dmGLContext != NULL) |
549 SDL_GL_DeleteContext(dmGLContext); | |
5 | 550 |
8 | 551 if (dmWindow != NULL) |
552 SDL_DestroyWindow(dmWindow); | |
6
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
553 |
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
554 SDL_Quit(); |
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
555 |
4d6fec8f0c64
Implement optional support for vertex/fragment shaders. Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
556 return 0; |
0
3d74a9dd96e4
Initial import of Thomas Trummer's original code.
Thomas Trummer
parents:
diff
changeset
|
557 } |