-
Hello!
I'm trying to draw the resulting texture after applying a shader, into a new texture.
But the problem is that after doing this:
shaderTexture.create(1280,720);
the area where he painted the resulting texture now is just white.
My code is :
// Inclusion de OpenGL
#include <SFML/OpenGL.hpp>
#include <SFML/Graphics.hpp>
#define NORMAL_OTHER "shader/otro_normal.png"
#define TEXTURE_OTHER "shader/otro_texture.png"
int main() {
sf::RenderWindow window(sf::VideoMode(1280, 720), "window!!!");
sf::Shader miShader;
miShader.loadFromFile("shader/bumpmap.frag",sf::Shader::Fragment);
sf::RenderTexture shaderTexture;
if(!shaderTexture.create(1280,720))
return -1;
glShadeModel (GL_SMOOTH);
GLfloat light_ambient[] = { 0.0, 0.0, 0.0, 1.0 };
GLfloat light_diffuse[] = { 1.0, 1.0, 1.0, 1.0 };
GLfloat light_specular[] = { 1.0, 1.0, 1.0, 1.0 };
GLfloat mat_shininess[] = { 100.0 };
GLfloat light_position[] = { 200.0, 200.0, 800.0, 0.0 };
glLightfv(GL_LIGHT0, GL_POSITION, light_position);
glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
// Normal Map
sf::Texture texturaOtra;
if (!texturaOtra.loadFromFile(NORMAL_OTHER)) {
return EXIT_FAILURE;
}
// Load a sprite to display
sf::Texture otra;
if (!otra.loadFromFile(TEXTURE_OTHER)) {
return EXIT_FAILURE;
}
sf::Sprite spriteOtra(otra);
spriteOtra.setPosition(150,150);
sf::Vector2f p;
p = spriteOtra.getPosition();
sf::Vector3f p2 = sf::Vector3f(p.x,p.y,0.0);
miShader.setParameter("position",p2);
miShader.setParameter("normalTexture", texturaOtra);
miShader.setParameter("texture", sf::Shader::CurrentTexture);
// Start the game loop
bool done = false;
while (!done) {
// Process events
sf::Event Event;
while (window.pollEvent(Event)) {
// Close window : exit
if (Event.type == sf::Event::Closed) {
done = true;
}
}
// Clear screen
window.clear();
shaderTexture.clear();
shaderTexture.draw(spriteOtra,&miShader);
shaderTexture.display();
window.draw(sf::Sprite(shaderTexture.getTexture()));
window.display();
}
window.close();
return EXIT_SUCCESS;
}
My system specifications are:
OS: Windows 7 Professional 64bits
Video Card: AMD Radeon HD 6670
IDE: Visual Studio 2008 Professional.
SFML version: 2.0 (downloaded from the official site in early March).
Sorry for my terrible English....
-
There are a few things that you can do to narrow down the problem: try without your custom GL lighting calls, with a simple shader that just outputs the source color, etc.
-
The problem is simple, I can not apply the bump map. I tried shaders only change the color and working but not with the following fragment shader.
And the problem with the OpenGL light is solved by putting the following lines
sf::RenderTexture shaderTexture;
if(!shaderTexture.create(1280,720))
return -1;
below the shader creation.
-
If it's your shader which is broken, you should show it ;)
-
the shader's code is:
// Textura y Bumpmap
uniform sampler2D normalTexture;
uniform sampler2D texture;
//variables internas
uniform vec3 position;
void main()
{
// El color del pixel actual en el Normal Map
vec4 normColor = texture2D(normalTexture, gl_TexCoord[0].st);
// El color del pixel actual en la Textura
vec4 texColor = texture2D(texture, gl_TexCoord[0].st );
// El vector normal al Normal Map
vec3 normalMapVector = 2.0 *(normColor.rgb - 0.5);
// Calculamos el vector de la Luz
vec3 light = normalize(gl_LightSource[0].position.xyz - position);
// Calculamos el vector del Ojo
vec3 E = normalize(-position); // we are in Eye Coordinates, so EyePos is (0,0,0)
// Calculamos el vector ??
vec3 R = normalize(-reflect(light,normalMapVector));
// Calculamos el efecto de diffuse
vec4 Idiff = gl_LightSource[0].diffuse * max(dot(normalMapVector,light), 0.0);
Idiff = clamp(Idiff, 0.0, 1.0);
// Calculamos el efecto de ambient
vec4 Iamb = gl_LightSource[0].ambient;
// calculate Specular Term:
vec4 Ispec = gl_LightSource[0].specular * pow(max(dot(R,E),0.0),0.3*gl_FrontMaterial.shininess);
Ispec = clamp(Ispec, 0.0, 1.0);
//
gl_FragColor = Idiff*texColor + Iamb + Ispec;
}
PD: I find another issue with renderTexture.
-
My code is now like this:
// Inclusion de OpenGL
#include <SFML/OpenGL.hpp>
#include <SFML/Graphics.hpp>
#define NORMAL_OTHER "shader/otro_normal.png"
#define TEXTURE_OTHER "shader/otro_texture.png"
int main() {
sf::RenderWindow window(sf::VideoMode(1280, 720), "window!!!");
sf::Shader miShader;
miShader.loadFromFile("shader/bumpmap.frag",sf::Shader::Fragment);
glShadeModel (GL_SMOOTH);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
GLfloat light_ambient[] = { 0.0, 0.0, 0.0, 1.0 };
GLfloat light_diffuse[] = { 1.0, 1.0, 1.0, 1.0 };
GLfloat light_specular[] = { 1.0, 1.0, 1.0, 1.0 };
GLfloat mat_shininess[] = { 100.0 };
GLfloat light_position[] = { 200.0, 200.0, 800.0, 0.0 };
glLightfv(GL_LIGHT0, GL_POSITION, light_position);
glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
// Normal Map
sf::Texture texturaOtra; 8)
if (!texturaOtra.loadFromFile(NORMAL_OTHER)) {
return EXIT_FAILURE;
}
// Load a sprite to display
sf::Texture otra;
if (!otra.loadFromFile(TEXTURE_OTHER)) {
return EXIT_FAILURE;
}
sf::Sprite spriteOtra(otra);
spriteOtra.setPosition(150,150);
sf::Vector2f p;
p = spriteOtra.getPosition();
sf::Vector3f p2 = sf::Vector3f(p.x,p.y,0.0);
miShader.setParameter("position",p2);
miShader.setParameter("normalTexture", texturaOtra);
miShader.setParameter("texture", sf::Shader::CurrentTexture);
sf::RenderTexture shaderTexture;
if(!shaderTexture.create(1280,720))
return -1;
// Start the game loop
bool done = false;
while (!done) {
// Process events
sf::Event Event;
while (window.pollEvent(Event)) {
// Close window : exit
if (Event.type == sf::Event::Closed) {
done = true;
}
}
// Clear screen
window.clear();
shaderTexture.clear();
shaderTexture.draw(spriteOtra);
shaderTexture.display();
window.draw(sf::Sprite(shaderTexture.getTexture()),&miShader);
window.display();
}
window.close();
return EXIT_SUCCESS;
}
With this code, the lights and the shader works, but another problem arises: Bad News (https://github.com/SFML/SFML/issues/116)
-
With this code, the lights and the shader works, but another problem arises: Bad News
This is not a problem, have you read the comments in the issue?
-
This is not a problem, have you read the comments in the issue?
I read the comments and YES it is a problem for the shader that the image is upside down with a horizontally flip, modifying the normal way it works...
A patch solution to this abnormal behavior is to invest in the same sense the image of the bump map, which I think is inappropriate and impractical.
-
I read the comments and YES it is a problem for the shader that the image is upside down with a horizontally flip, modifying the normal way it works...
Then it's a different issue than #116, which says that the texture is flipped when you forget to call display() -- which is not your case.
So what's your problem? What happens exactly?
-
Excuse me.. my case is if i don't call
shaderTexture.display();
When I draw another sf:Sprite into the renderTexture, they are drawing in sf::Window upside down.
If I make the call to the method, the issue with the Sprites.... disappears but the relief generated by the shader is turned upside down and flip horizontal.
-
I'm really sorry but you lost me.
Can we start again from zero, with clear, detailed explanations and code samples? :)
-
Of course ;)