Esempio n. 1
0
	// XXX Need a way to deal with blits with Camera/Lighting.
	void DisplayDeviceOpenGL::doBlitTexture(const TexturePtr& tex, int dstx, int dsty, int dstw, int dsth, float rotation, int srcx, int srcy, int srcw, int srch)
	{
		ASSERT_LOG(false, "DisplayDevice::doBlitTexture deprecated");
		ASSERT_LOG(!tex, "Texture passed in was not of expected type.");

		const float tx1 = float(srcx) / tex->width();
		const float ty1 = float(srcy) / tex->height();
		const float tx2 = srcw == 0 ? 1.0f : float(srcx + srcw) / tex->width();
		const float ty2 = srch == 0 ? 1.0f : float(srcy + srch) / tex->height();
		const float uv_coords[] = {
			tx1, ty1,
			tx2, ty1,
			tx1, ty2,
			tx2, ty2,
		};

		const float vx1 = float(dstx);
		const float vy1 = float(dsty);
		const float vx2 = float(dstx + dstw);
		const float vy2 = float(dsty + dsth);
		const float vtx_coords[] = {
			vx1, vy1,
			vx2, vy1,
			vx1, vy2,
			vx2, vy2,
		};

		// Apply blend mode from texture if there is any.
		BlendEquationScopeOGL be_scope(*tex);
		BlendModeScopeOGL bm_scope(*tex);

		glm::mat4 model = glm::translate(glm::mat4(1.0f), glm::vec3((vx1+vx2)/2.0f,(vy1+vy2)/2.0f,0.0f)) * glm::rotate(glm::mat4(1.0f), rotation, glm::vec3(0.0f,0.0f,1.0f)) * glm::translate(glm::mat4(1.0f), glm::vec3(-(vx1+vy1)/2.0f,-(vy1+vy1)/2.0f,0.0f));
		glm::mat4 mvp = glm::ortho(0.0f, 800.0f, 600.0f, 0.0f) * model;
		auto shader = OpenGL::ShaderProgram::defaultSystemShader();
		shader->makeActive();
		getDefaultShader()->setUniformsForTexture(tex);

		shader->setUniformValue(shader->getMvpUniform(), glm::value_ptr(mvp));
		shader->setUniformValue(shader->getColorUniform(), glm::value_ptr(glm::vec4(1.0f,1.0f,1.0f,1.0f)));
		// XXX the following line are only temporary, obviously.
		//shader->setUniformValue(shader->getUniform("discard"), 0);
		glEnableVertexAttribArray(shader->getVertexAttribute());
		glVertexAttribPointer(shader->getVertexAttribute(), 2, GL_FLOAT, GL_FALSE, 0, vtx_coords);
		glEnableVertexAttribArray(shader->getTexcoordAttribute());
		glVertexAttribPointer(shader->getTexcoordAttribute(), 2, GL_FLOAT, GL_FALSE, 0, uv_coords);

		glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);

		glDisableVertexAttribArray(shader->getTexcoordAttribute());
		glDisableVertexAttribArray(shader->getVertexAttribute());
	}
Esempio n. 2
0
/**
  * Set the color for ambient and diffuse lighting (or no lighting).
  * Alternatively use a color array and color attribute.
  */
void GLESRenderer::setAmbientAndDiffuseColor(const GLColorRGBA newVal)
{
    ambientAndDiffuseColor = newVal;
    if(bound && location_uAmbientAndDiffuseColor != -1)
            setUniformValue(location_uAmbientAndDiffuseColor,
                            ambientAndDiffuseColor.red(), ambientAndDiffuseColor.green(),
                            ambientAndDiffuseColor.blue(), ambientAndDiffuseColor.alpha());
}
Esempio n. 3
0
/**
  * Set the color for specular lighting.
  */
void GLESRenderer::setSpecularColor(const GLColorRGBA newVal)
{
    specularColor = newVal;
    if(bound && (location_uSpecularColor != -1))
            setUniformValue(location_uSpecularColor,
                            specularColor.red(), specularColor.green(),
                            specularColor.blue(), specularColor.alpha());
}
 void setWorldMatrix(GLfloat m[4][4]) {
     static bool init = true;
     if (init || memcmp(m, worldMatrix, sizeof(worldMatrix))) {
         setUniformValue("matWorld", m);
         memcpy(worldMatrix, m, sizeof(worldMatrix));
         init = false;
     }
 }
Esempio n. 5
0
void Z3DShaderProgram::bindTexture(const QString &name, GLenum target, GLuint textureId)
{
  m_textureUnitManager.nextAvailableUnit();
  m_textureUnitManager.activateCurrentUnit();
  glBindTexture(target, textureId);
  setUniformValue(name, m_textureUnitManager.getCurrentUnitNumber());
  glActiveTexture(GL_TEXTURE0);
  CHECK_GL_ERROR;
}
void PainterShaderProgram::setTextureMatrix(const Matrix3& textureMatrix)
{
    if(textureMatrix == m_textureMatrix)
        return;

    bind();
    setUniformValue(TEXTURE_MATRIX_UNIFORM, textureMatrix);
    m_textureMatrix = textureMatrix;
}
void PainterShaderProgram::setProjectionMatrix(const Matrix3& projectionMatrix)
{
    if(projectionMatrix == m_projectionMatrix)
        return;

    bind();
    setUniformValue(PROJECTION_MATRIX_UNIFORM, projectionMatrix);
    m_projectionMatrix = projectionMatrix;
}
void PainterShaderProgram::setTransformMatrix(const Matrix3& transformMatrix)
{
    if(transformMatrix == m_transformMatrix)
        return;

    bind();
    setUniformValue(TRANSFORM_MATRIX_UNIFORM, transformMatrix);
    m_transformMatrix = transformMatrix;
}
void PainterShaderProgram::setResolution(const Size& resolution)
{
    if(m_resolution == resolution)
        return;

    bind();
    setUniformValue(RESOLUTION_UNIFORM, (float)resolution.width(), (float)resolution.height());
    m_resolution = resolution;
}
void PainterShaderProgram::setOpacity(float opacity)
{
    if(m_opacity == opacity)
        return;

    bind();
    setUniformValue(OPACITY_UNIFORM, opacity);
    m_opacity = opacity;
}
Esempio n. 11
0
/**
  * Set modelview matrix. Updates mvpMatrix and normalMatrix too.
  * Call setPMatrix first.
  */
void GLESRenderer::setMvMatrix(const QMatrix4x4 newVal)
{
    mvMatrix = newVal;
    normalMatrix = mvMatrix.normalMatrix(); //invert and transpose mvMatrix
    invertedMvpMatrixValid = false; //delay matrix inversion until it is really neccessary

    if(bound && (location_uNormalMatrix != -1))
       setUniformValue(location_uNormalMatrix, normalMatrix);

    mvpMatrix = pMatrix * mvMatrix;
    if(bound && (location_uMvpMatrix != -1))
          setUniformValue(location_uMvpMatrix, mvpMatrix);
#ifdef DEBUG_GLESRENDERER
    ShaderDebugger::debugMatrix4x4(mvMatrix, "GLESRenderer uses modelview matrix:");
    ShaderDebugger::debugMatrix3x3(normalMatrix, "GLESRenderer uses normal matrix:");
    ShaderDebugger::debugMatrix4x4(mvpMatrix, "GLESRenderer uses  MVP matrix:");
#endif
}
void PainterShaderProgram::setColor(const Color& color)
{
    if(color == m_color)
        return;

    bind();
    setUniformValue(COLOR_UNIFORM, color);
    m_color = color;
}
Esempio n. 13
0
/**
  * Set light direction.
  */
void GLESRenderer::setLightDirection(const QVector3D & newVal)
{
    lightDirection = newVal;
#ifdef DEBUG_GLESRENDERER
    ShaderDebugger::debugVector3D(lightDirection, "GLESRenderer uses lightDirection in object space:");
#endif
    QMatrix4x4 nMatrix = QMatrix4x4(normalMatrix);
    lightDirection = (nMatrix * lightDirection);//transform to eye space
    halfPlaneVector = (lightDirection + QVector3D(0.0,0.0,1.0)).normalized();//eye direction is 0,0,1 in eye space
#ifdef DEBUG_GLESRENDERER
    ShaderDebugger::debugVector3D(lightDirection, "GLESRenderer uses lightDirection in eye space:");
    ShaderDebugger::debugVector3D(lightDirection, "GLESRenderer uses halfplane vector in eye space:");
#endif
    if(bound && (location_uLightDirection != -1))
          setUniformValue(location_uLightDirection, lightDirection);
    if(location_uHalfPlaneVector != -1)
      setUniformValue(location_uHalfPlaneVector, halfPlaneVector);
}
void PainterShaderProgram::updateTime()
{
    float time = g_clock.seconds() - m_startTime;
    if(m_time == time)
        return;

    bind();
    setUniformValue(TIME_UNIFORM, time);
    m_time = time;
}
Esempio n. 15
0
void Z3DShaderProgram::bindTexture(const QString &name, const Z3DTexture *texture)
{
  if (texture) {
    m_textureUnitManager.nextAvailableUnit();
    m_textureUnitManager.activateCurrentUnit();
    texture->bind();
    setUniformValue(name, m_textureUnitManager.getCurrentUnitNumber());
    glActiveTexture(GL_TEXTURE0);
    CHECK_GL_ERROR;
  }
}
Esempio n. 16
0
void ScalarDisplay::Bind (GLfloat minValue, GLfloat maxValue,
			  StatisticsType::Enum displayType, 
                          AverageCountType::Enum countType, GLfloat globalCount)
{
    ShaderProgram::Bind ();
    setUniformValue (m_displayTypeLocation, displayType);
    setUniformValue (m_countTypeLocation, countType);
    setUniformValue (m_globalCountLocation, globalCount);
    setUniformValue (m_minValueLocation, minValue);
    setUniformValue (m_maxValueLocation, maxValue);
    setUniformValue (m_colorBarTexUnitLocation, GetColorMapTexUnit ());
    setUniformValue (m_scalarAverageTexUnitLocation, 
                     GetScalarAverageTexUnit ());
}
Esempio n. 17
0
/**
  * Set size of points drawn with GL_POINTS.
  */
void GLESRenderer::setPointSize(int newVal)
{
    pointSize = newVal;
    if(bound && (location_uPointSize != -1))
          setUniformValue(location_uPointSize, pointSize);
}
Esempio n. 18
0
/**
  * Set the texture flag.
  */
void GLESRenderer::setTextureEnabled(bool newVal)
{
    textureEnabled = newVal;
    if(bound && (location_uTextureEnabled != -1))
          setUniformValue(location_uTextureEnabled, textureEnabled);
}
		void setValue(const std::vector<T> &data, GLsizei count = 1) { setUniformValue(id, data, count); }
Esempio n. 20
0
/**
  * Enable / disable lighting.
  */
void GLESRenderer::setLightingEnabled(bool newVal)
{
    lightingEnabled = newVal;
    if(bound && (location_uLightingEnabled != -1))
          setUniformValue(location_uLightingEnabled, lightingEnabled);
}
Esempio n. 21
0
/**
  * Enable / disable color array.
  */
void GLESRenderer::setColorArrayEnabled(bool newVal)
{
    colorArrayEnabled = newVal;
    if(bound && (location_uColorArrayEnabled != -1))
          setUniformValue(location_uColorArrayEnabled, colorArrayEnabled);
}
		void setValue(const BaseMatrix<mattype, i, j> &data, GLsizei count = 1, GLboolean transpose = GL_FALSE) 
		{
			setUniformValue(id, data, count, transpose);
		}
Esempio n. 23
0
/**
  * Set the shininess for specular lighting.
  */
void GLESRenderer::setShininess(float newVal)
{
    shininess = newVal;
    if(bound && (location_uShininess != -1))
            setUniformValue(location_uShininess, shininess);
}
		void setValue(const T& data) { setUniformValue(id, data); }
Esempio n. 25
0
/**
  * Bind program and transfer attribute and uniform data to the shaders.
  * Calls initialize, if not already initialized.
  */
bool GLESRenderer::bind()
{
    bool ok = true;
    if(!initialized)
       ok = initialize();
    if(!ok)
        return false;

    renderProgram->bind();
    //Activate uniforms
    //flags
    if(location_uColorArrayEnabled != -1)
      setUniformValue(location_uColorArrayEnabled, colorArrayEnabled);
    if(location_uLightingEnabled != -1)
      setUniformValue(location_uLightingEnabled, lightingEnabled);
    if(location_uTextureEnabled != -1)
        setUniformValue(location_uTextureEnabled, textureEnabled);
    //matrices
    if( location_uNormalMatrix != -1)
       setUniformValue(location_uNormalMatrix, normalMatrix);
    if(location_uMvpMatrix != -1)
      setUniformValue(location_uMvpMatrix, mvpMatrix);
    //lighting
    if(location_uAmbientAndDiffuseColor != -1)
        setUniformValue(location_uAmbientAndDiffuseColor,
                        ambientAndDiffuseColor.red(), ambientAndDiffuseColor.green(),
                        ambientAndDiffuseColor.blue(), ambientAndDiffuseColor.alpha());
    if(location_uAmbientLightBrightness != -1)
                setUniformValue(location_uAmbientLightBrightness, ambientLightBrightness);
    if(location_uLightDirection != -1)
      setUniformValue(location_uLightDirection, lightDirection);
    if(location_uSpecularColor != -1)
        setUniformValue(location_uSpecularColor,
                        specularColor.red(), specularColor.green(),
                        specularColor.blue(), specularColor.alpha());
    if(location_uShininess != -1)
            setUniformValue(location_uShininess, shininess);
    if(location_uHalfPlaneVector != -1)
      setUniformValue(location_uHalfPlaneVector, halfPlaneVector);
    //texture
    if(location_uTextureSampler != -1)
        setUniformValue(location_uTextureSampler, 0); //set sampler to use texture unit 0
    //point size
    if(location_uPointSize != -1)
          setUniformValue(location_uPointSize, pointSize);
    bound = true;
    return bound;
}
 void setBlurStep(GLfloat b) {
     if (b != blurstep) {
         setUniformValue("blurstep", b);
         blurstep = b;
     }
 }
 void setOpacity(GLfloat o) {
     if (o != opacity) {
         setUniformValue("opacity", o);
         opacity = o;
     }
 }
 void setTexture(GLuint t) {
     if (t != texture) {
         setUniformValue("texture", t);
         texture = t;
     }
 }
Esempio n. 29
0
/**
  * Set the dimming factor for ambient light.
  * Defaults to 0.2.
  */
void GLESRenderer::setAmbientLightBrightness(float newVal)
{
    ambientLightBrightness = newVal;
    if(bound && location_uAmbientLightBrightness != -1)
            setUniformValue(location_uAmbientLightBrightness, ambientLightBrightness);
}
Esempio n. 30
0
	void DisplayDeviceOpenGL::render(const Renderable* r) const
	{
		if(!r->isEnabled()) {
			// Renderable item not enabled then early return.
			return;
		}

		StencilScopePtr stencil_scope;
		if(r->hasClipSettings()) {
			ModelManager2D mm(r->getPosition().x, r->getPosition().y);
			auto clip_shape = r->getStencilMask();
			bool cam_set = false;
			if(clip_shape->getCamera() == nullptr && r->getCamera() != nullptr) {
				cam_set = true;
				clip_shape->setCamera(r->getCamera());
			}
			stencil_scope.reset(new StencilScopeOGL(r->getStencilSettings()));
			glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
			glDepthMask(GL_FALSE);
			glClear(GL_STENCIL_BUFFER_BIT);
			render(clip_shape.get());
			stencil_scope->applyNewSettings(keep_stencil_settings);
			glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
			glDepthMask(GL_TRUE);

			if(cam_set) {
				clip_shape->setCamera(nullptr);
			}
		}

		auto shader = r->getShader();
		shader->makeActive();

		BlendEquationScopeOGL be_scope(*r);
		BlendModeScopeOGL bm_scope(*r);

		// apply lighting/depth check/depth write here.
		bool use_lighting = r->isLightingStateSet() ? r->useLighting() : false;

		// Set the depth enable.
		if(r->isDepthEnableStateSet()) {
			if(get_current_depth_enable() != r->isDepthEnabled()) {
				if(r->isDepthEnabled()) {
					glEnable(GL_DEPTH_TEST);
				} else {
					glDisable(GL_DEPTH_TEST);
				}
				get_current_depth_enable() = r->isDepthEnabled();
			}
		} else {
			// We assume that depth is disabled if not specified.
			if(get_current_depth_enable() == true) {
				glDisable(GL_DEPTH_TEST);
				get_current_depth_enable() = false;
			}
		}

		glm::mat4 pmat(1.0f);
		glm::mat4 vmat(1.0f);
		if(r->getCamera()) {
			// set camera here.
			pmat = r->getCamera()->getProjectionMat();
			vmat = r->getCamera()->getViewMat();
		} else if(get_default_camera() != nullptr) {
			pmat = get_default_camera()->getProjectionMat();
			vmat = get_default_camera()->getViewMat();
		}

		if(use_lighting) {
			for(auto lp : r->getLights()) {
				/// xxx need to set lights here.
			}
		}
		
		if(r->getRenderTarget()) {
			r->getRenderTarget()->apply();
		}

		if(shader->getPUniform() != ShaderProgram::INVALID_UNIFORM) {
			shader->setUniformValue(shader->getPUniform(), glm::value_ptr(pmat));
		}

		if(shader->getMvUniform() != ShaderProgram::INVALID_UNIFORM) {
			glm::mat4 mvmat = vmat;
			if(is_global_model_matrix_valid() && !r->ignoreGlobalModelMatrix()) {
				mvmat *= get_global_model_matrix() * r->getModelMatrix();
			} else {
				mvmat *= r->getModelMatrix();
			}
			shader->setUniformValue(shader->getMvUniform(), glm::value_ptr(mvmat));
		}

		if(shader->getMvpUniform() != ShaderProgram::INVALID_UNIFORM) {
			glm::mat4 pvmat(1.0f);
			if(is_global_model_matrix_valid() && !r->ignoreGlobalModelMatrix()) {
				pvmat = pmat * vmat * get_global_model_matrix() * r->getModelMatrix();
			} else {
				pvmat = pmat * vmat * r->getModelMatrix();
			}
			shader->setUniformValue(shader->getMvpUniform(), glm::value_ptr(pvmat));
		}

		if(shader->getColorUniform() != ShaderProgram::INVALID_UNIFORM) {
			if(r->isColorSet()) {
				shader->setUniformValue(shader->getColorUniform(), r->getColor().asFloatVector());
			} else {
				shader->setUniformValue(shader->getColorUniform(), ColorScope::getCurrentColor().asFloatVector());
			}
		}

		shader->setUniformsForTexture(r->getTexture());

		// XXX we should make this either or with setting the mvp/color uniforms above.
		auto uniform_draw_fn = shader->getUniformDrawFunction();
		if(uniform_draw_fn) {
			uniform_draw_fn(shader);
		}

		// Loop through uniform render variables and set them.
		/*for(auto& urv : r->UniformRenderVariables()) {
			for(auto& rvd : urv->VariableDescritionList()) {
				auto rvdd = std::dynamic_pointer_cast<RenderVariableDeviceData>(rvd->GetDisplayData());
				ASSERT_LOG(rvdd != nullptr, "Unable to cast DeviceData to RenderVariableDeviceData.");
				shader->SetUniformValue(rvdd->GetActiveMapIterator(), urv->Value());
			}
		}*/

		// Need to figure the interaction with shaders.
		/// XXX Need to create a mapping between attributes and the index value below.
		for(auto as : r->getAttributeSet()) {
			//ASSERT_LOG(as->getCount() > 0, "No (or negative) number of vertices in attribute set. " << as->getCount());
			if((!as->isMultiDrawEnabled() && as->getCount() <= 0) || (as->isMultiDrawEnabled() && as->getMultiDrawCount() <= 0)) {
				//LOG_WARN("No (or negative) number of vertices in attribute set. " << as->getCount());
				continue;
			}
			GLenum draw_mode = convert_drawing_mode(as->getDrawMode());

			// apply blend, if any, from attribute set.
			BlendEquationScopeOGL be_scope(*as);
			BlendModeScopeOGL bm_scope(*as);

			if(shader->getColorUniform() != ShaderProgram::INVALID_UNIFORM && as->isColorSet()) {
				shader->setUniformValue(shader->getColorUniform(), as->getColor().asFloatVector());
			}

			for(auto& attr : as->getAttributes()) {
				if(attr->isEnabled()) {
					shader->applyAttribute(attr);
				}
			}

			if(as->isInstanced()) {
				if(as->isIndexed()) {
					as->bindIndex();
					// XXX as->GetIndexArray() should be as->GetIndexArray()+as->GetOffset()
					glDrawElementsInstanced(draw_mode, static_cast<GLsizei>(as->getCount()), convert_index_type(as->getIndexType()), as->getIndexArray(), as->getInstanceCount());
					as->unbindIndex();
				} else {
					glDrawArraysInstanced(draw_mode, static_cast<GLint>(as->getOffset()), static_cast<GLsizei>(as->getCount()), as->getInstanceCount());
				}
			} else {
				if(as->isIndexed()) {
					as->bindIndex();
					// XXX as->GetIndexArray() should be as->GetIndexArray()+as->GetOffset()
					glDrawElements(draw_mode, static_cast<GLsizei>(as->getCount()), convert_index_type(as->getIndexType()), as->getIndexArray());
					as->unbindIndex();
				} else {
					if(as->isMultiDrawEnabled()) {
						glMultiDrawArrays(draw_mode, as->getMultiOffsetArray().data(), as->getMultiCountArray().data(), as->getMultiDrawCount());
					} else {
						glDrawArrays(draw_mode, static_cast<GLint>(as->getOffset()), static_cast<GLsizei>(as->getCount()));
					}
				}
			}

			shader->cleanUpAfterDraw();
			glBindBuffer(GL_ARRAY_BUFFER, 0);
		}

		if(r->getRenderTarget()) {
			r->getRenderTarget()->unapply();
		}
	}