Created
February 8, 2018 10:30
-
-
Save KamilChmielewski/a113b48727ad9c959c54d3d59c581a7b to your computer and use it in GitHub Desktop.
OpenGL University 1st year Second Semester
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "Camera2.h" | |
#include "Player.h" | |
Camera2::Camera2() | |
{ | |
} | |
Camera2::~Camera2() | |
{ | |
} | |
void Camera2::Position_Camera(float pos_x, float pos_y, float pos_z, float view_x, float view_y, float view_z, float up_x, float up_y, float up_z) | |
{ | |
mPos = Vector3(pos_x, pos_y, pos_z); | |
mView = Vector3(view_x, view_y, view_z); | |
mUp = Vector3(up_x, up_y, up_z); | |
} | |
void Camera2::Move_Camera(float speed) | |
{ | |
Vector3 vVector; | |
vVector.x = mView.x - mPos.x; | |
vVector.y = mView.y - mPos.y; | |
vVector.z = mView.z - mPos.z; //The view vector | |
//forwards and backwards camera speed | |
mPos.x = mPos.x + vVector.x * speed; | |
mPos.z = mPos.z + vVector.z * speed; | |
mView.x = mView.x + vVector.x * speed; | |
mView.z = mView.z + vVector.z * speed; | |
} | |
void Camera2::Rotate_View(float speed) //Rotates the camera view | |
{ | |
Vector3 vVector; | |
vVector.x = mView.x - mPos.x; | |
vVector.y = mView.y - mPos.y; | |
vVector.z = mView.z - mPos.z; //The view vector | |
mView.z = (float)(mPos.z + sin(speed)*vVector.x + cos(speed)*vVector.z); | |
mView.x = (float)(mPos.x + cos(speed)*vVector.x - sin(speed)*vVector.z); | |
} | |
void Camera2::Rotate_Position(float speed) | |
{ | |
Vector3 vVector; | |
vVector.x = mPos.x - mView.x; | |
vVector.y = mPos.y - mView.y; | |
vVector.z = mPos.z - mView.z; | |
mPos.z = (float)(mView.z + sin(speed)*vVector.x + cos(speed)*vVector.z); | |
mPos.x = (float)(mView.x + cos(speed)*vVector.x - sin(speed)*vVector.z); | |
} | |
void Camera2::Mouse_Move(int wndWidth, int wndHieght) | |
{ | |
//MouseInfo mousePos; | |
POINT mousePos; | |
int mid_X = wndWidth >> 1; | |
int mid_Y = wndHieght >> 1; | |
float angle_Y = 0.0f; | |
float angle_Z = 0.0f; | |
GetCursorPos(&mousePos); //built in Microsoft function which gets the mouse curose 2D position (x, y) | |
if ((mousePos.x == mid_X) && (mousePos.y == mid_Y)) | |
{ | |
return; | |
} | |
SetCursorPos(mid_X, mid_Y); //Another microsoft function | |
//Get the direction from the mouse curose, set a manevering speed | |
angle_Y = (float) ( (mid_X - mousePos.x) ) / 1000; | |
angle_Z = (float) ( (mid_Y - mousePos.y) ) / 1000; | |
//This speed determines the cameras looks speed | |
mView.y += angle_Z * 2; | |
if (mView.y > 3.5f) | |
{ | |
mView.y = 3.5f; | |
} | |
if (mView.y < -5.4f) | |
{ | |
mView.y = -5.4f; | |
} | |
Rotate_Position(-angle_Y); | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma once | |
#include "Structures.h" | |
#include <math.h> | |
#include <Windows.h> | |
class Camera2 | |
{ | |
public: | |
Camera2(); | |
~Camera2(); | |
Vector3 mPos; | |
Vector3 mView; | |
Vector3 mUp; | |
void Move_Camera(float speed); | |
void Mouse_Move(int wndWidth, int wndHieght); | |
void Rotate_View(float speed); | |
void Rotate_Position(float speed); | |
void Position_Camera(float pos_x, float pos_y, float pos_z, | |
float view_x, float view_y, float view_z, | |
float up_x, float up_y, float up_z); | |
private: | |
}; | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "helloGl.h" | |
#define CAMERASPEED 0.05f | |
helloGl::helloGl(int argc, char* argv[]) | |
{ | |
srand(time(NULL)); | |
InitGL(argc, argv); | |
InitObjects(); | |
glutMainLoop(); | |
} | |
void helloGl::LightingForGame() | |
{ | |
_lightPosition = new Vector4(); | |
_lightPosition->x = 0.0; | |
_lightPosition->y = 0.0; | |
_lightPosition->z = 1.0; | |
_lightPosition->w = 0.0; | |
_lightData = new Lighting(); | |
_lightData->Ambient.x = 0.9; | |
_lightData->Ambient.y = 0.9; | |
_lightData->Ambient.z = 0.9; | |
_lightData->Ambient.w = 1.0; | |
_lightData->Diffuse.x = 0.0; | |
_lightData->Diffuse.y = 0.0; | |
_lightData->Diffuse.z = 0.0; | |
_lightData->Diffuse.w = 1.0; | |
_lightData->Specular.x = 0.5; | |
_lightData->Specular.y = 0.5; | |
_lightData->Specular.z = 0.5; | |
_lightData->Specular.w = 1.0; | |
glLightfv(GL_LIGHT0, GL_AMBIENT, &(_lightData->Ambient.x)); | |
glLightfv(GL_LIGHT0, GL_AMBIENT, &(_lightData->Ambient.y)); | |
glLightfv(GL_LIGHT0, GL_AMBIENT, &(_lightData->Ambient.z)); | |
glLightfv(GL_LIGHT0, GL_DIFFUSE, &(_lightData->Diffuse.x)); | |
glLightfv(GL_LIGHT0, GL_DIFFUSE, &(_lightData->Diffuse.y)); | |
glLightfv(GL_LIGHT0, GL_DIFFUSE, &(_lightData->Diffuse.z)); | |
glLightfv(GL_LIGHT0, GL_SPECULAR, &(_lightData->Specular.x)); | |
glLightfv(GL_LIGHT0, GL_SPECULAR, &(_lightData->Specular.y)); | |
glLightfv(GL_LIGHT0, GL_SPECULAR, &(_lightData->Specular.z)); | |
glLightfv(GL_LIGHT0, GL_POSITION, &(_lightPosition->x)); | |
glLightfv(GL_LIGHT0, GL_POSITION, &(_lightPosition->y)); | |
glLightfv(GL_LIGHT0, GL_POSITION, &(_lightPosition->z)); | |
glLightfv(GL_LIGHT0, GL_POSITION, &(_lightPosition->w)); | |
_material = new Material(); | |
_material->Ambient.x = 1.0; | |
_material->Ambient.y = 1.0; | |
_material->Ambient.z = 1.0; | |
_material->Ambient.w = 1.0; | |
_material->Diffuse.x = 0.0; | |
_material->Diffuse.y = 0.0; | |
_material->Diffuse.z = 0.0; | |
_material->Diffuse.w = 0.0; | |
_material->Specular.x = 1.0; | |
_material->Specular.y = 1.0; | |
_material->Specular.z = 1.0; | |
_material->Specular.w = 1.0; | |
_material->Shininess = 100.0f; | |
glMaterialfv(GL_FRONT, GL_AMBIENT, &(_material->Ambient.x)); | |
glMaterialfv(GL_FRONT, GL_AMBIENT, &(_material->Ambient.y)); | |
glMaterialfv(GL_FRONT, GL_AMBIENT, &(_material->Ambient.z)); | |
glMaterialfv(GL_FRONT, GL_DIFFUSE, &(_material->Diffuse.x)); | |
glMaterialfv(GL_FRONT, GL_DIFFUSE, &(_material->Diffuse.y)); | |
glMaterialfv(GL_FRONT, GL_DIFFUSE, &(_material->Diffuse.z)); | |
glMaterialfv(GL_FRONT, GL_SPECULAR, &(_material->Specular.x)); | |
glMaterialfv(GL_FRONT, GL_SPECULAR, &(_material->Specular.y)); | |
glMaterialfv(GL_FRONT, GL_SPECULAR, &(_material->Specular.z)); | |
glMaterialf(GL_FRONT, GL_SHININESS, _material->Shininess); | |
} | |
void helloGl::InitObjects() | |
{ | |
Texture2D* texture = new Texture2D(); | |
texture->LoadTexture("HUMMWV.jpg"); | |
ObjMesh* objMesh = ModelLoader::Load("HUMMWV.obj"); | |
Vector3 position; | |
position.x = 1.0f; | |
position.y = 1.0f; | |
position.z = 1.0f; | |
_player = new Player(objMesh, texture, position); | |
Vector3 _flatPos; | |
_flatPos.x = -5.0f; | |
_flatPos.y = 50.0f; | |
_flatPos.z = 1.0f; | |
Vector3 _whiteHousePos; | |
_whiteHousePos.x = -5.0f; | |
_whiteHousePos.y = 0.0f; | |
_whiteHousePos.z = 15.0f; | |
Vector3 _flatPos1; | |
_flatPos1.x = -5.0f; | |
_flatPos1.y = 1.0f; | |
_flatPos1.z = 30.0f; | |
Vector3 _whiteHousePos1; | |
_whiteHousePos1.x = -5.0f; | |
_whiteHousePos1.y = 0.0f; | |
_whiteHousePos1.z = 45.0f; | |
Vector3 terrainPosition; | |
terrainPosition.x = 1.0f; | |
terrainPosition.y = -1.0f; | |
terrainPosition.z = 1.0f; | |
Texture2D* textureTerrain = new Texture2D(); | |
textureTerrain->LoadTexture("GDT.tga"); | |
ObjMesh* objMeshTerrain = ModelLoader::Load("PaulShip.obj"); | |
for (int i = 0; i < 20; i++) | |
{ | |
Vector3 _randomPosition; | |
_randomPosition.x = rand() % 100; | |
_randomPosition.y = 100.0f; | |
_randomPosition.z = rand() % 100; | |
_terrain[i] = new Terrain(objMeshTerrain, textureTerrain, Vector3(rand() % 100, 100, rand() % 100)); | |
} | |
Texture2D* textureBody = new Texture2D(); | |
textureBody->LoadTexture("flat.tga"); | |
ObjMesh* flatMesh = ModelLoader::Load("flat.obj"); | |
Texture2D* textureWhiteHouse = new Texture2D(); | |
textureWhiteHouse->LoadTexture("whiteHouse.tga"); | |
ObjMesh* whiteHouseMesh = ModelLoader::Load("TheWhiteHouse.obj"); | |
Texture2D* textureRoad = new Texture2D(); | |
textureRoad->LoadTexture("road.png"); | |
ObjMesh* roadMesh = ModelLoader::Load("road.obj"); | |
bodyFlat = new SceneNode(flatMesh, textureBody); | |
bodyFlat->SetParentPosition(_flatPos); | |
bodyFlat1 = new SceneNode(flatMesh, textureBody); | |
bodyFlat1->SetChildPositionWS(_flatPos1); | |
bodyFlat->AddChild(bodyFlat1); | |
whiteHouse = new SceneNode(whiteHouseMesh, textureWhiteHouse); | |
whiteHouse->SetChildPositionWS(_whiteHousePos); | |
bodyFlat->AddChild(whiteHouse); | |
whiteHouse1 = new SceneNode(whiteHouseMesh, textureWhiteHouse); | |
whiteHouse1->SetChildPositionWS(_whiteHousePos1); | |
bodyFlat->AddChild(whiteHouse1); | |
//The camera info is used to set the look down approach with a 45 degree angle | |
_camera = new CameraInfo(); | |
_camera->eye.x = position.x + cos(45) * -10; | |
_camera->eye.y = position.y + cos(45) * 3; | |
_camera->eye.z = position.z + cos(45) * 0; | |
//Position //Targert View Height | |
objCamera.Position_Camera(_camera->eye.x, _camera->eye.y, _camera->eye.z, 0, 1.5f, 0, 0, 1.0f, 0); | |
//0, 1.5, 4.0 | |
} | |
void helloGl::InitGL(int argc, char* argv[]) | |
{ | |
GLUTCallbacks::Init(this); | |
glutInit(&argc, argv); | |
glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH); | |
glutInitWindowSize(WINDOW_WIDTH, WINDOW_HEIGHT); | |
glutInitDisplayMode(GLUT_DOUBLE); | |
glutCreateWindow("Simple OpenGL Program"); | |
glutDisplayFunc(GLUTCallbacks::Display); | |
glEnable(GL_DEPTH_TEST); | |
glutTimerFunc(REFRESHRATE, GLUTCallbacks::Timer, REFRESHRATE); | |
glutKeyboardFunc(GLUTCallbacks::Keyboard); | |
glutMotionFunc(GLUTCallbacks::Motion); | |
glutMouseFunc(GLUTCallbacks::Mouse); | |
glEnable(GL_TEXTURE_2D); | |
glEnable(GL_CULL_FACE); | |
glCullFace(GL_BACK); | |
// Use the Projection Matrix | |
glMatrixMode(GL_PROJECTION); | |
glLoadIdentity(); | |
// Set the viewport to be the entire window | |
glViewport(0, 0, 800, 800); | |
gluPerspective(45, 1, 0.1, 1000); | |
glEnable(GL_DEPTH_TEST); | |
glEnable(GL_TEXTURE_2D); | |
glEnable(GL_LIGHTING); | |
glEnable(GL_LIGHT0); | |
} | |
void helloGl::Display() | |
{ | |
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); //This clears the scene so we start a fresh each time | |
glPushMatrix(); | |
glTranslatef(objCamera.mView.x, 0.0f, objCamera.mView.z); | |
glScalef(0.125f, 0.5f, 0.125f); | |
_player->Draw(); | |
glPopMatrix(); | |
glPushMatrix(); | |
glRotatef(10, 0, 1, 0); | |
whiteHouse1->Draw(); | |
glPopMatrix(); | |
for (int i = 0; i < 20; i++) | |
{ | |
_terrain[i]->Draw(); | |
} | |
Draw_Grid(); | |
Vector3 v = { 1.0f, 1.0f, 1.0f }; | |
Colour c = { 1.0f, 0.0f, 0.0f }; | |
DrawString("WELCOME TO MY SCENE", &v, &c); | |
glPushMatrix(); //Drawing a cube | |
//bodyFlat->Draw(); | |
whiteHouse->Draw(); | |
bodyFlat1->Draw(); | |
glEnd(); | |
glPopMatrix(); | |
glFlush(); //Flushes the scene we just drew to the graphics card | |
glutSwapBuffers(); | |
} | |
void helloGl::Update() | |
{ | |
glMatrixMode(GL_MODELVIEW); | |
glLoadIdentity(); | |
_rotation += 0.5f; | |
if (_rotation >= 360.0f) | |
{ | |
_rotation = 0.0f; | |
} | |
_moveShip += 0.0125f; | |
for (int i = 0; i < 20; i++) | |
{ | |
_terrain[i]->Update(); | |
_terrain[i]->Move(Vector3(0.0f, 0.0f, _moveShip)); | |
} | |
//LightingForGame(); | |
ShowCursor(FALSE); //does not display curosr, Microsoft function | |
gluLookAt(objCamera.mPos.x, objCamera.mPos.y, objCamera.mPos.z, | |
objCamera.mView.x, objCamera.mView.y, objCamera.mView.z, | |
objCamera.mUp.x, objCamera.mUp.y, objCamera.mUp.z); | |
objCamera.Mouse_Move(WINDOW_WIDTH, WINDOW_HEIGHT); | |
whiteHouse->GetChildPosition(); | |
_player->Update(); | |
_player->GetX(); | |
// _terrain->Update(); | |
glutPostRedisplay(); | |
} | |
void helloGl::Draw_Grid() | |
{ | |
for (float i = -500; i <= 500; i += 5) | |
{ | |
glBegin(GL_LINES); | |
glColor3ub(150, 190, 150); | |
glVertex3f(-500, 0, i); | |
glVertex3f(500, 0, i); | |
glVertex3f(i, 0, -500); | |
glVertex3f(i, 0, 500); | |
glEnd(); | |
} | |
} | |
void helloGl::DrawString(const char * text, const Vector3 * position, const Colour * color) | |
{ | |
//glScalef(200, 200, 200); | |
glPushMatrix(); | |
glDisable(GL_TEXTURE); | |
glDisable(GL_LIGHTING); | |
glColor3f(1.0 , 0.0 , 0.0); | |
glTranslatef(position->x, position->y, position->z); | |
glRasterPos2f(0.0f, 0.0f); | |
glutBitmapString(GLUT_BITMAP_HELVETICA_18, (unsigned char*)text); | |
glEnable(GL_LIGHTING); | |
glEnable(GL_TEXTURE); | |
glPopMatrix(); | |
} | |
void helloGl::Keyboard(unsigned char key, int x, int y) //self explained | |
{ | |
if (key == 'w') | |
{ | |
objCamera.Move_Camera(CAMERASPEED); | |
} | |
if (key == 'a') | |
{ | |
objCamera.Rotate_Position(-CAMERASPEED); | |
} | |
if (key == 's') | |
{ | |
objCamera.Move_Camera(-CAMERASPEED); | |
} | |
if (key == 'd') | |
{ | |
objCamera.Rotate_Position(CAMERASPEED); | |
} | |
if (key == 'i') | |
{ | |
bodyFlat->MoveParent(Vector3(0.0f, 0.0f, 1.0f)); | |
whiteHouse->MoveParent(Vector3(0.0f, 0.0f, 1.0f)); | |
} | |
if (key == 'e') | |
{ | |
exit(0); | |
} | |
} | |
void helloGl::Motion(int x, int y) //Here in case I want to use them | |
{ | |
} | |
void helloGl::Mouse(int button, int state, int x, int y) //Here in case I want to use them | |
{ | |
} | |
helloGl::~helloGl() | |
{ | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma once | |
#include <Windows.h> //Required for OpenGL on Windows | |
#include <gl/GL.h> //OpenGL | |
#include <gl/GLU.h> //OpenGL Utilities | |
#include "GL\freeglut.h" //freeglut library | |
#include "GLUTCallback.h" | |
#include "ObjSceneObject.h" | |
#include "Player.h" | |
#include "Terrain.h" | |
#include "ModelLoader.h" | |
#include "Camera2.h" | |
#include "SceneNode.h" | |
#include <math.h> | |
#include <cstdlib> | |
#include <ctime> | |
//http://stackoverflow.com/questions/5681948/multiple-textures-opengl-glut-c // use recursion? | |
//https://cboard.cprogramming.com/game-programming/98968-multiple-textures-opengl.html //Potential multiple textures | |
#define REFRESHRATE 16 | |
class helloGl | |
{ | |
private: | |
CameraInfo* _camera; | |
Player* _player; | |
Terrain* _terrain[20]; | |
Camera2 objCamera; | |
//ScreenSize | |
int WINDOW_WIDTH = 1920; | |
int WINDOW_HEIGHT = 1080; | |
//Lighting | |
Lighting* _lightData; | |
Vector4* _lightPosition; | |
Material* _material; | |
//Doing testing for sceneNode | |
SceneNode* whiteHouse; | |
SceneNode* whiteHouse1; | |
SceneNode* bodyFlat; | |
SceneNode* bodyFlat1; | |
//variables | |
float _rotation; | |
float _moveShip; | |
public: | |
helloGl(int argc, char* argv[]); | |
void LightingForGame(); | |
void Display(); | |
~helloGl(void); | |
void InitObjects(); | |
void InitGL(int argc, char* argv[]); | |
void Update(); | |
//Testing Ground | |
void Draw_Grid(); | |
void DrawString(const char* text, const Vector3* position, const Colour* color); | |
void Keyboard(unsigned char key, int x, int y); | |
void Motion(int x, int y); | |
void Mouse(int button, int state, int x, int y); | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "ModelLoader.h" | |
using namespace std; | |
namespace ModelLoader | |
{ | |
void LoadVertices(ObjMesh& mesh, string& buffer); | |
void LoadFaces(ObjMesh& mesh, string &buffer); | |
void LoadTexCoords(ObjMesh& mesh, string &buffer); | |
void LoadNormals(ObjMesh& mesh, string &buffer); | |
std::vector<Vertex> tempVertices; | |
std::vector<Textures> tempTextures; | |
std::vector<Vertex> tempNormals; | |
void LoadVertices(ObjMesh & mesh, string& buffer) | |
{ | |
if (buffer[0] == 'v' && buffer[1] == ' ') | |
{ | |
//string buffer; | |
Vertex temp3; | |
float tmpX, tmpY, tmpZ; | |
sscanf_s(buffer.c_str(), "v %f %f %f", &tmpX, &tmpY, &tmpZ); | |
temp3.x = tmpX; | |
temp3.y = tmpY; | |
temp3.z = tmpZ; | |
tempVertices.push_back(temp3); | |
} | |
} | |
void LoadTexCoords(ObjMesh & mesh, string& buffer) | |
{ | |
if (buffer[0] == 'v' && buffer[1] == 't') | |
{ | |
Textures temp2; | |
float tmpU, tmpV; | |
sscanf_s(buffer.c_str(), "vt %f %f", &tmpU, &tmpV); | |
temp2.u = tmpU; | |
temp2.v = tmpV; | |
tempTextures.push_back(temp2); | |
} | |
} | |
void LoadNormals(ObjMesh & mesh, string& buffer) | |
{ | |
if (buffer[0] == 'v' && buffer[1] == 'n') | |
{ | |
Vertex temp3; | |
float tmpX, tmpY, tmpZ; | |
sscanf_s(buffer.c_str(), "vn %f %f %f", &tmpX, &tmpY, &tmpZ); | |
temp3.x = tmpX; | |
temp3.y = tmpY; | |
temp3.z = tmpZ; | |
tempNormals.push_back(temp3); | |
} | |
} | |
void LoadFaces(ObjMesh & mesh, string& buffer) | |
{ | |
if (buffer[0] == 'f' && buffer[1] == ' ') | |
{ | |
if (sscanf_s(buffer.c_str(), "f %i//%i %i//%i %i//%i", &mesh.vertexIndex[0], &mesh.normalIndex[0], &mesh.vertexIndex[1], &mesh.normalIndex[1], &mesh.vertexIndex[2], &mesh.normalIndex[2]) == 6) //Vertex and normal | |
{ | |
mesh.vertexIndices.push_back(mesh.vertexIndex[0]); | |
mesh.vertexIndices.push_back(mesh.vertexIndex[1]); | |
mesh.vertexIndices.push_back(mesh.vertexIndex[2]); | |
mesh.normalIndices.push_back(mesh.normalIndex[0]); | |
mesh.normalIndices.push_back(mesh.normalIndex[1]); | |
mesh.normalIndices.push_back(mesh.normalIndex[2]); | |
} | |
if (sscanf_s(buffer.c_str(), "f %i/%i/%i %i/%i/%i %i/%i/%i", &mesh.vertexIndex[0], &mesh.uvIndex[0], &mesh.normalIndex[0], | |
&mesh.vertexIndex[1], &mesh.uvIndex[1], &mesh.normalIndex[1], | |
&mesh.vertexIndex[2], &mesh.uvIndex[2], &mesh.normalIndex[2]) == 9) //vertex normal and texture | |
{ | |
mesh.textureEnabled = true; | |
mesh.vertexIndices.push_back(mesh.vertexIndex[0]); | |
mesh.vertexIndices.push_back(mesh.vertexIndex[1]); | |
mesh.vertexIndices.push_back(mesh.vertexIndex[2]); | |
mesh.normalIndices.push_back(mesh.normalIndex[0]); | |
mesh.normalIndices.push_back(mesh.normalIndex[1]); | |
mesh.normalIndices.push_back(mesh.normalIndex[2]); | |
mesh.uvIndices.push_back(mesh.uvIndex[0]); | |
mesh.uvIndices.push_back(mesh.uvIndex[1]); | |
mesh.uvIndices.push_back(mesh.uvIndex[2]); | |
} | |
if (sscanf_s(buffer.c_str(), "f %i/%i %i/%i %i/%i", &mesh.vertexIndex[0], &mesh.uvIndex[0], &mesh.vertexIndex[1], &mesh.uvIndex[1], &mesh.vertexIndex[2], &mesh.uvIndex[2]) == 6) //vertex and texture | |
{ | |
mesh.textureEnabled = true; | |
mesh.vertexIndices.push_back(mesh.vertexIndex[0]); | |
mesh.vertexIndices.push_back(mesh.vertexIndex[1]); | |
mesh.vertexIndices.push_back(mesh.vertexIndex[2]); | |
mesh.uvIndices.push_back(mesh.uvIndex[0]); | |
mesh.uvIndices.push_back(mesh.uvIndex[1]); | |
mesh.uvIndices.push_back(mesh.uvIndex[2]); | |
} | |
} | |
} | |
ObjMesh * ModelLoader::Load(char * path) | |
{ | |
ObjMesh* objMesh = new ObjMesh(); | |
ifstream file; | |
file.open(path); | |
string buffer; | |
if (!file.good()) | |
{ | |
cerr << "Can't open texture file " << path << endl; | |
return nullptr; | |
} | |
while (!file.eof()) | |
{ | |
getline(file, buffer); | |
if (buffer[0] == '#') | |
{ | |
continue; | |
} | |
else | |
{ | |
LoadVertices(*objMesh, buffer); | |
LoadNormals(*objMesh, buffer); | |
LoadTexCoords(*objMesh, buffer); | |
LoadFaces(*objMesh, buffer); | |
} | |
} | |
file.close(); | |
for (unsigned int i = 0; i < objMesh->vertexIndices.size(); i++) | |
{ | |
unsigned int vertexIndex = objMesh->vertexIndices[i]; | |
Vertex faceVertices; | |
faceVertices = tempVertices[vertexIndex - 1]; | |
objMesh->vertices.push_back(faceVertices); | |
} | |
for (unsigned int i = 0; i < objMesh->normalIndices.size(); i++) | |
{ | |
unsigned int normalIndex = objMesh->normalIndices[i]; | |
Vertex faceNormals; | |
faceNormals = tempNormals[normalIndex - 1]; | |
objMesh->normals.push_back(faceNormals); | |
} | |
for (unsigned int i = 0; i < objMesh->uvIndices.size(); i++) | |
{ | |
unsigned int uvIndex = objMesh->uvIndices[i]; | |
Textures faceTextures; | |
faceTextures = tempTextures[uvIndex - 1]; | |
objMesh->textures.push_back(faceTextures); | |
} | |
tempNormals.clear(); //Microsoft function which will clear the temp arrays allowing me to load in new arrays | |
tempTextures.clear(); | |
tempVertices.clear(); | |
return objMesh; | |
} | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma once | |
#include "Structures.h" | |
#include <vector> | |
#include <iostream> | |
#include <fstream> | |
#include <string> | |
#include <sstream> | |
namespace ModelLoader | |
{ | |
ObjMesh* Load(char* path); | |
}; | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "ObjSceneObject.h" | |
ObjSceneObject::ObjSceneObject(ObjMesh * mesh, Texture2D * texture) | |
{ | |
_mesh = mesh; | |
_texture = texture; | |
} | |
ObjSceneObject::~ObjSceneObject(void) | |
{ | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma once | |
#include "Structures.h" | |
#include "Texture2D.h" | |
class ObjSceneObject | |
{ | |
protected: | |
ObjMesh* _mesh; | |
Texture2D* _texture; | |
public: | |
ObjSceneObject(ObjMesh* mesh, Texture2D* texture); | |
virtual ~ObjSceneObject(void); | |
virtual void Update() = 0; | |
virtual void Draw() = 0; | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "Player.h" | |
#include "helloGl.h" | |
Player::Player(ObjMesh * mesh, Texture2D * texture, Vector3 position) : ObjSceneObject(mesh, texture) | |
{ | |
_position.x = position.x; | |
_position.y = position.y; | |
_position.z = position.z; | |
} | |
void Player::Move(Vector3 moveAmount) | |
{ | |
_position.x += moveAmount.x; | |
_position.y += moveAmount.y; | |
_position.z += moveAmount.z; | |
} | |
GLfloat Player::GetX() | |
{ | |
std::cout << _position.x << std::endl; | |
return _position.x; | |
} | |
GLfloat Player::GetY() | |
{ | |
return _position.y; | |
} | |
GLfloat Player::GetZ() | |
{ | |
return _position.z; | |
} | |
void Player::Update() | |
{ | |
} | |
void Player::Draw() | |
{ | |
if (_mesh->textureEnabled == true) | |
{ | |
glBindTexture(GL_TEXTURE_2D, _texture->GetID()); | |
glEnableClientState(GL_NORMAL_ARRAY); | |
glEnableClientState(GL_VERTEX_ARRAY); | |
glEnableClientState(GL_TEXTURE_COORD_ARRAY); | |
glVertexPointer(3, GL_FLOAT, 0, &_mesh->vertices[0]); | |
glNormalPointer(GL_FLOAT, 0, &_mesh->normals[0]); | |
glTexCoordPointer(2, GL_FLOAT, 0, &_mesh->textures[0]); | |
glPushMatrix(); | |
glScalef(0.1f, 0.1f, 0.1f); | |
glTranslatef(_position.x, _position.y, _position.z); | |
glDrawArrays(GL_TRIANGLES, 0, _mesh->vertices.size()); | |
glPopMatrix(); | |
glDisableClientState(GL_VERTEX_ARRAY); | |
glDisableClientState(GL_NORMAL_ARRAY); | |
glDisableClientState(GL_TEXTURE_COORD_ARRAY); | |
} | |
else | |
{ | |
glEnableClientState(GL_NORMAL_ARRAY); | |
glEnableClientState(GL_VERTEX_ARRAY); | |
glVertexPointer(3, GL_FLOAT, 0, &_mesh->vertices[0]); | |
glNormalPointer(GL_FLOAT, 0, &_mesh->normals[0]); | |
glPushMatrix(); | |
glTranslatef(-4.0f, -0.0f, -6.0f); | |
glDrawArrays(GL_TRIANGLES, 0, _mesh->vertices.size()); | |
glPopMatrix(); | |
glDisableClientState(GL_VERTEX_ARRAY); | |
glDisableClientState(GL_NORMAL_ARRAY); | |
} | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma once | |
#include "Structures.h" | |
#include "ObjSceneObject.h" | |
#include "GLUTCallback.h" | |
class Player : public ObjSceneObject | |
{ | |
private: | |
Vector3 _position; | |
Vector3 _rotation; | |
public: | |
Player(ObjMesh* mesh, Texture2D* texture, Vector3 position); | |
//Player Movement | |
void Move(Vector3 moveAmount); | |
//Used by camera for third person view | |
GLfloat GetX(); | |
GLfloat GetY(); | |
GLfloat GetZ(); | |
void Update(); | |
void Draw(); | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma once | |
#include <Windows.h> //Required for OpenGL on Windows | |
#include <gl/GL.h> //OpenGL | |
#include <gl/GLU.h> //OpenGL Utilities | |
#include "GL\freeglut.h" //freeglut library | |
#include <vector> | |
struct Vertex | |
{ | |
GLfloat x, y, z; | |
}; | |
struct Colour | |
{ | |
GLfloat r, b, g; | |
}; | |
struct Textures | |
{ | |
GLfloat u, v; | |
}; | |
struct Vector3 | |
{ | |
GLfloat x; | |
GLfloat y; | |
GLfloat z; | |
Vector3() | |
{ | |
this->x = 0.0f; | |
this->y = 0.0f; | |
this->z = 0.0f; | |
} | |
Vector3(GLfloat x, GLfloat y, GLfloat z) | |
{ | |
this->x = x; | |
this->y = y; | |
this->z = z; | |
} | |
}; | |
struct treenode | |
{ | |
char data; | |
treenode * child; | |
treenode * sibling; | |
}; | |
struct CameraInfo | |
{ | |
Vector3 eye; | |
Vector3 center; | |
Vector3 up; | |
}; | |
struct ObjMesh | |
{ | |
std::vector<Vertex> vertices; | |
std::vector<Vertex> normals; | |
std::vector<Textures> textures; | |
bool textureEnabled = false; | |
//setting up the indices | |
unsigned int vertexIndex[3], uvIndex[3], normalIndex[3]; | |
std::vector<unsigned int> vertexIndices, uvIndices, normalIndices; | |
}; | |
struct Vector4 | |
{ | |
GLfloat x, y, z, w; | |
}; | |
struct Lighting | |
{ | |
Vector4 Ambient; | |
Vector4 Diffuse; | |
Vector4 Specular; | |
}; | |
struct Material | |
{ | |
Vector4 Ambient; | |
Vector4 Diffuse; | |
Vector4 Specular; | |
GLfloat Shininess; | |
}; | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "Terrain.h" | |
Terrain::Terrain(ObjMesh * mesh, Texture2D * texture, Vector3 position) : ObjSceneObject(mesh, texture) | |
{ | |
_position.x = position.x; | |
_position.y = position.y; | |
_position.z = position.z; | |
} | |
void Terrain::Move(Vector3 moveAmount) | |
{ | |
_position.x += moveAmount.x; | |
_position.y += moveAmount.y; | |
_position.z += moveAmount.z; | |
} | |
void Terrain::Update() | |
{ | |
} | |
void Terrain::Draw() | |
{ | |
if (_mesh->textureEnabled == true) | |
{ | |
glBindTexture(GL_TEXTURE_2D, _texture->GetID()); | |
glEnableClientState(GL_NORMAL_ARRAY); | |
glEnableClientState(GL_VERTEX_ARRAY); | |
glEnableClientState(GL_TEXTURE_COORD_ARRAY); | |
glVertexPointer(3, GL_FLOAT, 0, &_mesh->vertices[0]); | |
glNormalPointer(GL_FLOAT, 0, &_mesh->normals[0]); | |
glTexCoordPointer(2, GL_FLOAT, 0, &_mesh->textures[0]); | |
glPushMatrix(); | |
glScalef(0.1f, 0.1f, 0.1f); | |
glTranslatef(_position.x, _position.y, _position.z); | |
glDrawArrays(GL_TRIANGLES, 0, _mesh->vertices.size()); | |
glPopMatrix(); | |
glDisableClientState(GL_VERTEX_ARRAY); | |
glDisableClientState(GL_NORMAL_ARRAY); | |
glDisableClientState(GL_TEXTURE_COORD_ARRAY); | |
} | |
else | |
{ | |
glEnableClientState(GL_NORMAL_ARRAY); | |
glEnableClientState(GL_VERTEX_ARRAY); | |
glVertexPointer(3, GL_FLOAT, 0, &_mesh->vertices[0]); | |
glNormalPointer(GL_FLOAT, 0, &_mesh->normals[0]); | |
glPushMatrix(); | |
glTranslatef(-4.0f, -0.0f, -6.0f); | |
glDrawArrays(GL_TRIANGLES, 0, _mesh->vertices.size()); | |
glPopMatrix(); | |
glDisableClientState(GL_VERTEX_ARRAY); | |
glDisableClientState(GL_NORMAL_ARRAY); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma once | |
#include "Structures.h" | |
#include "ObjSceneObject.h" | |
#include "GLUTCallback.h" | |
class Terrain : public ObjSceneObject | |
{ | |
private: | |
Vector3 _position; | |
public: | |
Terrain(ObjMesh* mesh, Texture2D* texture, Vector3 position); | |
void Move(Vector3 moveAmount); | |
void Update(); | |
void Draw(); | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma once | |
#include <Windows.h> //Required for OpenGL on Windows | |
#include <gl/GL.h> //OpenGL | |
#include <gl/GLU.h> //OpenGL Utilities | |
#include "GL\freeglut.h" //freeglut library | |
#include <SOIL.h> | |
class Texture2D | |
{ | |
private: | |
GLuint _ID; //Texture ID | |
int _width, _height; | |
public: | |
Texture2D(); | |
~Texture2D(); | |
int LoadTexture(const char* textureFileName); | |
GLuint GetID() const { return _ID; } | |
int GetWidth() const { return _width; } | |
int GetHeight() const { return _height; } | |
}; | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma once | |
#include <Windows.h> //Required for OpenGL on Windows | |
#include <gl/GL.h> //OpenGL | |
#include <gl/GLU.h> //OpenGL Utilities | |
#include "GL\freeglut.h" //freeglut library | |
#include <SOIL.h> | |
class Texture2D | |
{ | |
private: | |
GLuint _ID; //Texture ID | |
int _width, _height; | |
public: | |
Texture2D(); | |
~Texture2D(); | |
int LoadTexture(const char* textureFileName); | |
GLuint GetID() const { return _ID; } | |
int GetWidth() const { return _width; } | |
int GetHeight() const { return _height; } | |
}; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment