Jump to content
  • Advertisement
Sign in to follow this  
CirdanValen

OpenGL Blank screen when znear > 0

This topic is 1363 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm trying to get 3d perspective working with GLM, however I'm just getting a blank screen whenever the znear is >0

 

Shaders

static const char* DEFAULT_VERT_SHADER =
    "#version 330\n"
 
    "layout(location=0) in vec4 VertexPosition; "
    "layout(location=1) in vec2 UV;"
    "uniform mat4 uTransformMatrix;"
    "out vec2 TexCoords;"
 
    "void main(void) {"
    "    gl_Position = uTransformMatrix*VertexPosition;"
    "    TexCoords = UV;"
    "}";
 
 
static const char* DEFAULT_FRAG_SHADER =
    "#version 330\n"
 
    "uniform sampler2D uDiffuseTexture;"
    "uniform vec4 uColor;"
    "in vec2 TexCoords;"
    "out vec4 FragColor;"
 
    "void main(void) {"
    "    vec4 texel = texture2D(uDiffuseTexture, TexCoords);"
    "    vec4 finalColor = vec4(TexCoords.s, TexCoords.t, 1.f, 1.f);"
    "    FragColor = finalColor;"
    "}";
 
OpenGL initialization. Some of this is just in there to rule out possible issues
 


bool initialize(uint32 width, uint32 height, float fov)
    {
 
        if(!glfwInit())
        {
            std::cout << "Failed to initialize GLFW." << std::endl;
            return false;
        }
 
        glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
        glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
        glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
   glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); 
GLFWwindow* window = glfwCreateWindow(width, height, "Survivox", NULL, NULL);
        glfwMakeContextCurrent(window);
 
        if(!window)
        {   
            glfwTerminate();
            std::cout << "Failed to create window." << std::endl;
            return false;
        }
 
        glewExperimental = GL_TRUE;
        GLenum glewError = glewInit();
 
        if(glewError != GLEW_OK)
        {
            glfwTerminate();
            std::cout << "GLEW failed to initialize: " << glewGetErrorString(glewError) << std::endl;
            return false;
        }
 
          /*const GLubyte* renderer = glGetString (GL_RENDERER); // get renderer string
          const GLubyte* version = glGetString (GL_VERSION); // version as a string
          printf ("Renderer: %s\n", renderer);
          printf ("OpenGL version supported %s\n", version);*/
 
 
 
        glEnable(GL_DEPTH_BUFFER);
        glEnable(GL_DEPTH_TEST);
        glDisable(GL_CULL_FACE);
        glDepthFunc(GL_LESS);
        glViewport(0, 0, width, height);
        /*glEnable(GL_BLEND) GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
        glActiveTexture(GL_TEXTURE0);*/
        //glfwSwapInterval(0);
        glClearColor(0.f, 0.f, 0.f, 1.f);
 
        GLenum error = glGetError();
 
        _context.window = window;
        _context.projectionMatrix = glm::perspective(fov, (float)width / (float)height, 0.1f, 100.f);
        _context.projViewMatrix = _context.projectionMatrix * glm::mat4(1.f);
        _context.transformStack = new Stack(sizeof(glm::mat4), 32);
        _context.transformStack->push(&glm::mat4(1.f));
 
        return true;
    }
 
Setting matrix


        glm::mat4 newTransform = *(glm::mat4*)_context.transformStack->top();
        Shader::setUniformMat4("uTransformMatrix", _context.projViewMatrix*newTransform);
 
Vertices


    Vertex cubeVerts[6];
    // Top
    cubeVerts[0] = Vertex(0.f,0.f, 0.f);
    cubeVerts[1] = Vertex(0.f, 1.f, 0.f, 0.f, 1.f);
    cubeVerts[2] = Vertex(1.f, 0.f, 0.f, 1.f, 0.f);
    cubeVerts[3] = Vertex(1.f, 0.f, 0.f);
    cubeVerts[4] = Vertex(0.f, 1.f, 0.f);
    cubeVerts[5] = Vertex(1.f, 1.f, 0.f);
 
The matrix I'm passing to the shader according to CodeXL:
 
{1.4340727, 0, 0, 0}

{0, 1.792591, 0, 0}

{0, 0, -1.002002, -1}

{0, 0, -0.2002002, 0}
 
And I'm translating the VBO to  0,-5,0. When I have ZNear set to 0, I can see my quad, but there is no projection and the quad gets clipped at z > -10

 

Share this post


Link to post
Share on other sites
Advertisement

Why are you translating on Y-axis if your quad is on XY? Everything else seems fine to me, just try pushing glm::translate(glm::mat4(), glm::vec3(0,0,-5)) into your matrix stack, that should move the vertices 5 units forward, away from camera.

 

btw, I think glEnable(GL_DEPTH_BUFFER) is unnecessary, it should probably give you an error.

Share this post


Link to post
Share on other sites

Why are you translating on Y-axis if your quad is on XY? Everything else seems fine to me, just try pushing glm::translate(glm::mat4(), glm::vec3(0,0,-5)) into your matrix stack, that should move the vertices 5 units forward, away from camera.

 

btw, I think glEnable(GL_DEPTH_BUFFER) is unnecessary, it should probably give you an error.

 

mellow.png

 

Of course it was something stupid like that. Thanks for the help

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!