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 model;
uniform mat4 view;
uniform mat4 projection;
void main()
{
gl_Position = projection * view * model * vec4(position.x , position.y , position.z , 1.0f);
// ourColor = color;
TexCoord = vec2( 1 - texCoord.x , texCoord.y );
}
*****************************************************************************
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);
~TestApp();
private:
GLuint VBO, VAO, EBO;
Shader* ourShader;
Texture* texture;
Texture* texture2;
static float addX;
glm::vec3 cubePositions[10];
};
************************************************************************
TestApp.cpp
#include "TestApp.h"
#include <SOIL.h>
// static value
float TestApp::addX = 0.0f;
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()
{
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 ****************************
glm::mat4 view;
// Note that we're translating the scene in the reverse direction of where we want to move
view = glm::translate(view, glm::vec3(0.0f + TestApp::addX, 0.0f, -3.0f));
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");
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;
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::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;
if (TestApp::addX < -1.0f)
{
TestApp::addX = -1.0f;
}
if (TestApp::addX > 1.0f)
{
TestApp::addX = 1.0f;
}
}
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.
}
ไม่มีความคิดเห็น:
แสดงความคิดเห็น