void Chunk::init() { if (!isInit) { // Create the blocks m_blocks = new Block**[CHUNK_SIZE]; for (int i = 0; i < CHUNK_SIZE; i++) { m_blocks[i] = new Block*[CHUNK_SIZE]; for (int j = 0; j < CHUNK_SIZE; j++) { m_blocks[i][j] = new Block[CHUNK_SIZE]; } } if (!wasInit){ int R = distribution(randEngine); int G = distribution(randEngine); int B = distribution(randEngine); m_color = Color8(R, G, B, 255); wasInit = true; } shouldUpdate = true; isInit = true; } }
int main() { if (!glfwInit()) // Initialize glfw and check for errors. { Debug_Log("glfw failed to init"); } Window m_window; // Create a window. if (m_window.init(WINDOW_WITDH, WINDOW_HEIGHT)) // Initialize the window. { Debug_Log(m_window.getTitle() << " window has been initiated"); } if (glewInit()) // Initialize glew and check for errors, please note that you must init glew after creating a window and initializing it, and before using any gl calls. { Debug_Log("glew failed to init"); } InputManager m_inputManager; // Create an input manager object m_inputManager.init(m_window.getWindowHandler()); // Init it GLProgram m_program;// Create a GLProgram object for debug purposes m_program.loadShaders("Shaders/Shader.vert", "Shaders/Shader.frag"); // test if the shaders compile GLuint m_vao; /// this is the vertex array object, unimportant for now, and the program can run without it. glGenVertexArrays(1, &m_vao); glBindVertexArray(m_vao); Vertex2D verts[6]; verts[0].setPosition(0.0f, 1.0f); verts[0].setColor(Color8(255, 255, 0)); verts[1].setPosition(1.0f, 0.0f); verts[1].setColor(Color8(255, 0, 255)); verts[2].setPosition(0.0f, 0.0f); verts[2].setColor(Color8(0, 255, 0)); verts[3].setPosition(1.0f, 0.0f); verts[3].setColor(Color8(0, 0, 255)); verts[4].setPosition(1.0f, 1.0f); verts[4].setColor(Color8(0, 255, 255)); verts[5].setPosition(0.0f, 1.0f); verts[5].setColor(Color8(255, 0, 0)); Debug_Log("size of vertecies" << sizeof(verts)); Debug_Log(sizeof(Vertex3D)); GLuint m_vbo; ///< this is the vertex buffer object. glGenBuffers(1, &m_vbo); glBindBuffer(GL_ARRAY_BUFFER, m_vbo); glBufferData(GL_ARRAY_BUFFER, sizeof(verts), verts, GL_STATIC_DRAW); /* VERTEX:1 POSITION - 1 COLOR - 1 TEXTURE-COORD -1 VERTEX:2 POSITION-1 COLOR-1 TEXTURE-COORD-1 */ //glUseProgram(m_program.getID()); m_program.addAttribute("vertexPos", 2, GL_FLOAT, GL_FALSE, sizeof(Vertex2D), (void*)offsetof(Vertex2D,position)); m_program.addAttribute("vertexColor", 3, GL_UNSIGNED_BYTE, GL_TRUE, sizeof(Vertex2D), (void*)offsetof(Vertex2D, color)); glm::mat4 projection; /*= glm::perspective(45.0f, 4.0f / 3.0f, 0.1f, 100.0f);*/ glm::mat4 view; /*glm::lookAt(glm::vec3(0.0, -5.0, 0.0), glm::vec3(0.0, 0.0, 0.0), glm::vec3(0.0, 0.0, 1.0));*/ glm::mat4 model;/* = glm::mat4(1.0f);*/ glm::mat4 MVP = projection * view * model; GLint mvpLocation = glGetUniformLocation(m_program.getID(), "MVP"); glUniformMatrix4fv(mvpLocation, 1, GL_FALSE, glm::value_ptr(MVP)); while (!m_window.shouldWindowClose()) // The game loop. { m_inputManager.update(); if(m_inputManager.isKeyPressed(KEYS::ESC)) // Check if pressed escape. { m_window.setWindowClose(); // Close the window if you press the escape button. } glClearColor(1.0f, 1.0f, 1.0f, 1.0f); // set the background color glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); m_program.use(); glDrawArrays(GL_TRIANGLES, 0, 6); m_program.unuse(); m_window.update(); // Update the window. } glDeleteBuffers(1, &m_vbo); glDeleteVertexArrays(1, &m_vao); m_program.deleteProgram(); m_window.destroy(); return 0; }
void Chunk::draw(Renderer* renderer, glm::vec2 &ChunkPos, BlockClicked ¤tBlock) { Color8 colors[] {Color8(102, 51, 0, 255), Color8(124, 252, 0, 255), Color8(255, 255, 255, 255) }; //Debug_Log("Chunk is drawing"); for (int i = 0; i < CHUNK_SIZE; i++) { for (int j = 0; j < CHUNK_SIZE; j++) { for (int k = 0; k < CHUNK_SIZE; k++) { bool shouldDraw = true; Color8 blockColor = colors[0]; // bool isGrass = false; if (!m_blocks[i][j][k].getActive()) { shouldDraw = false; } bool jPositive = false; if (shouldDraw) { bool iNegative = false; if (i > 0) iNegative = m_blocks[i - 1][j][k].getActive(); bool iPositive = false; if (i < CHUNK_SIZE - 1) iPositive = m_blocks[i + 1][j][k].getActive(); bool jNegative = false; if (j > 0) jNegative = m_blocks[i][j - 1][k].getActive(); if (j < CHUNK_SIZE - 1) jPositive = m_blocks[i][j + 1][k].getActive(); bool kNegative = false; if (k > 0) kNegative = m_blocks[i][j][k - 1].getActive(); bool kPositive = false; if (k < CHUNK_SIZE - 1) kPositive = m_blocks[i][j][k + 1].getActive(); if (iNegative && iPositive && jNegative &&jPositive && kNegative && kPositive) { shouldDraw = false; } if (!jPositive) { blockColor = colors[1]; } if ((currentBlock.blockX == i) && (currentBlock.blockY == j) && (currentBlock.blockZ == k)) { blockColor = colors[2]; } } if (rows.empty()) { rows.emplace_back(k, k, blockColor, shouldDraw); } else { if ((!rows.back().color.isEquals(blockColor)) || (rows.back().shouldbeDrawn != shouldDraw)) { rows.emplace_back(k, k, blockColor, shouldDraw); } else { rows.back().end = k; } } } for (int index = 0; index < rows.size(); index++) { rows[index].Update(); //Debug_Log("position of the row is" << i << "," << j << "," << (int)rows[index].start << "," << (int)rows[index].end<<" should be drawn "<<rows[index].shouldbeDrawn); if (rows[index].shouldbeDrawn) { glm::vec3 rowStart(ChunkPos.x + i, j, ChunkPos.y + rows[index].start); glm::vec3 scale(BLOCK_WIDTH, BLOCK_WIDTH, BLOCK_WIDTH*rows[index].length); renderer->draw(rowStart, scale, rows[index].color); } } rows.clear(); } } }
int _tmain(int argc, _TCHAR* argv[]) { Camera* camera = &Camera(); camera->position.x = 0; camera->position.y = 0; camera->position.z = 0; camera->viewPort.x = 240; camera->viewPort.y = 240; camera->focalDistance=100; camera->fogDistance=150; Scene* mainScene = new Scene(*camera); for(int i=0;i<=3;i++) { //for(int j=0;j<4;j++) { Color8 green; green.setColor(0, 255, 0, 255); Color8 red; red.setColor(255, 0, 0, 255); Color8 blue; blue.setColor(0, 0, 255, 255); Color8 white; white.setColor(255, 255, 255, 255); Vertice*v1=new Vertice(0, 0, 0, green); Vertice*v2=new Vertice(0, 50, 0, red); Vertice*v3=new Vertice(50, 0, 0, blue); Triangle* t = new Triangle(*v1, *v2, *v3); std::list<Triangle> tris; tris.push_back(*t); Object3D* triangle = new Object3D(MESH, tris, Vector3(i*50-50*2, 40, 20)); //triangle->rotate(-PI/8*(i), 0, 0); //mainScene->addObject(triangle); /*Color8* yellow = &Color8(i*55, 255, 255, 255); Sphere* cube2 = &Sphere(20, &Vertice(i*30, 66, 0, *yellow)); //cube2->scale(&Vector3(1, 2, 1)); mainScene->addObject((Object3D*)cube2);*/ } } Color8 white; white.setColor(255, 255, 255, 255); Object3D* plane = new Object3D(MESH, getPlane(30, white), Vector3(0, 0, 0)); plane ->rotate(PI/2, 0, 0); //mainScene->addObject(plane); Color8 color2; color2.setColor(0, 0, 255, 255); Sphere* sphere = new Sphere(40,&Vertice(30, 30, 30, white)); mainScene->addObject((Object3D*)sphere); Sphere* sphere2 = new Sphere(40,&Vertice(-30, 30, 60, white)); mainScene->addObject((Object3D*)sphere2); Sphere* sphere6 = new Sphere(60,&Vertice(0, 0, 60, Color8(255, 0, 0, 255))); mainScene->addObject((Object3D*)sphere6); Cylinder* sphere3 = new Cylinder(&Vector3(0, 20, 50), &Vector3(0, 20, 0), 20, &Color8(255, 0, 0, 255)); //mainScene->addObject((Object3D*)sphere3); Cylinder* sphere5 = new Cylinder(&Vector3(0, 0, 0), &Vector3(10, 30, 20), 10, &color2); //mainScene->addObject((Object3D*)sphere5); Cylinder* sphere4 = new Cylinder(&Vector3(-50, 0, 20), &Vector3(0, 0, 30), 20, &color2); //mainScene->addObject((Object3D*)sphere4); Light* light = new Light(DIRECTIONAL, Vector3(), Color8(255, 255, 255, 255), Vector3(1, 0, 0)); //mainScene->addLight(light); Light* light2 = new Light(DIRECTIONAL, Vector3(), Color8(255, 255, 255, 255), Vector3(1, 1, 0)); mainScene->addLight(light2); Renderer* renderer = new Renderer(*mainScene); ppm_image testImage = renderer->render(PERSPECTIVE); BmpExporter* exporter = new BmpExporter("test.bmp"); exporter->export2(&testImage); return 0; }