Renderer.cpp 19.9 KB
Newer Older
Gargaj's avatar
add SDL    
Gargaj committed
1
2
3
#ifdef _WIN32
#include <windows.h>
#endif
4
#include <SDL.h>
Gargaj's avatar
Gargaj committed
5
#include <GLee.h>
6
7
8
#ifdef __APPLE__
#include <OpenGL/glu.h>
#else
9
#include <GL/glu.h>
10
#endif
Gargaj's avatar
add SDL    
Gargaj committed
11
12
#include "../Renderer.h"

Gargaj's avatar
Gargaj committed
13
14
#define STBI_HEADER_FILE_ONLY
#include <stb_image.c>
Gargaj's avatar
Gargaj committed
15
#include "../external/scintilla/include/Scintilla.h"
Gargaj's avatar
Gargaj committed
16

17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
const char * shaderKeyword =
  "discard struct if else switch case default break goto return for while do continue";

const char * shaderType = 
  "attribute const in inout out uniform varying invariant "
  "centroid flat smooth noperspective layout patch sample "
  "subroutine lowp mediump highp precision "
  "void float vec2 vec3 vec4 bvec2 bvec3 bvec4 ivec2 ivec3 ivec4 "
  "uvec2 uvec3 uvec4 dvec2 dvec3 dvec4 "
  "sampler1D sampler2D sampler3D isampler2D isampler1D isampler3D "
  "usampler1D usampler2D usampler3D "
  "sampler1DShadow sampler2DShadow sampler1DArray sampler2DArray "
  "sampler1DArrayShadow sampler2DArrayShadow "
  "samplerCube samperCubeShadow samperCubeArrayShadow ";

const char * shaderBuiltin = 
  "radians degrees sin cos tan asin acos atan sinh "
  "cosh tanh asinh acosh atanh pow exp log exp2 "
  "log2 sqrt inversesqrt abs sign floor trunc round "
  "roundEven ceil fract mod modf min max clamp mix "
  "step smoothstep isnan isinf floatBitsToInt floatBitsToUint "
  "intBitsToFloat uintBitsToFloat fma frexp ldexp packUnorm2x16 "
  "packUnorm4x8 packSnorm4x8 unpackUnorm2x16 unpackUnorm4x8 "
  "unpackSnorm4x8 packDouble2x32 unpackDouble2x32 length distance "
  "dot cross normalize ftransform faceforward reflect "
  "refract matrixCompMult outerProduct transpose determinant "
  "inverse lessThan lessThanEqual greaterThan greaterThanEqual "
  "equal notEqual any all not uaddCarry usubBorrow "
  "umulExtended imulExtended bitfieldExtract bitfildInsert "
  "bitfieldReverse bitCount findLSB findMSB textureSize "
  "textureQueryLOD texture textureProj textureLod textureOffset "
  "texelFetch texelFetchOffset textureProjOffset textureLodOffset "
  "textureProjLod textureProjLodOffset textureGrad textureGradOffset "
  "textureProjGrad textureProjGradOffset textureGather "
  "textureGatherOffset texture1D texture2D texture3D texture1DProj "
  "texture2DProj texture3DProj texture1DLod texture2DLod "
  "texture3DLod texture1DProjLod texture2DProjLod texture3DProjLod "
  "textureCube textureCubeLod shadow1D shadow2D shadow1DProj "
  "shadow2DProj shadow1DLod shadow2DLod shadow1DProjLod "
  "shadow2DProjLod dFdx dFdy fwidth interpolateAtCentroid "
  "interpolateAtSample interpolateAtOffset noise1 noise2 noise3 "
  "noise4 EmitStreamVertex EndStreamPrimitive EmitVertex "
  "EndPrimitive barrier "
  "gl_VertexID gl_InstanceID gl_Position gl_PointSize "
  "gl_ClipDistance gl_PrimitiveIDIn gl_InvocationID gl_PrimitiveID "
  "gl_Layer gl_PatchVerticesIn gl_TessLevelOuter gl_TessLevelInner "
  "gl_TessCoord gl_FragCoord gl_FrontFacing gl_PointCoord "
  "gl_SampleID gl_SamplePosition gl_FragColor gl_FragData "
  "gl_FragDepth gl_SampleMask gl_ClipVertex gl_FrontColor "
  "gl_BackColor gl_FrontSecondaryColor gl_BackSecondaryColor "
  "gl_TexCoord gl_FogFragCoord gl_Color gl_SecondaryColor "
  "gl_Normal gl_Vertex gl_MultiTexCoord0 gl_MultiTexCoord1 "
  "gl_MultiTexCoord2 gl_MultiTexCoord3 gl_MultiTexCoord4 "
  "gl_MultiTexCoord5 gl_MultiTexCoord6 gl_MultiTexCoord7 gl_FogCoord "
  "gl_MaxVertexAttribs gl_MaxVertexUniformComponents gl_MaxVaryingFloats "
  "gl_MaxVaryingComponents gl_MaxVertexOutputComponents "
  "gl_MaxGeometryInputComponents gl_MaxGeometryOutputComponents "
  "gl_MaxFragmentInputComponents gl_MaxVertexTextureImageUnits "
  "gl_MaxCombinedTextureImageUnits gl_MaxTextureImageUnits "
  "gl_MaxFragmentUniformComponents gl_MaxDrawBuffers gl_MaxClipDistances "
  "gl_MaxGeometryTextureImageUnits gl_MaxGeometryOutputVertices "
  "gl_MaxGeometryTotalOutputComponents gl_MaxGeometryUniformComponents "
  "gl_MaxGeometryVaryingComponents gl_MaxTessControlInputComponents "
  "gl_MaxTessControlOutputComponents gl_MaxTessControlTextureImageUnits "
  "gl_MaxTessControlUniformComponents "
  "gl_MaxTessControlTotalOutputComponents "
  "gl_MaxTessEvaluationInputComponents gl_MaxTessEvaluationOutputComponents "
  "gl_MaxTessEvaluationTextureImageUnits "
  "gl_MaxTessEvaluationUniformComponents gl_MaxTessPatchComponents "
  "gl_MaxPatchVertices gl_MaxTessGenLevel gl_MaxTextureUnits "
  "gl_MaxTextureCoords gl_MaxClipPlanes "
  "gl_DepthRange gl_ModelViewMatrix gl_ProjectionMatrix "
  "gl_ModelViewProjectionMatrix gl_TextureMatrix gl_NormalMatrix "
  "gl_ModelViewMatrixInverse gl_ProjectionMatrixInverse "
  "gl_ModelViewProjectionMatrixInverse gl_TextureMatrixInverse "
  "gl_ModelViewMatrixTranspose gl_ProjectionMatrixTranspose "
  "gl_ModelViewProjectionMatrixTranspose gl_TextureMatrixTranspose "
  "gl_ModelViewMatrixInverseTranspose gl_ProjectionMatrixInverseTranspose "
  "gl_ModelViewProjectionMatrixInverseTranspose "
  "gl_TextureMatrixInverseTranspose gl_NormalScale gl_ClipPlane "
  "gl_Point gl_FrontMaterial gl_BackMaterial gl_LightSource "
  "gl_LightModel gl_FrontLightModelProduct gl_BackLightModelProduct "
  "gl_FrontLightProduct gl_BackLightProduct gl_TextureEnvColor "
  "gl_EyePlaneS gl_EyePlaneT gl_EyePlaneR gl_EyePlaneQ "
  "gl_ObjectPlaneS gl_ObjectPlaneT gl_ObjectPlaneR gl_ObjectPlaneQ "
  "gl_Fog";

Gargaj's avatar
add SDL    
Gargaj committed
104
105
namespace Renderer
{
106
  char * defaultShaderFilename = "shader.glsl";
Gargaj's avatar
Gargaj committed
107
108
109
110
  char defaultShader[65536] = 
    "#version 430 core\n"
    "\n"
    "uniform float fGlobalTime; // in seconds\n"
111
    "uniform vec2 v2Resolution; // viewport resolution (in pixels)\n"
Gargaj's avatar
Gargaj committed
112
    "\n"
113
114
115
116
117
118
119
    "uniform sampler1D texFFT; // towards 0.0 is bass / lower freq, towards 1.0 is higher / treble freq\n"
    "{%textures:begin%}" // leave off \n here
    "uniform sampler2D {%textures:name%};\n"
    "{%textures:end%}" // leave off \n here
    "{%midi:begin%}" // leave off \n here
    "float {%midi:name%};\n"
    "{%midi:end%}" // leave off \n here
Gargaj's avatar
Gargaj committed
120
    "\n"
121
    "layout(location = 0) out vec4 out_color; // out_color must be written in order to see anything\n"
Gargaj's avatar
Gargaj committed
122
    "\n"
123
124
125
126
127
    "vec4 plas( vec2 v, float time )\n"
    "{\n"
    "  float c = 0.5 + sin( v.x * 10.0 ) + cos( sin( time + v.y ) * 20.0 );\n"
    "  return vec4( sin(c * 0.2 + cos(time)), c * 0.15, cos( c * 0.1 + time / .4 ) * .25, 1.0 );\n"
    "}\n"
Gargaj's avatar
Gargaj committed
128
129
    "void main(void)\n"
    "{\n"
130
    "  vec2 uv = gl_TexCoord[0].xy;\n"
Gargaj's avatar
Gargaj committed
131
132
133
134
135
136
137
138
139
140
141
142
    "  uv -= 0.5;\n"
    "  uv /= vec2(v2Resolution.y / v2Resolution.x, 1);\n"
    "\n"
    "  vec2 m;\n"
    "  m.x = atan(uv.x / uv.y) / 3.14;\n"
    "  m.y = 1 / length(uv) * .2;\n"
    "  float d = m.y;\n"
    "\n"
    "  float f = texture( texFFT, d ).r * 100;\n"
    "  m.x += sin( fGlobalTime ) * 0.1;\n"
    "  m.y += fGlobalTime * 0.25;\n"
    "\n"
143
144
145
    "  vec4 t = plas( m * 3.14, fGlobalTime ) / d;\n"
    "  t = clamp( t, 0.0, 1.0 );\n"
    "  out_color = f + t;\n"
Gargaj's avatar
Gargaj committed
146
147
    "}";

Gargaj's avatar
Gargaj committed
148
  SDL_Surface * mScreen = NULL;
Gargaj's avatar
add SDL    
Gargaj committed
149
150
  bool run = true;

Gargaj's avatar
Gargaj committed
151
152
  GLuint theShader = NULL;

153
154
  int nWidth = 0;
  int nHeight = 0;
Gargaj's avatar
add SDL    
Gargaj committed
155
156
  bool Open( RENDERER_SETTINGS * settings )
  {
Gargaj's avatar
Gargaj committed
157
    theShader = NULL;
Gargaj's avatar
add SDL    
Gargaj committed
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
    if( SDL_Init( SDL_INIT_VIDEO | SDL_INIT_TIMER ) < 0)
    {
      return false;
    }

    uint32_t flags = SDL_HWSURFACE|SDL_OPENGLBLIT;
    if (settings->windowMode == RENDERER_WINDOWMODE_FULLSCREEN)
      flags |= SDL_FULLSCREEN;

    SDL_GL_SetAttribute( SDL_GL_RED_SIZE, 8 );
    SDL_GL_SetAttribute( SDL_GL_GREEN_SIZE, 8 );
    SDL_GL_SetAttribute( SDL_GL_BLUE_SIZE, 8 );
    SDL_GL_SetAttribute( SDL_GL_ALPHA_SIZE, 8 );
    SDL_GL_SetAttribute( SDL_GL_DEPTH_SIZE, 24 );
    SDL_GL_SetAttribute( SDL_GL_STENCIL_SIZE, 8 );
    SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, true );

175
176
    nWidth = settings->nWidth;
    nHeight = settings->nHeight;
Gargaj's avatar
add SDL    
Gargaj committed
177
178
179
180
181
182
183
    mScreen = SDL_SetVideoMode( settings->nWidth, settings->nHeight, 32, flags );
    if (!mScreen)
    {
      SDL_Quit();
      return false;
    }

184
    SDL_EnableUNICODE(true);
Gargaj's avatar
add SDL    
Gargaj committed
185
186
    SDL_EnableKeyRepeat(250, 20);

Gargaj's avatar
Gargaj committed
187
188
189
    if (settings->bVsync)
      wglSwapIntervalEXT(1);

Gargaj's avatar
add SDL    
Gargaj committed
190
191
192
193
    run = true;

    return true;
  }
Gargaj's avatar
Gargaj committed
194

Gargaj's avatar
Gargaj committed
195
196
  KeyEvent keyEventBuffer[512];
  int keyEventBufferCount = 0;
Gargaj's avatar
Gargaj committed
197
198
  MouseEvent mouseEventBuffer[512];
  int mouseEventBufferCount = 0;
Gargaj's avatar
add SDL    
Gargaj committed
199
200
  void StartFrame()
  {
Gargaj's avatar
Gargaj committed
201
    keyEventBufferCount = 0;
Gargaj's avatar
Gargaj committed
202
    mouseEventBufferCount = 0;
Gargaj's avatar
add SDL    
Gargaj committed
203
204
205
206
207
208
209
210
211
212
213
214
215
    SDL_Event	E;
    while (SDL_PollEvent(&E))
    {
      if (E.type == SDL_QUIT) 
      {
        run = false;
      }
      else if (E.type == SDL_KEYDOWN)
      {
        if (E.key.keysym.sym == SDLK_F4 && (E.key.keysym.mod == KMOD_LALT || E.key.keysym.mod == KMOD_RALT)) 
        {
          run = false;
        }
Gargaj's avatar
Gargaj committed
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
        int sciKey;
        switch(E.key.keysym.sym)
        {
          case SDLK_DOWN:         sciKey = SCK_DOWN;      break;
          case SDLK_UP:           sciKey = SCK_UP;        break;
          case SDLK_LEFT:         sciKey = SCK_LEFT;      break;
          case SDLK_RIGHT:        sciKey = SCK_RIGHT;     break;
          case SDLK_HOME:         sciKey = SCK_HOME;      break;
          case SDLK_END:          sciKey = SCK_END;       break;
          case SDLK_PAGEUP:       sciKey = SCK_PRIOR;     break;
          case SDLK_PAGEDOWN:     sciKey = SCK_NEXT;      break;
          case SDLK_DELETE:       sciKey = SCK_DELETE;    break;
          case SDLK_INSERT:       sciKey = SCK_INSERT;    break;
          case SDLK_ESCAPE:       sciKey = SCK_ESCAPE;    break;
          case SDLK_BACKSPACE:    sciKey = SCK_BACK;      break;
          case SDLK_TAB:          sciKey = SCK_TAB;       break;
          case SDLK_RETURN:       sciKey = SCK_RETURN;    break;
          case SDLK_KP_PLUS:      sciKey = SCK_ADD;       break;
          case SDLK_KP_MINUS:     sciKey = SCK_SUBTRACT;  break;
          case SDLK_KP_DIVIDE:    sciKey = SCK_DIVIDE;    break;
          case SDLK_LSUPER:       sciKey = SCK_WIN;       break;
          case SDLK_RSUPER:       sciKey = SCK_RWIN;      break;
          case SDLK_MENU:         sciKey = SCK_MENU;      break;
          case SDLK_SLASH:        sciKey = '/';           break;
          case SDLK_ASTERISK:     sciKey = '`';           break;
          case SDLK_LEFTBRACKET:  sciKey = '[';           break;
          case SDLK_BACKSLASH:    sciKey = '\\';          break;
          case SDLK_RIGHTBRACKET: sciKey = ']';           break;
          case SDLK_LSHIFT:
          case SDLK_RSHIFT:
          case SDLK_LALT:
          case SDLK_RALT:
          case SDLK_LCTRL:
          case SDLK_RCTRL:
            sciKey = 0;
            break;
          default:
            sciKey = E.key.keysym.sym;
        }

        if (sciKey)
        {
          keyEventBuffer[keyEventBufferCount].ctrl  = E.key.keysym.mod & KMOD_LCTRL  || E.key.keysym.mod & KMOD_RCTRL;
          keyEventBuffer[keyEventBufferCount].alt   = E.key.keysym.mod & KMOD_LALT   || E.key.keysym.mod & KMOD_RALT;
          keyEventBuffer[keyEventBufferCount].shift = E.key.keysym.mod & KMOD_LSHIFT || E.key.keysym.mod & KMOD_RSHIFT;
          keyEventBuffer[keyEventBufferCount].scanCode = sciKey;
          keyEventBuffer[keyEventBufferCount].character = E.key.keysym.unicode;
          keyEventBufferCount++;
        }
Gargaj's avatar
add SDL    
Gargaj committed
265
266

      }
267
268
269
270
271
272
273
274
      else if (E.type == SDL_MOUSEMOTION)
      {
        mouseEventBuffer[mouseEventBufferCount].eventType = MOUSEEVENTTYPE_MOVE;
        mouseEventBuffer[mouseEventBufferCount].x = E.motion.x;
        mouseEventBuffer[mouseEventBufferCount].y = E.motion.y;
        if (E.motion.state & SDL_BUTTON(1)) mouseEventBuffer[mouseEventBufferCount].button = MOUSEBUTTON_LEFT;
        mouseEventBufferCount++;
      }
Gargaj's avatar
add SDL    
Gargaj committed
275
276
      else if (E.type == SDL_MOUSEBUTTONDOWN)
      {
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
        mouseEventBuffer[mouseEventBufferCount].eventType = MOUSEEVENTTYPE_DOWN;
        mouseEventBuffer[mouseEventBufferCount].x = E.button.x;
        mouseEventBuffer[mouseEventBufferCount].y = E.button.y;
        switch(E.button.button)
        {
        case SDL_BUTTON_MIDDLE: mouseEventBuffer[mouseEventBufferCount].button = MOUSEBUTTON_MIDDLE; break;
        case SDL_BUTTON_RIGHT:  mouseEventBuffer[mouseEventBufferCount].button = MOUSEBUTTON_RIGHT; break;
        case SDL_BUTTON_LEFT:   
        default:                mouseEventBuffer[mouseEventBufferCount].button = MOUSEBUTTON_LEFT; break;
        }
        mouseEventBufferCount++;
      }
      else if (E.type == SDL_MOUSEBUTTONUP)
      {
        mouseEventBuffer[mouseEventBufferCount].eventType = MOUSEEVENTTYPE_UP;
Gargaj's avatar
Gargaj committed
292
293
294
295
        mouseEventBuffer[mouseEventBufferCount].x = E.button.x;
        mouseEventBuffer[mouseEventBufferCount].y = E.button.y;
        switch(E.button.button)
        {
296
297
298
299
        case SDL_BUTTON_MIDDLE: mouseEventBuffer[mouseEventBufferCount].button = MOUSEBUTTON_MIDDLE; break;
        case SDL_BUTTON_RIGHT:  mouseEventBuffer[mouseEventBufferCount].button = MOUSEBUTTON_RIGHT; break;
        case SDL_BUTTON_LEFT:   
        default:                mouseEventBuffer[mouseEventBufferCount].button = MOUSEBUTTON_LEFT; break;
Gargaj's avatar
Gargaj committed
300
301
        }
        mouseEventBufferCount++;
Gargaj's avatar
add SDL    
Gargaj committed
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
      }
    }
    glClearColor(0.08f, 0.18f, 0.18f, 1.0f);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
  }
  void EndFrame()
  {
    SDL_GL_SwapBuffers();
  }
  bool WantsToQuit()
  {
    return !run;
  }
  void Close()
  {
    SDL_Quit();
  }
Gargaj's avatar
Gargaj committed
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391

  void RenderFullscreenQuad()
  {
    glUseProgram(theShader);
    glColor4f(0.0f, 1.0f, 0.0f, 1.0f);
    glBegin(GL_QUADS);
      glTexCoord2f(0.0f, 0.0f); glVertex2f(-1.00f, -1.00f);
      glTexCoord2f(1.0f, 0.0f); glVertex2f( 1.00f, -1.00f);
      glTexCoord2f(1.0f, 1.0f); glVertex2f( 1.00f,  1.00f);
      glTexCoord2f(0.0f, 1.0f); glVertex2f(-1.00f,  1.00f);
    glEnd();
    glUseProgram(0);
  }

  bool ReloadShader( char * szShaderCode, int nShaderCodeSize, char * szErrorBuffer, int nErrorBufferSize )
  {
    GLuint prg = glCreateProgram();
    GLuint shd = glCreateShader(GL_FRAGMENT_SHADER);
    GLint size = 0;
    GLint result = 0;

    glShaderSource(shd, 1, (const GLchar**)&szShaderCode, &nShaderCodeSize);
    glCompileShader(shd);
    glGetShaderInfoLog(shd, nErrorBufferSize, &size, szErrorBuffer);
    glGetShaderiv(shd, GL_COMPILE_STATUS, &result);
    if (!result)
    {
      glDeleteProgram(prg);
      glDeleteShader(shd);
      return false;
    }

    glAttachShader(prg, shd);
    glLinkProgram(prg);
    glGetProgramInfoLog(prg, nErrorBufferSize - size, &size, szErrorBuffer + size);
    glGetProgramiv(prg, GL_LINK_STATUS, &result);
    if (!result)
    {
      glDeleteProgram(prg);
      glDeleteShader(shd);
      return false;
    }

    glDeleteProgram(theShader);

    theShader = prg;

    return true;
  }

  void SetShaderConstant( char * szConstName, float x )
  {
    GLint location = glGetUniformLocation( theShader, szConstName );
    if ( location != -1 )
    {
      glProgramUniform1fEXT( theShader, location, x );
    }
  }

  void SetShaderConstant( char * szConstName, float x, float y )
  {
    GLint location = glGetUniformLocation( theShader, szConstName );
    if ( location != -1 )
    {
      glProgramUniform2fEXT( theShader, location, x, y );
    }
  }

Gargaj's avatar
Gargaj committed
392
393
  struct GLTexture : public Texture
  {
394
    GLuint ID;
Gargaj's avatar
Gargaj committed
395
396
397
398
    int unit;
  };

  int textureUnit = 0;
Gargaj's avatar
Gargaj committed
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
  Texture * CreateRGBA8TextureFromFile( char * szFilename )
  {
    int comp = 0;
    int width = 0;
    int height = 0;
    unsigned char * c = stbi_load( szFilename, (int*)&width, (int*)&height, &comp, STBI_rgb_alpha );
    if (!c) return NULL;

    GLuint glTexId = 0;
    glGenTextures( 1, &glTexId );
    glBindTexture( GL_TEXTURE_2D, glTexId );

    glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );
    glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );
    glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
    glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );

    GLenum internalFormat = GL_SRGB8_ALPHA8;
    GLenum srcFormat = GL_RGBA;

    glTexImage2D( GL_TEXTURE_2D, 0, internalFormat, width, height, 0, srcFormat, GL_UNSIGNED_BYTE, c );

    stbi_image_free(c);

Gargaj's avatar
Gargaj committed
423
    GLTexture * tex = new GLTexture();
Gargaj's avatar
Gargaj committed
424
425
    tex->width = width;
    tex->height = height;
426
    tex->ID = glTexId;
Gargaj's avatar
Gargaj committed
427
    tex->type = TEXTURETYPE_2D;
Gargaj's avatar
Gargaj committed
428
    tex->unit = textureUnit++;
Gargaj's avatar
Gargaj committed
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
    return tex;
  }

  Texture * Create1DR32Texture( int w )
  {
    GLuint glTexId = 0;
    glGenTextures( 1, &glTexId );
    glBindTexture( GL_TEXTURE_1D, glTexId );

    glTexParameteri( GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_REPEAT );
    glTexParameteri( GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
    glTexParameteri( GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );

    float * data = new float[w];
    for ( int i = 0; i < w; ++i )
      data[i] = 1.0f;

    glTexImage1D( GL_TEXTURE_1D, 0, GL_R32F, w, 0, GL_RED, GL_FLOAT, data );

    delete[] data;

    glBindTexture( GL_TEXTURE_1D, 0 );

Gargaj's avatar
Gargaj committed
452
    GLTexture * tex = new GLTexture();
Gargaj's avatar
Gargaj committed
453
454
    tex->width = w;
    tex->height = 1;
455
    tex->ID = glTexId;
Gargaj's avatar
Gargaj committed
456
    tex->type = TEXTURETYPE_1D;
Gargaj's avatar
Gargaj committed
457
    tex->unit = textureUnit++;
Gargaj's avatar
Gargaj committed
458
459
460
461
462
    return tex;
  }

  void SetShaderTexture( char * szTextureName, Texture * tex )
  {
463
464
465
    if (!tex)
      return;

Gargaj's avatar
Gargaj committed
466
467
468
    GLint location = glGetUniformLocation( theShader, szTextureName );
    if ( location != -1 )
    {
Gargaj's avatar
Gargaj committed
469
470
      glProgramUniform1iEXT( theShader, location, ((GLTexture*)tex)->unit );
      glActiveTexture( GL_TEXTURE0 + ((GLTexture*)tex)->unit );
Gargaj's avatar
Gargaj committed
471
472
      switch( tex->type)
      {
473
474
        case TEXTURETYPE_1D: glBindTexture( GL_TEXTURE_1D, ((GLTexture*)tex)->ID ); break;
        case TEXTURETYPE_2D: glBindTexture( GL_TEXTURE_2D, ((GLTexture*)tex)->ID ); break;
Gargaj's avatar
Gargaj committed
475
476
477
478
479
480
      }
    }
  }

  bool UpdateR32Texture( Texture * tex, float * data )
  {
Gargaj's avatar
Gargaj committed
481
    glActiveTexture( GL_TEXTURE0 + ((GLTexture*)tex)->unit );
482
    glBindTexture( GL_TEXTURE_1D, ((GLTexture*)tex)->ID );
Gargaj's avatar
Gargaj committed
483
484
485
486
487
    glTexSubImage1D( GL_TEXTURE_1D, 0, 0, tex->width, GL_RED, GL_FLOAT, data );

    return true;
  }

Gargaj's avatar
Gargaj committed
488
  void StartTextRendering()
489
490
491
492
493
494
495
496
497
498
499
500
501
  {
    glUseProgram(0);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();

    glOrtho(0, nWidth, 0, nHeight, 0, 500);
    glTranslatef(0, nHeight, 0);
    glScalef(1, -1, 1);

    glMatrixMode(GL_MODELVIEW);

    glPushAttrib(GL_ALL_ATTRIB_BITS);

Gargaj's avatar
Gargaj committed
502
503
504
505
506
507
    for (int i=0; i<8; i++)
    {
      glActiveTexture( GL_TEXTURE0 + i );
      glBindTexture(GL_TEXTURE_2D, NULL);
    }
    glActiveTexture( GL_TEXTURE0 );
508
    glEnable(GL_TEXTURE_2D);
Gargaj's avatar
Gargaj committed
509

510
511
512
    glEnable(GL_BLEND);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  }
Gargaj's avatar
Gargaj committed
513
514
515
516
517
518
519
520
521
522
523
524
525
  void SetTextRenderingViewport( Scintilla::PRectangle rect )
  {
    glEnable(GL_SCISSOR_TEST);
    glScissor( rect.left, nHeight - rect.bottom, rect.right - rect.left, rect.bottom - rect.top );
    glLoadIdentity();
    glTranslatef( rect.left, rect.top, 0.0 );
  }
  void EndTextRendering()
  {
    glPopAttrib();
    glDisable(GL_SCISSOR_TEST);
    glDisable(GL_BLEND);
  }
526

Gargaj's avatar
Gargaj committed
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
  Texture * CreateA8TextureFromData( int w, int h, unsigned char * data )
  {
    GLuint glTexId = 0;
    glGenTextures(1, &glTexId);
    glBindTexture(GL_TEXTURE_2D, glTexId);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, w, h, 0, GL_ALPHA, GL_UNSIGNED_BYTE, data);
    
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);

    GLTexture * tex = new GLTexture();
    tex->width = w;
    tex->height = h;
    tex->ID = glTexId;
    tex->type = TEXTURETYPE_2D;
    tex->unit = 0; // this is always 0 cos we're not using shaders here
    return tex;
  }

  void ReleaseTexture( Texture * tex )
  {
    glDeleteTextures(1, &((GLTexture*)tex)->ID );
  }

  void BindTexture( Texture * tex )
  {
Gargaj's avatar
Gargaj committed
552
553
    if (tex) glEnable(GL_TEXTURE_2D);
    else glDisable(GL_TEXTURE_2D);
554
    glBindTexture(GL_TEXTURE_2D, tex ? ((GLTexture*)tex)->ID : NULL );
Gargaj's avatar
Gargaj committed
555
556
  }

557
  void RenderQuad( const Vertex & a, const Vertex & b, const Vertex & c, const Vertex & d )
Gargaj's avatar
Gargaj committed
558
559
560
561
562
563
564
565
566
  {
    glBegin(GL_QUADS);
    glColor4ubv( (GLubyte*)&a.c ); glTexCoord2f( a.u, a.v ); glVertex2f( a.x, a.y );
    glColor4ubv( (GLubyte*)&b.c ); glTexCoord2f( b.u, b.v ); glVertex2f( b.x, b.y );
    glColor4ubv( (GLubyte*)&c.c ); glTexCoord2f( c.u, c.v ); glVertex2f( c.x, c.y );
    glColor4ubv( (GLubyte*)&d.c ); glTexCoord2f( d.u, d.v ); glVertex2f( d.x, d.y );
    glEnd();
  }

567
  void RenderLine( const Vertex & a, const Vertex & b )
Gargaj's avatar
Gargaj committed
568
569
570
571
572
573
574
  {
    glBegin(GL_LINES);
    glColor4ubv( (GLubyte*)&a.c ); glTexCoord2f( a.u, a.v ); glVertex2f( a.x, a.y );
    glColor4ubv( (GLubyte*)&b.c ); glTexCoord2f( b.u, b.v ); glVertex2f( b.x, b.y );
    glEnd();
  }

575
}