Commit 2291d3c7 authored by Duaa's avatar Duaa
Browse files

Implemented all the labs except 9, 13 and 14

parent 179574b8
......@@ -35,6 +35,10 @@ set( INCLUDE
include/SceneObject.hpp
include/Character.hpp
include/stbiImage.h
include/Billboard.hpp
include/Text.hpp
include/Snow.hpp
)
set( SRCS
src/Landscape.cpp
......@@ -42,6 +46,10 @@ set( SRCS
src/GameManager.cpp
src/SceneObject.cpp
src/Character.cpp
src/Billboard.cpp
src/Text.cpp
src/Snow.cpp
app/main.cpp
)
......
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE QtCreatorProject>
<!-- Written by QtCreator 4.13.3, 2021-03-18T20:44:44. -->
<!-- Written by QtCreator 4.13.3, 2021-04-19T15:44:04. -->
<qtcreator>
<data>
<variable>EnvironmentId</variable>
......
......@@ -24,6 +24,21 @@ float moveX, moveY;
void init()
{
//code to check opengl version
char* buff = (char*)glGetString(GL_VERSION);
std::cout << buff << std::endl;
//code to check vendor and graphics card
buff = (char*)glGetString(GL_VENDOR);
std::cout << buff << std::endl;
buff = (char*)glGetString(GL_RENDERER);
std::cout << buff << std::endl;
//code to check supported extensions
buff = (char*)glGetString(GL_EXTENSIONS);
std::cout << buff << std::endl;
glClearColor(0.0, 0.0, 0.0, 0.0);
glShadeModel(GL_SMOOTH);
glEnable(GL_DEPTH_TEST);
......
#pragma once
#include <windows.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include "../../glm-master/glm/gtc/matrix_transform.hpp"
#include "SceneObject.hpp"
#include "Camera.hpp"
using P3 = glm::vec3;
using V3 = glm::vec3;
using V4 = glm::vec4;
using sptrSO = std::shared_ptr<SceneObject>;
using sptrC = std::shared_ptr<Camera>;
class Billboard : public SceneObject {
public:
Billboard();
Billboard(GLfloat width, GLfloat height, P3 startPos, sptrSO sceneObj, sptrC cam);
~Billboard();
void privateInit();
void privateRender();
void privateUpdate();
sptrSO sceneObj;
sptrC cam;
protected:
private:
GLfloat width_;
GLfloat height_;
P3 startPos_;
GLuint billboard_;
};
......@@ -20,6 +20,11 @@ class Camera : public SceneObject
void moveDown();
void moveBackward();
void moveForward();
void rotateXPosDir();
void rotateXNegDir();
void rotateYPosDir();
void rotateYNegDir();
protected:
void privateInit();
......
......@@ -22,6 +22,7 @@ class Character : public SceneObject
float armor_;
float rotation;
GLuint pyramid;
GLuint character_;
};
......@@ -8,6 +8,9 @@
#include "Landscape.hpp"
#include "Character.hpp"
#include "Camera.hpp"
#include "Billboard.hpp"
#include "Text.hpp"
#include "Snow.hpp"
class GameManager : public SceneObject
{
......@@ -26,5 +29,8 @@ class GameManager : public SceneObject
std::shared_ptr<Landscape> ls_;
std::shared_ptr<Character> character_;
std::shared_ptr<Camera> cam_;
std::shared_ptr<Billboard> billboard_;
std::shared_ptr<Text> text_;
std::shared_ptr<Snow> snow_;
};
......@@ -24,12 +24,16 @@ class Landscape : public SceneObject
std::vector< glm::vec3 > vertexArray_; // Maybe two-dim vector and several arrays
GLuint index[98304];
// normal array.
std::vector<glm::vec3> nArray_;
GLuint nIndex_[98304];
int SizeofArray = 63364;
glm::vec3 n[98304];
GLuint list;
int sizeOfArray = 98304;
// texture coord array
std::vector<glm::vec2> texCoordArray_;
GLuint texName_;
};
......@@ -32,6 +32,7 @@
//
// The storage of the children is handled by smart pointers, this is because
using M4 = glm::mat4;
class SceneObject
{
public:
......@@ -64,10 +65,12 @@ class SceneObject
// This member contains the time since last frame. It is set
// before privateUpdate is called.
double fps_;
long double elapsed_;
double dt_;
// This is the transformation-matrix of the scene object.
// Relative to the object's parent. Defaults to the identity matrix.
glm::mat4 matrix_;
M4 matrix_;
private:
// List of all SceneObjects that belong to the current object.
......
#pragma once
#include <windows.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include "../../glm-master/glm/gtc/matrix_transform.hpp"
#include "SceneObject.hpp"
#include "GameManager.hpp"
#include "Camera.hpp"
#include <string>
#include <vector>
using P3 = glm::vec3;
using V3 = glm::vec3;
using M4 = glm::mat4;
using String = std::string;
using s_ptrC = std::shared_ptr<Camera>;
typedef struct // Create A Structure For Particle
{
bool active; // Active (Yes/No)
float life; // Particle Life
float fade; // Fade Speed
float x; // X Position
float y; // Y Position
float z; // Z Position
float xi; // X Direction
float yi; // Y Direction
float zi; // Z Direction
float xg; // X Gravity
float yg; // Y Gravity
float zg; // Z Gravity
}
particles; // Particles Structure
class Snow : public SceneObject {
public:
void privateInit();
void privateRender();
void privateUpdate();
void texture();
protected:
private:
GLuint texName_;
float w_, l_, h_;
P3 startPos_;
V3 wind_;
int densitycm_;
};
#pragma once
#include <windows.h>
#include <GL/GL.h>
#include <GL/GLU.h>
#include "../../glm-master/glm/gtc/matrix_transform.hpp"
#include "SceneObject.hpp"
#include "GameManager.hpp"
auto const STROKETEXT = 0;
auto const BITMAPTEXT = 1;
using P3 = glm::vec3;
class Text : public SceneObject {
public:
Text(char* string, P3 pos, void* font, int type);
void renderStrokeCharacter();
void renderBitmapCharacter();
void privateInit();
void privateRender();
void privateUpdate();
private:
P3 pos_;
void *font_;
char *string_;
int LengthOfString_;
int type_;
};
#define _USE_MATH_DEFINES
#include "../include/Billboard.hpp"
#include "../include/GameManager.hpp"
#include "../../glm-master/glm/gtc/matrix_transform.hpp"
#include <GL/gl.h>
#include <cmath>
#include <iostream>
Billboard::Billboard() {}
Billboard::Billboard(GLfloat width, GLfloat height, P3 startPos, sptrSO sceneObj, sptrC cam)
: width_(width), height_(height), startPos_(startPos), sceneObj(sceneObj), cam(cam) {}
Billboard::~Billboard() {}
void Billboard::privateInit() {
matrix_ = glm::translate(matrix_, startPos_);
billboard_ = glGenLists(1);
glNewList(billboard_, GL_COMPILE);
glBegin(GL_QUADS);
glColor3f(1.0f, 2.0f, 1.0f);
glNormal3f(0.0f, 0.0f, 1.0f);
glVertex3f(-width_/2.0f, 0.0f, 0.0f);
glVertex3f(width_/2.0f, 0.0f, 0.0f);
glVertex3f(width_/2.0f, height_, 0.0f);
glVertex3f(-width_/2.0f, height_, 0.0f);
glEnd();
glEndList();
}
void Billboard::privateRender() {
//Error handling
GLenum err = glGetError();
if (err != GL_NO_ERROR)
std::cout << gluErrorString(err) << std::endl;
glEnable(GL_COLOR_MATERIAL);
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
glCallList(billboard_);
glDisable(GL_COLOR_MATERIAL);
}
void Billboard::privateUpdate() {
auto const sceneObjMatrix = sceneObj->getMatrix();
auto const camMatrix = cam->getMatrix();
// Rotation
P3 const camPos = P3(-camMatrix[3][0], -camMatrix[3][1], -camMatrix[3][2]);
V3 const camUp = V3(camMatrix[0][1], camMatrix[1][1], camMatrix[2][1]);
P3 const billBoardPos = P3(matrix_[3][0], matrix_[3][1], matrix_[3][2]);
V3 const faceofBillboard = glm::normalize(camPos - billBoardPos); //billboard faces which way
V3 const camRight = V3( camMatrix[0][0], camMatrix[1][0], camMatrix[2][0] );
matrix_[0] = V4(camRight, 0); //Changing the billboard matrix so it always faces the cam even when the cam is rotating
matrix_[1] = V4(camUp, 0);
matrix_[2] = V4(faceofBillboard, 0);
// Position - makes the billboard follow the object its attached to
matrix_[3][0] = sceneObjMatrix[3][0] + startPos_.x;
matrix_[3][1] = sceneObjMatrix[3][1] + startPos_.y;
matrix_[3][2] = sceneObjMatrix[3][2] + startPos_.z;
}
......@@ -56,3 +56,25 @@ void Camera::moveBackward()
matrix_ = glm::translate(matrix_, glm::vec3(0.0f, 0.0f, -1.0f));
}
//Camera rotation
void Camera::rotateXPosDir()
{
glm::mat4 rotate = glm::rotate(glm::mat4(1.0f), 0.05f, glm::vec3(1.0f, 0.0f, 0.0f));
matrix_ = rotate * matrix_;
}
void Camera::rotateXNegDir()
{
glm::mat4 rotate = glm::rotate(glm::mat4(1.0f), -0.05f, glm::vec3(1.0f, 0.0f, 0.0f));
matrix_ = rotate * matrix_;
}
void Camera::rotateYPosDir()
{
glm::mat4 rotate = glm::rotate(glm::mat4(1.0f), 0.05f, glm::vec3(0.0f, 1.0f, 0.0f));
matrix_ = rotate * matrix_;
}
void Camera::rotateYNegDir()
{
glm::mat4 rotate = glm::rotate(glm::mat4(1.0f), -0.05f, glm::vec3(0.0f, 1.0f, 0.0f));
matrix_ = rotate * matrix_;
}
......@@ -3,7 +3,9 @@
#include <iostream>
#include <cmath>
#include "../include/stbiImage.h"
//#include "../include/stbi_implementation.h"
static GLuint name;
Character::Character()
{
......@@ -17,7 +19,7 @@ Character::~Character()
void Character::privateInit()
{
//loading image and applying texture
GLuint name;
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glGenTextures(1, &name);
glBindTexture(GL_TEXTURE_2D, name);
......@@ -35,7 +37,7 @@ void Character::privateInit()
else{
std::cout<<"Unable to load file"<< std::endl;;
std::cout<<"Unable to load pyramid image"<< std::endl;;
}
stbi_image_free(data);
......@@ -54,20 +56,83 @@ void Character::privateInit()
{size, -size, 0.0f},
{size, size, 0.0f},
{-size, size, 0.0f}};
glVertex3fv(v0[0]);
//calculate normals of character (pyramid)
float norm = sqrt((size * size) + (sizez * sizez));
GLfloat n[4][3] = {{0.0f, -sizez/norm, size/norm},
{sizez/norm, 0.0f, size/norm},
{0.0f, sizez/norm, size/norm},
{-sizez/norm, 0.0f, size/norm}};
glColor3f(0.7f, 0.7f, 1.0f);
glNormal3fv(n[0]); //set current normal vector
glTexCoord2f(0.5, 0.0); //set current texture coordinate
glVertex3fv(v0[0]); //starting address of vertex array
glColor3f(0.0f, 0.7f, 0.0f);
glNormal3fv(n[0]);
glTexCoord2f(0.0, 1.0);
glVertex3fv(v0[1]);
glColor3f(0.5f, 0.0f, 1.0f);
glNormal3fv(n[0]);
glTexCoord2f(1.0, 1.0);
glVertex3fv(v0[2]);
glColor3f(0.0f, 0.5f, 1.0f);
glNormal3fv(n[1]);
glTexCoord2f(0.5, 0.0);
glVertex3fv(v0[0]);
glColor3f(0.2f, 0.8f, 0.0f);
glNormal3fv(n[1]);
glTexCoord2f(1.0, 1.0);
glVertex3fv(v0[2]);
glColor3f(0.8f, 0.2f, 1.0f);
glNormal3fv(n[1]);
glTexCoord2f(0.0, 1.0);
glVertex3fv(v0[3]);
glColor3f(0.7f, 0.7f, 1.0f);
glNormal3fv(n[2]);
glTexCoord2f(0.5, 0.0);
glVertex3fv(v0[0]);
glColor3f(0.6f, 0.3f, 1.0f);
glNormal3fv(n[2]);
glTexCoord2f(0.0, 1.0);
glVertex3fv(v0[3]);
glColor3f(0.7f, 0.7f, 0.0f);
glNormal3fv(n[2]);
glTexCoord2f(1.0, 1.0);
glVertex3fv(v0[4]);
glColor3f(0.4f, 0.1f, 1.0f);
glNormal3fv(n[3]);
glTexCoord2f(0.5, 0.0);
glVertex3fv(v0[0]);
glColor3f(0.3f, 0.3f, 1.0f);
glNormal3fv(n[3]);
glTexCoord2f(1.0, 1.0);
glVertex3fv(v0[4]);
glColor3f(0.7f, 0.7f, 0.0f);
glNormal3fv(n[3]);
glTexCoord2f(0.0, 1.0);
glVertex3fv(v0[1]);
glEnd();
glBegin(GL_TRIANGLE_STRIP);
glVertex3fv(v0[1]);
glVertex3fv(v0[4]);
glVertex3fv(v0[2]);
glVertex3fv(v0[3]);
glColor3f(1.0f, 0.0f, 0.0f); glVertex3fv(v0[1]);
glColor3f(0.0f, 1.0f, 0.0f); glVertex3fv(v0[4]);
glColor3f(0.0f, 0.0f, 1.0f); glVertex3fv(v0[2]);
glColor3f(0.0f, 1.0f, 0.0f); glVertex3fv(v0[3]);
glEnd();
......@@ -82,20 +147,25 @@ void Character::privateInit()
GLfloat posOfLight[] = {-20.0, 100.0, -80.0, 1.0};
glLightfv(GL_LIGHT0, GL_POSITION, posOfLight);
// glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
// glCallList(pyramid);
}
void Character::privateRender()
{
//error handling
GLenum err = glGetError();
if (err != GL_NO_ERROR)
std::cout << gluErrorString(err) << std::endl;
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, name);
glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE);
glEnable(GL_COLOR_MATERIAL);
glColor3f(0.0f, 0.0f, 1.0f);
glColor3f(1.0f, 0.0f, 0.0f);
glColor3f(0.0f, 1.0f, 0.0f);
glMatrixMode(GL_MODELVIEW);
glTranslated(0.0, -25.0 + std::sin(rotation * M_PI / 45) * 20.0, -100.0);
glRotatef(rotation, 0.0f, 1.0f, 0.0f);
......@@ -103,12 +173,15 @@ void Character::privateRender()
glCallList(pyramid);
glDisable(GL_COLOR_MATERIAL);
glBindTexture(GL_TEXTURE_2D, NULL);
glDisable(GL_TEXTURE_2D);
glDisable(GL_LIGHT0);
glDisable(GL_LIGHTING);
rotation += 0.05f;
if (rotation >= 360.0f)
rotation -= 360.0f;
glDisable(GL_LIGHT0);
glDisable(GL_LIGHTING);
}
......
#include "../include/GameManager.hpp"
#include <GL/glut.h>
#include <string>
#include <iostream>
GameManager::GameManager()
{
......@@ -13,6 +17,15 @@ void GameManager::privateInit()
// Set default OpenGL states
glEnable(GL_CULL_FACE);
//Adding fog in game application
glEnable(GL_LINEAR_MIPMAP_LINEAR);
glEnable(GL_FOG);
GLfloat color[] = {0.5f, 0.5f, 0.5f, 1.0f};
glFogfv(GL_FOG_COLOR, color);
glFogf(GL_FOG_DENSITY, 0.006f);
glFogi(GL_FOG_MODE, GL_EXP);
// Adding the camera to the scene
cam_.reset(new Camera());
......@@ -22,11 +35,35 @@ void GameManager::privateInit()
character_.reset(new Character());
this->addSubObject(character_);
billboard_.reset(new Billboard(
20.0f, 10.0f, P3(0.0f, 20.0f, 0.0f), character_, cam_));
this->addSubObject(billboard_);
text_.reset(new Text(
"Bitmap Text", {-3.0f, 25.0f, 0.0f}, GLUT_BITMAP_TIMES_ROMAN_24, 1 ));
this->addSubObject(text_);
//uncomment following to see stroke text
// text_.reset(new Text(
// "Stroke Text", {-3.0f, 20.0f, 0.0f}, GLUT_STROKE_ROMAN, 0 ));
// this->addSubObject(text_);
snow_.reset(new Snow());
this->addSubObject(snow_);
}
void GameManager::privateRender()
{
// Nothing to render
//error handling
GLenum err = glGetError();
if (err != GL_NO_ERROR)
std::cout << gluErrorString(err) << std::endl;
}
void GameManager::privateUpdate()
......
#include "../include/Landscape.hpp"
#include <random>
#include "../include/stbiImage.h"
#include <iostream>
static GLuint tName;
Landscape::Landscape()
{
......@@ -11,8 +15,35 @@ Landscape::~Landscape()
void Landscape::privateInit()
{
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glGenTextures(1, &tName);
glBindTexture(GL_TEXTURE_2D, tName);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
int width, height, color;