コード例 #1
0
void ColoredCubeApp::updateScene(float dt)
{
	D3DApp::updateScene(dt);
	xLine.update(dt);
	yLine.update(dt);
	zLine.update(dt);
	quad1.update(dt);
	quad2.update(dt);
	quad3.update(dt);
	quad4.update(dt);
	quad5.update(dt);

	for(int i = 0; i < WALL_SIZE; ++i) {
		wall[i].update(dt);
	}

	D3DXVECTOR3 pos(0.0f,0.0f,15.0f);
	D3DXVECTOR3 target(0.0f, 0.0f, 0.0f);
	D3DXVECTOR3 up(0.0f, 1.0f, 0.0f);
	D3DXMatrixLookAtLH(&mView, &pos, &target, &up);
}
コード例 #2
0
ファイル: main.cpp プロジェクト: MekaGem/opengl-course-mipt
int main() {
    std::string dataRoot;
#ifdef DATA_ROOT
    dataRoot = DATA_ROOT;
#else
    std::cerr << "No DATA_ROOT path found" << std::endl;
    return -1;
#endif

    GLFWwindow *window = nullptr;

    if (initWindow(window)) {
        return -1;
    }

    Shader shader(dataRoot + "/data/shaders/shader.vert", dataRoot + "/data/shaders/shader.frag");

    Texture textures(dataRoot + "/data/assets/textures.png");
    Texture normals(dataRoot + "/data/assets/normals.png");

    std::vector<GLfloat> vertices;
    std::vector<GLuint> indices;

    std::vector<Quad> quads;

    Map map(10, 10);
    for (int x = 0; x < map.getWidth(); ++x) {
        for (int y = 0; y < map.getHeight(); ++y) {
            if (map.isPassable(x, y)) {
                quads.push_back(Quad(glm::vec3(x, 0, -y), glm::vec3(0, 0, -1), glm::vec3(1, 0, 0), 0.5f, 0.0f));
                quads.push_back(Quad(glm::vec3(x, 1, -y), glm::vec3(1, 0, 0), glm::vec3(0, 0, -1), 0.0f, 0.5f));
            }
            for (int direction = 0; direction < 4; ++direction) {
                int cx = x + dx[direction];
                int cy = y + dy[direction];
                if (map.isPassable(x, y) && !map.isPassable(cx, cy)) {
                    quads.push_back(Quad(
                            glm::vec3(x, 0, -y) + WALL_SHIFT[direction],
                            glm::vec3(0, 1, 0),
                            WALL_DIRECTION[direction],
                            0.0f,
                            0.0f
                    ));
                }
            }
        }
    }

    for (int index = 0; index < quads.size(); ++index) {
        Quad quad = quads[index];
        quad.update(vertices);

        indices.push_back(GLuint(4 * index + 0));
        indices.push_back(GLuint(4 * index + 1));
        indices.push_back(GLuint(4 * index + 2));

        indices.push_back(GLuint(4 * index + 2));
        indices.push_back(GLuint(4 * index + 3));
        indices.push_back(GLuint(4 * index + 0));
    }

    GLuint VBO, VAO, EBO;
    createBuffers(VBO, VAO, EBO, vertices, indices);

    glm::mat4 projection = glm::perspective(45.0f, WIDTH / (float) HEIGHT, 0.1f, 100.0f);
    glm::vec3 lamp(map.getWidth() / 2, 0.9f, -map.getHeight() / 2);

    float time = (float) glfwGetTime();

    while (!glfwWindowShouldClose(window)) {
        glfwPollEvents();

        float cTime = (float) glfwGetTime();
        float delta = cTime - time;
        time = cTime;

        update(map, delta);

        glClearColor(117 / 255.0f, 187 / 255.0f, 253 / 255.0f, 1.0f);
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

        shader.use();

        GLint textureLocation = glGetUniformLocation(shader.get(), "textureSampler");
        glUniform1i(textureLocation, 0);

        GLint normalLocation = glGetUniformLocation(shader.get(), "normalSampler");
        glUniform1i(normalLocation, 1);

        glActiveTexture(GL_TEXTURE0);
        glBindTexture(GL_TEXTURE_2D, textures.get());

        glActiveTexture(GL_TEXTURE1);
        glBindTexture(GL_TEXTURE_2D, normals.get());

        GLfloat timeValue = (GLfloat) glfwGetTime();

        GLfloat xValue = (GLfloat) (sin(timeValue) / 4) + 0.25f;
        GLint xValueLocation = glGetUniformLocation(shader.get(), "xValue");
        glUniform1f(xValueLocation, xValue);

        GLfloat yValue = (GLfloat) (cos(timeValue + 0.2) / 4) + 0.25f;
        GLint yValueLocation = glGetUniformLocation(shader.get(), "yValue");
        glUniform1f(yValueLocation, yValue);

        glm::mat4 model;
        model = glm::rotate(model, rotation, glm::vec3(0.0f, 1.0f, 0.0f));
        model = glm::translate(model, -cameraPos);

        GLint modelLocation = glGetUniformLocation(shader.get(), "model");
        glUniformMatrix4fv(modelLocation, 1, GL_FALSE, glm::value_ptr(model));

        glm::mat4 view;
        glm::vec3 eye(0.0f, 0.5f, 0.0f);
        view = glm::lookAt(eye, eye + cameraFront, cameraUp);

        GLint viewLocation = glGetUniformLocation(shader.get(), "view");
        glUniformMatrix4fv(viewLocation, 1, GL_FALSE, glm::value_ptr(view));

        GLint projectionLocation = glGetUniformLocation(shader.get(), "projection");
        glUniformMatrix4fv(projectionLocation, 1, GL_FALSE, glm::value_ptr(projection));

        GLint lightSourceLocation = glGetUniformLocation(shader.get(), "lightSource");
        if (spotlight) {
            glUniform3f(lightSourceLocation, cameraPos.x, cameraPos.y + 0.5f, cameraPos.z);
        } else {
            glUniform3f(lightSourceLocation, lamp.x, lamp.y, lamp.z);
        }

        glm::vec3 lookDirection = glm::normalize(glm::rotate(cameraFront, -rotation, cameraUp));
        GLint lookDirectionLocation = glGetUniformLocation(shader.get(), "lookDirection");
        glUniform3f(lookDirectionLocation, lookDirection.x, lookDirection.y, lookDirection.z);

        GLint spotlightLocation = glGetUniformLocation(shader.get(), "spotlight");
        glUniform1i(spotlightLocation, spotlight);

        glBindVertexArray(VAO);
        glDrawElements(GL_TRIANGLES, (GLuint) indices.size(), GL_UNSIGNED_INT, 0);
        glBindVertexArray(0);

        glfwSwapBuffers(window);
    }

    disposeBuffers(VBO, VAO, EBO);

    glfwTerminate();
    return 0;
}