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
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:
");
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