rev |
line source |
nuclear@44
|
1 #include <limits.h>
|
nuclear@15
|
2 #include "opengl.h"
|
nuclear@15
|
3 #include "renderer.h"
|
nuclear@23
|
4 #include "level.h"
|
nuclear@15
|
5 #include "sdr.h"
|
nuclear@15
|
6 #include "datapath.h"
|
nuclear@15
|
7
|
nuclear@41
|
8 static unsigned int load_sdr(const char *vfname, const char *pfname);
|
nuclear@35
|
9
|
nuclear@41
|
10 Renderer *rend;
|
nuclear@18
|
11
|
nuclear@15
|
12
|
nuclear@41
|
13 Renderer::Renderer()
|
nuclear@41
|
14 {
|
nuclear@41
|
15 width = height = 0;
|
nuclear@41
|
16 }
|
nuclear@17
|
17
|
nuclear@41
|
18 Renderer::~Renderer()
|
nuclear@41
|
19 {
|
nuclear@41
|
20 }
|
nuclear@17
|
21
|
nuclear@41
|
22 bool Renderer::init(int xsz, int ysz)
|
nuclear@41
|
23 {
|
nuclear@41
|
24 width = xsz;
|
nuclear@41
|
25 height = ysz;
|
nuclear@17
|
26
|
nuclear@41
|
27 rend = this;
|
nuclear@41
|
28 return true;
|
nuclear@41
|
29 }
|
nuclear@15
|
30
|
nuclear@41
|
31 int Renderer::get_tangent_location() const
|
nuclear@41
|
32 {
|
nuclear@41
|
33 return -1;
|
nuclear@41
|
34 }
|
nuclear@15
|
35
|
nuclear@41
|
36 unsigned int Renderer::get_current_program() const
|
nuclear@41
|
37 {
|
nuclear@41
|
38 return 0;
|
nuclear@41
|
39 }
|
nuclear@21
|
40
|
nuclear@41
|
41 void Renderer::resize(int xsz, int ysz)
|
nuclear@41
|
42 {
|
nuclear@41
|
43 width = xsz;
|
nuclear@41
|
44 height = ysz;
|
nuclear@41
|
45 }
|
nuclear@21
|
46
|
nuclear@41
|
47
|
nuclear@41
|
48 // ---- fallback forward renderer ----
|
nuclear@41
|
49 FwdRenderer::FwdRenderer()
|
nuclear@17
|
50 {
|
nuclear@41
|
51 sdrprog = 0;
|
nuclear@41
|
52 tang_attr = -1;
|
nuclear@41
|
53 }
|
nuclear@41
|
54
|
nuclear@41
|
55 FwdRenderer::~FwdRenderer()
|
nuclear@41
|
56 {
|
nuclear@41
|
57 if(sdrprog) {
|
nuclear@41
|
58 free_program(sdrprog);
|
nuclear@32
|
59 }
|
nuclear@41
|
60 }
|
nuclear@21
|
61
|
nuclear@41
|
62 bool FwdRenderer::init(int xsz, int ysz)
|
nuclear@41
|
63 {
|
nuclear@41
|
64 width = xsz;
|
nuclear@41
|
65 height = ysz;
|
nuclear@19
|
66
|
nuclear@41
|
67 if(glUseProgram && (sdrprog = load_sdr("fallback.v.glsl", "fallback.p.glsl"))) {
|
nuclear@41
|
68 tang_attr = get_attrib_loc(sdrprog, "attr_tangent");
|
nuclear@41
|
69 set_uniform_int(sdrprog, "tex_dif", 0);
|
nuclear@41
|
70 set_uniform_int(sdrprog, "tex_norm", 1);
|
nuclear@23
|
71 }
|
nuclear@17
|
72 return true;
|
nuclear@17
|
73 }
|
nuclear@17
|
74
|
nuclear@41
|
75 int FwdRenderer::get_tangent_location() const
|
nuclear@17
|
76 {
|
nuclear@41
|
77 return tang_attr;
|
nuclear@17
|
78 }
|
nuclear@17
|
79
|
nuclear@41
|
80 unsigned int FwdRenderer::get_current_program() const
|
nuclear@33
|
81 {
|
nuclear@41
|
82 return sdrprog;
|
nuclear@33
|
83 }
|
nuclear@33
|
84
|
nuclear@41
|
85 void FwdRenderer::render(const Level *level) const
|
nuclear@35
|
86 {
|
nuclear@41
|
87 glPushAttrib(GL_ENABLE_BIT);
|
nuclear@41
|
88 glUseProgram(sdrprog);
|
nuclear@35
|
89
|
nuclear@41
|
90 glEnable(GL_LIGHTING);
|
nuclear@23
|
91 level->draw();
|
nuclear@20
|
92
|
nuclear@41
|
93 glPopAttrib();
|
nuclear@19
|
94
|
nuclear@18
|
95 glUseProgram(0);
|
nuclear@17
|
96 }
|
nuclear@17
|
97
|
nuclear@18
|
98 static unsigned int load_sdr(const char *vfname, const char *pfname)
|
nuclear@15
|
99 {
|
nuclear@15
|
100 char vsfile[PATH_MAX], psfile[PATH_MAX];
|
nuclear@15
|
101 const char *fname;
|
nuclear@18
|
102 unsigned int prog;
|
nuclear@15
|
103
|
nuclear@18
|
104 if((fname = datafile_path(vfname))) {
|
nuclear@17
|
105 strcpy(vsfile, fname);
|
nuclear@17
|
106 } else {
|
nuclear@17
|
107 vsfile[0] = 0;
|
nuclear@17
|
108 }
|
nuclear@18
|
109 if((fname = datafile_path(pfname))) {
|
nuclear@17
|
110 strcpy(psfile, fname);
|
nuclear@17
|
111 } else {
|
nuclear@17
|
112 psfile[0] = 0;
|
nuclear@17
|
113 }
|
nuclear@18
|
114 if(!(prog = create_program_load(vsfile, psfile))) {
|
nuclear@18
|
115 fprintf(stderr, "failed to load shader program (%s, %s)\n", vsfile, psfile);
|
nuclear@18
|
116 return 0;
|
nuclear@15
|
117 }
|
nuclear@18
|
118 return prog;
|
nuclear@15
|
119 }
|