วันพฤหัสบดีที่ 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;
}

หมุน และ 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

วันพุธที่ 14 ธันวาคม พ.ศ. 2559

โปรแกรมเปลี่ยน wall ไปเป็น awesome face โดยใช้ sine

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 float xOffset;



void main()

{

    gl_Position = vec4(position.x + xOffset , position.y , position.z , 1.0f);

    ourColor = color;

 TexCoord = vec2( 1 - texCoord.x ,  texCoord.y );



}




*********************************************************************************
Shader.frag



#version 330 core



in vec3 ourColor;



in vec2 TexCoord;



out vec4 color;



uniform sampler2D ourTexture1;

uniform sampler2D ourTexture2;



uniform float value;





void main()

{

    color = mix( texture( ourTexture1 , TexCoord ) , texture( ourTexture2 , TexCoord ) , value ) ;



}



*********************************************************************************
TestApp.h



#pragma once



#include <iostream>

#include <GL/glew.h>

#include "Shader.h"

#include "Texture.h"



class TestApp

{

public:

 TestApp();

 void Update();

 

 

 ~TestApp();



private:

 GLuint VBO, VAO, EBO;

 Shader* ourShader;

 Texture* texture;

 Texture* texture2;

};



***********************************************************************
TestApp.cpp



#include "TestApp.h"





#include <GL/glew.h>

#include <glfw3.h>

#include <SOIL.h>

#include "Shader.h"



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



}



void TestApp::Update()

{

 glClearColor(0.2f, 0.3f, 0.3f, 1.0f);

 glClear(GL_COLOR_BUFFER_BIT);







 ourShader->Use();







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





 // Now draw the triangle

 glBindVertexArray(VAO);

 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.

}



วันจันทร์ที่ 12 ธันวาคม พ.ศ. 2559

4.class Texture



Texture.h

#pragma once

#include <iostream>

#include <GL/glew.h>

#include <SOIL.h>

class Texture
{
public:
Texture(std::string fileName);

GLuint getTextureID();

void bindTexture();

~Texture();

private:
GLuint texture;


};

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

Texture.cpp

#include "Texture.h"



Texture::Texture(std::string fileName)
{
glGenTextures(1, &this->texture);
glBindTexture(GL_TEXTURE_2D, this->texture); // All upcoming GL_TEXTURE_2D operations now have effect on this texture object
  // Set the texture wrapping parameters
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); // Set texture wrapping to GL_REPEAT (usually basic wrapping method)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
// Set texture filtering parameters
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
// Load image, create texture and generate mipmaps
int width, height;
unsigned char* image = SOIL_load_image(fileName.c_str(), &width, &height, 0, SOIL_LOAD_RGB);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, image);
glGenerateMipmap(GL_TEXTURE_2D);
SOIL_free_image_data(image);
glBindTexture(GL_TEXTURE_2D, 0); // Unbind texture when done, s
}


void Texture::bindTexture()
{
glBindTexture(GL_TEXTURE_2D, this->texture);

}


GLuint Texture::getTextureID()
{
return this->texture;
}


Texture::~Texture()
{
}

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

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;


void main()
{
    gl_Position = vec4(position.x  , position.y , position.z , 1.0f);
    ourColor = color;
TexCoord = texCoord;
}

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

shader.frag

#version 330 core

in vec3 ourColor;

in vec2 TexCoord;

out vec4 color;




uniform sampler2D ourTexture;

void main()
{


color = texture(ourTexture, TexCoord) * vec4(ourColor, 1.0f);


}

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

TestApp.h

#pragma once

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

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

void putTextureInRectangleProgram();

void changeGreenToBlackProgram();

~TestApp();

private:
GLuint VBO, VAO, EBO;

Shader* ourShader;

Texture* texture;
};

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


TestApp.cpp

#include "TestApp.h"


#include <GL/glew.h>
#include <glfw3.h>

#include "Shader.h"



GLfloat offset = 0.0f;

TestApp::TestApp()
{

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


GLfloat vertices[] = {
// Positions          // Colors
0.5f, 0.5f, 0.0f,   1.0f, 0.0f, 0.0f,      1.0f, 1.0f,      // Bottom Left
0.5f, -0.5f, 0.0f,   0.0f, 1.0f, 0.0f,     1.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, 1.0f     // Top Right
};

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




// generate vao , vbo , veo
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);


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

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


// Unbind
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



// After Unbind VAO
// Load and create a texture 
texture = new Texture("container.jpg");







}

void TestApp::Update()
{

glClearColor(0.7f, 0.3f, 0.3f, 1.0f);     // Background color
glClear(GL_COLOR_BUFFER_BIT);



texture->bindTexture();

ourShader->Use();



// Now draw the triangle
glBindVertexArray(VAO);
// with ebo
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);



glBindVertexArray(0);
}

TestApp::~TestApp()
{
delete ourShader;

delete texture;

// Properly de-allocate all resources once they've outlived their purpose
glDeleteVertexArrays(1, &this->VAO);
glDeleteBuffers(1, &this->VBO);
glDeleteBuffers(1, &this->EBO);

}

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

ผลที่ได้








วันอังคารที่ 29 พฤศจิกายน พ.ศ. 2559

3.class Shader


1.สร้างโค้ด shader.vert

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

out vec3 ourColor;

void main()
{
    gl_Position = vec4(position, 1.0f);
    ourColor = color;
}

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


2.สร้างโค้ด shader.frag

#version 330 core

in vec3 ourColor;

out vec4 color;

void main()
{
    color = vec4(ourColor, 1.0f);
}

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

3. อัปเดทโค้ด TestApp.h

#pragma once

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

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


~TestApp();

private:
GLuint VBO, VAO, EBO;
Shader* ourShader;
};

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

4. อัปเดทโค้ด TestApp.cpp

#include "TestApp.h"


#include <GL/glew.h>
#include <glfw3.h>

#include "Shader.h"

TestApp::TestApp()
{



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


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

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



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

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

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

}

void TestApp::Update()
{
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);

ourShader->Use();

// Now draw the triangle
glBindVertexArray(VAO);
glDrawArrays(GL_TRIANGLES, 0, 3);
glBindVertexArray(0);
}

TestApp::~TestApp()
{
delete ourShader;
// 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.
}

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

ผลที่ได้


วันจันทร์ที่ 28 พฤศจิกายน พ.ศ. 2559

2. สร้าง class Application , Window , TestApp



Application.h

#pragma once

#include "Window.h"

#include "TestApp.h"

class Application
{
public:
Application();
bool Run();
void SwapBuffers();


~Application();

private:
Window* window;

TestApp* testApp;

bool running;


};

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

Application.cpp

#include "Application.h"



void key_callback(GLFWwindow* window, int key, int scancode, int action, int mode)
{
std::cout << key << std::endl;
if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
glfwSetWindowShouldClose(window, GL_TRUE);

}


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

glfwSetKeyCallback(this->window->GetGLFWwindow() , key_callback);

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

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

Window.h

#pragma once

#include <iostream>

#include <GL/glew.h>

#include <glfw3.h>

class Window
{
public:
Window(int w , int h , std::string name);
void Update();
GLFWwindow* GetGLFWwindow();




~Window();

private:
std::string nameWindow;
GLFWwindow* window;
};

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

Window.cpp

#include "Window.h"


Window::Window(int w, int h, std::string name)
{
this->nameWindow = name;

glfwInit();
// Set all the required options for GLFW
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);

// Create a GLFWwindow object that we can use for GLFW's functions
window = glfwCreateWindow(w, h, name.c_str(), nullptr, nullptr);

if (window == nullptr)
{
std::cout << "Failed to create GLFW window" << std::endl;
glfwTerminate();

}
glfwMakeContextCurrent(window);



// Set this to true so GLEW knows to use a modern approach to retrieving function pointers and extensions
glewExperimental = GL_TRUE;
// Initialize GLEW to setup the OpenGL Function pointers
if (glewInit() != GLEW_OK)
{
std::cout << "Failed to initialize GLEW" << std::endl;

}

// Define the viewport dimensions
int width, height;
glfwGetFramebufferSize(window, &width, &height);
glViewport(0, 0, width, height);


}



GLFWwindow *Window::GetGLFWwindow()
{
return window;

}

void Window::Update()
{
glfwPollEvents();




}


Window::~Window()
{
glfwTerminate();
}


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



TestApp.h

#pragma once
class TestApp
{
public:
TestApp();
void Update();


~TestApp();



};



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

TestApp.cpp

#include "TestApp.h"


#include <GL/glew.h>


TestApp::TestApp()
{
}

void TestApp::Update()
{
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
}

TestApp::~TestApp()
{
}


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

1. ดาวน์โหลด glfw , glew และ สร้างหน้าต่าง



1. ดาวน์โหลด GLFW ( 32 bit window binary นะจ๊ะ ) จาก http://www.glfw.org/download.html

2. ดาวน์โหลด GLEW ( 32 bit window binary นะจ๊ะ )  จาก http://glew.sourceforge.net/

3. แล้วเมิงก็ set include libraly เอาเองง่ายๆ

4. set linker libraly ก็ง่ายๆ

5. ต่อไป set input เป็น
          - glfw3.lib
          - glew32s.lib
          - opengl32.lib

6. ตรง Preprocessor definition เพื่ม GLEW_STATIC ลงไปด้วย

7. ใส่โค้ลลงไป


#include <iostream>

#include <GL/glew.h>

// GLFW
#include <GLFW/glfw3.h>


// Function prototypes
void key_callback(GLFWwindow* window, int key, int scancode, int action, int mode);

// Window dimensions
const GLuint WIDTH = 800, HEIGHT = 600;


int main()
{
    std::cout << "Starting GLFW context, OpenGL 3.3" << std::endl;
    // Init GLFW
    glfwInit();
    // Set all the required options for GLFW
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
    glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);

    // Create a GLFWwindow object that we can use for GLFW's functions
    GLFWwindow* window = glfwCreateWindow(WIDTH, HEIGHT, "LearnOpenGL", nullptr, nullptr);    
    if (window == nullptr)
    {
        std::cout << "Failed to create GLFW window" << std::endl;
        glfwTerminate();
        return -1;
    }
    glfwMakeContextCurrent(window);
    // Set the required callback functions
    glfwSetKeyCallback(window, key_callback);

    // Set this to true so GLEW knows to use a modern approach to retrieving function pointers and extensions
    glewExperimental = GL_TRUE;
    // Initialize GLEW to setup the OpenGL Function pointers
    if (glewInit() != GLEW_OK)
    {
        std::cout << "Failed to initialize GLEW" << std::endl;
        return -1;
    }    

    // Define the viewport dimensions
    int width, height;
    glfwGetFramebufferSize(window, &width, &height);  
    glViewport(0, 0, width, height);

    // Game loop
    while (!glfwWindowShouldClose(window))
    {
        // Check if any events have been activiated (key pressed, mouse moved etc.) and call corresponding response functions
        glfwPollEvents();

        // Render
        // Clear the colorbuffer
        glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
        glClear(GL_COLOR_BUFFER_BIT);

        // Swap the screen buffers
        glfwSwapBuffers(window);
    }

    // Terminate GLFW, clearing any resources allocated by GLFW.
    glfwTerminate();
    return 0;
}

// Is called whenever a key is pressed/released via GLFW
void key_callback(GLFWwindow* window, int key, int scancode, int action, int mode)
{
    std::cout << key << std::endl;
    if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
        glfwSetWindowShouldClose(window, GL_TRUE);
}


จะได้หน้าต่างสีเขียวๆ ออกมา