Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "game.h"
- #include <iostream>
- #include <glm/gtc/matrix_transform.hpp>
- // Assignment 2
- #include <stdio.h>
- #include <stdlib.h>
- #include <fstream>
- #include <iterator>
- #include <vector>
- using namespace std;
- static void printMat(const glm::mat4 mat)
- {
- std::cout << " matrix:" << std::endl;
- for (int i = 0; i < 4; i++)
- {
- for (int j = 0; j < 4; j++)
- std::cout << mat[j][i] << " ";
- std::cout << std::endl;
- }
- }
- Game::Game() : Scene()
- {
- }
- Game::Game(float angle, float relationWH, float near, float far) : Scene(angle, relationWH, near, far)
- {
- }
- //parse line to vec4
- glm::vec4 Game::parseVec4(const std::string& line)
- {
- unsigned int tokenLength = line.length();
- const char* tokenString = line.c_str();
- unsigned int vertIndexStart = 2;
- while (vertIndexStart < tokenLength)
- {
- if (tokenString[vertIndexStart] != ' ')
- break;
- vertIndexStart++;
- }
- unsigned int vertIndexEnd = FindNextChar(vertIndexStart, tokenString, tokenLength, ' ');
- float x = ParseFloatValue(line, vertIndexStart, vertIndexEnd);
- vertIndexStart = vertIndexEnd + 1;
- vertIndexEnd = FindNextChar(vertIndexStart, tokenString, tokenLength, ' ');
- float y = ParseFloatValue(line, vertIndexStart, vertIndexEnd);
- vertIndexStart = vertIndexEnd + 1;
- vertIndexEnd = FindNextChar(vertIndexStart, tokenString, tokenLength, ' ');
- float z = ParseFloatValue(line, vertIndexStart, vertIndexEnd);
- vertIndexStart = vertIndexEnd + 1;
- vertIndexEnd = FindNextChar(vertIndexStart, tokenString, tokenLength, ' ');
- float w = ParseFloatValue(line, vertIndexStart, vertIndexEnd);
- return glm::vec4(x, y, z, w);
- //glm::vec3(atof(tokens[1].c_str()), atof(tokens[2].c_str()), atof(tokens[3].c_str()))
- }
- void Game::PrintScene()
- {
- int positional_index = 0;
- std::cout << "coeffs: Num of objects = " << sizes[0] << "\nNum of lights= " << sizes[1] << std::endl;
- std::cout << "Objects: " << std::endl;
- for (int i = 0; i < sizes[0]; i++)
- {
- std::cout << "Object: (" << objects[i].x << " , " << objects[i].y << " , " << objects[i].z << " , " << objects[i].w << ")" << std::endl;
- std::cout << "Object Ambient:: (" << objColors[i].x << " , " << objColors[i].y << " , " << objColors[i].z << " , " << objColors[i].w << ")" << std::endl;
- }
- std::cout << "\n" << std::endl;
- for (int i = 0; i < sizes[1]; i++) {
- std::cout << "Light: (" << lightsDirection[i].x << " , " << lightsDirection[i].y << " , " << lightsDirection[i].z << " , " << lightsDirection[i].w << ")" << std::endl;
- std::cout << "Light Intensity:: (" << lightsIntensity[i].x << " , " << lightsIntensity[i].y << " , " << lightsIntensity[i].z << " , " << lightsIntensity[i].w << ")" << std::endl;
- if (lightsDirection[i].w != 0) {
- std::cout << "Light Position:: (" << lightPosition[positional_index].x << " , " << lightPosition[positional_index].y << " , " << lightPosition[positional_index].z << " , " << lightPosition[positional_index].w << ")" << std::endl;
- positional_index++;
- }
- }
- std::cout << "\n" << std::endl;
- std::cout << "Reflective objects:" << std::endl;
- std::cout << "Num of reflective objects: " << sizes[2] << std::endl;
- for (int i = 0; i < sizes[2]; i++) {
- std::cout << "Index of reflective object: " << objReflective[i].x << std::endl;
- std::cout << "Reflective Object: (" << objects[objReflective[i].x].x << " , " << objects[objReflective[i].x].y << " , "
- << objects[objReflective[i].x].z << " , " << objects[objReflective[i].x].w << ")" << std::endl;
- }
- }
- void Game::Init()
- {
- unsigned int texIDs[1] = { 0 };
- AddShader("../res/shaders/pickingShader");
- AddShader("../res/shaders/basicShaderAM");
- AddTexture("../res/textures/box0.bmp", false);
- AddMaterial(texIDs, 1);
- AddShape(Plane, -1, TRIANGLES);
- pickedShape = 0;
- SetShapeMaterial(0, 0);
- MoveCamera(0, zTranslate, 10);
- pickedShape = -1;
- // -----------------------------------------------Assignment 2-----------------------------------------------
- std::ifstream file;
- file.open("scene.txt");
- int ref_index = 0;
- std::string line;
- if (file.is_open())
- {
- while (file.good())
- {
- getline(file, line);
- unsigned int lineLength = line.length();
- if (lineLength < 2)
- continue;
- const char* lineCStr = line.c_str();
- switch (lineCStr[0])
- {
- case 'e':
- eye = parseVec4(line);
- break;
- case 'a':
- ambient = parseVec4(line);
- break;
- case 'r':
- // objReflective.push_back(glm::vec2(ref_index, 0));
- case 'o':
- case 't':
- ref_index++;
- objects.push_back(parseVec4(line));
- break;
- case 'c':
- objColors.push_back(parseVec4(line));
- break;
- case 'd':
- lightsDirection.push_back(parseVec4(line));
- break;
- case 'i':
- lightsIntensity.push_back(parseVec4(line));
- break;
- case 'p':
- lightPosition.push_back(parseVec4(line));
- break;
- }
- }
- sizes = glm::vec4(objects.size(), lightsDirection.size(), objReflective.size(), 800);
- }
- else
- {
- char buf[100];
- //std::cout<<"can not open file!"<<std::endl;
- strerror_s(buf, errno);
- std::cerr << "Error: " << buf;
- sizes = glm::vec4(0, 0, 0, 0);
- }
- //ReadPixel(); //uncomment when you are reading from the z-buffer
- }
- void Game::Update(const glm::mat4& MVP, const glm::mat4& Model, const int shaderIndx)
- {
- Shader* s = shaders[shaderIndx];
- int r = ((pickedShape + 1) & 0x000000FF) >> 0;
- int g = ((pickedShape + 1) & 0x0000FF00) >> 8;
- int b = ((pickedShape + 1) & 0x00FF0000) >> 16;
- textures[materials[shapes[pickedShape]->GetMaterial()]->GetTexture(0)]->Bind(0);
- s->Bind();
- s->SetUniformMat4f("MVP", MVP);
- s->SetUniformMat4f("Normal", Model);
- //s->SetUniform4fv("eye", &eye, eye.length());
- //s->SetUniform4fv("eye", &eye, 4);
- s->SetUniform4f("eye", eye.x, eye.y, eye.z, eye.w);
- s->SetUniform4f("ambient", ambient.x, ambient.y, ambient.z, ambient.w);
- s->SetUniform4f("objects", objects[0].x, objects[0].y, objects[0].z, objects[0].w);
- s->SetUniform4f("objColors", objects[0].x, objects[0].y, objects[0].z, objects[0].w);
- s->SetUniform4f("lightsDirection", lightsDirection[0].x, lightsDirection[0].y, lightsDirection[0].z, lightsDirection[0].w);
- s->SetUniform4f("lightsIntensity", lightsIntensity[0].x, lightsIntensity[0].y, lightsIntensity[0].z, lightsIntensity[0].w);
- s->SetUniform4f("lightsPosition", lightPosition[0].x, lightPosition[0].y, lightPosition[0].z, lightPosition[0].w);
- s->SetUniform4f("sizes", sizes.x, sizes.y, sizes.z, sizes.w);
- // TODO: which size do i send?
- //s->SetUniform4fv("eye", &eye, eye.length());
- //s->SetUniform4fv("eye", &eye, 4);
- //s->SetUniform4f("eye", eye.x, eye.y, eye.z, eye.w);
- //s->SetUniform4fv("ambient", &ambient, ambient.length());
- //s->SetUniform4fv("objects", &objects[0], objects.size());
- //s->SetUniform4fv("objColors", &objColors[0], objColors.size());
- //s->SetUniform4fv("lightsDirection", &lightsDirection[0], lightsDirection.size());
- //s->SetUniform4fv("lightsIntensity", &lightsIntensity[0], lightsIntensity.size());
- //s->SetUniform4fv("lightsPosition", &lightPosition[0], lightPosition.size());
- //s->SetUniform4fv("sizes", &sizes, sizes.length());
- //if (objReflective.size() != 0) {
- // s->SetUniform2fv("objReflective", &objReflective, objReflective.size());
- //}
- // -----------------------------------------------Assignment 2 end-----------------------------------------------
- //s->SetUniform4f("lightDirection", 0.0f , 0.0f, -1.0f, 0.0f);
- //if(shaderIndx == 0)
- // s->SetUniform4f("lightColor",r/255.0f, g/255.0f, b/255.0f,1.0f);
- //else
- // s->SetUniform4f("lightColor",0.7f,0.8f,0.1f,1.0f);
- s->Unbind();
- }
- void Game::WhenRotate()
- {
- }
- void Game::WhenTranslate()
- {
- PrintScene();
- }
- void Game::Motion()
- {
- if (isActive)
- {
- }
- }
- Game::~Game(void)
- {
- }
- /* BACKUP
- #include "game.h"
- #include <iostream>
- #include <glm/gtc/matrix_transform.hpp>
- // Assignment 2
- #include <stdio.h>
- #include <stdlib.h>
- #include <fstream>
- #include <iterator>
- #include <vector>
- using namespace std;
- static void printMat(const glm::mat4 mat)
- {
- std::cout<<" matrix:"<<std::endl;
- for (int i = 0; i < 4; i++)
- {
- for (int j = 0; j < 4; j++)
- std::cout<< mat[j][i]<<" ";
- std::cout<<std::endl;
- }
- }
- Game::Game() : Scene()
- {
- }
- Game::Game(float angle ,float relationWH, float near, float far) : Scene(angle,relationWH,near,far)
- {
- }
- void Game::Init()
- {
- unsigned int texIDs[1] = { 0 };
- AddShader("../res/shaders/pickingShader");
- AddShader("../res/shaders/basicShader");
- AddTexture("../res/textures/box0.bmp",false);
- AddMaterial(texIDs, 1);
- AddShape(Plane,-1,TRIANGLES);
- pickedShape = 0;
- SetShapeMaterial(0,0);
- MoveCamera(0,zTranslate,10);
- pickedShape = -1;
- //ReadPixel(); //uncomment when you are reading from the z-buffer
- }
- void Game::Update(const glm::mat4 &MVP,const glm::mat4 &Model,const int shaderIndx)
- {
- Shader *s = shaders[shaderIndx];
- int r = ((pickedShape+1) & 0x000000FF) >> 0;
- int g = ((pickedShape+1) & 0x0000FF00) >> 8;
- int b = ((pickedShape+1) & 0x00FF0000) >> 16;
- textures[materials[shapes[pickedShape]->GetMaterial()]->GetTexture(0)]->Bind(0);
- s->Bind();
- s->SetUniformMat4f("MVP", MVP);
- s->SetUniformMat4f("Normal",Model);
- // -----------------------------------------------Assignment 2-----------------------------------------------
- glm::vec4 coordData = getCoordSize();
- s->SetUniform1f("theMinusX", coordData.x);
- s->SetUniform1f("thePlusX", coordData.y);
- s->SetUniform1f("theMinusY", coordData.z);
- s->SetUniform1f("thePlusY", coordData.w);
- FILE* fp;
- char buffer[255];
- char neededBuffer[255];
- fp = fopen("scene.txt", "r");
- glm::ivec4 sizes = glm::ivec4(0, 0, 0, 0);
- std::vector<glm::vec4> objectList;
- std::vector<glm::vec4> objectColorList;
- while (fgets(buffer, 255, (FILE*)fp)) {
- char letter;
- float x, y, z, w;
- sscanf(buffer, "%c %f %f %f %f", &letter, &x, &y, &z, &w);
- switch (letter) {
- case 'e':
- s->SetUniform4f("eye", x, y, z, w);
- break;
- case 'a':
- s->SetUniform4f("ambient", x, y, z, w);
- break;
- case 'o':
- ++sizes.x;
- objectList.push_back(glm::vec4(x, y, z, w));
- //s->SetUniform4f("objects", x, y, z, w);
- break;
- case 'r':
- break;
- case 't':
- break;
- case 'c':
- objectColorList.push_back(glm::vec4(x, y, z, w));
- //s->SetUniform4f("objColors", x, y, z, w);
- break;
- case 'd':
- s->SetUniform4f("lightsDirection", x, y, z, w);
- ++sizes.y;
- break;
- case 'p':
- s->SetUniform4f("lightPosition", x, y, z, w);
- break;
- case 'i':
- s->SetUniform4f("lightsIntensity", x, y, z, w);
- break;
- default: break;
- }
- }
- s->SetUniform4i("sizes", objectList.size(), objectColorList.size(), sizes.z, sizes.w);
- fclose(fp);
- //}
- glm::vec4* objects = new glm::vec4[objectList.size()];
- glm::vec4* objectsColors = new glm::vec4[objectColorList.size()];
- int k = 0;
- for (auto i = objectList.begin(); i != objectList.end(); ++i) {
- objects[k] = *i;
- ++k;
- }
- s->SetUniform4fv("objects", objects, objectList.size());
- cout << sc
- k = 0;
- for (auto i = objectColorList.begin(); i != objectColorList.end(); ++i) {
- objectsColors[k] = *i;
- ++k;
- }
- s->SetUniform4fv("objColors", objectsColors, objectColorList.size());
- // -----------------------------------------------Assignment 2 end-----------------------------------------------
- //s->SetUniform4f("lightDirection", 0.0f , 0.0f, -1.0f, 0.0f);
- //if(shaderIndx == 0)
- // s->SetUniform4f("lightColor",r/255.0f, g/255.0f, b/255.0f,1.0f);
- //else
- // s->SetUniform4f("lightColor",0.7f,0.8f,0.1f,1.0f);
- s->Unbind();
- }
- void Game::WhenRotate()
- {
- }
- void Game::WhenTranslate()
- {
- }
- void Game::Motion()
- {
- if(isActive)
- {
- }
- }
- Game::~Game(void)
- {
- }
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement