1 // ==============================================================
2 // This file is part of Glest (www.glest.org)
3 //
4 // Copyright (C) 2001-2010 Mark Vejvoda
5 //
6 // You can redistribute this code and/or modify it under
7 // the terms of the GNU General Public License as published
8 // by the Free Software Foundation; either version 2 of the
9 // License, or (at your option) any later version
10 // ==============================================================
11
12
13 #include "base_renderer.h"
14 #include <cassert>
15 #include "opengl.h"
16 #include "vec.h"
17
18 using namespace Shared::Graphics;
19 using namespace Shared::Graphics::Gl;
20
21 namespace Shared { namespace Graphics {
22
23 // ===============================================
24 // class Renderer
25 // ===============================================
26
initMapSurface(int clientW,int clientH)27 void BaseRenderer::initMapSurface(int clientW, int clientH) {
28 assertGl();
29
30 glFrontFace(GL_CW);
31 glEnable(GL_CULL_FACE);
32 glPolygonMode(GL_FRONT, GL_FILL);
33 glClearColor(0.5, 0.5, 0.5, 1.0);
34
35 assertGl();
36 }
37
renderMap(MapPreview * map,int x,int y,int clientW,int clientH,int cellSize,bool grid,bool heightMap,bool hideWater)38 void BaseRenderer::renderMap(MapPreview *map, int x, int y,
39 int clientW, int clientH, int cellSize, bool grid, bool heightMap, bool hideWater) {
40 float alt=0;
41 float showWater=0;
42
43 assertGl();
44
45 glMatrixMode(GL_PROJECTION);
46 glLoadIdentity();
47 glOrtho(0, clientW, 0, clientH, 1, -1);
48 glViewport(0, 0, clientW, clientH);
49 glMatrixMode(GL_MODELVIEW);
50 glPushMatrix();
51 glPushAttrib(GL_CURRENT_BIT);
52
53 glTranslatef(static_cast<float>(x), static_cast<float>(y), 0.0f);
54 glLineWidth(1);
55 glClear(GL_COLOR_BUFFER_BIT);
56 glColor3f(0, 0, 0);
57
58 for (int j = 0; j < map->getH(); j++) {
59 for (int i = 0; i < map->getW(); i++) {
60 if (i * cellSize + x > -cellSize
61 && i * cellSize + x < clientW
62 && clientH - cellSize - j * cellSize + y > -cellSize
63 && clientH - cellSize - j * cellSize + y < clientH) {
64 bool isCliff=false; // needed to speedup things
65 //surface
66 alt = map->getHeight(i, j) / 20.f;
67 showWater = map->getWaterLevel()/ 20.f - alt;
68 showWater = (showWater > 0)? showWater:0;
69 if(hideWater){
70 showWater = 0;
71 }
72 Vec3f surfColor;
73 switch (map->getSurface(i, j)) {
74 case st_Grass: surfColor = Vec3f(0.0, 0.8f * alt, 0.f + showWater); break;
75 case st_Secondary_Grass: surfColor = Vec3f(0.4f * alt, 0.6f * alt, 0.f + showWater); break;
76 case st_Road: surfColor = Vec3f(0.6f * alt, 0.3f * alt, 0.f + showWater); break;
77 case st_Stone: surfColor = Vec3f(0.7f * alt, 0.7f * alt, 0.7f * alt + showWater); break;
78 case st_Ground: surfColor = Vec3f(0.7f * alt, 0.5f * alt, 0.3f * alt + showWater); break;
79 }
80 if(heightMap){
81 surfColor = Vec3f(1.f * alt, 1.f * alt, 1.f * alt + showWater);
82 }
83 if(map->getCliffLevel()>0)
84 {// we maybe need to render cliff surfColor
85 if(map->isCliff(i, j)){
86 surfColor = Vec3f(0.95f * alt, 0.8f * alt, 0.0f * alt + showWater);
87 isCliff=true;
88 }
89 }
90 glColor3fv(surfColor.ptr());
91
92 glBegin(GL_TRIANGLE_STRIP);
93 glVertex2i(i * cellSize, clientH - j * cellSize - cellSize);
94 glVertex2i(i * cellSize, clientH - j * cellSize);
95 glVertex2i(i * cellSize + cellSize, clientH - j * cellSize - cellSize);
96 glVertex2i(i * cellSize + cellSize, clientH - j * cellSize);
97 glEnd();
98 if(!heightMap){
99 //objects
100 switch (map->getObject(i, j)) {
101 case 0: glColor3f(0.f, 0.f, 0.f); break;
102 case 1: glColor3f(1.f, 0.f, 0.f); break;
103 case 2: glColor3f(1.f, 1.f, 1.f); break;
104 case 3: glColor3f(0.5f, 0.5f, 1.f); break;
105 case 4: glColor3f(0.f, 0.f, 1.f); break;
106 case 5: glColor3f(0.5f, 0.5f, 0.5f); break;
107 case 6: glColor3f(1.f, 0.8f, 0.5f); break;
108 case 7: glColor3f(0.f, 1.f, 1.f); break;
109 case 8: glColor3f(0.7f, 0.1f, 0.3f); break;
110 case 9: glColor3f(0.5f, 1.f, 0.1f); break;
111 case 10: glColor3f(1.f, 0.2f, 0.8f); break;
112 }
113
114 if (map->getObject(i, j) != 0 || isCliff ) {
115 glPointSize(cellSize / 2.f);
116 glBegin(GL_POINTS);
117 glVertex2i(i * cellSize + cellSize / 2, clientH - j * cellSize - cellSize / 2);
118 glEnd();
119 }
120
121 // bool found = false;
122
123 //height lines
124 // if (!found) {
125
126 glColor3fv((surfColor*0.5f).ptr());
127 //left
128 if (grid || (i > 0 && map->getHeight(i - 1, j) > map->getHeight(i, j))) {
129 glBegin(GL_LINES);
130 glVertex2i(i * cellSize, clientH - (j + 1) * cellSize);
131 glVertex2i(i * cellSize, clientH - j * cellSize);
132 glEnd();
133 }
134 //down
135 if (grid || (j > 0 && map->getHeight(i, j - 1) > map->getHeight(i, j))) {
136 glBegin(GL_LINES);
137 glVertex2i(i * cellSize, clientH - j * cellSize);
138 glVertex2i((i + 1) * cellSize, clientH - j * cellSize);
139 glEnd();
140 }
141
142 glColor3fv((surfColor*2.f).ptr());
143 //left
144 if (i > 0 && map->getHeight(i - 1, j) < map->getHeight(i, j)) {
145 glBegin(GL_LINES);
146 glVertex2i(i * cellSize, clientH - (j + 1) * cellSize);
147 glVertex2i(i * cellSize, clientH - j * cellSize);
148 glEnd();
149 }
150 if (j > 0 && map->getHeight(i, j - 1) < map->getHeight(i, j)) {
151 glBegin(GL_LINES);
152 glVertex2i(i * cellSize, clientH - j * cellSize);
153 glVertex2i((i + 1) * cellSize, clientH - j * cellSize);
154 glEnd();
155 }
156 // }
157 //resources
158 switch (map->getResource(i, j)) {
159 case 1: glColor3f(1.f, 1.f, 0.f); break;
160 case 2: glColor3f(0.5f, 0.5f, 0.5f); break;
161 case 3: glColor3f(1.f, 0.f, 0.f); break;
162 case 4: glColor3f(0.f, 0.f, 1.f); break;
163 case 5: glColor3f(0.5f, 0.5f, 1.f); break;
164 }
165
166 if (map->getResource(i, j) != 0) {
167 glBegin(GL_LINES);
168 glVertex2i(i * cellSize, clientH - j * cellSize - cellSize);
169 glVertex2i(i * cellSize + cellSize, clientH - j * cellSize);
170 glVertex2i(i * cellSize, clientH - j * cellSize);
171 glVertex2i(i * cellSize + cellSize, clientH - j * cellSize - cellSize);
172 glEnd();
173 }
174 }
175
176 }
177 }
178 }
179
180 //start locations
181 glLineWidth(3);
182 for (int i = 0; i < map->getMaxFactions(); i++) {
183 switch (i) {
184 case 0: glColor3f(1.f, 0.f, 0.f); break;
185 case 1: glColor3f(0.f, 0.f, 1.f); break;
186 case 2: glColor3f(0.f, 1.f, 0.f); break;
187 case 3: glColor3f(1.f, 1.f, 0.f); break;
188 case 4: glColor3f(1.f, 1.f, 1.f); break;
189 case 5: glColor3f(0.f, 1.f, 0.8f); break;
190 case 6: glColor3f(1.f, 0.5f, 0.f); break;
191 case 7: glColor3f(1.f, 0.5f, 1.f); break;
192 }
193 glBegin(GL_LINES);
194 glVertex2i((map->getStartLocationX(i) - 1) * cellSize, clientH - (map->getStartLocationY(i) - 1) * cellSize);
195 glVertex2i((map->getStartLocationX(i) + 1) * cellSize + cellSize, clientH - (map->getStartLocationY(i) + 1) * cellSize - cellSize);
196 glVertex2i((map->getStartLocationX(i) - 1) * cellSize, clientH - (map->getStartLocationY(i) + 1) * cellSize - cellSize);
197 glVertex2i((map->getStartLocationX(i) + 1) * cellSize + cellSize, clientH - (map->getStartLocationY(i) - 1) * cellSize);
198 glEnd();
199 }
200
201 glPopMatrix();
202 glPopAttrib();
203
204 assertGl();
205 }
206
207
208 }} // end namespace
209