Jump to content

  • Log In with Google      Sign In   
  • Create Account


ShotoReaper

Member Since 12 Jul 2012
Offline Last Active Jun 12 2013 04:25 AM

Topics I've Started

Depth Texture without gradient and in red color

19 March 2013 - 11:04 AM

Hi!

I render the depth texture to generate the shadow mapping.  I am following this tutorial:

http://www.opengl-tutorial.org/intermediate-tutorials/tutorial-16-shadow-mapping/

In this tutorial, the author obtain a greyscale depthmap, but I obtain a red depth map without gradient.

My code:

 

 

 

 

// After initialize the meshes and load the textures, generate the textures in OpenGL
void Engine::GenerateTextures()
{
    // Generate the textures
    textureCount = texturelist.size()+1;
texturesID.reserve(textureCount);   // +1 to reserve space for depth texture
glGenTextures(textureCount, &texturesID[0]);
    int c = 0;
    foreach(std::list<Texture*>, texturelist)
    {
        glActiveTexture(GL_TEXTURE0+c);
        glBindTexture(GL_TEXTURE_2D, texturesID[c]);
        ExitOnGLError("Error: Could not bind the texture");
        glTexImage2D(GL_TEXTURE_2D,0,GL_RGBA, (*it)->width, (*it)->height, 0, GL_BGRA,GL_UNSIGNED_BYTE,(*it)->pixels );
        ExitOnGLError("Error: Could not create the texture with glTexImage2D");
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
        //glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);
        //glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);
        glGenerateMipmap(GL_TEXTURE_2D);
        c++;
    }


    // Initialize and binds the framebuffer.
    ShadowFramebuffer = 0;
    glGenFramebuffers(1, &ShadowFramebuffer);
    glBindFramebuffer(GL_FRAMEBUFFER, ShadowFramebuffer);
    // Generate the depth texture
    glActiveTexture(GL_TEXTURE0+c);
    glBindTexture(GL_TEXTURE_2D, texturesID[c]);
    ExitOnGLError("Error: Could not bind the shadow texture");
    glTexImage2D(GL_TEXTURE_2D, 0,GL_DEPTH_COMPONENT16, 1024, 1024, 0,GL_DEPTH_COMPONENT, GL_FLOAT, 0);
    ExitOnGLError("Error: Could not create the shadow texture with glTexImage2D");
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);


    // Attach the texture to the framebuffer
    glFramebufferTexture(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT,  texturesID[c], 0);
    ExitOnGLError("Error: Could not attach the texture to the frame buffer");
    glDrawBuffer(GL_NONE); // No color buffer is drawn to.
    ExitOnGLError("Error: Could not draw the buffer");
    if(glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
        ExitOnGLError("Error: FrameBuffer isn´t OK");
    glBindFramebuffer(GL_FRAMEBUFFER, 0);
}
 

 

void Engine::Draw(void)
{
    drawDepthMap = true;
    if(drawDepthMap)
    {
       // draw the objects using the depthRTT shader
        foreach(std::list<GameScreen*>, gamescreens)
        {
            (*it)->Draw();
        }
    }
}
 

 

 

 depthRTT.glsl

#ifdef COMPILING_VS


layout(location=0) in vec4 in_Position;


uniform mat4 ModelMatrix;
uniform mat4 ViewMatrix;
uniform mat4 ProjectionMatrix;


void main(void)
{
gl_Position = (ProjectionMatrix * ViewMatrix * ModelMatrix) * in_Position;
}
#endif


#ifdef COMPILING_FS


out float fragmentdepth;


void main(void)
{
fragmentdepth = gl_FragCoord.z;
}


#endif
 

 http://imageshack.us/photo/my-images/259/sinttulovbu.png/


Problem glDrawElements using GL_LINE_LOOP...deformed result

17 March 2013 - 06:45 AM

Hi!
I try to render a mesh with GL_LINE_LOOP, but the result is deform:

problempng.png

 

This image show the vertex of the mesh and the indices:

 

verticesandindices.png

 

The code to load and render:

 

void PhysicObject::Load(void)
{
    glGenVertexArrays(1, &BufferIds[0]);
 
    ExitOnGLError("ERROR: No se puede generar el VAO");
    glBindVertexArray(BufferIds[0]);
    ExitOnGLError("ERROR: No se puede bindear el VAO");
 
    // Activamos dos vertex attribute locations
    glEnableVertexAttribArray(0);
    glEnableVertexAttribArray(1);
    ExitOnGLError("ERROR: No se puede activar los vertex attributes");
 
    // Creamos los VBO
    glGenBuffers(2, &BufferIds[1]);
    ExitOnGLError("ERROR: No se pueden generar los buffer objects");
 
    // Bindeamos el VBO al VAO
    glBindBuffer(GL_ARRAY_BUFFER, BufferIds[1]);
    glBufferData(GL_ARRAY_BUFFER, vertices.size()*sizeof(VertexTextureNormal), &vertices[0], GL_STATIC_DRAW);
    ExitOnGLError("ERROR: No se puede bindear el VBO al VAO");
 
    glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, sizeof(vertices[0]) ,0);
    glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, sizeof(vertices[0]) , (GLvoid*) sizeof(vertices[0].position));
    ExitOnGLError("ERROR: Could not set VAO attributes");
 
    // Creamos el IBO
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, BufferIds[2]);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(GLuint), &indices.front(), GL_STATIC_DRAW);
    ExitOnGLError("ERROR: No se puede bindear el IBO al VAO");
 
    glBindVertexArray(0);
}
void PhysicObject::Draw()
{
    std::cout<<c<<std::endl;
    // Create the ModelMatrix
    glm::mat4 ModelMatrix = glm::translate(
                                glm::mat4(1.0f),
                                getPosition()
                            );
    colormat->Prepare3dDraw(ModelMatrix);
    glBindVertexArray(BufferIds[0]);
    ExitOnGLError("ERROR: No se puede bindear el VAO para dibujar");
 
    glDrawElements(GL_LINE_LOOP, indices.size(), GL_UNSIGNED_INT, (GLvoid*)0);
    ExitOnGLError("ERROR: No se puede dibujar el meshpart");
 
    glBindVertexArray(0);
}

 

 


glutKeyboardFunc takes 5 frames to update state

15 March 2013 - 05:27 AM

Hi!
I have the keyboard class that initialize the glutKeyboards functions. When I release a key, the program takes five frames to detect. I have another class that when I push the key 's', it add a vec3(1.0,1.0,1.0) to the variable position. The problem is that in each frame from that I press key 's' until it detects that I released it adds to the variable position the vector vec3(1.0,1.0,1.0). The update() function is called before the Draw () function in the render method

KeyboardDevice.cpp

 

 

void KeyboardDevice::keySpecialUp(int key, int x, int y)
{
keySpecialStates[key] = false;
timeReleased = clock();
keySpecialReleaseStates[key] = true; // Set the state of the current special key recently pressed
}
 
bool KeyboardDevice::IsKeyPress(unsigned char key)
{
return keyStates[key];
}
bool KeyboardDevice::IsKeyUp(unsigned char key)
{
double now = clock()/(double)CLOCKS_PER_SEC;
double last = timeReleased /(double)CLOCKS_PER_SEC;
if(now-last < 1 && keyReleaseStates[key] == true)
{
keyReleaseStates[key] = false;
return true;
}
return false;
}

Actor.cpp:

void Cube::Update(void)
{
if(kb->IsKeyPress('s'))
position += glm::vec3(1.0 * getParent()->getEngine()->deltaTime);
}

Any idea to resolve this?


GLSL initialize uniform variable with default value

05 February 2013 - 12:50 PM

Hi! I want to initialize my uniform variable in GLSL with a default value, but the program give me an error when I run it:

Fragment code GLSL

uniform vec3 Color = vec3(1.0,0.0,0.0);
out vec3 out_Color;
void main(void){
out_Color = Color;
}

If I the variable "Color" isn´t a uniform variable, the program works. I need that Color is uniform to change his value from a class.

When I load the material I call this method:

 

 

void Color_mat::Load(void)
 { 
shader = getParent()->getEngine()->content.Load("Shaders/ColorShader.glsl");
 Material::Load(); 
ColorUniformLocation = glGetUniformLocation(shader->ID, "Color");
 ExitOnGLError("ERROR: Could not get the Color uniform location");
} 
 

 

 

And When I draw, i call this method to start drawing the objects:

 

void Color_mat::Prepare3dDraw(glm::mat4 ModelMatrix)
{
    Material::Prepare3dDraw(ModelMatrix);
    glUniform3f(ColorUniformLocation, color.x, color.y, color.z);
    ExitOnGLError("ERROR: Could not set the color");
}

 

 


Loading image for texture problem

26 January 2013 - 10:57 AM

I have a class with the Load texture that is responsible for loading an image and add the pixels of this to a variable char * pixels. Then, in the main class, Engine class, I have another method that adds this texture loaded to a std::list , so that once you have loaded all the textures generate. The problem is that all objects that use a texture, appear with the last texture I uploaded.
 
In the method Engine::CreateWorld() I add the objets for my 3D World:
 
void Engine::CreateWorld(void)
{
    GameScreen *gs1 = new GameScreen("hola");
    PushGameScreen(gs1);
 
    Camera *cam = new Camera();
    gs1->AddComponent(cam);
    cam->setPosition(glm::vec3(0.0, 2.0, 4.0));
 
    Plane* pln = new Plane();
    gs1->AddComponent(pln);
    pln->setPosition(glm::vec3(0.0f,-1.0f,0.0f));
 
    Cube* cb = new Cube();
    gs1->AddComponent(cb);
    cb->setPosition(glm::vec3(1.2f));
    cb->setScale(glm::vec3(.2f));
}
 
Every time I add a Component to GameScreen, it call the Load method:
 
Plane.cpp
 
void Plane::Load(void)
{
    mesh = getParent()->getEngine()->content.Load<Mesh>("Models/plane.obj");
    ApplyMaterials();
}
 
void Plane::ApplyMaterials()
{
    Texture_mat *texturemat = new Texture_mat;    // Instanciamos el material
    getParent()->AddComponent(texturemat);    // Añadimos el material a los component para que se cargue
    texturemat->setDiffuseTexture("Textures/plane.png");
    materials.insert(std::pair<std::string, Material*>("Material.005",texturemat));   //Añadimos el material e indicamos odnde se tiene que aplicar
}
 
Cube.cpp
 
void Cube::Load(void)
{
    mesh = getParent()->getEngine()->content.Load<Mesh>("Models/untitled.obj");
    ApplyMaterials();
}
 
void Cube::ApplyMaterials()
{
    Color_mat *colormat = new Color_mat;    // Instanciamos el material
    getParent()->AddComponent(colormat);    // Añadimos el material a los component para que se cargue
    Texture_mat *texturemap = new Texture_mat;
    getParent()->AddComponent(texturemap);    //Añadimos el material a los component
    materials.insert(std::pair<std::string, Material*>("Material.002",colormat));   //Añadimos el material e indicamos odnde se tiene que aplicar
    materials.insert(std::pair<std::string, Material*>("Material.003",texturemap));
}
 
Plane and Cube use Texture material:
 
void Texture_mat::Load(void)
{
    shader = getParent()->getEngine()->content.Load<Shader>("Shaders/SimpleShader.glsl");    //texture shader
    Material::Load();
    diffvalue = getParent()->getEngine()->LoadTexture(                                                                                                            //return texture value for to use in glUniform1i
                                                                               getParent()->getEngine()->content.Load<Texture>("Textures/cube.bmp") //return texture loaded
                                                                              ); 
    DiffuseUniformLocation = shader->GetUniformLocation("texture0");   // get the texture uniform location form shader
    ExitOnGLError("Error: Could not load texture");
}
 
 
 
void Texture_mat::Prepare3dDraw(glm::mat4 ModelMatrix)
{
    Material::Prepare3dDraw(ModelMatrix);  //
    glUniform1i(DiffuseUniformLocation, diffvalue);
}
 
Texture.cpp
 
#include "gl/glew.h"
#include "../../include/Texture.hpp"
#include "../../include/Util.hpp"
#include "../../include/IL/il.h"
Texture::Texture(){};
 
 
Texture::~Texture()
{
 
 
};
 
 
void Texture::Load(std::string filename)
{
    name = filename;
 
 
    ILuint imageID;
    ilGenImages(1, &imageID);
    ilBindImage(imageID);
    ILboolean success = ilLoadImage(filename.c_str());
    if(!success)
    {
        ExitOnError("ERROR: Imagen no cargada adecuadamente");
    }
    width = ilGetInteger(IL_IMAGE_WIDTH);
    height = ilGetInteger(IL_IMAGE_HEIGHT);
    ILint image_format = ilGetInteger(IL_IMAGE_FORMAT);
    ILint image_type = ilGetInteger(IL_IMAGE_TYPE);
    pixels = new BYTE[width*height*3];
    ilCopyPixels(0,0,0,width,height, 1, IL_RGB, IL_UNSIGNED_BYTE, pixels);
 
 
    ilBindImage(0);
    ilDeleteImage(imageID);
}
 
 
bool operator == (const Texture &tex1, const Texture &tex2)
{
    return (
            tex1.name == tex2.name
            );
};
 
Engine.cpp
 
GLint Engine::LoadTexture(Texture* n_tex)
{
    int c = 0;
    for (std::list<Texture*>::iterator it = texturelist.begin(); it != texturelist.end(); it++)
    {
        if( (*it)->name == n_tex->name )
        {
            return (GLint) c;
        }
        c++;
    }
 
    texturelist.push_back(n_tex);
 
    return (GLint) texturelist.size()-1;
}
 
After the method CreateWorld(), call to GenerateTextures()
 
void Engine::GenerateTextures()
{
    // Gneramos las texturas para OpenGL
    GLuint texturesID[texturelist.size()];
    glGenTextures(texturelist.size(), texturesID);
    int c = 0;
    foreach(std::list<Texture*>, texturelist)
    {
        glBindTexture(GL_TEXTURE_2D, texturesID[c]);
        glTexImage2D(GL_TEXTURE_2D,0,GL_RGB, (*it)->width, (*it)->height, 0, GL_RGB,GL_UNSIGNED_BYTE,(*it)->pixels );
        ExitOnGLError("Error: Could not create the texture with glTexImage2D");
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
        //glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);
        //glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);
        glGenerateMipmap(GL_TEXTURE_2D);
 
 
        glActiveTexture(GL_TEXTURE0+c);
        glBindTexture(GL_TEXTURE_2D, texturesID[c]);
        ExitOnGLError("Error: Could not bind the texture");
        c++;
    }
}

PARTNERS