Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 3 of 3

Thread: Cubes array shows only one Cube

  1. #1
    Junior Member Newbie
    Join Date
    Dec 2017
    Posts
    5

    Cubes array shows only one Cube

    hello, i'm creating a simple game, and i'm trying to display few cubes in the screen, but somehow my cube array is showing only one cube, doesn't matter how big the array is.
    here is my source:

    Cube.hpp:
    Code :
    #ifndef _CUBE_HPP
    #define _CUBE_HPP
     
    #include <GL/glew.h>
    #include <glm/glm.hpp>
    #include <cstring>
     
    #include "functions.hpp"
     
    class Cube : public Functions
    {
    	private:
    		GLuint VertexArrayID;
    		GLuint Texture, TextureID;
     
    		GLuint VertexBuffer;
    		GLuint uvBuffer;
     
    		std::vector<glm::vec3> vertices;
    		std::vector<glm::vec2> uvs;
    		std::vector<glm::vec3> normals;
     
    		glm::vec3 scale = glm::vec3(0.5f, 0.5f, 0.5f);
    		glm::vec3 position;
     
    	public:
    		explicit Cube(glm::vec3 position,
    				 	  GLuint& programID);
     
    		void Draw(GLuint& programID);
     
    		inline glm::vec3 getScale() const
    		{
    			return scale;
    		}
     
    		inline glm::vec3 getPosition() const
    		{
    			return position;
    		}
     
    		virtual ~Cube();
    };
     
    #endif

    Cube.cpp:
    Code :
    #include "cube.hpp"
     
    Cube::Cube(glm::vec3 position,
    		   GLuint& programID)
    {
    	// Creating VAO
    	glGenVertexArrays(1, &VertexArrayID);
    	glBindVertexArray(VertexArrayID);
     
    	// ----- Loading Cube.obj ----- //
    	bool res = Load3D("res/cube.obj", vertices, uvs, normals);
    	if(res == false)
    		exit(EXIT_FAILURE);
     
    	// Set inside Buffers
    	glGenBuffers(1, &VertexBuffer);
    	glBindBuffer(GL_ARRAY_BUFFER, VertexBuffer);
    	glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(glm::vec3), &vertices[0], GL_STATIC_DRAW);
     
    	// Load Textures
    	Texture = LoadBMP("res/block.bmp");
    	TextureID = glGetUniformLocation(programID, "myTextureSampler");
     
    	glGenBuffers(1, &uvBuffer);
    	glBindBuffer(GL_ARRAY_BUFFER, uvBuffer);
    	glBufferData(GL_ARRAY_BUFFER, uvs.size() * sizeof(glm::vec2), &uvs[0], GL_STATIC_DRAW);
    }
     
    void Cube::Draw(GLuint& programID)
    {
    	// Use Shaders
    	glUseProgram(programID);
     
    	// Bind Texture to Cube
    	glActiveTexture(GL_TEXTURE0);
    	glBindTexture(GL_TEXTURE_2D, Texture);
    	glUniform1i(TextureID, 0);
     
    	// Draw Cube
    	glEnableVertexAttribArray(0);
    	glBindBuffer(GL_ARRAY_BUFFER, VertexBuffer);
    	glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, (void*)0);
     
    	glEnableVertexAttribArray(1);
    	glBindBuffer(GL_ARRAY_BUFFER, uvBuffer);
    	glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, (void*)0);
     
    	glDrawArrays(GL_TRIANGLES, 0, vertices.size());
     
    	glDisableVertexAttribArray(0);
    	glDisableVertexAttribArray(1);
    }
     
    Cube::~Cube()
    {
    	// Delete Buffers
    	glDeleteBuffers(1, &VertexBuffer);
    	glDeleteBuffers(1, &uvBuffer);
     
    	glDeleteTextures(1, &Texture);
     
    	glDeleteVertexArrays(1, &VertexArrayID);
    }

    main.cpp:
    Code :
    #include <GL/glew.h>
    #include <GLFW/glfw3.h>
     
    #include "src/window.hpp"
    #include "src/cube.hpp"
    #include "src/camera.hpp"
    #include "src/functions.hpp"
     
    #define WIDTH 800
    #define HEIGHT 800
     
    int main()
    {
    	Functions funcs;
     
    	Window window(WIDTH, HEIGHT, "Simple Game");
     
    	// Creating Shader
    	GLuint programID = funcs.LoadShaders("src/shaders/cube.vert", "src/shaders/cube.frag");
     
    	// Creating Cube
    	Cube cube[2] = 
    	{
    		Cube(glm::vec3(5, 5, 5), programID),
    		Cube(glm::vec3(7, 7, 7), programID)
    	};
     
    	Camera camera(glm::vec3(0, 0, 5), 90.f, 0.001f);
    	camera.Setup(*window.getWindow(), programID);
     
    	glClearColor(0.53f, 0.81f, 0.98f, 1.0f);
     
    	while(window.Running())
    	{
    		glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
     
    		for(unsigned int i = 0; i < 2; i++)
    		{
    			cube[i].Draw(programID);
    			camera.Update(cube[i].getScale(), cube[i].getPosition());
    		}
     
    		window.SwapBuffers();
    	}
     
    	glDeleteProgram(programID);
     
    	return 0;
    }

    i have tried to get my VAO outside the class and access his address everytime i add new cube but that failed.
    i also tried to put everything in my class and create an Add function to add more cubes with vectors but that failed.

    thanks for help...

  2. #2
    Senior Member OpenGL Guru Dark Photon's Avatar
    Join Date
    Oct 2004
    Location
    Druidia
    Posts
    4,173
    Take a look at your Cube constructor. You're not doing anything with position.

    Also, you didn't show Camera::Update(), which is where that position "would" be used if you were setting it. That said, I have no idea what object positions have to do with updating the camera.

  3. #3
    Junior Member Newbie
    Join Date
    Dec 2017
    Posts
    5
    Quote Originally Posted by Dark Photon View Post
    Take a look at your Cube constructor. You're not doing anything with position.

    Also, you didn't show Camera::Update(), which is where that position "would" be used if you were setting it. That said, I have no idea what object positions have to do with updating the camera.
    that's what happend when i'm trying for over an 2 hours to fix that. i start missing simple things...
    i added the position to the constructor and it worked !

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •