วันพฤหัสบดีที่ 15 ธันวาคม พ.ศ. 2559

ย้ายกล้องแบบลื่นๆ


TestApp.h


#pragma once

#include <iostream>
#include <GL/glew.h>
#include <glfw3.h>
#include "Shader.h"
#include "Texture.h"


#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>


class TestApp
{
public:
TestApp();
void Update();

static void controller(GLFWwindow* window, int key, int scancode, int action, int mode);

static void DoMovement();

~TestApp();

private:
GLuint VBO, VAO, EBO;
Shader* ourShader;
Texture* texture;
Texture* texture2;


// cubes 10 positions
glm::vec3 cubePositions[10];


static glm::vec3 cameraPos;
static glm::vec3 cameraFront;
static glm::vec3 cameraUp;

};

************************************************************************

TestApp.cpp

#include "TestApp.h"

#include <SOIL.h>




// Inintialize static value

glm::vec3 TestApp::cameraPos = glm::vec3(0.0f, 0.0f, 3.0f);

glm::vec3 TestApp::cameraFront = glm::vec3(0.0f, 0.0f, -1.0f);

glm::vec3 TestApp::cameraUp = glm::vec3(0.0f, 1.0f, 0.0f);



bool keys[1024];



TestApp::TestApp()
: cubePositions{
glm::vec3(0.0f,  0.0f,  0.0f),
glm::vec3(2.0f,  5.0f, -15.0f),
glm::vec3(-1.5f, -2.2f, -2.5f),
glm::vec3(-3.8f, -2.0f, -12.3f),
glm::vec3(2.4f, -0.4f, -3.5f),
glm::vec3(-1.7f,  3.0f, -7.5f),
glm::vec3(1.3f, -2.0f, -2.5f),
glm::vec3(1.5f,  2.0f, -2.5f),
glm::vec3(1.5f,  0.2f, -1.5f),
glm::vec3(-1.3f,  1.0f, -1.5f)
}
{

glEnable(GL_DEPTH_TEST);

ourShader = new Shader("shader.vert", "shader.frag");


GLfloat vertices[] = {
-0.5f, -0.5f, -0.5f,  0.0f, 0.0f,
0.5f, -0.5f, -0.5f,   1.0f, 0.0f,
0.5f,  0.5f, -0.5f,   1.0f, 1.0f,
0.5f,  0.5f, -0.5f,   1.0f, 1.0f,
-0.5f,  0.5f, -0.5f,  0.0f, 1.0f,
-0.5f, -0.5f, -0.5f,  0.0f, 0.0f,

-0.5f, -0.5f,  0.5f,  0.0f, 0.0f,
0.5f, -0.5f,  0.5f,  1.0f, 0.0f,
0.5f,  0.5f,  0.5f,  1.0f, 1.0f,
0.5f,  0.5f,  0.5f,  1.0f, 1.0f,
-0.5f,  0.5f,  0.5f,  0.0f, 1.0f,
-0.5f, -0.5f,  0.5f,  0.0f, 0.0f,

-0.5f,  0.5f,  0.5f,  1.0f, 0.0f,
-0.5f,  0.5f, -0.5f,  1.0f, 1.0f,
-0.5f, -0.5f, -0.5f,  0.0f, 1.0f,
-0.5f, -0.5f, -0.5f,  0.0f, 1.0f,
-0.5f, -0.5f,  0.5f,  0.0f, 0.0f,
-0.5f,  0.5f,  0.5f,  1.0f, 0.0f,

0.5f,  0.5f,  0.5f,  1.0f, 0.0f,
0.5f,  0.5f, -0.5f,  1.0f, 1.0f,
0.5f, -0.5f, -0.5f,  0.0f, 1.0f,
0.5f, -0.5f, -0.5f,  0.0f, 1.0f,
0.5f, -0.5f,  0.5f,  0.0f, 0.0f,
0.5f,  0.5f,  0.5f,  1.0f, 0.0f,

-0.5f, -0.5f, -0.5f,  0.0f, 1.0f,
0.5f, -0.5f, -0.5f,  1.0f, 1.0f,
0.5f, -0.5f,  0.5f,  1.0f, 0.0f,
0.5f, -0.5f,  0.5f,  1.0f, 0.0f,
-0.5f, -0.5f,  0.5f,  0.0f, 0.0f,
-0.5f, -0.5f, -0.5f,  0.0f, 1.0f,

-0.5f,  0.5f, -0.5f,  0.0f, 1.0f,
0.5f,  0.5f, -0.5f,  1.0f, 1.0f,
0.5f,  0.5f,  0.5f,  1.0f, 0.0f,
0.5f,  0.5f,  0.5f,  1.0f, 0.0f,
-0.5f,  0.5f,  0.5f,  0.0f, 0.0f,
-0.5f,  0.5f, -0.5f,  0.0f, 1.0f
};



glGenVertexArrays(1, &VAO);
glGenBuffers(1, &VBO);


// Bind the Vertex Array Object first, then bind and set vertex buffer(s) and attribute pointer(s).
glBindVertexArray(VAO);

glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);



// Vertices
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), (GLvoid*)0);
glEnableVertexAttribArray(0);


// Texture
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), (GLvoid*)(3 * sizeof(GLfloat)));
glEnableVertexAttribArray(2);



glBindBuffer(GL_ARRAY_BUFFER, 0); // Note that this is allowed, the call to glVertexAttribPointer registered VBO as the currently bound vertex buffer object so afterwards we can safely unbind

glBindVertexArray(0); // Unbind VAO (it's always a good thing to unbind any buffer/array to prevent strange bugs), remember: do NOT unbind the EBO, keep it bound to this VAO



texture = new Texture("awesome_face.jpg");

texture2 = new Texture("wall.jpg");



}

void TestApp::Update()
{


this->DoMovement();


glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );



ourShader->Use();


// ******* TEXTURE *******
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, texture->getTextureID() );
glUniform1i(glGetUniformLocation(ourShader->Program, "ourTexture1"), 0);


glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, texture2->getTextureID());
glUniform1i(glGetUniformLocation(ourShader->Program, "ourTexture2"), 1);

float timeValue = glfwGetTime();
GLfloat value = (sin(timeValue) / 2) + 0.5;
glUniform1f(glGetUniformLocation(ourShader->Program, "value"), value);





// ************ VALUE OF MODEL , VIEW , PROJECTION ******************************



// Camera/View transformation
glm::mat4 view;
view = glm::lookAt(cameraPos, cameraPos + cameraFront, cameraUp);


glm::mat4 projection;
projection = glm::perspective( glm::radians(45.0f), (GLfloat)800 / (GLfloat)500, 0.1f, 100.0f);







// ************ LOCATION OF MODEL , VIEW , PROJECTION IN SHADER PROGRAM ******************************
// ************ AND SET VALUE OF IT TO SHADER PROGRAM ******************************
GLuint modelLoc = glGetUniformLocation(ourShader->Program, "model");
//glUniformMatrix4fv(modelLoc, 1, GL_FALSE, glm::value_ptr(model));


GLuint viewLoc = glGetUniformLocation(ourShader->Program, "view");
glUniformMatrix4fv(viewLoc, 1, GL_FALSE, glm::value_ptr(view));

GLuint projectionLoc = glGetUniformLocation(ourShader->Program, "projection");
glUniformMatrix4fv(projectionLoc, 1, GL_FALSE, glm::value_ptr(projection));




// ****************** NOW DRAW IT *************************
glBindVertexArray(VAO);

for (GLuint i = 0; i < 10; i++)
{
glm::mat4 model;
model = glm::translate(model, cubePositions[i]);
GLfloat angle = 20.0f * i+20;
model = glm::rotate(model, (GLfloat)glfwGetTime() * glm::radians(angle), glm::vec3(1.0f, 0.3f, 0.5f));
glUniformMatrix4fv(modelLoc, 1, GL_FALSE, glm::value_ptr(model));

glDrawArrays(GL_TRIANGLES, 0, 36);
}


glBindVertexArray(0);
}




void TestApp::DoMovement()
{

GLfloat cameraSpeed = 0.05f;

if (keys[GLFW_KEY_W])
cameraPos += cameraSpeed * cameraFront;
if (keys[GLFW_KEY_S])
cameraPos -= cameraSpeed * cameraFront;
if (keys[GLFW_KEY_A])
cameraPos -= glm::normalize(glm::cross(cameraFront, cameraUp)) * cameraSpeed;
if (keys[GLFW_KEY_D])
cameraPos += glm::normalize(glm::cross(cameraFront, cameraUp)) * cameraSpeed;
}



void TestApp::controller(GLFWwindow* window, int key, int scancode, int action, int mode)
{


if (key >= 0 && key < 1024)
{
if (action == GLFW_PRESS)
keys[key] = true;
else if (action == GLFW_RELEASE)
keys[key] = false;
}


if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
glfwSetWindowShouldClose(window, GL_TRUE);


}



TestApp::~TestApp()
{
delete ourShader;

delete texture;

delete texture2;

// Properly de-allocate all resources once they've outlived their purpose
glDeleteVertexArrays(1, &this->VAO);
glDeleteBuffers(1, &this->VBO);
// Terminate GLFW, clearing any resources allocated by GLFW.
}


ไม่มีความคิดเห็น:

แสดงความคิดเห็น