scg3  0.6
scg_internals.cpp
Go to the documentation of this file.
1 
8 /*
9  * Copyright 2014 Volker Ahlers
10  *
11  * Licensed under the Apache License, Version 2.0 (the "License");
12  * you may not use this file except in compliance with the License.
13  * You may obtain a copy of the License at
14  *
15  * http://www.apache.org/licenses/LICENSE-2.0
16  *
17  * Unless required by applicable law or agreed to in writing, software
18  * distributed under the License is distributed on an "AS IS" BASIS,
19  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20  * See the License for the specific language governing permissions and
21  * limitations under the License.
22  */
23 
24 #include <cassert>
25 #include <sstream>
26 #include "scg_internals.h"
27 #include "scg_utilities.h"
28 
29 namespace scg {
30 
31 
32 const OGLAttrib OGLConstants::VERTEX = { "vVertex", 0 };
33 const OGLAttrib OGLConstants::COLOR = { "vColor", 1 };
34 const OGLAttrib OGLConstants::NORMAL = { "vNormal", 2 };
35 const OGLAttrib OGLConstants::TEX_COORD_0 = { "vTexCoord0", 3 };
36 const OGLAttrib OGLConstants::TEX_COORD_1 = { "vTexCoord1", 4 };
37 const OGLAttrib OGLConstants::TANGENT = { "vTangent", 5 };
38 const OGLAttrib OGLConstants::BINORMAL = { "vBinormal", 6 };
39 
40 const OGLFragData OGLConstants::FRAG_COLOR = { "fragColor", 0 };
41 
42 const OGLUniformBlock OGLConstants::LIGHT = { "LightBlock", 0 };
43 const OGLUniformBlock OGLConstants::MATERIAL = { "MaterialBlock", 1 };
44 
45 const char* OGLConstants::MODEL_VIEW_MATRIX = "modelViewMatrix";
46 const char* OGLConstants::PROJECTION_MATRIX = "projectionMatrix";
47 const char* OGLConstants::MVP_MATRIX = "mvpMatrix";
48 const char* OGLConstants::NORMAL_MATRIX = "normalMatrix";
49 const char* OGLConstants::TEXTURE_MATRIX = "textureMatrix";
50 const char* OGLConstants::COLOR_MATRIX = "colorMatrix";
51 const char* OGLConstants::N_LIGHTS = "nLights";
52 const char* OGLConstants::GLOBAL_AMBIENT_LIGHT = "globalAmbientLight";
53 const char* OGLConstants::TIME = "time";
54 
55 const OGLSampler OGLConstants::TEXTURE0 = { "texture0", 0 };
56 const OGLSampler OGLConstants::TEXTURE1 = { "texture1", 1 };
57 
58 
60  glBindAttribLocation(program, VERTEX.location, VERTEX.name);
61  glBindAttribLocation(program, COLOR.location, COLOR.name);
62  glBindAttribLocation(program, NORMAL.location, NORMAL.name);
63  glBindAttribLocation(program, TEX_COORD_0.location, TEX_COORD_0.name);
64  glBindAttribLocation(program, TEX_COORD_1.location, TEX_COORD_1.name);
65  glBindAttribLocation(program, TANGENT.location, TANGENT.name);
66  glBindAttribLocation(program, BINORMAL.location, BINORMAL.name);
67 
68  glBindFragDataLocation(program, FRAG_COLOR.location, FRAG_COLOR.name);
69 
70  assert(!checkGLError());
71 }
72 
73 
74 void OGLConstants::bindUniformBlocks(GLuint program) {
75  GLuint lightIndex = glGetUniformBlockIndex(program, LIGHT.name);
76  if (lightIndex != GL_INVALID_INDEX) {
77  glUniformBlockBinding(program, lightIndex, LIGHT.bindingPoint);
78  }
79  GLuint materialIndex = glGetUniformBlockIndex(program, MATERIAL.name);
80  if (materialIndex != GL_INVALID_INDEX) {
81  glUniformBlockBinding(program, materialIndex, MATERIAL.bindingPoint);
82  }
83 
84  assert(!checkGLError());
85 }
86 
87 
88 void OGLConstants::bindSamplers(GLuint program) {
89  // glProgramUniform() is not used in order to keep OpenGL 3.2 compatibility
90  SCG_SAVE_AND_SWITCH_PROGRAM(program, programOld);
91  glUniform1i(glGetUniformLocation(program, TEXTURE0.name), TEXTURE0.texUnit);
92  glUniform1i(glGetUniformLocation(program, TEXTURE1.name), TEXTURE1.texUnit);
93  SCG_RESTORE_PROGRAM(program, programOld);
94 
95  assert(!checkGLError());
96 }
97 
98 
99 }
static const char * PROJECTION_MATRIX
static const OGLFragData FRAG_COLOR
static const OGLAttrib VERTEX
static const OGLAttrib TANGENT
static const OGLAttrib COLOR
static const char * COLOR_MATRIX
static const char * GLOBAL_AMBIENT_LIGHT
static const OGLUniformBlock MATERIAL
static void bindSamplers(GLuint program)
const GLchar * name
static const OGLSampler TEXTURE1
const GLchar * name
const GLchar * name
Utility functions that are used by different classes.
static const char * N_LIGHTS
static const char * TEXTURE_MATRIX
int checkGLError()
Check if an OpenGL error has occured since last call of checkGLError().
static const char * MODEL_VIEW_MATRIX
static const OGLAttrib BINORMAL
static void bindAttribFragDataLocations(GLuint program)
#define SCG_SAVE_AND_SWITCH_PROGRAM(_program, _programOld)
#define SCG_RESTORE_PROGRAM(_program, _programOld)
static const OGLAttrib TEX_COORD_0
static void bindUniformBlocks(GLuint program)
static const OGLAttrib NORMAL
static const OGLUniformBlock LIGHT
Definition: Animation.h:28
const GLchar * name
static const OGLSampler TEXTURE0
static const char * TIME
Internal definitions required by most classes.
static const char * NORMAL_MATRIX
static const char * MVP_MATRIX
static const OGLAttrib TEX_COORD_1