Jump to content
  • Advertisement


  • Content Count

  • Joined

  • Last visited

Community Reputation

126 Neutral

About htmlboss

  • Rank

Personal Information

  • Role
  • Interests

Recent Profile Visitors

The recent visitors block is disabled and is not being shown to other users.

  1. I'm trying to combine a forward rendered skybox with deferred rendered geometry, but for some reason the geometry becomes black when I blit the depth buffer to the default framebuffer to render the skybox (otherwise the skybox draws over everything). Here are 2 photos since they're worth 2000 words  :P   Before blitting:   [attachment=34867:Untitled.png]   After blitting:   [attachment=34866:Untitled.png]   What could be the culprit? Blending? Wrong shader code? Let me know if any code samples are needed. Thanks!  :D   I'm also planning to figure out what's with the massive performance drop between the two (11 ms vs 3 ms)...probably bad texture binding.
  2. htmlboss

    Help with glDrawElementsInstanced

    That fixed it! I'm still trying to wrap my head around OpenGL being STATE-DRIVEN (it doesn't help that I'm mixing object oriented C++ and procedural OpenGL, but I guess that's part of the learning process)    Thanks so much! 
  3. htmlboss

    Help with glDrawElementsInstanced

    Here is the code for the other arrays (this function is called in each Mesh's c-tor) and the Vertex struct (if it helps any). The setup for the offset attribute is separate because some models may not need to be drawn using instancing.: void Mesh::setupMesh() { glGenVertexArrays(1, &m_vao); glGenBuffers(1, &m_vbo); glGenBuffers(1, &m_ebo); glBindVertexArray(m_vao); glBindBuffer(GL_ARRAY_BUFFER, m_vbo); glBufferData(GL_ARRAY_BUFFER, m_vertices.size() * sizeof(Vertex), &m_vertices.at(0), GL_STATIC_DRAW); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_ebo); glBufferData(GL_ELEMENT_ARRAY_BUFFER, m_indices.size() * sizeof(GLuint), &m_indices.at(0), GL_STATIC_DRAW); // Vertex Attributes // Vertex Positions glEnableVertexAttribArray(0); glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (GLvoid*)0); // Vertex Normals glEnableVertexAttribArray(1); glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (GLvoid*)offsetof(Vertex, Normal)); // Vertex Texture Coords glEnableVertexAttribArray(2); glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (GLvoid*)offsetof(Vertex, TexCoords)); glBindVertexArray(0); } Vertex: struct Vertex { glm::vec3 Position; glm::vec3 Normal; glm::vec2 TexCoords; }; And yes, this technically should allow me to move each vertex individually (learnopengl.com discussed how the same mesh data is just shared to the other instance, except for an offset from the original position variable in the Vertex struct, instead of allocating a new "floor" model and wasting memory).
  4. SOLVED   I've been reading through the OpenGL instancing tutorial on learnopengl.com, and I'm trying to implement it using glDrawElementsInstanced, since I have Assimp doing my mesh loading. My problem is that the "floor" (see the wood-textured model in the attached pic) is drawing perfectly fine, but the positional offsets for instancing that I'm passing to OpenGL have no effect. What could be the problem? Thanks in advance!    floorVS.glsl: #version 430 layout (location = 0) in vec3 position; layout (location = 1) in vec3 normal; layout (location = 2) in vec2 texCoords; layout (location = 3) in vec3 offset; // For 3D instancing layout (std140, binding = 0) uniform Matrices { mat4 projection; mat4 view; }; uniform mat4 model; out vec2 TexCoords; out vec3 FragPos; out vec3 Normal; void main() { gl_Position = projection * view * model * vec4(position + offset, 1.0); // Add provided offset FragPos = vec3(model * vec4(position, 1.0f)); Normal = mat3(transpose(inverse(model))) * normal; TexCoords = texCoords; } I have a Model class that contains numerous Mesh classes (since a complete model may contain several objects), so I need to make sure each mesh gets the offset data:   This function is run per mesh to separately add instancing data: void Mesh::SetInstancing(const std::vector<glm::vec3>& instanceOffsets) { m_instanceOffsets = instanceOffsets; glGenBuffers(1, &m_instanceVBO); glBindBuffer(GL_ARRAY_BUFFER, m_instanceVBO); glBufferData(GL_ARRAY_BUFFER, sizeof(glm::vec3) * m_instanceOffsets.size(), &m_instanceOffsets.at(0), GL_STATIC_DRAW); glBindBuffer(GL_ARRAY_BUFFER, 0); // Vertex Instance offset glEnableVertexAttribArray(3); glBindBuffer(GL_ARRAY_BUFFER, m_instanceVBO); glVertexAttribPointer(3, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), (GLvoid*)0); glBindBuffer(GL_ARRAY_BUFFER, 0); glVertexAttribDivisor(3, 1); // Tell OpenGL this is an instanced vertex attribute. glBindVertexArray(0); } Now to draw the mesh. The bindTexures function is irrelevant to my question (it just loops through a vector of Textures and binds them): void Mesh::DrawInstanced(const Shader& shader) { bindTextures(shader); // Draw instanced mesh glBindVertexArray(m_vao); glDrawElementsInstanced(GL_TRIANGLES, m_indices.size(), GL_UNSIGNED_INT, 0, m_instanceOffsets.size()); glBindVertexArray(0); } Picture: There should be 2 floor object being rendered here (and the displayed floor should not be at the origin): [attachment=33104:help.png]   Please let me know of any more clarification is needed!
  5. With the draw loop for the asteroids, how would I change the active texture to the image texture found at the filepath that I pass to the Asteroid constructor? void MainGame::prepareTextures() { //compile shaders and get Texlocations initShaders("Shaders/background.vert", "Shaders/background.frag"); GLint TexLoc = _colorProgram.getUniformLocation("bgTexture"); glActiveTexture(GL_TEXTURE0); } void MainGame::drawGame() { ... //Asteroids m_spriteBatch.Begin(); //draw asteroids for (int i = 0; i < m_asteroids.size(); i++) { m_asteroids.draw(m_spriteBatch); } m_spriteBatch.End(); m_spriteBatch.renderbatch(); ... } void Asteroids::draw(Engine::SpriteBatch& spriteBatch) { glm::vec4 uv(0.0f, 0.0f, 1.0f, 1.0f); //convert m_imgNum to string and remove trailing zeros std::string strImgNum = std::to_string(m_imgNum); strImgNum.erase(strImgNum.find_last_not_of('0') + 1, std::string::npos); //construct filpath std::string filePath = m_dir + strImgNum + ".png"; static Engine::GLTexture texture = Engine::ResourceManager::GetTexture(filePath, 0, 0, 32, 4); Engine::Color color; color.r = 255; color.g = 255; color.b = 255; color.a = 255; glm::vec4 posAndSize = glm::vec4(m_posX, m_posY, m_width, m_height); spriteBatch.Draw(posAndSize, uv, texture.id, 0.0f, color); } void SpriteBatch::renderbatch() { glBindVertexArray(m_vao); for (unsigned int i = 0; i < m_renderBatches.size(); i++) { glBindTexture(GL_TEXTURE_2D, m_renderBatches.texture); glDrawArrays(GL_TRIANGLES, m_renderBatches.offset, m_renderBatches.numVertices); } glBindVertexArray(0); } }
  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!