简体   繁体   中英

Creating a Model class in OpenGl

Hi All I am trying to create a model class in opengl and create an instance of the model class in a game. I am able to just render one object and the second object doesnt render.

I had the same problem when all of it was in the same class. I created a seperate VBO and VAO for bot the objects and the problems was solved.

Now I am trying to create a class out of it and now I am facing same problem. Since it is a class aren't separate VAO and VBO created??

My model class and game class are as below.

#include "FlatModel.h"

#include "Camera.h"

GLfloat vertices[] = {

//position              
-0.5f, -0.5f, 0.0f,   
-0.5f, 0.5f, 0.0f,   
0.5f, 0.5f, 0.0f,     
0.5f, -0.5f, 0.0f    
};

GLuint indices[] = {
// front
0, 1, 2,
0, 2, 3
};


 FlatModel::FlatModel(Camera* _camera, glm::vec3 color) {

camera = _camera;
objectColor = color;

ShaderLoader *shaderLoader = 0;
program = shaderLoader->CreateProgram("shaders/FlatModel.vs","shaders/FlatModel.fs");

glEnable(GL_DEPTH_TEST);
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);

glGenVertexArrays(1, &vao);
glBindVertexArray(vao);

glGenBuffers(1, &vbo);
glGenBuffers(1, &ebo);

glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);

glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices),  indices, GL_STATIC_DRAW);

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

glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
}

FlatModel::~FlatModel() {
}

void FlatModel::update(GLfloat time) {

GLint currentTimeLocation = glGetUniformLocation(program, "currentTime");
glUniform1f(currentTimeLocation, time);

GLint objectColorLoc = glGetUniformLocation(program, "objectColor");
glUniform3f(objectColorLoc, objectColor.x, objectColor.y, objectColor.z);

glm::mat4 mvp = camera->getprojectionMatrix() * camera->getViewMatrix() * model;
GLint mvpLoc = glGetUniformLocation(program, "mvp");
glUniformMatrix4fv(mvpLoc, 1, GL_FALSE, glm::value_ptr(mvp));
}


 void FlatModel::render() {

glUseProgram(this->program);
glBindVertexArray(vao);

glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
glBindVertexArray(0);

}

And here is the game class

#include "Game.h"

void Game::GameInit(){

camera = new Camera(45.0f, WIDTH, HEIGHT, 0.1f, 100.0f);
camera->setCameraSpeed(0.05f);

ground = new FlatModel(camera, glm::vec3(1.0f, 1.0f, 0.5f));
ground->setScale(glm::vec3(3.0f, 0.5f, 1.0f));
ground->setPosition(glm::vec3(0.0f, -0.5f, 0.0f));

box = new FlatModel(camera, glm::vec3(1.0f, 0.0f, 0.0f));
box->setScale(glm::vec3(1.0f, 0.5f, 1.0f));
box->setPosition(glm::vec3(0.0f, 0.5f, 0.0f));
}

void Game::GameUpdate(){

camera->update();

GLfloat currentTime = glutGet(GLUT_ELAPSED_TIME);
currentTime = currentTime / 1000;

ground->update(currentTime);
box->update(currentTime);
}

void Game::GameDraw(){

glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

ground->render();
box->render();

}

Here is the output. Only the box shows and the ground doesnt.

在此处输入图片说明

You have to pass uniforms to shader, in Draw call not in Update. Because you activate your shader in Draw.

Check this example http://in2gpu.com/2015/06/04/drawing-a-cube/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM