View Full Version : Trying to render objects on click

03-09-2017, 12:52 PM
Hello, I'm trying to "spawn" a bullet each time the user left-clicks, but it disappears once it's been rendered. It's being rendered at the right spot, but won't stay rendered (I think it's because I'm rendering it in an if statement?). I essentially just want to be able to left lick, a new bullet will be rendered on screen at my position, then I can move and render another (so there will be multiple floating bullets). Here's my code so far:

static int lcOldState = GLFW_RELEASE;
int lcNewState = glfwGetMouseButton(window, GLFW_MOUSE_BUTTON_LEFT);
if (lcNewState == GLFW_RELEASE && lcOldState == GLFW_PRESS && bulletCount > 0) {

glm::vec3 bulletPosition = glm::vec3(getPosition().x, getPosition().y, getPosition().z);
glm::quat bulletRotation = glm::normalize(glm::quat(glm::vec3(1.5708, 0, 0)));

glBindTexture(GL_TEXTURE_2D, bulletTexture);
glUniform1i(TextureID, 0);

glm::mat4 BulletRotationMatrix = glm::toMat4(bulletRotation);
glm::mat4 BulletTranslationMatrix = translate(mat4(), glm::vec3(0.35, 0, -3));
glm::mat4 BulletScaleMatrix = scale(mat4(), glm::vec3(0.1f, 0.1f, 0.1f));
glm::mat4 ModelMatrix2 = BulletTranslationMatrix * BulletRotationMatrix * BulletScaleMatrix;
glm::mat4 MVP2 = ProjectionMatrix * ModelMatrix2;

// Send our transformation to the currently bound shader,
// in the "MVP" uniform
glUniformMatrix4fv(MatrixID, 1, GL_FALSE, &MVP2[0][0]);
glUniformMatrix4fv(ModelMatrixID, 1, GL_FALSE, &ModelMatrix2[0][0]);

// 1st attribute buffer : vertices
glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer2);
0, // attribute
3, // size
GL_FLOAT, // type
GL_FALSE, // normalized?
0, // stride
(void*)0 // array buffer offset

// 2nd attribute buffer : UVs
glBindBuffer(GL_ARRAY_BUFFER, uvbuffer2);
1, // attribute
2, // size
GL_FLOAT, // type
GL_FALSE, // normalized?
0, // stride
(void*)0 // array buffer offset

// 3rd attribute buffer : normals
glBindBuffer(GL_ARRAY_BUFFER, normalbuffer2);
2, // attribute
3, // size
GL_FLOAT, // type
GL_FALSE, // normalized?
0, // stride
(void*)0 // array buffer offset

// Index buffer
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, elementbuffer2);

// Draw the triangles
glDrawElements(GL_TRIANGLES, bulletIndices.size(), GL_UNSIGNED_SHORT, (void*)0);

btDefaultMotionState* bulletMotionstate = new btDefaultMotionState(btTransform(
btQuaternion(bulletRotation.x, bulletRotation.y, bulletRotation.z, bulletRotation.w),
btVector3(bulletPosition.x, bulletPosition.y, bulletPosition.z)

btRigidBody::btRigidBodyConstructionInfo bulletRigidBodyCI(bulletMass, bulletMotionstate, bulletCollisionShape, bulletInertia);
btRigidBody *bulletRigidBody = new btRigidBody(bulletRigidBodyCI);


btTransform bulletTrans;
bulletPosition.x = bulletTrans.getOrigin().getX();
bulletPosition.y = bulletTrans.getOrigin().getY();
bulletPosition.z = bulletTrans.getOrigin().getZ();

lcOldState = lcNewState;

That's in my main game's do-while loop, where I do ray picking and render the static objects. What can I do to make the bullet objects stay after being rendered?

I also need to get that created bullet's position so that I can make a rigid body at that position, but since I need the bullet to be created at the end of my gun model (and thus the bullet isn't in world space, but camera space) then I'm not sure how to do this.