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

เลื่อนวัตถุ ซ้าย - ขวา โดยใช้ ปุ่ม a ,d


TestApp.h

#pragma once

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

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

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


~TestApp();

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

static float addX;
};

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


TestApp.cpp

#include "TestApp.h"

#include <SOIL.h>

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



// static value
float TestApp::addX = 0.0f;


TestApp::TestApp()
{

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


GLfloat vertices[] = {
// Positions          // Color 
-0.5f, -0.5f, 0.0f,   1.0f, 0.0f, 0.0f,    3.0f , 3.0f , // Bottom Left
-0.5f, 0.5f, 0.0f,   0.0f, 1.0f, 0.0f,     3.0f , 0.0f , // Top Left
0.5f,  0.5f, 0.0f,   0.0f, 0.0f, 1.0f ,    0.0f , 0.0f , // Top Right
0.5f,  -0.5f, 0.0f,   0.0f, 0.0f, 1.0f ,   0.0f , 3.0f  // Top Right
};

GLuint indices[] = {  // Note that we start from 0!
0, 1, 3,   // First Triangle
1, 2, 3    // Second Triangle
};

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

// 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);

glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);



glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(GLfloat), (GLvoid*)0);
glEnableVertexAttribArray(0);

glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(GLfloat), (GLvoid*)(3 * sizeof(GLfloat)) );
glEnableVertexAttribArray(1);

// Texture
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(GLfloat), (GLvoid*)(6 * 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");


glm::vec4 vec(5.0f, 0.0f, 0.0f, 1.0f);
glm::mat4 trans;
trans = glm::translate(trans, glm::vec3(1.0f, 1.0f, 0.0f));
vec = trans * vec;
std::cout << vec.x << vec.y << vec.z << vec.w << std::endl;


}

void TestApp::Update()
{
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
glClear(GL_COLOR_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);



// ******* FIRST TRANSFORM - ROTATE *******
glm::mat4 trans;
trans = glm::translate(trans, glm::vec3(0.5f, -0.5f, 0.0f));
trans = glm::rotate(trans, (GLfloat)glfwGetTime() *glm::radians(-50.0f), glm::vec3(0.0f, 0.0f, 1.0f));


GLuint transformLoc = glGetUniformLocation(ourShader->Program, "transform");
glUniformMatrix4fv(transformLoc, 1, GL_FALSE, glm::value_ptr(trans));





// Now draw the triangle
glBindVertexArray(VAO);

// Draw container 1
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);



// ******* SECOND TRANSFORM - POP IN POP OUT *******
trans = glm::mat4(); // Reset it to an identity matrix
trans = glm::translate(trans, glm::vec3(-0.5f + TestApp::addX , 0.5f, 0.0f));
GLfloat scaleAmount = sin(glfwGetTime());
trans = glm::scale(trans, glm::vec3(scaleAmount, scaleAmount, scaleAmount));
glUniformMatrix4fv(transformLoc, 1, GL_FALSE, glm::value_ptr(trans));



// Draw container 2
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);




glBindVertexArray(0);
}



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

std::cout << addX << std::endl;

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

if (key == GLFW_KEY_A && action == GLFW_PRESS)
TestApp::addX -= 0.1f ;

if (key == GLFW_KEY_D && action == GLFW_PRESS)
TestApp::addX += 0.1f;

}



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);
glDeleteBuffers(1, &this->EBO);
// Terminate GLFW, clearing any resources allocated by GLFW.
}


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


Application.cpp

#include "Application.h"


Application::Application()
{
running = false;
window = new Window(800 , 500 , "SUA_APP");
testApp = new TestApp();


glfwSetKeyCallback(this->window->GetGLFWwindow() , TestApp::controller );

running = true;
}

bool Application::Run()
{
running = !glfwWindowShouldClose(this->window->GetGLFWwindow()  );
this->testApp->Update();
this->window->Update();
this->SwapBuffers();

return running;
 

}

void Application::SwapBuffers()
{
glfwSwapBuffers(this->window->GetGLFWwindow() );
}

Application::~Application()
{
delete testApp;
delete window;
}

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

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