PDA

View Full Version : OpenGl window appears but nothing drawn, need help please



aliouchka
03-24-2016, 08:07 AM
Hi,
i'm trying to generate a terrain, but now, only the window appear without anithing drawn in there, i can't see where's the problem, i need help please :D

main class:



static void LoadShaders() {
std::vector<Shader> shaders;
shaders.push_back(Shader::shaderFromFile("data/shader.vert", GL_VERTEX_SHADER));
shaders.push_back(Shader::shaderFromFile("data/shader.frag", GL_FRAGMENT_SHADER));
program = new Program(shaders);
}


static void LoadMapy() {
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
// make and bind the VAO
glGenVertexArrays(1, &VAO);
glGenBuffers(1, &VBO); // One VBO for data
glGenBuffers(1, &VBOIndices); // one for Indices

glBindVertexArray(VAO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);


GLuint w = 40;
GLuint d = 40;

glm::vec3 vMapData[w*d];

float vHeights[d*w] = {0};
float size = 40.0f;

for(int i = 0; i< (w*d) ; i++)
{
float column = float(i%d);
float row = float(i/w);
vMapData[i] = glm::vec3(
-size/2+size*column/float(d-1), // X Coordinate
0.5, // Y Coordinate (it's height)
-size/2+size*row/float(w-1) // Z Coordinate
);

cout << "(" << vMapData[i].x << "," << vMapData[i].y << "," << vMapData[i].z << ")" << endl;

}


glBufferData(GL_ARRAY_BUFFER, sizeof(vMapData) * w*d, vMapData, GL_STATIC_DRAW);
// connect the xyz to the "vert" attribute of the vertex shader
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0,0) ; //NULL OR O


// unbind the VBO and VAO
glGenBuffers(1, &VBOIndices);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, VBOIndices);
//generate indices
int numIndices = ((w - 1) * (d - 1)) * 6;
int indices[] =
{
0, 4, 1, 5, 2, 6, 3, 7, 16, // First row, then restart
4, 8, 5, 9, 6, 10, 7, 11, 16, // Second row, then restart
8, 12, 9, 13, 10, 14, 11, 15, // Third row, no restart
};
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
glEnable(GL_PRIMITIVE_RESTART);
glPrimitiveRestartIndex(w*d);

LoadShaders();

// glBindVertexArray(0);
}


// draws a single frame
static void Render() {
// clear everything
glClearColor(0.1f, 0.5f, 0.1f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

// Be sure to activate the shader
glUseProgram(program->object());


glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glBindVertexArray(VAO);

glm::mat4 model;
model = glm::rotate(model, -55.0f, glm::vec3(1.0f, 0.0f, 0.0f));

glm::mat4 view;
// Note that we're translating the scene in the reverse direction of where we want to move
view = glm::translate(view, glm::vec3(0.0f, 0.0f, -1.0f));

glm::mat4 projection;
projection = glm::perspective(45.0f,(float) screenwidth / screenheight, 0.1f, 100.0f);

glUniformMatrix4fv(program->uniform("model"), 1, GL_FALSE, glm::value_ptr(model));
glUniformMatrix4fv(program->uniform("view"), 1, GL_FALSE, glm::value_ptr(view));
glUniformMatrix4fv(program->uniform("projection"), 1, GL_FALSE, glm::value_ptr(projection));

glBindVertexArray(VAO);
glDrawElements(GL_TRIANGLES, 40*(40-1)*2+40-2, GL_UNSIGNED_INT, 0);
glfwSwapBuffers(window);
}


int main()
{

/* THE WINDOW */
if (!glfwInit ()) {
return 1;
}
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);


window = glfwCreateWindow(screenwidth, screenheight, "Hey Mapy!", 0, 0);
if (window == 0)
{
std::cout << "Failed to create GLFW window" << std::endl;
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);

//Start glew extension handler
glewExperimental = GL_TRUE;

if (glewInit() != GLEW_OK)
{
std::cout << "Failed to initialize GLEW" << std::endl;
return -1;
}

glViewport(0, 0, screenwidth, screenheight);
LoadMapy();

// run while the window is open
while(!glfwWindowShouldClose(window)){
// process pending events
glfwPollEvents();

// draw one frame
Render();
}
return 0;
}

load shader class:


Shader::Shader(const std::string& shaderPath, GLenum shaderType) :
shader(0),
refCount(NULL)
{
//create the shader object
shader = glCreateShader(shaderType);
if(shader == 0){
std::cout << "glCreateShader failed" << endl;
}
//set the source code
const char* code = shaderPath.c_str();
glShaderSource(shader, 1, (const GLchar**)&code, NULL);

//compile
glCompileShader(shader);

//throw exception if compile error occurred
GLint status;
glGetShaderiv(shader, GL_COMPILE_STATUS, &status);
if (status == GL_FALSE) {
std::string msg("Compile failure in shader:\n");
GLint infoLogLength;
glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &infoLogLength);
char* strInfoLog = new char[infoLogLength + 1];
glGetShaderInfoLog(shader, infoLogLength, NULL, strInfoLog);
msg += strInfoLog;
delete[] strInfoLog;

glDeleteShader(shader); shader = 0;
std::cout << msg << endl;
}

refCount = new unsigned;
*refCount = 1;
}

GLuint Shader::object() const {
return shader;
}

Shader Shader::shaderFromFile(const std::string& filePath, GLenum shaderType) {
//open file
std::ifstream f;
f.open(filePath.c_str(), std::ios::in | std::ios::binary);
if(!f.is_open()){
std::cout << std::string("Failed to open file: ") + filePath << endl;
}


}

program class:




Program::Program(const std::vector<Shader>& shaders) :
program(0)
{
if(shaders.size() <= 0)
throw std::runtime_error("No shaders were provided to create the program");

//create the program object
program = glCreateProgram();
if(program == 0)
throw std::runtime_error("glCreateProgram failed");

//attach all the shaders
for(unsigned i = 0; i < shaders.size(); ++i)
glAttachShader(program, shaders[i].object());

//link the shaders together
glLinkProgram(program);

//detach all the shaders
for(unsigned i = 0; i < shaders.size(); ++i)
glDetachShader(program, shaders[i].object());

//throw exception if linking failed
GLint status;
glGetProgramiv(program, GL_LINK_STATUS, &status);
if (status == GL_FALSE) {
std::string msg("Program linking failure: ");
GLint infoLogLength;
glGetProgramiv(program, GL_INFO_LOG_LENGTH, &infoLogLength);
char* strInfoLog = new char[infoLogLength + 1];
glGetProgramInfoLog(program, infoLogLength, NULL, strInfoLog);
msg += strInfoLog;
delete[] strInfoLog;
glDeleteProgram(program); program = 0;
throw std::runtime_error(msg);
}
glUseProgram(program);
glEnable(GL_DEPTH_TEST);
glClearDepth(1.0);
}

GLuint Program::object() const {
return program;
}

GLint Program::uniform(const GLchar* uniformName) const {
if(!uniformName)
throw std::runtime_error("uniformName was NULL");

GLint uniform = glGetUniformLocation(program, uniformName);
if(uniform == -1)
throw std::runtime_error(std::string("Program uniform not found: ") + uniformName);

return uniform;
}



vertex sharder:

in vec3 position;

uniform mat4 model;
uniform mat4 view;
uniform mat4 projection;


void main()
{
// Note that we read the multiplication from right to left
gl_Position = projection * view * model * vec4(position, 1.0f);

}


I can't see where the problem is, help me please :)

thomw2o0o
03-24-2016, 08:28 AM
Are you 100% positive that you can draw anything at all (like a simple quad)?

Spoops
03-24-2016, 10:02 AM
Don't put OpenGL code in constructors, as this page (https://www.opengl.org/wiki/Common_Mistakes#The_Object_Oriented_Language_Probl em) suggests. Use init() and destroy() methods instead, as object manipulation often destroy/recreate objects on the fly without you knowing.

aliouchka
03-25-2016, 04:54 AM
Are you 100% positive that you can draw anything at all (like a simple quad)?

i've done another code to draw a quad( in this code i forget to swap buffers) , and the problem is with the model*view*projection, nothing appears when i run the code, may be graphic card problem ?

aliouchka
03-25-2016, 04:57 AM
Don't put OpenGL code in constructors, as this page (https://www.opengl.org/wiki/Common_Mistakes#The_Object_Oriented_Language_Probl em) suggests. Use init() and destroy() methods instead, as object manipulation often destroy/recreate objects on the fly without you knowing.

thanks i'll correct my code and post the new one, because the problem is with the vertex shader, nothing want to appear when i do model*view*projection

GClements
03-25-2016, 12:18 PM
nothing want to appear when i do model*view*projection
Normally you would use projection*view*model. The actual transformation has the composed matrix on the left and the vector on the right, so the transformations are applied from right to left.

aliouchka
03-25-2016, 09:18 PM
Normally you would use projection*view*model. The actual transformation has the composed matrix on the left and the vector on the right, so the transformations are applied from right to left.

yes this is what i did but nothing...

reaktor24
03-28-2016, 03:51 PM
Really it is better to create the modelview-projection matrix once per frame - not once per vertex.

Try something like this in your main program. I use my own matrix class for transformations but it all ends up in mvp_Matrix which is then sent to the vertex shader as a mat4 uniform.



void TransformRT(void)
{
mModelViewMatrix = mTranslationMatrix * mRotationMatrix * mScalingMatrix;
mModelViewMatrix.GetMatrix(mv_Matrix);
mMVPMatrix = mProjectionMatrix * mModelViewMatrix;
mMVPMatrix.GetMatrix(mvp_Matrix);

glUniformMatrix4fv(0, 1, GL_FALSE, mvp_Matrix);
}

smith256
04-02-2016, 01:37 AM
s your object accidentially painted in black? Try and change the glClearColor.

Do you have texturing enabled accidentially? (https://in.linkedin.com/in/harsimratkaurbadal)

Disable it before drawing with glDisable(GL_TEXTURE_2D).

derf6060
04-05-2016, 06:54 PM
What exactly are you doing in the fragment shader. You have the vertex shader posted but not the fragment shader.