250 lines
10 KiB
C++
250 lines
10 KiB
C++
#include "Render_Utils.h"
|
||
|
||
#include <algorithm>
|
||
#include <list>
|
||
#include "glew.h"
|
||
#include "freeglut.h"
|
||
#include <assimp/Importer.hpp>
|
||
#include <assimp/scene.h>
|
||
#include <assimp/postprocess.h>
|
||
#include "../GameUtils.h"
|
||
#include "../Spaceship.h"
|
||
|
||
|
||
|
||
void Core::RenderContext::initFromAssimpMesh(aiMesh* mesh) {
|
||
vertexArray = 0;
|
||
vertexBuffer = 0;
|
||
vertexIndexBuffer = 0;
|
||
|
||
std::vector<float> textureCoord;
|
||
std::vector<unsigned int> indices;
|
||
//tex coord must be converted to 2d vecs
|
||
for (unsigned int i = 0; i < mesh->mNumVertices; i++)
|
||
{
|
||
if (mesh->mTextureCoords[0] != nullptr) {
|
||
textureCoord.push_back(mesh->mTextureCoords[0][i].x);
|
||
textureCoord.push_back(mesh->mTextureCoords[0][i].y);
|
||
}
|
||
else {
|
||
textureCoord.push_back(0.0f);
|
||
textureCoord.push_back(0.0f);
|
||
}
|
||
}
|
||
if (mesh->mTextureCoords[0] == nullptr) {
|
||
std::cout << "no uv coords\n";
|
||
}
|
||
for (unsigned int i = 0; i < mesh->mNumFaces; i++)
|
||
{
|
||
aiFace face = mesh->mFaces[i];
|
||
// retrieve all indices of the face and store them in the indices vector
|
||
for (unsigned int j = 0; j < face.mNumIndices; j++)
|
||
indices.push_back(face.mIndices[j]);
|
||
}
|
||
|
||
unsigned int vertexDataBufferSize = sizeof(float) * mesh->mNumVertices * 3;
|
||
unsigned int vertexNormalBufferSize = sizeof(float) * mesh->mNumVertices * 3;
|
||
unsigned int vertexTexBufferSize = sizeof(float) * mesh->mNumVertices * 2;
|
||
unsigned int vertexTangentBufferSize = sizeof(float) * mesh->mNumVertices * 3;
|
||
unsigned int vertexBiTangentBufferSize = sizeof(float) * mesh->mNumVertices * 3;
|
||
|
||
unsigned int vertexElementBufferSize = sizeof(unsigned int) * indices.size();
|
||
size = indices.size();
|
||
|
||
glGenVertexArrays(1, &vertexArray);
|
||
glBindVertexArray(vertexArray);
|
||
|
||
|
||
glGenBuffers(1, &vertexIndexBuffer);
|
||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vertexIndexBuffer);
|
||
glBufferData(GL_ELEMENT_ARRAY_BUFFER, vertexElementBufferSize, &indices[0], GL_STATIC_DRAW);
|
||
|
||
glGenBuffers(1, &vertexBuffer);
|
||
glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
|
||
//std::cout << vertexBuffer;
|
||
glEnableVertexAttribArray(0);
|
||
glEnableVertexAttribArray(1);
|
||
glEnableVertexAttribArray(2);
|
||
glEnableVertexAttribArray(3);
|
||
glEnableVertexAttribArray(4);
|
||
|
||
glBufferData(GL_ARRAY_BUFFER, vertexDataBufferSize + vertexNormalBufferSize + vertexTexBufferSize + vertexTangentBufferSize + vertexBiTangentBufferSize, NULL, GL_STATIC_DRAW);
|
||
|
||
glBufferSubData(GL_ARRAY_BUFFER, 0, vertexDataBufferSize, mesh->mVertices);
|
||
|
||
glBufferSubData(GL_ARRAY_BUFFER, vertexDataBufferSize, vertexNormalBufferSize, mesh->mNormals);
|
||
|
||
glBufferSubData(GL_ARRAY_BUFFER, vertexDataBufferSize + vertexNormalBufferSize, vertexTexBufferSize, &textureCoord[0]);
|
||
|
||
glBufferSubData(GL_ARRAY_BUFFER, vertexDataBufferSize + vertexNormalBufferSize + vertexTexBufferSize, vertexTangentBufferSize, mesh->mTangents);
|
||
|
||
glBufferSubData(GL_ARRAY_BUFFER, vertexDataBufferSize + vertexNormalBufferSize + vertexTexBufferSize + vertexTangentBufferSize, vertexBiTangentBufferSize, mesh->mBitangents);
|
||
|
||
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, (void*)(0));
|
||
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, (void*)(vertexDataBufferSize));
|
||
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 0, (void*)(vertexNormalBufferSize + vertexDataBufferSize));
|
||
glVertexAttribPointer(3, 3, GL_FLOAT, GL_FALSE, 0, (void*)(vertexDataBufferSize + vertexNormalBufferSize + vertexTexBufferSize));
|
||
glVertexAttribPointer(4, 3, GL_FLOAT, GL_FALSE, 0, (void*)(vertexDataBufferSize + vertexNormalBufferSize + vertexTexBufferSize + vertexTangentBufferSize));
|
||
|
||
}
|
||
|
||
void Core::DrawVertexArray(const float * vertexArray, int numVertices, int elementSize )
|
||
{
|
||
glVertexAttribPointer(0, elementSize, GL_FLOAT, false, 0, vertexArray);
|
||
glEnableVertexAttribArray(0);
|
||
|
||
glDrawArrays(GL_TRIANGLES, 0, numVertices);
|
||
}
|
||
|
||
void Core::DrawVertexArrayIndexed( const float * vertexArray, const int * indexArray, int numIndexes, int elementSize )
|
||
{
|
||
glVertexAttribPointer(0, elementSize, GL_FLOAT, false, 0, vertexArray);
|
||
glEnableVertexAttribArray(0);
|
||
|
||
glDrawElements(GL_TRIANGLES, numIndexes, GL_UNSIGNED_INT, indexArray);
|
||
}
|
||
|
||
|
||
void Core::DrawVertexArray( const VertexData & data )
|
||
{
|
||
int numAttribs = std::min(VertexData::MAX_ATTRIBS, data.NumActiveAttribs);
|
||
for(int i = 0; i < numAttribs; i++)
|
||
{
|
||
glVertexAttribPointer(i, data.Attribs[i].Size, GL_FLOAT, false, 0, data.Attribs[i].Pointer);
|
||
glEnableVertexAttribArray(i);
|
||
}
|
||
glDrawArrays(GL_TRIANGLES, 0, data.NumVertices);
|
||
}
|
||
|
||
void Core::DrawContext(Core::RenderContext& context)
|
||
{
|
||
|
||
glBindVertexArray(context.vertexArray);
|
||
glDrawElements(
|
||
GL_TRIANGLES, // mode
|
||
context.size, // count
|
||
GL_UNSIGNED_INT, // type
|
||
(void*)0 // element array buffer offset
|
||
);
|
||
glBindVertexArray(0);
|
||
}
|
||
|
||
glm::mat4 Core::createPerspectiveMatrix()
|
||
{
|
||
|
||
glm::mat4 perspectiveMatrix;
|
||
float n = 0.05;
|
||
float f = 200.;
|
||
float a1 = glm::min(GameUtils::getInstance()->getAspectRatio(), 1.f);
|
||
float a2 = glm::min(1 / GameUtils::getInstance()->getAspectRatio(), 1.f);
|
||
perspectiveMatrix = glm::mat4({
|
||
1,0.,0.,0.,
|
||
0.,GameUtils::getInstance()->getAspectRatio(),0.,0.,
|
||
0.,0.,(f + n) / (n - f),2 * f * n / (n - f),
|
||
0.,0.,-1.,0.,
|
||
});
|
||
|
||
|
||
perspectiveMatrix = glm::transpose(perspectiveMatrix);
|
||
|
||
return perspectiveMatrix;
|
||
}
|
||
|
||
void Core::drawObjectPBR(Core::RenderContext& context, glm::mat4 modelMatrix, glm::vec3 color, float roughness, float metallic, GLuint program) {
|
||
Spaceship* spaceship = Spaceship::getInstance();
|
||
glm::mat4 viewProjectionMatrix = Core::createPerspectiveMatrix() * spaceship->createCameraMatrix();
|
||
glm::mat4 transformation = viewProjectionMatrix * modelMatrix;
|
||
glUniformMatrix4fv(glGetUniformLocation(program, "transformation"), 1, GL_FALSE, (float*)&transformation);
|
||
glUniformMatrix4fv(glGetUniformLocation(program, "modelMatrix"), 1, GL_FALSE, (float*)&modelMatrix);
|
||
|
||
glUniform1f(glGetUniformLocation(program, "exposition"), 1.f);
|
||
|
||
glUniform1f(glGetUniformLocation(program, "roughness"), roughness);
|
||
glUniform1f(glGetUniformLocation(program, "metallic"), metallic);
|
||
|
||
glUniform3f(glGetUniformLocation(program, "color"), color.x, color.y, color.z);
|
||
|
||
glUniform3f(glGetUniformLocation(program, "cameraPos"), spaceship->cameraPos.x, spaceship->cameraPos.y, spaceship->cameraPos.z);
|
||
|
||
|
||
const int NUM_LIGHTS = 23;
|
||
|
||
glm::vec3 lightsPositions[NUM_LIGHTS];
|
||
glm::vec3 lightsColors[NUM_LIGHTS];
|
||
glm::vec3 lightsDirections[NUM_LIGHTS];
|
||
|
||
/*glUniform3f(glGetUniformLocation(program, "sunDir"), sun.sunDir.x, sun.sunDir.y, sun.sunDir.z);
|
||
glUniform3f(glGetUniformLocation(program, "sunColor"), sun.sunColor.x, sun.sunColor.y, sun.sunColor.z);*/
|
||
|
||
std::list<Sun*>* suns = GameUtils::getInstance()->getSuns();
|
||
glm::vec3 firstSunPos = suns->front()->getPosition();
|
||
glm::vec3 firstSunColor = suns->front()->getColor();
|
||
|
||
glUniform3f(glGetUniformLocation(program, "lightPos"), firstSunPos.x, firstSunPos.y, firstSunPos.z);
|
||
glUniform3f(glGetUniformLocation(program, "lightColor"), firstSunColor.x, firstSunColor.y, firstSunColor.z);
|
||
|
||
glUniform3f(glGetUniformLocation(program, "spotlightConeDir"), spaceship->spotlightConeDir.x, spaceship->spotlightConeDir.y, spaceship->spotlightConeDir.z);
|
||
glUniform3f(glGetUniformLocation(program, "spotlightPos"), spaceship->spotlightPos.x, spaceship->spotlightPos.y, spaceship->spotlightPos.z);
|
||
glUniform3f(glGetUniformLocation(program, "spotlightColor"), spaceship->spotlightColor.x, spaceship->spotlightColor.y, spaceship->spotlightColor.z);
|
||
glUniform1f(glGetUniformLocation(program, "spotlightPhi"), spaceship->spotlightPhi);
|
||
Core::DrawContext(context);
|
||
}
|
||
<<<<<<< HEAD
|
||
void Core::drawSkybox(Core::RenderContext& context, glm::mat4 modelMatrix, GLuint textureID, GLuint program) {
|
||
Spaceship* spaceship = Spaceship::getInstance();
|
||
glDisable(GL_DEPTH_TEST);
|
||
glUseProgram(program);
|
||
glm::mat4 viewProjectionMatrix = Core::createPerspectiveMatrix() * spaceship->createCameraMatrix();
|
||
glm::mat4 transformation = viewProjectionMatrix * modelMatrix;
|
||
glUniformMatrix4fv(glGetUniformLocation(program, "transformation"), 1, GL_FALSE, (float*)&transformation);
|
||
|
||
glBindTexture(GL_TEXTURE_CUBE_MAP, textureID);
|
||
Core::DrawContext(context);
|
||
glEnable(GL_DEPTH_TEST);
|
||
|
||
}
|
||
=======
|
||
|
||
void Core::drawFireball(const glm::mat4& model, float radius, const glm::mat4& view, const glm::mat4& cameraMatrix) {
|
||
//GLuint shaderProgram = GameUtils::getInstance()->shaderLoader->CreateProgram("shaders/fireball.vert", "shaders/fireball.frag");
|
||
//
|
||
//// Ustaw aktywny program cieniuj<75>cy
|
||
//glUseProgram(shaderProgram);
|
||
|
||
//// Przeka<6B> warto<74>ci uniform do shadera
|
||
//glUniform3fv(glGetUniformLocation(shaderProgram, "cameraPosition"), 1, glm::value_ptr(cameraPosition));
|
||
//glUniform3fv(glGetUniformLocation(shaderProgram, "flareColor"), 1, glm::value_ptr(flareColor));
|
||
//glUniform3fv(glGetUniformLocation(shaderProgram, "lightPosition"), 1, glm::value_ptr(lightPosition));
|
||
|
||
//// Oblicz normalne dla efektu <20>wiat<61>a
|
||
//glm::mat3 normalMatrix = glm::transpose(glm::inverse(glm::mat3(model)));
|
||
|
||
//// Przeka<6B> macierze do shadera
|
||
//glUniformMatrix4fv(glGetUniformLocation(shaderProgram, "model"), 1, GL_FALSE, glm::value_ptr(model));
|
||
//glUniformMatrix4fv(glGetUniformLocation(shaderProgram, "view"), 1, GL_FALSE, glm::value_ptr(view));
|
||
//glUniformMatrix4fv(glGetUniformLocation(shaderProgram, "projection"), 1, GL_FALSE, glm::value_ptr(projection));
|
||
//glUniformMatrix3fv(glGetUniformLocation(shaderProgram, "normalMatrix"), 1, GL_FALSE, glm::value_ptr(normalMatrix));
|
||
|
||
//// Tutaj mo<6D>esz umie<69>ci<63> kod odpowiedzialny za rysowanie przygotowanego obiektu
|
||
//// Przyk<79>adowy kod, zak<61>adaj<61>c, <20>e masz funkcj<63> renderuj<75>c<EFBFBD> obiekt:
|
||
//// renderObject();
|
||
|
||
//// Wy<57><79>cz program cieniuj<75>cy
|
||
|
||
//glDeleteProgram(shaderProgram);
|
||
}
|
||
|
||
void Core::loadModelToContext(std::string path, Core::RenderContext& context)
|
||
{
|
||
Assimp::Importer import;
|
||
const aiScene* scene = import.ReadFile(path, aiProcess_Triangulate | aiProcess_CalcTangentSpace);
|
||
|
||
if (!scene || scene->mFlags & AI_SCENE_FLAGS_INCOMPLETE || !scene->mRootNode)
|
||
{
|
||
std::cout << "ERROR::ASSIMP::" << import.GetErrorString() << std::endl;
|
||
return;
|
||
}
|
||
context.initFromAssimpMesh(scene->mMeshes[0]);
|
||
}
|
||
>>>>>>> 62afe67 (add shooting)
|