Working on porting to mac compatible shaders

This commit is contained in:
Brad Davis 2015-02-05 12:18:58 -08:00
parent 230d761ec3
commit dc1cc84c1c
4 changed files with 21 additions and 29 deletions

View file

@ -41,16 +41,6 @@
#include "sdf_text_vert.h"
#include "sdf_text_frag.h"
namespace Shaders {
// Normally we could use 'enum class' to avoid namespace pollution,
// but we want easy conversion to GLuint
namespace Attributes {
enum {
Position = 0, TexCoord = 1,
};
}
}
// Helper functions for reading binary data from an IO device
template<class T>
void readStream(QIODevice & in, T & t) {
@ -290,7 +280,7 @@ QRectF Glyph::textureBounds(const glm::vec2 & textureSize) const {
void Font::setupGL() {
_texture = TexturePtr(
new QOpenGLTexture(_image, QOpenGLTexture::DontGenerateMipMaps));
new QOpenGLTexture(_image, QOpenGLTexture::GenerateMipMaps));
_program = ProgramPtr(new QOpenGLShaderProgram());
if (!_program->create()) {
qFatal("Could not create text shader");
@ -334,7 +324,6 @@ void Font::setupGL() {
_vertices->bind();
_vertices->allocate(&vertexData[0],
sizeof(TextureVertex) * vertexData.size());
_indices = BufferPtr(new QOpenGLBuffer(QOpenGLBuffer::IndexBuffer));
_indices->create();
_indices->bind();
@ -342,13 +331,12 @@ void Font::setupGL() {
GLsizei stride = (GLsizei) sizeof(TextureVertex);
void* offset = (void*) offsetof(TextureVertex, tex);
glEnableVertexAttribArray(Shaders::Attributes::Position);
glVertexAttribPointer(Shaders::Attributes::Position, 3, GL_FLOAT, false,
stride, nullptr);
glEnableVertexAttribArray(Shaders::Attributes::TexCoord);
glVertexAttribPointer(Shaders::Attributes::TexCoord, 2, GL_FLOAT, false,
stride, offset);
int posLoc = _program->attributeLocation("Position");
int texLoc = _program->attributeLocation("TexCoord");
glEnableVertexAttribArray(posLoc);
glVertexAttribPointer(posLoc, 3, GL_FLOAT, false, stride, nullptr);
glEnableVertexAttribArray(texLoc);
glVertexAttribPointer(texLoc, 2, GL_FLOAT, false, stride, offset);
_vao->release();
}
@ -424,6 +412,8 @@ glm::vec2 Font::drawString(float x, float y, const QString & str,
if (effectType == TextRenderer::OUTLINE_EFFECT) {
_program->setUniformValue("Outline", true);
}
// Needed?
glEnable(GL_TEXTURE_2D);
_texture->bind();
_vao->bind();
@ -481,6 +471,8 @@ glm::vec2 Font::drawString(float x, float y, const QString & str,
_vao->release();
_program->release();
// FIXME, needed?
// glDisable(GL_TEXTURE_2D);
return advance;
}

View file

@ -11,8 +11,8 @@
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
uniform sampler2D Font;
uniform vec4 Color = vec4(1);
uniform bool Outline = false;
uniform vec4 Color;
uniform bool Outline;
varying vec2 vTexCoord;
@ -23,7 +23,7 @@ const float outlineExpansion = 0.2;
void main() {
// retrieve signed distance
float sdf = texture(Font, vTexCoord).r;
float sdf = texture2D(Font, vTexCoord).r;
if (Outline) {
if (sdf > interiorCutoff) {
sdf = 1.0 - sdf;

View file

@ -10,15 +10,15 @@
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
//
uniform mat4 Projection = mat4(1);
uniform mat4 ModelView = mat4(1);
uniform mat4 Projection;
uniform mat4 ModelView;
layout(location = 0) in vec3 Position;
layout(location = 1) in vec2 TexCoord;
attribute vec3 Position;
attribute vec2 TexCoord;
out vec2 vTexCoord;
varying vec2 vTexCoord;
void main() {
vTexCoord = TexCoord;
gl_Position = Projection * ModelView * vec4(Position, 1);
gl_Position = Projection * ModelView * vec4(Position, 1.0);
}

View file

@ -160,7 +160,7 @@ void QTestWindow::draw() {
// Draw the text itself
_textRenderer[i]->draw(offsets[i].x, offsets[i].y, str,
glm::vec4(COLORS[i], 1.0f), glm::vec2(200, 200));
glm::vec4(COLORS[i], 1.0f));
}
_context->swapBuffers(this);