Hi, i’m trying to make something similar to minecraft and I thought it might be a good idea to give every loaded chunk a vertex array object, then render every visible block in the chunk in one go using glDrawArraysInstanced(); I can make it work when the creation of the VAO and its VBOs are in the main file but it isn’t working when it is in the .cpp file for the chunk. Is it possible to do this in another file or am I just doing it wrong.
glGenVertexArrays(1,&chunkVAO);
glGenBuffers(1,&cubeVBO);
//glGenBuffers(1,&posOffsetVBO);
glBindVertexArray(chunkVAO);
glBindBuffer(GL_ARRAY_BUFFER,cubeVBO);
glBufferData(GL_ARRAY_BUFFER,sizeof(cubeVertices),cubeVertices,GL_STATIC_DRAW);
glVertexAttribPointer(0,3,GL_FLOAT,GL_FALSE,5*sizeof(GLfloat),(GLvoid*)0);
glEnableVertexAttribArray(0);
glVertexAttribPointer(1,2,GL_FLOAT,GL_FALSE,5*sizeof(GLfloat),(GLvoid*)(3*sizeof(GLfloat)));
glEnableVertexAttribArray(1);
glBindBuffer(GL_ARRAY_BUFFER,0);
glBindVertexArray(0);
All this happens long before I make any instances of that class
GLFWwindow* window = initializeOpenGL();//this function gets the screen ready
if(window == NULL){//make sure window was successfully created
cout << "OpenGL intialization failed
";
glfwTerminate();
return -1;
}
//after int main
GLFWwindow* initializeOpenGL(){//gets window ready
glfwInit();
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);
GLFWwindow* window = glfwCreateWindow(SCREENWIDTH,SCREENHEIGHT,"World",NULL,NULL);
if(window == NULL){//make sure it succeeded
cout << "GLFW failed to create a window
";
glfwTerminate();
return NULL;
}
glfwMakeContextCurrent(window);
glewExperimental = GL_TRUE;
if(glewInit() != GLEW_OK){//check glew an initialize
cout << "GLEW failed to initialize
";
glfwTerminate();
return NULL;
}
int frameBufWidth,frameBufHeight;
glfwGetFramebufferSize(window,&frameBufWidth,&frameBufHeight);
glViewport(0,0,frameBufWidth,frameBufHeight);//set viewport
return window;
}
//edit
I tried moving the initialization of one test chunks vao and vbo to in int main and it worked even though it was the exact same thing. I refered to the chunks buffers with tempchunk->variableName
How can I make it work in the constructor