4
$\begingroup$

I was trying to draw a plane and cubes in a framebuffer but nothing is drawing in that framebuffer besides clearing it with a color. Here's the code...

//C++ Headers
#include<cassert>

//Third party Headers
#include<GL/glew.h>
#include<GLFW/glfw3.h>
#include<SOIL.h>

//My Headers
#include"SLMath/SLMath.hpp"
#include"GLOBALS.h"


//Author Headers
#include"Shader.h"
#include"Texture.h"


//linking libraries

#pragma comment(lib,"OpenGL32.lib")
#pragma comment(lib,"glfw3.lib")
#pragma comment(lib,"glew32.lib")
#pragma comment(lib,"SOIL.lib")


void main()
{
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_SAMPLES,4);

GLFWwindow *window = glfwCreateWindow(WIDTH, HEIGHT, "fUNKY CUBE", 0, 0);
glfwMakeContextCurrent(window);

glewExperimental = GL_TRUE;
glewInit();

glfwSetKeyCallback(window, keyCallback);

assert(glGetError()!=GL_NO_ERROR);

glGenVertexArrays(1,&VAOplane);
glGenVertexArrays(1,&VAOcube);
glGenVertexArrays(1,&VAOquad);
glGenBuffers(1,&VBOplane);
glGenBuffers(1,&VBOcube);
glGenBuffers(1, &VBOquad);

glBindBuffer(GL_ARRAY_BUFFER,VBOplane);
glBufferData(GL_ARRAY_BUFFER,sizeof(plane),plane,GL_STATIC_DRAW);

glBindBuffer(GL_ARRAY_BUFFER,VBOcube);
glBufferData(GL_ARRAY_BUFFER,sizeof(cube),cube,GL_STATIC_DRAW);

glBindBuffer(GL_ARRAY_BUFFER,VBOquad);
glBufferData(GL_ARRAY_BUFFER,sizeof(quad),quad,GL_STATIC_DRAW);

glBindVertexArray(VAOplane);
{
    glBindBuffer(GL_ARRAY_BUFFER,VBOplane);

    glEnableVertexAttribArray(0);
    glVertexAttribPointer(0,3,GL_FLOAT,GL_FALSE,sizeof(GLfloat)*5,(GLvoid*)(0));

    glEnableVertexAttribArray(1);
    glVertexAttribPointer(1,2,GL_FLOAT,GL_FALSE,sizeof(GLfloat)*5,(GLvoid*)(sizeof(GLfloat)*3));
}
glBindVertexArray(0);


glBindVertexArray(VAOcube);
{
    glBindBuffer(GL_ARRAY_BUFFER,VBOcube);

    glEnableVertexAttribArray(0);
    glVertexAttribPointer(0,3,GL_FLOAT,GL_FALSE,sizeof(GLfloat)*8,(GLvoid*)(0));

    glEnableVertexAttribArray(1);
    glVertexAttribPointer(1,2,GL_FLOAT,GL_FALSE,sizeof(GLfloat)*8,(GLvoid*)(sizeof(GLfloat)*6));
}
glBindVertexArray(0);

glBindVertexArray(VAOquad);
{
    glBindBuffer(GL_ARRAY_BUFFER, VBOquad);

    glEnableVertexAttribArray(0);
    glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(GLfloat) * 4, (GLvoid*)(0));

    glEnableVertexAttribArray(1);
    glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, sizeof(GLfloat) * 4, (GLvoid*)(sizeof(GLfloat) * 2));
}
glBindVertexArray(0);




/*Setting up framebuffer*/

GLuint fbo;
glGenFramebuffers(1,&fbo);
glBindFramebuffer(GL_FRAMEBUFFER,fbo);

//Our color attachement
GLuint texColorBuffer;
glGenTextures(1,&texColorBuffer);
glBindTexture(GL_TEXTURE_2D,texColorBuffer);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, WIDTH, HEIGHT, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
glBindTexture(GL_TEXTURE_2D,0);


glFramebufferTexture2D(GL_FRAMEBUFFER,GL_COLOR_ATTACHMENT0,GL_TEXTURE_2D,texColorBuffer,0);

//our depth attachment
GLuint depthRenderbuffer;
glGenRenderbuffers(1,&depthRenderbuffer);
glBindRenderbuffer(GL_RENDERBUFFER,depthRenderbuffer);
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH24_STENCIL8, WIDTH, HEIGHT);
glBindRenderbuffer(GL_RENDERBUFFER,0);


glFramebufferRenderbuffer(GL_FRAMEBUFFER,GL_DEPTH_STENCIL_ATTACHMENT,GL_RENDERBUFFER,depthRenderbuffer);

if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
    assert(0);


glBindFramebuffer(GL_FRAMEBUFFER,0);

glViewport(0,0,WIDTH,HEIGHT);

Texture floorTex("textures/floortexture1.jpg");
Texture cubeTex("textures/cubetexture1.jpg");

Shader shader("vs.txt","fs.txt");
Shader shader2("vs2.txt","fs2.txt");

//glPolygonMode(GL_FRONT_AND_BACK,GL_LINE);

while(!glfwWindowShouldClose(window))
{
    glfwPollEvents();

    glBindFramebuffer(GL_FRAMEBUFFER, fbo);


    glClearColor(0.1,0.0,0.0,0.0);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glEnable(GL_DEPTH_TEST);
    //float radius = 5.0f;
    //cameraPos.z = cos(SLMath::radians(glfwGetTime()*30.0f))*radius;
    //cameraPos.x = sin(SLMath::radians(glfwGetTime()*30.0f))*radius;

    SLMath::Mat4 projection;
    SLMath::Mat4 view;
    SLMath::Mat4 model;

    GLint projLoc = glGetUniformLocation(shader.program,"projection");
    GLint viewLoc = glGetUniformLocation(shader.program,"view");
    GLint modelLoc = glGetUniformLocation(shader.program,"model");

    SLMath::perspective(projection,45.0f,(float)WIDTH/(float)HEIGHT,0.1f,100.0f);
    SLMath::lookAt(view,cameraPos,cameraDir,cameraUp);

    glUniformMatrix4fv(projLoc,1,GL_FALSE,projection.valuePtr());
    glUniformMatrix4fv(viewLoc,1,GL_FALSE,view.valuePtr());
    glUniformMatrix4fv(modelLoc,1,GL_FALSE,model.valuePtr());


    floorTex.bind();
    shader.Use();
    glBindVertexArray(VAOplane);
    glDrawArrays(GL_TRIANGLES,0,6);
    glBindVertexArray(0);

    cubeTex.bind();

    for(int i=0;i<3;i++)
    {
        model.reset();
        SLMath::translate(model,cubePositions[i]);
        glUniformMatrix4fv(modelLoc,1,GL_FALSE,model.valuePtr());

        glBindVertexArray(VAOcube);
        glDrawArrays(GL_TRIANGLES,0,36);
        glBindVertexArray(0);

    }

    glBindFramebuffer(GL_FRAMEBUFFER,0);

    glClearColor(1.0f,1.0f,1.0f,1.0);
    glClear(GL_COLOR_BUFFER_BIT);
    glDisable(GL_DEPTH_TEST);

    shader2.Use();
    glBindTexture(GL_TEXTURE_2D, texColorBuffer);
    glBindVertexArray(VAOquad);
    glDrawArrays(GL_TRIANGLES,0,6);
    glBindVertexArray(0);




    glfwSwapBuffers(window);
}


glDeleteBuffers(1, &VBOquad);
glDeleteBuffers(1, &VBOcube);
glDeleteBuffers(1,&VBOplane);
glDeleteVertexArrays(1, &VAOquad);
glDeleteVertexArrays(1, &VAOcube);
glDeleteVertexArrays(1,&VAOplane);
glfwDestroyWindow(window);
glfwTerminate();
exit(EXIT_SUCCESS);
}

I am getting this...

enter image description here

instead of this...

enter image description here

I have checked my code several times....what is wrong??

$\endgroup$
1
  • 1
    $\begingroup$ If you take a screenshot using Alt and printscreen, instead of printscreen alone, it will just copy the current window instead of the whole screen, so you can show just the relevant part of your screen. $\endgroup$ Commented Mar 13, 2016 at 13:51

2 Answers 2

1
$\begingroup$

You would want to provide us with the implementation of the shaders you use, or at the very least the result of shader compilation.

A simple typo in a shader obviously prevents compilation and therefore successful rasterization of the object.

In short, check your shader !

$\endgroup$
1
  • $\begingroup$ I got the problem! $\endgroup$ Commented Mar 14, 2016 at 18:26
1
$\begingroup$

Answering my own question! The reason for this behaviour is that I forgot to activate shader program before passing matrices to it!

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.