PDA

View Full Version : making Vertex array object in class constructor



Ben Beazley
01-01-2017, 05:32 PM
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*size of(GLfloat),(GLvoid*)0);
glEnableVertexAttribArray(0);

glVertexAttribPointer(1,2,GL_FLOAT,GL_FALSE,5*size of(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\n";
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_COR E_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\n";
glfwTerminate();
return NULL;
}

glfwMakeContextCurrent(window);

glewExperimental = GL_TRUE;
if(glewInit() != GLEW_OK){//check glew an initialize
cout << "GLEW failed to initialize\n";
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

john_connor
01-02-2017, 01:56 AM
there is nothing wrong with that code
if you put that code into the constructor of a class, you have to make sure that you create instances of that class after creating the window + GL context and after initializing OpenGL functions (e.g. GLEW, whatever you use)

Ben Beazley
01-02-2017, 04:19 AM
there is nothing wrong with that code
if you put that code into the constructor of a class, you have to make sure that you create instances of that class after creating the window + GL context and after initializing OpenGL functions (e.g. GLEW, whatever you use)

That's what I do, but it still doesn't work, I added my code for initializing the window and opengl above.

OceanJeff40
01-04-2017, 09:02 AM
What I have done in my code, with classes that share/use the same context (ie. VAO, VBO, EBO, textures, etc.), is with the initial object, the 0th instance, I perform the functions to Bind the VAO, VBO and EBO with related opengl functions, then, I just copy all the related variables to (1 to end of instances) variables, and I'm good.

So, I set up the BindVao function in my classes, only run it for instance[0].BindVao. Then create a for loop (for int I=1; I<instance.numberOfInstances; I++) { copy variables from instance[0] to instance[I] }

That way, during your initialization, I'm not running more opengl function calls, and creating more variables than necessary.

Jeff