Well I think this should work but it gives segmentation fault when building the shader.
Main.cpp
#include <iostream>
#include <sstream>
#include <GL/glew.h>
#include <SFML/Graphics.hpp>
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include "../codeblocks/Legend of Pedro/src/LoPIcon.h"
#include "3dSprite.hpp"
int main()
{
glm::mat4 orthoMatrix;
glm::mat4 perspectiveMatrix;
glm::mat4 cameraMatrix;
sf::RenderWindow App;
sf::ContextSettings Settings;
Settings.DepthBits = 24; // Request a 24 bits depth buffer
Settings.StencilBits = 8; // Request a 8 bits stencil buffer
Settings.AntialiasingLevel = 8; // Request 2 levels of antialiasing
Settings.MajorVersion = 2;
Settings.MinorVersion = 1;
std::cout << "Building sprite" << std::endl;
d3Sprite hi;
if(false)//fullscreen
App.Create(sf::VideoMode(800, 600, sf::VideoMode::GetDesktopMode().BitsPerPixel), "Legend Of Pedro", sf::Style::Fullscreen ,Settings);
else
App.Create(sf::VideoMode(800, 600, sf::VideoMode::GetDesktopMode().BitsPerPixel), "Legend Of Pedro", sf::Style::Default ,Settings);
App.SetActive();
App.EnableVerticalSync(false);
App.SetFramerateLimit(0);
App.ShowMouseCursor(false);
std::cout << "Using resolution: "<< App.GetWidth() << "x" << App.GetHeight() << std::endl;
App.SetIcon( LoPIcon.width, LoPIcon.height, LoPIcon.pixel_data );
try
{
std::cout << "Using OpenGL " << App.GetSettings().MajorVersion << "." << App.GetSettings().MinorVersion << std::endl;
if ((App.GetSettings().MajorVersion < Settings.MajorVersion) || (App.GetSettings().MajorVersion == Settings.MajorVersion && App.GetSettings().MinorVersion < Settings.MinorVersion))
{
std::cout << "Sorry but a minimum of OpenGL "<< Settings.MajorVersion <<"."<< Settings.MinorVersion <<" is required"<<std::endl;
std::cout << "Try updating your drivers." << std::endl;
return false;
}
}
catch(int e)
{
std::cout << "Failed to get OpenGL version. " << e << std::endl;
return false;
}
//*/
GLenum err = glewInit();
if (GLEW_OK != err)
{
// Problem: glewInit failed, something is seriously wrong.
std::cout << "Error: " << glewGetErrorString(err) << std::endl;
return false;
}
else
{
std::cout << "Using GLEW " << glewGetString(GLEW_VERSION) << std::endl;
}
std::cout << "Using an " << glGetString(GL_VENDOR) <<" graphics card."<< std::endl;
if(8 != App.GetSettings().AntialiasingLevel)
{
std::cout << "Antialiasing Level of " << 8 << " was unavailable, using level " << App.GetSettings().AntialiasingLevel << " instead." << std::endl;
}
glEnable(GL_DEPTH_TEST);
// Accept fragment if it closer to the camera than the former one
glDepthFunc(GL_LESS);
// Cull triangles which normal is not towards the camera
glEnable(GL_CULL_FACE);
glEnable (GL_BLEND);
glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
float width = App.GetWidth();
float height = App.GetHeight();
orthoMatrix = glm::ortho<float>(0.0f, width, height, 0.0f, 0.0f, 1.0f);
perspectiveMatrix = glm::perspective<float>(75, (width / height) ,0.1f,100.0f);
cameraMatrix = glm::lookAt(glm::vec3(0.0f, 3.0f, 5.0f), // Camera is here glm::vec3( 0, 0, 5 );
glm::vec3(0.0f), // and looks here : at the same position, plus "direction"
glm::vec3( 0, 1, 0 ) // Head is up (set to 0,-1,0 to look upside-down)
);
hi.Initiate(&perspectiveMatrix, &cameraMatrix);
while(App.IsOpened())
{
sf::Event Event;
while (App.PollEvent(Event))
{
if (Event.Type == sf::Event::Closed)
App.Close();
// Escape key pressed
//if ((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Keyboard::Escape))
// App.Close();
if ((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Keyboard::F5))
App.Capture().SaveToFile("screenshot.png");
if (Event.Type == sf::Event::Resized)
glViewport(0, 0, Event.Size.Width, Event.Size.Height);
}
if(!App.IsOpened()) break;
hi.Update(App, true);
// Update(App.GetFrameTime()/1000.f);
App.SetActive();
glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
hi.Draw();
//if(gameStateManager->States()[gameStateManager->States().size() - 1] == m_GameState)
//Draw();
//else
//{
App.SaveGLStates();
// glDisable(GL_CULL_FACE);
glDisable(GL_DEPTH_TEST);
glDisable(GL_BLEND);
std::stringstream ss;
ss << App.GetFrameTime();
ss << " ms";
sf::Text fps(ss.str(), sf::Font::GetDefaultFont(), 15);
fps.SetPosition(5.0f,5.0f);
App.Draw(fps);
App.RestoreGLStates();
// glEnable(GL_CULL_FACE);
glEnable(GL_DEPTH_TEST);
glEnable (GL_BLEND);
glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
//}
App.Display();
}
}
Shader.hpp
#ifndef SHADER_HPP_INCLUDE
#define SHADER_HPP_INCLUDE
#pragma once
#include <GL/glew.h>
#include <iostream>
#include <string>
#include <fstream>
#include <vector>
#include <glm/glm.hpp>
class Shader
{
GLuint VertexShaderID;
GLuint FragmentShaderID;
std::string FragmentShaderCode;
std::string VertexShaderCode;
public:
Shader();
Shader(const std::string &vertFileName, const std::string &fragFileName);
~Shader();
GLuint shaderID;
void LoadVertexShader(std::string &fileName);
void LoadFragmentShader(std::string &fileName);
void CompileVertexShader(GLint &Result, int &InfoLogLength);
void CompileFragmentShader(GLint &Result, int &InfoLogLength);
};
#endif //SHADER_HPP_INCLUDE
Shader.cpp
#include "Shader.hpp"
#include <glm/glm.hpp>
Shader::Shader() {}
Shader::Shader(const std::string &vertFileName, const std::string &fragFileName)
{
// Create the shaders
VertexShaderID = glCreateShader(GL_VERTEX_SHADER);
FragmentShaderID = glCreateShader(GL_FRAGMENT_SHADER);
// Read the Vertex Shader code from the file
std::string VertexShaderCode;
std::ifstream VertexShaderStream(vertFileName.c_str(), std::ios::in);
if(VertexShaderStream.is_open())
{
std::string Line = "";
while(getline(VertexShaderStream, Line))
VertexShaderCode += "\n" + Line;
VertexShaderStream.close();
}
// Read the Fragment Shader code from the file
std::string FragmentShaderCode;
std::ifstream FragmentShaderStream(fragFileName.c_str(), std::ios::in);
if(FragmentShaderStream.is_open())
{
std::string Line = "";
while(getline(FragmentShaderStream, Line))
FragmentShaderCode += "\n" + Line;
FragmentShaderStream.close();
}
GLint Result = GL_FALSE;
int InfoLogLength;
// Compile Vertex Shader
printf("Compiling shader : %s\n", vertFileName.c_str());
char const * VertexSourcePointer = VertexShaderCode.c_str();
glShaderSource(VertexShaderID, 1, &VertexSourcePointer , NULL);
glCompileShader(VertexShaderID);
// Check Vertex Shader
glGetShaderiv(VertexShaderID, GL_COMPILE_STATUS, &Result);
glGetShaderiv(VertexShaderID, GL_INFO_LOG_LENGTH, &InfoLogLength);
std::vector<char> VertexShaderErrorMessage(InfoLogLength);
glGetShaderInfoLog(VertexShaderID, InfoLogLength, NULL, &VertexShaderErrorMessage[0]);
fprintf(stdout, "%s\n", &VertexShaderErrorMessage[0]);
// Compile Fragment Shader
printf("Compiling shader : %s\n", fragFileName.c_str());
char const * FragmentSourcePointer = FragmentShaderCode.c_str();
glShaderSource(FragmentShaderID, 1, &FragmentSourcePointer , NULL);
glCompileShader(FragmentShaderID);
// Check Fragment Shader
glGetShaderiv(FragmentShaderID, GL_COMPILE_STATUS, &Result);
glGetShaderiv(FragmentShaderID, GL_INFO_LOG_LENGTH, &InfoLogLength);
std::vector<char> FragmentShaderErrorMessage(InfoLogLength);
glGetShaderInfoLog(FragmentShaderID, InfoLogLength, NULL, &FragmentShaderErrorMessage[0]);
fprintf(stdout, "%s\n", &FragmentShaderErrorMessage[0]);
// Link the program
fprintf(stdout, "Linking program\n");
GLuint ProgramID = glCreateProgram();
glAttachShader(ProgramID, VertexShaderID);
glAttachShader(ProgramID, FragmentShaderID);
glLinkProgram(ProgramID);
// Check the program
glGetProgramiv(ProgramID, GL_LINK_STATUS, &Result);
glGetProgramiv(ProgramID, GL_INFO_LOG_LENGTH, &InfoLogLength);
std::vector<char> ProgramErrorMessage( glm::max(InfoLogLength, int(1)) );
glGetProgramInfoLog(ProgramID, InfoLogLength, NULL, &ProgramErrorMessage[0]);
fprintf(stdout, "%s\n", &ProgramErrorMessage[0]);
Shader::shaderID = ProgramID;
}
Shader::~Shader()
{
glDeleteProgram(shaderID);
glDeleteShader(VertexShaderID);
glDeleteShader(FragmentShaderID);
}
void Shader::LoadVertexShader(std::string &fileName)
{
try
{
std::ifstream VertexShaderStream(fileName.c_str(), std::ios::in);
if(VertexShaderStream.is_open())
{
std::string Line = "";
while(getline(VertexShaderStream, Line))
VertexShaderCode += "\n" + Line;
VertexShaderStream.close();
}
}
catch(int e)
{
std::cout << "Failed to open Vertex shader, using basic shader." << std::endl << e << std::endl;
//VertexShaderCode =
}
}
void Shader::LoadFragmentShader(std::string &fileName)
{
try
{
std::ifstream FragmentShaderStream(fileName.c_str(), std::ios::in);
if(FragmentShaderStream.is_open())
{
std::string Line = "";
while(getline(FragmentShaderStream, Line))
FragmentShaderCode += "\n" + Line;
FragmentShaderStream.close();
}
}
catch(int e)
{
std::cout << "Failed to open Frag shader, using basic shader." << std::endl << e << std::endl;
FragmentShaderCode = "void main(){ gl_FragColor = vec4(1,1,1,1); }";
}
}
void Shader::CompileVertexShader(GLint &Result, int &InfoLogLength)
{
// Compile Vertex Shader
std::cout << "Compiling Vertex Shader" << std::endl;
char const * VertexSourcePointer = VertexShaderCode.c_str();
glShaderSource(VertexShaderID, 1, &VertexSourcePointer , NULL);
glCompileShader(VertexShaderID);
// Check Vertex Shader
glGetShaderiv(VertexShaderID, GL_COMPILE_STATUS, &Result);
glGetShaderiv(VertexShaderID, GL_INFO_LOG_LENGTH, &InfoLogLength);
std::vector<char> VertexShaderErrorMessage(InfoLogLength);
glGetShaderInfoLog(VertexShaderID, InfoLogLength, NULL, &VertexShaderErrorMessage[0]);
std::cout << &VertexShaderErrorMessage[0] << std::endl;
}
void Shader::CompileFragmentShader(GLint &Result, int &InfoLogLength)
{
// Compile Fragment Shader
std::cout << "Compiling Fragment Shader" << std::endl;
char const * FragmentSourcePointer = FragmentShaderCode.c_str();
glShaderSource(FragmentShaderID, 1, &FragmentSourcePointer , NULL);
glCompileShader(FragmentShaderID);
// Check Fragment Shader
glGetShaderiv(FragmentShaderID, GL_COMPILE_STATUS, &Result);
glGetShaderiv(FragmentShaderID, GL_INFO_LOG_LENGTH, &InfoLogLength);
std::vector<char> FragmentShaderErrorMessage(InfoLogLength);
glGetShaderInfoLog(FragmentShaderID, InfoLogLength, NULL, &FragmentShaderErrorMessage[0]);
std::cout << &FragmentShaderErrorMessage[0] << std::endl;
}
LoPIcon.h
ifndef LOPICON_H_INCLUDED
#define LOPICON_H_INCLUDED
#pragma once
static const struct
{
unsigned int width;
unsigned int height;
unsigned int bytes_per_pixel; /* 3:RGB, 4:RGBA */
unsigned char pixel_data[32 * 32 * 4 + 1];
} LoPIcon =
{
32, 32, 4,
"\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
"\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\273\252|\0\273\252|\0\0\0\0\0\0\0\0\0\0"
"\0\0\0\0\0\0\0\0\0\ ...",
};
#endif //LOPICON_H_INCLUDED
Drawable.hpp
#ifndef DRAWABLE_HPP_INCLUDE
#define DRAWABLE_HPP_INCLUDE
#pragma once
#include <glm/glm.hpp>
// glm::translate, glm::rotate, glm::scale
#include <glm/gtc/matrix_transform.hpp>
// glm::value_ptr
#include <glm/gtc/type_ptr.hpp>
#include <iostream>
class Drawable
{
protected:
glm::mat4 *perspectiveMatrix;
glm::mat4 *cameraMatrix;
glm::mat4 MVP;
void printMat4x4(const glm::mat4 & mat);
public:
glm::mat4 modelMatrix;
Drawable();
virtual ~Drawable();
virtual void Initiate(glm::mat4 *persMat, glm::mat4 *camMat);
virtual void Update();
virtual void Draw();
};
#endif
#ifndef DRAWABLE_HPP_INCLUDE
#define DRAWABLE_HPP_INCLUDE
#pragma once
#include <glm/glm.hpp>
// glm::translate, glm::rotate, glm::scale
#include <glm/gtc/matrix_transform.hpp>
// glm::value_ptr
#include <glm/gtc/type_ptr.hpp>
#include <iostream>
class Drawable
{
protected:
glm::mat4 *perspectiveMatrix;
glm::mat4 *cameraMatrix;
glm::mat4 MVP;
void printMat4x4(const glm::mat4 & mat);
public:
glm::mat4 modelMatrix;
Drawable();
virtual ~Drawable();
virtual void Initiate(glm::mat4 *persMat, glm::mat4 *camMat);
virtual void Update();
virtual void Draw();
};
#endif[/code]
Drawable.cpp
#include "Drawable.hpp"
Drawable::Drawable()
{
}
Drawable::~Drawable()
{
}
void Drawable::Initiate(glm::mat4 *persMat, glm::mat4 *camMat)
{
perspectiveMatrix = persMat;
cameraMatrix = camMat;
modelMatrix = glm::mat4(1.0f);
MVP = (*perspectiveMatrix) * (*cameraMatrix) * modelMatrix;
}
void Drawable::Update()
{
MVP = (*perspectiveMatrix) * (*cameraMatrix) * modelMatrix;
}
void Drawable::Draw()
{
}
void Drawable::printMat4x4(const glm::mat4 & mat)
{
std::cout << mat[0].x << "|" << mat[0].y << "|" << mat[0].z << "|" << mat[0].w << std::endl;
std::cout << mat[1].x << "|" << mat[1].y << "|" << mat[1].z << "|" << mat[1].w << std::endl;
std::cout << mat[2].x << "|" << mat[2].y << "|" << mat[2].z << "|" << mat[2].w << std::endl;
std::cout << mat[3].x << "|" << mat[3].y << "|" << mat[3].z << "|" << mat[3].w << std::endl;
}
Quad.hpp
#ifndef QUAD_HPP_INCLUDE
#define QUAD_HPP_INCLUDE
#pragma once
#include <GL/glew.h>
#include <glm/glm.hpp>
class Quad
{
protected:
GLfloat vertexList[3*6];
// This will identify our vertex buffer
GLuint vertexbuffer;
public:
Quad();
Quad(glm::vec3 *eVertexList);
~Quad();
void Initiate();
void Draw();
};
#endif
Quad.cpp
#include "Quad.hpp"
Quad::Quad()
{
vertexList[0] = 0.5f;
vertexList[1] = 1.0f;
vertexList[2] = 0.0f;
vertexList[3] = -0.5f;
vertexList[4] = 1.0f;
vertexList[5] = 0.0f;
vertexList[6] = -0.5f;
vertexList[7] = 0.0f;
vertexList[8] = 0.0f;
vertexList[9] = 0.5f;
vertexList[10] = 1.0f;
vertexList[11] = 0.0f;
vertexList[12] = -0.5f;
vertexList[13] = 0.0f;
vertexList[14] = 0.0f;
vertexList[15] = 0.5f;
vertexList[16] = 0.0f;
vertexList[17] = 0.0f;
// Generate 1 buffer, put the resulting identifier in vertexbuffer
glGenBuffers(1, &vertexbuffer);
// The following commands will talk about our 'vertexbuffer' buffer
glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
// Give our vertices to OpenGL.
glBufferData(GL_ARRAY_BUFFER, sizeof(vertexList), vertexList, GL_STATIC_DRAW);
}
Quad::Quad(glm::vec3 *eVertexList)
{
vertexList[0] = eVertexList[0].x;
vertexList[1] = eVertexList[0].y;
vertexList[2] = eVertexList[0].z;
vertexList[3] = eVertexList[1].x;
vertexList[4] = eVertexList[1].y;
vertexList[5] = eVertexList[1].z;
vertexList[6] = eVertexList[2].x;
vertexList[7] = eVertexList[2].y;
vertexList[8] = eVertexList[2].z;
vertexList[9] = eVertexList[3].x;
vertexList[10] = eVertexList[3].y;
vertexList[11] = eVertexList[3].z;
vertexList[12] = eVertexList[4].x;
vertexList[13] = eVertexList[4].y;
vertexList[14] = eVertexList[4].z;
vertexList[15] = eVertexList[5].x;
vertexList[16] = eVertexList[5].y;
vertexList[17] = eVertexList[5].z;
}
Quad::~Quad()
{
}
void Quad::Initiate()
{
}
void Quad::Draw()
{
}
d3Sprite.hpp
#ifndef D3SPRITE_HPP_INCLUDED
#define D3SPRITE_HPP_INCLUDED
#pragma once
#include <GL/glew.h>
#include <SFML/Graphics.hpp>
#include <glm/glm.hpp>
// glm::translate, glm::rotate, glm::scale
#include <glm/gtc/matrix_transform.hpp>
// glm::value_ptr
#include <glm/gtc/type_ptr.hpp>
#include "./Drawable.hpp"
#include "../codeblocks/Legend of Pedro/src/Shader.hpp"
class d3Sprite : public Drawable
{
public:
d3Sprite(glm::vec3 pos = glm::vec3(0.0f));
~d3Sprite();
virtual void Initiate(glm::mat4 *persMat, glm::mat4 *camMat);
virtual void Update(sf::RenderWindow &App, bool blah = false);
virtual void Draw();
glm::vec3 position;
glm::vec3 rotation;
private:
GLuint MatrixID;
GLuint positionID;
GLuint UVID;
GLuint AlphaID;
GLfloat alphaAmount;
Shader shader;
GLfloat vertexList[3*6];
GLfloat UVList[6*2];
// This will identify our vertex buffer
GLuint vertexbuffer;
GLuint texturebuffer;
GLuint uvbuffer;
GLuint TextureID;
glm::vec3 mRotation;
glm::vec3 mPosition;
bool rotating;
};
#endif
d3Sprite.cpp
#include "./3dSprite.hpp"
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtx/transform.hpp>
#include <math.h>
d3Sprite::d3Sprite( glm::vec3 pos)
{
std::cout << "Building shader" << std::endl;
shader = Shader("./vert.vert" ,"./frag.frag");
std::cout << "Building img" << std::endl;
sf::Image img;
img.LoadFromFile("./pedro.png");
position = pos;
mPosition = glm::vec3(0.0f);
alphaAmount = 0.0f;
rotation = glm::vec3(0.0f);
mRotation = glm::vec3(0.0f);
rotating = false;
//img.FlipVertically();
UVList[0] = 1.0f;
UVList[1] = 0.0f;
UVList[2] = 0.0f;
UVList[3] = 0.0f;
UVList[4] = 0.0f;
UVList[5] = 1.0f;
UVList[6] = 1.0f;
UVList[7] = 0.0f;
UVList[8] = 0.0f;
UVList[9] = 1.0f;
UVList[10] = 1.0f;
UVList[11] = 1.0f;
vertexList[0] = 0.5f;
vertexList[1] = 1.0f;
vertexList[2] = 0.0f;
vertexList[3] = -0.5f;
vertexList[4] = 1.0f;
vertexList[5] = 0.0f;
vertexList[6] = -0.5f;
vertexList[7] = 0.0f;
vertexList[8] = 0.0f;
vertexList[9] = 0.5f;
vertexList[10] = 1.0f;
vertexList[11] = 0.0f;
vertexList[12] = -0.5f;
vertexList[13] = 0.0f;
vertexList[14] = 0.0f;
vertexList[15] = 0.5f;
vertexList[16] = 0.0f;
vertexList[17] = 0.0f;
std::cout << "Building buffers" << std::endl;
// Generate 1 buffer, put the resulting identifier in vertexbuffer
glGenBuffers(1, &vertexbuffer);
// The following commands will talk about our 'vertexbuffer' buffer
glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
// Give our vertices to OpenGL.
glBufferData(GL_ARRAY_BUFFER, sizeof(vertexList), vertexList, GL_STATIC_DRAW);
MatrixID = glGetUniformLocation(shader.shaderID, "MVP");
AlphaID = glGetUniformLocation(shader.shaderID, "Alpha");
positionID = glGetAttribLocation(shader.shaderID, "Position");
TextureID = glGetUniformLocation(shader.shaderID, "myTexture");
UVID = glGetAttribLocation(shader.shaderID, "UV");
glGenBuffers(1, &uvbuffer);
glBindBuffer(GL_ARRAY_BUFFER, uvbuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(UVList), UVList, GL_STATIC_DRAW);
glGenTextures(1, &texturebuffer);
// "Bind" the newly created texture : all future texture functions will modify this texture
glBindTexture(GL_TEXTURE_2D, texturebuffer);
// Give the image to OpenGL
glTexImage2D(GL_TEXTURE_2D, 0,GL_RGBA, img.GetWidth(), img.GetHeight(), 0, GL_RGBA, GL_UNSIGNED_BYTE,img.GetPixelsPtr() );
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
}
d3Sprite::~d3Sprite()
{
// Cleanup VBO and shader
glDeleteBuffers(1, &vertexbuffer);
glDeleteBuffers(1, &uvbuffer);
glDeleteTextures(1, &texturebuffer);
}
void d3Sprite::Initiate(glm::mat4 *persMat, glm::mat4 *camMat)
{
Drawable::Initiate(persMat, camMat);
}
void d3Sprite::Update(sf::RenderWindow &App, bool blah )
{
if(alphaAmount < 0.0f) alphaAmount = 0.0f;
if(alphaAmount > 1.0f) alphaAmount = 1.0f;
if(rotation.y > 1.0f)
{
rotating = true;
rotation.y -= 0.5f * App.GetFrameTime();
mRotation.y += 0.5f * App.GetFrameTime();
}
else if(rotation.y < -1.0f)
{
rotating = true;
rotation.y += 0.5f * App.GetFrameTime();
mRotation.y -= 0.5f * App.GetFrameTime();
}
else
{
mRotation.y += rotation.y;
rotation.y = 0.0f;
rotating = false;
}
if(rotating == false) mPosition = position;
else position = mPosition;
glm::mat4 modelTranslate = glm::translate( glm::mat4(1.0f), mPosition) ;
glm::mat4 modelRotate = glm::rotate( glm::mat4(1.0f), mRotation.y, glm::vec3(0.0f, 1.0f, 0.0f));
modelMatrix = (modelTranslate * modelRotate) * modelRotate;
if(blah)
{
/*glm::mat4 camTranslate = glm::translate( modelTranslate , glm::vec3(0.0f, 3.0f, 5.0f) );
glm::mat4 lookAtCam = glm::lookAt(
glm::vec3(0.0f, 3.0f, 5.0f), // Camera is here glm::vec3( 0, 0, 5 );
glm::vec3(0.0f), // and looks here : at the same position, plus "direction"
glm::vec3( 0, 1, 0 ) // Head is up (set to 0,-1,0 to look upside-down)
);
glm::mat4 camRotate = glm::rotate( glm::mat4(1.0f) , mRotation.y * -1, glm::vec3(0.0f, 1.0f, 0.0f));
engine->cameraMatrix = modelTranslate * lookAtCam * camRotate;// camRotate;// (camTranslate);
*/
if((((int)(mRotation.y / 90.0f)) % 2) == 0)
{
*cameraMatrix = glm::lookAt(
mPosition + glm::vec3(0.0f, 3.0f, 5.0f), // Camera is here glm::vec3( 0, 0, 5 );
mPosition, // and looks here : at the same position, plus "direction"
glm::vec3( 0, 1, 0 ) // Head is up (set to 0,-1,0 to look upside-down)
);
}
else
{
*cameraMatrix = glm::lookAt(
mPosition + glm::vec3(0.0f, 3.0f, -5.0f), // Camera is here glm::vec3( 0, 0, 5 );
mPosition, // and looks here : at the same position, plus "direction"
glm::vec3( 0, 1, 0 ) // Head is up (set to 0,-1,0 to look upside-down)
);
}
}
Drawable::Update();
modelMatrix = glm::mat4(1.0f);
}
void d3Sprite::Draw()
{
glUseProgram(shader.shaderID);
glUniformMatrix4fv(MatrixID, 1, GL_FALSE, &MVP[0][0]);
glUniform1f(AlphaID , alphaAmount);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, texturebuffer);
glUniform1i(TextureID, 0);
// 1rst attribute buffer : vertices
glEnableVertexAttribArray(positionID);
glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
glVertexAttribPointer(
positionID, // attribute 0. No particular reason for 0, but must match the layout in the shader.
3, // size
GL_FLOAT, // type
GL_FALSE, // normalized?
0, // stride
(void*)0 // array buffer offset
);
// 2nd attribute buffer : UVs
glEnableVertexAttribArray(UVID);
glBindBuffer(GL_ARRAY_BUFFER, uvbuffer);
glVertexAttribPointer(
UVID, // attribute. No particular reason for 1, but must match the layout in the shader.
2, // size : U+V => 2
GL_FLOAT, // type
GL_FALSE, // normalized?
0, // stride
(void*)0 // array buffer offset
);
// Send our transformation to the currently bound shader,
// in the "MVP" uniform
// For each model you render, since the MVP will be different (at least the M part)
// Draw the triangle !
glDrawArrays(GL_TRIANGLES, 0, 6); // Starting from vertex 0; 3 vertices total -> 1 triangle
glDisableVertexAttribArray(UVID);
glDisableVertexAttribArray(positionID);
}