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

หมุน และ pop in , pop out วัตถุ


Shader.vert

#version 330 core
layout (location = 0) in vec3 position;
layout (location = 1) in vec3 color;
layout (location = 2) in vec2 texCoord;

out vec3 ourColor;

out vec2 TexCoord;

uniform mat4 transform;



void main()
{
    gl_Position = transform * vec4(position.x  , position.y , position.z , 1.0f);
    ourColor = color;
TexCoord = vec2( 1 - texCoord.x ,  texCoord.y );

}

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

TestApp.cpp

#include "TestApp.h"


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


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


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, 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);
}

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.
}



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

อธิบาย

trans = glm::translate(trans, glm::vec3(0.5f, -0.5f, 0.0f));
คือ เลื่อนไปทางขวา 0.5 , เลื่อนลงล่าง 0.5f

trans = glm::rotate(trans, (GLfloat)glfwGetTime() *glm::radians(-50.0f), glm::vec3(0.0f, 0.0f, 1.0f));
คือ หมุนรอบแกน z

GLfloat scaleAmount = sin(glfwGetTime());
คือ จะได้ค่าตั้งแต่ 0 - 1

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

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