10 #include <glm/glm.hpp> 17 std::string BatchGL::vertexSource =
22 "uniform mat4 projection;" 28 "out vec4 vertexColor;" 29 "out vec2 textureCoord;" 32 " vertexColor = color;" 33 " textureCoord = texCoord;" 34 " mat4 mvp = projection * view * model;" 35 " gl_Position = mvp * vec4(position, 1.0f);" 38 std::string BatchGL::fragmentSource =
41 "uniform sampler2D texImage;" 43 "in vec4 vertexColor;" 44 "in vec2 textureCoord;" 49 " vec4 textureColor = texture(texImage, textureCoord);" 50 " fragColor = vertexColor * textureColor;" 53 Texture* BatchGL::defaultTexture;
55 ShaderProgram* BatchGL::defaultShaderProgram;
57 BatchGL::BatchGL(
int capacity) :
Batch() {
60 BatchGL::defaultTexture =
new Texture();
61 unsigned char data[] = {0xFF, 0xFF, 0xFF, 0xFF};
62 BatchGL::defaultTexture->bind();
63 BatchGL::defaultTexture->uploadImage(1, 1, data);
91 vertexShader.
source(BatchGL::vertexSource);
101 fragmentShader.source(BatchGL::fragmentSource);
102 fragmentShader.compile();
105 if (!fragmentShader.getCompileStatus()) {
111 shaderProgram->attach(&vertexShader);
112 shaderProgram->attach(&fragmentShader);
113 shaderProgram->link();
116 if (!shaderProgram->getLinkStatus()) {
121 BatchGL::defaultShaderProgram = shaderProgram;
124 shaderProgram->use();
130 shaderProgram->bindFragmentDataLocation(0,
"fragColor");
133 shaderProgram->setUniform(
"texImage", 0);
150 delete shaderProgram;
177 vec4 lastColor =
colors.back();
186 BatchGL::defaultTexture->bind();
198 shaderProgram->
use();
279 return shaderProgram;
283 shaderProgram = program;
286 shaderProgram->
use();
Equivalent to GL_ARRAY_BUFFER.
void flush() override
Flushes the batch, could be used if the batch would overflow.
void specifyVertexAttributes()
Specifies the vertex attributes.
int getAttributeLocation(std::string name)
Gets the location of an attribute variable with specified name.
This class wraps an OpenGL shader object.
Primitive
This enum wraps Primitive types of OpenGL.
void color(float r, float g, float b)
Defines a new color with specified components.
void bind()
Binds the vertex array object.
Equivalent to GL_VERTEX_SHADER.
static void logError(std::string message)
Logs an error string to cerr.
void compile()
Compiles the shader object.
void source(std::string source)
Sets the source code of the shader object.
void pointAttribute(unsigned int index, int size, ValueType type, bool normalized, int stride, long offset)
Sets the vertex attribute pointer.
This class wraps an OpenGL vertex array object.
This class wraps an OpenGL shader program.
This class wraps an OpenGL buffer object.
Generic namespace for the SimpleGL framework.
std::vector< glm::vec3 > vertices
The storage for vertices.
bool drawing
Tells if the batch is drawing.
static void drawArrays(Primitive mode, int first, int count)
Renders vertex data with the current bound VBO.
This class wraps an OpenGL texture object.
void use()
Uses the shader program for the current rendering state.
void texCoord(float s, float t)
Defines a new texture coordinate with specified values.
void resetShaderProgram()
Resets to the default shader program.
Equivalent to GL_DYNAMIC_DRAW.
void normal(float nx, float ny, float nz)
Defines a new normal with specified values.
void begin() override
Starts drawing with TRIANGLES mode.
int numVertices
The counter for the number of vertices.
void bind(BufferTarget target)
Binds the buffer object with the specified target.
std::vector< glm::vec2 > texCoords
The storage for texture coordinates.
void uploadSubData(BufferTarget target, long offset, long size, float *data)
Uploads the specified data to the buffer object starting at the offset.
Equivalent to GL_FRAGMENT_SHADER.
This class defines methods for batch rendering.
void enableAttribute(unsigned int index)
Enables a vertex attribute.
Transformation transformation
The transformation for the batch.
void uploadData(BufferTarget target, long size, float *data, BufferUsage usage)
Uploads the specified data to the buffer object.
void applyTransform()
Applies the MVP transformation if necessary.
void setUniform(int location, int value)
Sets the value of an uniform variable.
Equivalent to GL_TRIANGLES.
std::vector< glm::vec4 > colors
The storage for colors.
bool getCompileStatus()
Checks the compile status of the shader object.
~BatchGL(void)
Destroys a batch.
void setShaderProgram(ShaderProgram *program)
Sets the current shader program of the batch.
std::string getInfoLog()
Gets the info log of the shader object.
std::vector< glm::vec3 > normals
The storage for normals.
ShaderProgram * getShaderProgram()
Gets the current shader program of the batch.