Colobot
glutil.h
1 /*
2  * This file is part of the Colobot: Gold Edition source code
3  * Copyright (C) 2001-2016, Daniel Roux, EPSITEC SA & TerranovaTeam
4  * http://epsitec.ch; http://colobot.info; http://github.com/colobot
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14  * See the GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program. If not, see http://gnu.org/licenses
18  */
19 
20 #pragma once
21 
22 // config.h must be included first
23 #include "common/config.h"
24 
25 #include "common/make_unique.h"
26 
27 #include "graphics/core/device.h"
28 
29 #include "math/intpoint.h"
30 #include "math/vector.h"
31 
32 #include <GL/glew.h>
33 
34 #include <string>
35 #include <memory>
36 
37 struct SDL_Surface;
38 
39 
40 // Graphics module namespace
41 namespace Gfx
42 {
43 
44 enum FramebufferSupport
45 {
46  FBS_NONE,
47  FBS_EXT,
48  FBS_ARB,
49 };
50 
51 bool InitializeGLEW();
52 
53 FramebufferSupport DetectFramebufferSupport();
54 
56 std::unique_ptr<CDevice> CreateDevice(const DeviceConfig &config, const std::string& name);
57 
59 // \return First digit is major part, second digit is minor part.
60 int GetOpenGLVersion();
61 
63 // \return First digit is major part, second digit is minor part.
64 int GetOpenGLVersion(int &major, int &minor);
65 
67 // \return true if all extensions are supported
68 bool AreExtensionsSupported(std::string list);
69 
71 std::string GetHardwareInfo(bool full = false);
72 
74 int ClearGLErrors();
75 
77 bool CheckGLErrors();
78 
81 
82 CompFunc TranslateGLCompFunc(GLenum flag);
83 
84 GLenum TranslateGfxCompFunc(CompFunc func);
85 
86 BlendFunc TranslateGLBlendFunc(GLenum flag);
87 
88 GLenum TranslateGfxBlendFunc(BlendFunc func);
89 
90 bool InPlane(Math::Vector normal, float originPlane, Math::Vector center, float radius);
91 
92 GLenum TranslateTextureCoordinate(int index);
93 
94 GLenum TranslateTextureCoordinateGen(int index);
95 
96 std::string GetLastShaderError();
97 
98 GLint LoadShader(GLint type, const char* filename);
99 
100 GLint LinkProgram(int count, GLint shaders[]);
101 
102 // TODO: Moved this here temporarily only to remove code duplication in CGLDeviceXX
104 {
105  SDL_Surface* actualSurface = nullptr;
106  SDL_Surface* convertedSurface = nullptr;
107  GLenum sourceFormat = 0;
108  bool alpha = false;
109 };
110 
111 PreparedTextureData PrepareTextureData(ImageData* imageData, TexImgFormat format);
112 
114 {
115 public:
116  CGLFrameBufferPixels(std::size_t size)
117  : m_pixels(MakeUniqueArray<GLubyte>(size))
118  {}
119 
120  void* GetPixelsData() override
121  {
122  return static_cast<void*>(m_pixels.get());
123  }
124 
125 private:
126  std::unique_ptr<GLubyte[]> m_pixels;
127 };
128 
129 std::unique_ptr<CGLFrameBufferPixels> GetGLFrameBufferPixels(Math::IntPoint size);
130 
132 {
134  GLint enabled = -1;
136  GLint type = -1;
138  GLint position = -1;
140  GLint ambient = -1;
142  GLint diffuse = -1;
144  GLint specular = -1;
146  GLint attenuation = -1;
148  GLint spotDirection = -1;
150  GLint spotExponent = -1;
152  GLint spotCutoff = -1;
153 };
154 
156 {
157  // Uniforms
159  GLint projectionMatrix = -1;
161  GLint viewMatrix = -1;
163  GLint modelMatrix = -1;
165  GLint shadowMatrix = -1;
167  GLint normalMatrix = -1;
168 
170  GLint primaryTexture = -1;
172  GLint secondaryTexture = -1;
174  GLint shadowTexture = -1;
175 
177  GLint textureEnabled[3] = {};
178 
179  // Alpha test parameters
181  GLint alphaTestEnabled = -1;
183  GLint alphaReference = -1;
184 
186  GLint fogEnabled = -1;
188  GLint fogRange = -1;
190  GLint fogColor = -1;
191 
193  GLint shadowColor = -1;
194 
196  GLint lightingEnabled = -1;
198  GLint ambientColor = -1;
200  GLint diffuseColor = -1;
202  GLint specularColor = -1;
203 
204  LightLocations lights[8] = {};
205 };
206 
207 } // namespace Gfx
std::unique_ptr< CDevice > CreateDevice(const DeviceConfig &config, const std::string &name)
Creates OpenGL device.
Definition: glutil.cpp:72
Definition: device.h:304
CompFunc
Type of function used to compare values.
Definition: device.h:183
int ClearGLErrors()
Clears OpenGL errors.
Definition: glutil.cpp:297
TexImgFormat
Format of image data.
Definition: texture.h:42
bool AreExtensionsSupported(std::string list)
Checks if extensions in space-delimited list are supported.
Definition: glutil.cpp:107
std::string GetHardwareInfo(bool full)
Returns information about graphics card.
Definition: glutil.cpp:171
Definition: glutil.h:155
BlendFunc
Type of blending function.
Definition: device.h:199
GLenum TranslateGfxPrimitive(PrimitiveType type)
Translate Gfx primitive type to OpenGL primitive type.
Definition: glutil.cpp:330
Definition: glutil.h:131
Definition: glutil.h:103
PrimitiveType
Type of primitive to render.
Definition: device.h:265
Namespace for (new) graphics code.
Definition: app.h:49
Implementation-specific image data.
Definition: image.h:41
Vector struct and related functions.
int GetOpenGLVersion()
Returns OpenGL version.
Definition: glutil.cpp:91
Definition: glutil.h:113
3D (3x1) vector
Definition: vector.h:53
Abstract graphics device - CDevice class and related structs/enums.
2D Point with integer coords
Definition: intpoint.h:41
IntPoint struct.
bool CheckGLErrors()
Checks for OpenGL errors.
Definition: glutil.cpp:313