Hi,
I'm trying to render a fractal using a shader, and all I get is a black screen when I render the VertexArray.
I created a quad which covers the entire screen:
// set up vertex array
this->m_pQuad = new sf::VertexArray( sf::Quads, 4 );
(*this->m_pQuad)[0].position = sf::Vector2f( 0, 0 );
(*this->m_pQuad)[1].position = sf::Vector2f( static_cast<float>(window->getSize().x), 0.0 );
(*this->m_pQuad)[2].position = sf::Vector2f( static_cast<float>(window->getSize().x), static_cast<float>(window->getSize().y) );
(*this->m_pQuad)[3].position = sf::Vector2f( 0.0, static_cast<float>(window->getSize().y) );
And in my render routine, I do the following:
// set shader uniforms
this->m_pShader->setParameter( "maxX", maxX );
this->m_pShader->setParameter( "maxY", maxY );
this->m_pShader->setParameter( "minX", minX );
this->m_pShader->setParameter( "minY", minY );
this->m_pShader->setParameter( "maxIterations", maxIterations );
// render to window
window->draw( *this->m_pQuad, this->m_pShader );
The quad draws to the window if I change the last line to this:
// render to window
window->draw( *this->m_pQuad );
The vertex shader looks like this:
void main()
{
// transform the vertex position
gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
// transform the texture coordinates
gl_TexCoord[0] = gl_TextureMatrix[0] * gl_MultiTexCoord0;
// forward the vertex color
gl_FrontColor = gl_Color;
}
The fragment shader looks like this:
void main( void )
{
vec4 colour = vec4(0.4f, 0.8f 0.1f, 0.0);
gl_FragColor = colour;
}
Here is the entire class:
cFractalFrame.hpp// -------------------------------------------------------------------
// Handles drawing of the fractal
// -------------------------------------------------------------------
#ifndef _CFRACTALFRAME_HPP_
#define _CFRACTALFRAME_HPP_
// -------------------------------------------------------------------
// include files
#include <SFML/Graphics.hpp>
#include <SFML/Window.hpp>
namespace GPUFractalZoomer {
class cFractalFrame
{
public:
// factal types
enum FractalType
{
Mandelbrot
};
// constructor, destructor
cFractalFrame( sf::RenderWindow* window );
~cFractalFrame( void );
// load a fractal
void load( FractalType type );
// render fractal to window
void render( sf::RenderWindow* window );
private:
// use a quad to render the fractal
sf::VertexArray* m_pQuad;
// fragment program of the current fractal
sf::Shader* m_pShader;
// fractal parameters to be passed to the shader
float minX;
float maxX;
float minY;
float maxY;
float maxIterations;
};
} // namespace GPUFractalZoomer
#endif // _CFRACTALFRAME_HPP_
cFractalFrame.cpp// -------------------------------------------------------------------
// Handles drawing of the fractal
// -------------------------------------------------------------------
// -------------------------------------------------------------------
// include files
#include <cFractalFrame.hpp>
#include <cException.hpp>
#include <iostream>
namespace GPUFractalZoomer {
// -------------------------------------------------------------------
// constructor
cFractalFrame::cFractalFrame( sf::RenderWindow* window ) :
m_pQuad( NULL ),
m_pShader( NULL )
{
// debug
std::cout << "constructed cFractalFrame" << std::endl;
// set up vertex array
this->m_pQuad = new sf::VertexArray( sf::Quads, 4 );
(*this->m_pQuad)[0].position = sf::Vector2f( 0, 0 );
(*this->m_pQuad)[1].position = sf::Vector2f( static_cast<float>(window->getSize().x), 0.0 );
(*this->m_pQuad)[2].position = sf::Vector2f( static_cast<float>(window->getSize().x), static_cast<float>(window->getSize().y) );
(*this->m_pQuad)[3].position = sf::Vector2f( 0.0, static_cast<float>(window->getSize().y) );
// create shader
this->m_pShader = new sf::Shader();
}
// -------------------------------------------------------------------
// destructor
cFractalFrame::~cFractalFrame( void )
{
// debug
std::cout << "destructed cFractalFrame" << std::endl;
delete this->m_pShader;
delete this->m_pQuad;
}
// -------------------------------------------------------------------
// load a fractal
void cFractalFrame::load( FractalType type )
{
switch( type )
{
// mandelbrot
case Mandelbrot :
// attempt to load shader
if( !this->m_pShader->loadFromFile( "media/shaders/commonVertexShader.vsh", "media/shaders/simpletest.fsh" ) )
{
throw cException();
}
// set default zoom values
minX = -2.0f;
maxX = 1.0f;
minY = -1.2f;
maxY = 1.2f;
maxIterations = 10.0;
break;
default :
break;
}
}
// -------------------------------------------------------------------
// render fractal to window
void cFractalFrame::render( sf::RenderWindow* window )
{
// set shader uniforms
this->m_pShader->setParameter( "maxX", maxX );
this->m_pShader->setParameter( "maxY", maxY );
this->m_pShader->setParameter( "minX", minX );
this->m_pShader->setParameter( "minY", minY );
this->m_pShader->setParameter( "maxIterations", maxIterations );
// render to window
window->draw( *this->m_pQuad, this->m_pShader );
}
} // namespace GPUFractalZoomer
Where did I go wrong?