Commit dd3e9339badf3e5f79d85bc09b1ff6cc38224557

Authored by Olav Kjartan Larseng
1 parent 1189153e

Added shaders: phong, chess and red

Showing 54 changed files with 471 additions and 60 deletions   Show diff stats
SpaceShooterV4.zip 0 → 100644
No preview for this file type
SpaceShooterV4/SpaceShooterV4.vcxproj
... ... @@ -147,32 +147,42 @@
147 147 <ResourceCompile Include="SpaceShooterV4.rc" />
148 148 </ItemGroup>
149 149 <ItemGroup>
150   - <ClInclude Include="Bullet.h" />
151   - <ClInclude Include="Collision.h" />
152   - <ClInclude Include="Enemy.h" />
153 150 <ClInclude Include="include\BattleField.hpp" />
  151 + <ClInclude Include="include\Bullet.h" />
154 152 <ClInclude Include="include\Camera.hpp" />
  153 + <ClInclude Include="include\Collision.h" />
  154 + <ClInclude Include="include\Enemy.h" />
155 155 <ClInclude Include="include\FpsCounter.hpp" />
156 156 <ClInclude Include="include\GameManager.hpp" />
157 157 <ClInclude Include="include\Input.hpp" />
158 158 <ClInclude Include="include\SceneObject.hpp" />
  159 + <ClInclude Include="include\Shader.hpp" />
159 160 <ClInclude Include="include\SpaceShip.hpp" />
  161 + <ClInclude Include="include\Spotlight.h" />
160 162 <ClInclude Include="include\Text.h" />
161   - <ClInclude Include="Spotlight.h" />
162 163 </ItemGroup>
163 164 <ItemGroup>
164 165 <ClCompile Include="app\main.cpp" />
165   - <ClCompile Include="Bullet.cpp" />
166   - <ClCompile Include="Collision.cpp" />
167   - <ClCompile Include="Enemy.cpp" />
168   - <ClCompile Include="Spotlight.cpp" />
169 166 <ClCompile Include="src\BattleField.cpp" />
  167 + <ClCompile Include="src\Bullet.cpp" />
170 168 <ClCompile Include="src\Camera.cpp" />
  169 + <ClCompile Include="src\Collision.cpp" />
  170 + <ClCompile Include="src\Enemy.cpp" />
171 171 <ClCompile Include="src\GameManager.cpp" />
172 172 <ClCompile Include="src\SceneObject.cpp" />
  173 + <ClCompile Include="src\Shader.cpp" />
173 174 <ClCompile Include="src\SpaceShip.cpp" />
  175 + <ClCompile Include="src\Spotlight.cpp" />
174 176 <ClCompile Include="src\Text.cpp" />
175 177 </ItemGroup>
  178 + <ItemGroup>
  179 + <None Include="assets\shaders\chess.frag" />
  180 + <None Include="assets\shaders\chess.vert" />
  181 + <None Include="assets\shaders\phong.frag" />
  182 + <None Include="assets\shaders\phong.vert" />
  183 + <None Include="red.frag" />
  184 + <None Include="red.vert" />
  185 + </ItemGroup>
176 186 <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
177 187 <ImportGroup Label="ExtensionTargets">
178 188 </ImportGroup>
... ...
SpaceShooterV4/SpaceShooterV4.vcxproj.filters
... ... @@ -13,6 +13,9 @@
13 13 <UniqueIdentifier>{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}</UniqueIdentifier>
14 14 <Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions>
15 15 </Filter>
  16 + <Filter Include="Shaders">
  17 + <UniqueIdentifier>{9074850e-65e0-40e8-8d0b-98ac4a83243b}</UniqueIdentifier>
  18 + </Filter>
16 19 </ItemGroup>
17 20 <ItemGroup>
18 21 <ResourceCompile Include="SpaceShooterV4.rc">
... ... @@ -41,22 +44,22 @@
41 44 <ClInclude Include="include\SpaceShip.hpp">
42 45 <Filter>Header Files</Filter>
43 46 </ClInclude>
44   - <ClInclude Include="Enemy.h">
  47 + <ClInclude Include="include\Text.h">
45 48 <Filter>Header Files</Filter>
46 49 </ClInclude>
47   - <ClInclude Include="Bullet.h">
  50 + <ClInclude Include="include\Shader.hpp">
48 51 <Filter>Header Files</Filter>
49 52 </ClInclude>
50   - <ClInclude Include="Spotlight.h">
  53 + <ClInclude Include="include\Bullet.h">
51 54 <Filter>Header Files</Filter>
52 55 </ClInclude>
53   - <ClInclude Include="include\Text.h">
  56 + <ClInclude Include="include\Collision.h">
54 57 <Filter>Header Files</Filter>
55 58 </ClInclude>
56   - <ClInclude Include="Collision.h">
  59 + <ClInclude Include="include\Enemy.h">
57 60 <Filter>Header Files</Filter>
58 61 </ClInclude>
59   - <ClInclude Include="Data.h">
  62 + <ClInclude Include="include\Spotlight.h">
60 63 <Filter>Header Files</Filter>
61 64 </ClInclude>
62 65 </ItemGroup>
... ... @@ -79,20 +82,43 @@
79 82 <ClCompile Include="src\SpaceShip.cpp">
80 83 <Filter>Source Files</Filter>
81 84 </ClCompile>
82   - <ClCompile Include="Enemy.cpp">
  85 + <ClCompile Include="src\Text.cpp">
83 86 <Filter>Source Files</Filter>
84 87 </ClCompile>
85   - <ClCompile Include="Bullet.cpp">
  88 + <ClCompile Include="src\Shader.cpp">
86 89 <Filter>Source Files</Filter>
87 90 </ClCompile>
88   - <ClCompile Include="Spotlight.cpp">
  91 + <ClCompile Include="src\Bullet.cpp">
89 92 <Filter>Source Files</Filter>
90 93 </ClCompile>
91   - <ClCompile Include="src\Text.cpp">
  94 + <ClCompile Include="src\Collision.cpp">
92 95 <Filter>Source Files</Filter>
93 96 </ClCompile>
94   - <ClCompile Include="Collision.cpp">
  97 + <ClCompile Include="src\Enemy.cpp">
95 98 <Filter>Source Files</Filter>
96 99 </ClCompile>
  100 + <ClCompile Include="src\Spotlight.cpp">
  101 + <Filter>Source Files</Filter>
  102 + </ClCompile>
  103 + </ItemGroup>
  104 + <ItemGroup>
  105 + <None Include="red.vert">
  106 + <Filter>Shaders</Filter>
  107 + </None>
  108 + <None Include="red.frag">
  109 + <Filter>Shaders</Filter>
  110 + </None>
  111 + <None Include="assets\shaders\chess.frag">
  112 + <Filter>Shaders</Filter>
  113 + </None>
  114 + <None Include="assets\shaders\chess.vert">
  115 + <Filter>Shaders</Filter>
  116 + </None>
  117 + <None Include="assets\shaders\phong.frag">
  118 + <Filter>Shaders</Filter>
  119 + </None>
  120 + <None Include="assets\shaders\phong.vert">
  121 + <Filter>Shaders</Filter>
  122 + </None>
97 123 </ItemGroup>
98 124 </Project>
99 125 \ No newline at end of file
... ...
SpaceShooterV4/app/main.cpp
... ... @@ -53,29 +53,23 @@ void init()
53 53  
54 54 void display()
55 55 {
56   - glClearColor(1, 1, 0.5, 1);
  56 + //glClearColor(1, 1, 0.5, 1);
57 57  
58 58 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
59 59  
60 60 gm->update(counter.fps());
61 61 gm->render();
62 62  
63   - //if(keyPressed[KEY_ID_W]==true) gm->getCam()->moveForward();
64 63 if (keyPressed[KEY_ID_W] == true) gm->getSpaceShip()->moveForward();
65 64 if (keyPressed[KEY_ID_A] == true) gm->getCam()->moveLeft(gm->getSpaceShip()->moveLeft());
66 65 if (keyPressed[KEY_ID_D] == true) gm->getCam()->moveRight(gm->getSpaceShip()->moveRight());
67 66 if (keyPressed[KEY_ID_S] == true) gm->getSpaceShip()->moveBackward();
68   - //if(keyPressed[KEY_ID_SPACE]==true) gm->getCam()->moveUp();
  67 +
69 68 if (keyPressed[KEY_ID_C] == true) gm->getSpaceShip()->changeWeapon();
70 69  
71   - //if (keyPressed[KEY_ID_X] == true) gm->fireFromSpaceship();
72 70 if (keyPressed[KEY_ID_SPACE] == true) gm->fireFromSpaceship();
73 71  
74   - //if (keyPressed[KEY_ID_LEFTARROW] == true) gm->getSpaceShip()->moveLeft();
75   - //if (keyPressed[KEY_ID_RIGHTARROW] == true) gm->getSpaceShip()->moveRight();
76   - //if (keyPressed[KEY_ID_UPARROW] == true) gm->getSpaceShip()->moveForward();
77   - //if (keyPressed[KEY_ID_DOWNARROW] == true) gm->getSpaceShip()->moveBackward();
78   -
  72 +
79 73 // Lets try out text
80 74  
81 75 glPushMatrix();
... ... @@ -117,7 +111,7 @@ void display()
117 111  
118 112 glutSwapBuffers();
119 113 glutPostRedisplay();
120   -
  114 + glClearColor(0, 0, 0, 1);
121 115 }
122 116  
123 117 void keyDown(unsigned char key, int x, int y)
... ...
SpaceShooterV4/assets/shaders/chess.frag 0 → 100644
  1 +
  2 +varying vec4 texCoord;
  3 +
  4 +void main(void)
  5 +{
  6 + float color = 0.0;
  7 +
  8 + if ( mod( floor(texCoord.x * 10.0) + floor(texCoord.y * 10.0) , 2.0 ) == 0.0 ) {
  9 + color = 1.0;
  10 + }
  11 +
  12 + gl_FragColor = vec4(0.0, 0.0, color, 1.0);
  13 +}
0 14 \ No newline at end of file
... ...
SpaceShooterV4/assets/shaders/chess.vert 0 → 100644
  1 +varying vec4 texCoord;
  2 +
  3 +void main(void)
  4 +{
  5 + texCoord = gl_MultiTexCoord0;
  6 + gl_Position = ftransform();
  7 +
  8 +}
0 9 \ No newline at end of file
... ...
SpaceShooterV4/assets/shaders/phong.frag 0 → 100644
  1 +varying vec3 Normal;
  2 +
  3 +void main ()
  4 +{
  5 +
  6 + vec3 lightVec = vec3(gl_LightSource[0].position);
  7 + vec3 diffuseMaterial = gl_FrontLightProduct[0].diffuse.xyz;
  8 + vec3 specularMaterial = gl_FrontLightProduct[0].specular.xyz;
  9 +
  10 + vec3 NormalVec = normalize(Normal);
  11 + // calculate half angle vector
  12 + vec3 eyeVec = vec3(0.0, 0.0, 1.0);
  13 + vec3 halfVec = normalize(lightVec + eyeVec);
  14 +
  15 + // calculate diffuse component
  16 + vec3 diffuse = vec3(max(0.5*dot(NormalVec, lightVec), 0.5)) * diffuseMaterial;
  17 +
  18 + // calculate specular component
  19 + vec3 specular = vec3(max(dot(NormalVec, halfVec), 0.0));
  20 + specular = pow(specular.x, 32.0) * specularMaterial;
  21 +
  22 + // combine diffuse and specular contributions and output final vertex color
  23 + gl_FragColor.rgb = diffuse + specular;
  24 +}
... ...
SpaceShooterV4/assets/shaders/phong.vert 0 → 100644
  1 +varying vec3 Normal;
  2 +
  3 +void main()
  4 +{
  5 + Normal = gl_NormalMatrix * gl_Normal;
  6 + gl_FrontColor = gl_Color;
  7 + gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
  8 +}
0 9 \ No newline at end of file
... ...
SpaceShooterV4/assets/shaders/red.frag 0 → 100644
  1 +void main(void)
  2 +{
  3 + gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);
  4 +}
0 5 \ No newline at end of file
... ...
SpaceShooterV4/assets/shaders/red.vert 0 → 100644
  1 +void main(void)
  2 +{
  3 + gl_Position = ftransform();
  4 +}
0 5 \ No newline at end of file
... ...
SpaceShooterV4/include/BattleField.hpp
1 1 #pragma once
2 2  
  3 +#include <GL/glew.h>
3 4 #include <windows.h>
4 5 #include <../include/GL/freeglut.h>
5 6 #include <gl/gl.h>
... ...
SpaceShooterV4/Bullet.h renamed to SpaceShooterV4/include/Bullet.h
1 1 #pragma once
  2 +#include <GL/glew.h>
2 3 #include <windows.h>
3 4 #include <GL/gl.h>
4 5 #include <GL/glu.h>
... ...
SpaceShooterV4/include/Camera.hpp
1 1 #pragma once
2   -
  2 +#include <GL/glew.h>
3 3 #include <windows.h>
4 4 #include <gl/gl.h>
5 5 #include <gl/glu.h>
... ...
SpaceShooterV4/Collision.h renamed to SpaceShooterV4/include/Collision.h
1 1 #pragma once
  2 +#include <GL/glew.h>
2 3 #include "include\glm\glm.hpp"
3 4 #include "include\SceneObject.hpp"
4 5  
... ...
SpaceShooterV4/Enemy.h renamed to SpaceShooterV4/include/Enemy.h
1 1 #pragma once
  2 +
  3 +#include <GL/glew.h>
  4 +#include "Shader.hpp"
2 5 #include <windows.h>
3 6 #include <GL/gl.h>
4 7 #include <GL/glu.h>
... ... @@ -38,10 +41,12 @@ class Enemy : public SceneObject
38 41  
39 42 bool _markDelete = false;
40 43 float _counter = 0;
41   - float _inc_speed = 0.01;
  44 + float _inc_speed = 0.01f;
42 45 GLuint _texture;
43 46 Trajectory _trajectory = Trajectory::FORWARD;
44 47  
  48 + Shader phong;
  49 +
45 50 public:
46 51 float _life = 2;
47 52  
... ...
SpaceShooterV4/include/FpsCounter.hpp
1 1 #pragma once
2   -
  2 +#include <GL/glew.h>
3 3 #include <chrono>
4 4 #include <iostream>
5 5  
... ...
SpaceShooterV4/include/GameManager.hpp
1 1 #pragma once
2   -
  2 +#include <GL/glew.h>
3 3 #include <windows.h>
4 4 #include <GL/gl.h>
5 5 #include <GL/glu.h>
... ...
SpaceShooterV4/include/SceneObject.hpp
1 1 #pragma once
2   -
  2 +#include <GL/glew.h>
3 3 #include <vector>
4 4 #include "glm/glm.hpp"
5 5 #include <memory>
... ...
SpaceShooterV4/include/Shader.hpp 0 → 100644
  1 +#ifndef _SHADER_HPP_
  2 +#define _SHADER_HPP_
  3 +
  4 +#include <GL/glew.h>
  5 +#include <windows.h>
  6 +#include <GL/GL.h>
  7 +#include <vector>
  8 +#include <stdlib.h>
  9 +#include <stdio.h>
  10 +
  11 +
  12 +enum ShaderType {V_SHADER, F_SHADER};
  13 +
  14 +class Shader
  15 +{
  16 + public:
  17 + Shader();
  18 + ~Shader();
  19 +
  20 + // enable and disable program
  21 + void enable();
  22 + void disable();
  23 + GLuint getProg();
  24 +
  25 + bool initShaders(char *fileName);
  26 +
  27 +
  28 + private:
  29 +
  30 + GLuint vs_, fs_, prog_;
  31 + GLchar *vertexShaderSource_, *fragmentShaderSource_;
  32 +
  33 + int shaderSize(char *fileName, ShaderType shaderType);
  34 + int readShader(char *fileName, ShaderType shaderType,
  35 + char *shaderText, int size);
  36 +
  37 + //int loadShaderSource(const GLchar *vertexShader, const GLchar *fragmentShader);
  38 +
  39 +};
  40 +
  41 +#endif //_SHADER_HPP_
  42 +
... ...
SpaceShooterV4/include/SpaceShip.hpp
1 1 #pragma once
2 2  
  3 +#include <GL/glew.h>
3 4 #include <windows.h>
4 5 #include <GL/gl.h>
5 6 #include <GL/glu.h>
6 7 #include <chrono>
7 8 #include <ctime>
8 9 #include "include/SceneObject.hpp"
  10 +#include "Shader.hpp"
  11 +
9 12  
10 13 class SpaceShip : public SceneObject
11 14 {
... ... @@ -35,6 +38,8 @@ class SpaceShip : public SceneObject
35 38 int amountOfWeapons = 2;
36 39 int _weapon = 0;
37 40  
  41 + Shader red;
  42 +
38 43  
39 44 public:
40 45 void fire();
... ...
SpaceShooterV4/Spotlight.h renamed to SpaceShooterV4/include/Spotlight.h
1 1 #pragma once
  2 +#include <GL/glew.h>
2 3 #include <windows.h>
3 4 #include <GL/gl.h>
4 5 #include <GL/glu.h>
... ...
SpaceShooterV4/include/Text.h
... ... @@ -2,6 +2,7 @@
2 2 #ifndef _TEXT_H_
3 3 #define _TEXT_H_
4 4  
  5 +#include <GL/glew.h>
5 6 #include <GL/gl.h>
6 7 #include "Vector3.hpp"
7 8  
... ...
SpaceShooterV4/src/BattleField.cpp
... ... @@ -88,6 +88,7 @@ void BattleField::privateRender()
88 88 glDisable(GL_PRIMITIVE_RESTART);
89 89 glDisable(GL_TEXTURE_2D);
90 90 glBindTexture(GL_TEXTURE_2D, 0);
  91 + glClearColor(0.0, 0.0, 0.0, 1.0);
91 92 }
92 93  
93 94 void BattleField::privateUpdate()
... ...
SpaceShooterV4/Bullet.cpp renamed to SpaceShooterV4/src/Bullet.cpp
1   -#include "Bullet.h"
  1 +#include "include/Bullet.h"
2 2 #include "include/glm/glm.hpp"
3 3 #include "include/glm/gtc/matrix_transform.hpp"
4   -#include "Collision.h"
5   -#include "Enemy.h"
  4 +#include "include/Collision.h"
  5 +#include "include/Enemy.h"
6 6  
7 7  
8 8 Bullet::Bullet(const glm::mat4& startMatrix)
... ... @@ -44,6 +44,7 @@ void Bullet::privateRender()
44 44 glTexCoord2f(1.0f, 1.0f); glVertex3f(size, size, size);
45 45 glTexCoord2f(0.0f, 1.0f); glVertex3f(-size, size, size);
46 46 glEnd();
  47 + glClearColor(0.0, 0.0, 0.0, 1.0);
47 48 }
48 49  
49 50 void Bullet::privateUpdate()
... ...
SpaceShooterV4/Collision.cpp renamed to SpaceShooterV4/src/Collision.cpp
1   -#include "Collision.h"
  1 +#include "include/Collision.h"
2 2 #include <iostream>
3 3  
4 4  
... ...
SpaceShooterV4/Enemy.cpp renamed to SpaceShooterV4/src/Enemy.cpp
  1 +
  2 +#include <GL/glew.h>
  3 +#include "include/Enemy.h"
1 4 #include "include/glm/glm.hpp"
2 5 #include "include/glm/gtc/matrix_transform.hpp"
3   -#include "Enemy.h"
4 6 #include <iostream>
5 7 #include "include\soil\SOIL.h"
6 8 #define _USE_MATH_DEFINES
7 9 #include <math.h>
  10 +#include <GL/glut.h>
8 11  
9 12  
10 13 Enemy::Enemy()
... ... @@ -48,18 +51,32 @@ void Enemy::privateInit()
48 51 printf("SOIL loading error: '%s'\n", SOIL_last_result());
49 52 }
50 53  
  54 + phong.initShaders("assets/shaders/phong");
51 55 }
52 56  
53 57 void Enemy::privateRender()
54 58 {
  59 + phong.enable();
55 60 // Enable texturing before render
56   - glEnable(GL_TEXTURE_2D);
57   - glBindTexture(GL_TEXTURE_2D, _texture);
  61 + //glEnable(GL_TEXTURE_2D);
  62 + //glBindTexture(GL_TEXTURE_2D, _texture);
  63 +
  64 + //glEnable(GL_COLOR_MATERIAL);
  65 + //glColorMaterial(GL_FRONT, GL_DIFFUSE);
58 66  
59   - glEnable(GL_COLOR_MATERIAL);
60   - glColorMaterial(GL_FRONT, GL_DIFFUSE);
  67 + glEnable(GL_LIGHT0);
  68 + GLfloat ambient[] = { 0.0, 0.0, 0.0, 1.0 };
  69 + GLfloat spec[] = { 1.0, 1.0, 1.0, 1.0 };
  70 + GLfloat diffuse[] = { 1.0, 1.0, 1.0, 1.0 };
  71 + glMaterialfv(GL_FRONT, GL_AMBIENT, ambient);
  72 + glMaterialfv(GL_FRONT, GL_SPECULAR, spec);
  73 + glMaterialfv(GL_FRONT, GL_DIFFUSE, diffuse);
61 74  
  75 +
62 76 glColor3f(1.0f, 1.0f, 1.0f);
  77 +
  78 + glutSolidSphere(10, 30, 30);
  79 + /*
63 80 glBegin(GL_QUADS);
64 81 float size = 5.0f;
65 82 glNormal3f(0.0f, 0.0f, 1.0f);
... ... @@ -68,10 +85,11 @@ void Enemy::privateRender()
68 85 glTexCoord2f(1.0f, 1.0f); glVertex3f(size, size, size);
69 86 glTexCoord2f(0.0f, 1.0f); glVertex3f(-size, size, size);
70 87 glEnd();
71   -
72   - glBindTexture(GL_TEXTURE_2D, 0);
73   - glDisable(GL_TEXTURE_2D);
  88 + */
  89 + //glBindTexture(GL_TEXTURE_2D, 0);
  90 + //glDisable(GL_TEXTURE_2D);
74 91 glDisable(GL_COLOR_MATERIAL);
  92 + phong.disable();
75 93 }
76 94  
77 95 void Enemy::privateUpdate()
... ...
SpaceShooterV4/src/GameManager.cpp
  1 +#include <GL/glew.h>
1 2 #include "../include/GameManager.hpp"
2 3 #include <chrono>
3 4 #include <random>
... ... @@ -56,7 +57,8 @@ void GameManager::privateInit()
56 57  
57 58 void GameManager::privateRender()
58 59 {
59   - // Nothing to render
  60 + GLfloat light_pos[] = { 1.0, 1.0, 1.0, 0.0 };
  61 + glLightfv(GL_LIGHT0, GL_POSITION, light_pos);
60 62 }
61 63  
62 64 void GameManager::privateUpdate()
... ...
SpaceShooterV4/src/Shader.cpp 0 → 100644
  1 +#define _CRT_SECURE_NO_WARNINGS
  2 +
  3 +#include "include\Shader.hpp"
  4 +#include <windows.h>
  5 +#include <iostream>
  6 +#include <string.h>
  7 +#include <stdio.h>
  8 +#include <io.h>
  9 +#include <fcntl.h>
  10 +
  11 +using namespace std;
  12 +
  13 +Shader::Shader()
  14 +{
  15 +}
  16 +
  17 +Shader::~Shader()
  18 +{
  19 +}
  20 +
  21 +bool Shader::initShaders(char *fileName)
  22 +{
  23 + int vSize, fSize;
  24 +
  25 + // Allocate memory to hold the source of our shaders.
  26 + vSize = shaderSize(fileName, V_SHADER);
  27 + fSize = shaderSize(fileName, F_SHADER);
  28 +
  29 + if((vSize == -1) || (fSize == -1))
  30 + {
  31 + cout << "Cannot determine size of the shader " << fileName << endl;
  32 + return false;
  33 + }
  34 +
  35 + vertexShaderSource_ = (GLchar *) malloc(vSize);
  36 + fragmentShaderSource_ = (GLchar *) malloc(fSize);
  37 +
  38 + // Read the source code
  39 + if(!readShader(fileName, V_SHADER, vertexShaderSource_, vSize))
  40 + {
  41 + printf("Cannot read the file %s.vert\n", fileName);
  42 + return false;
  43 + }
  44 +
  45 + if(!readShader(fileName, F_SHADER, fragmentShaderSource_, fSize))
  46 + {
  47 + printf("Cannot read the file %s.frag\n", fileName);
  48 + return false;
  49 + }
  50 +
  51 + GLint vertCompiled, fragCompiled; // status values
  52 + GLint linked;
  53 + int infologLength = 0;
  54 + int charsWritten = 0;
  55 + GLchar *infoLog;
  56 +
  57 + // Create a vertex shader object and a fragment shader object
  58 + vs_ = glCreateShader(GL_VERTEX_SHADER);
  59 + fs_ = glCreateShader(GL_FRAGMENT_SHADER);
  60 +
  61 + const char *vertS = vertexShaderSource_;
  62 + const char *fragS = fragmentShaderSource_;
  63 + // Load source code strings into shaders
  64 + glShaderSource(vs_, 1, &vertS, NULL);
  65 + glShaderSource(fs_, 1, &fragS, NULL);
  66 +
  67 + // Compile the vertex shader, and print out
  68 + // the compiler log file.
  69 + glCompileShader(vs_);
  70 + glGetShaderiv(vs_, GL_COMPILE_STATUS, &vertCompiled);
  71 + glGetShaderiv(vs_, GL_INFO_LOG_LENGTH, &infologLength);
  72 + if (infologLength > 0)
  73 + {
  74 + infoLog = (GLchar *)malloc(infologLength);
  75 + if (infoLog == NULL)
  76 + {
  77 + printf("ERROR: Could not allocate InfoLog buffer\n");
  78 + exit(1);
  79 + }
  80 + glGetShaderInfoLog(vs_, infologLength, &charsWritten, infoLog);
  81 + printf("Shader InfoLog:\n%s\n\n", infoLog);
  82 + free(infoLog);
  83 + }
  84 +
  85 + // Compile the fragment shader, and print out
  86 + // the compiler log file.
  87 +
  88 + glCompileShader(fs_);
  89 + glGetShaderiv(fs_, GL_COMPILE_STATUS, &fragCompiled);
  90 + glGetShaderiv(fs_, GL_INFO_LOG_LENGTH, &infologLength);
  91 + if (infologLength > 0)
  92 + {
  93 + infoLog = (GLchar *)malloc(infologLength);
  94 + if (infoLog == NULL)
  95 + {
  96 + printf("ERROR: Could not allocate InfoLog buffer\n");
  97 + exit(1);
  98 + }
  99 + glGetShaderInfoLog(fs_, infologLength, &charsWritten, infoLog);
  100 + printf("Shader InfoLog:\n%s\n\n", infoLog);
  101 + free(infoLog);
  102 + }
  103 +
  104 + if (!vertCompiled || !fragCompiled)
  105 + return false;
  106 +
  107 + // Create a program object and attach the two compiled shaders
  108 + prog_ = glCreateProgram();
  109 + glAttachShader(prog_, vs_);
  110 + glAttachShader(prog_, fs_);
  111 +
  112 + // Link the program object and print out the info log
  113 +
  114 + glLinkProgram(prog_);
  115 + glGetProgramiv(prog_, GL_LINK_STATUS, &linked);
  116 + glGetProgramiv(prog_, GL_INFO_LOG_LENGTH, &infologLength);
  117 + if (infologLength > 0)
  118 + {
  119 + infoLog = (GLchar *)malloc(infologLength);
  120 + if (infoLog == NULL)
  121 + {
  122 + printf("ERROR: Could not allocate InfoLog buffer\n");
  123 + exit(1);
  124 + }
  125 + glGetProgramInfoLog(prog_, infologLength, &charsWritten, infoLog);
  126 + printf("Program InfoLog:\n%s\n\n", infoLog);
  127 + free(infoLog);
  128 + }
  129 + if (!linked)
  130 + return false;
  131 +
  132 + return true;
  133 +}
  134 +
  135 +
  136 +
  137 +int Shader::shaderSize(char *fileName, ShaderType shaderType)
  138 +{
  139 + // Returns the size in bytes of the shader fileName.
  140 + // If an error occurred, it returns -1.
  141 + //
  142 + // File name convention:
  143 + //
  144 + // <fileName>.vert
  145 + // <fileName>.frag
  146 + //
  147 + int fd;
  148 + char name[100];
  149 + int count = -1;
  150 +
  151 + strcpy(name, fileName);
  152 +
  153 + switch(shaderType)
  154 + {
  155 + case V_SHADER:
  156 + strcat(name, ".vert");
  157 + break;
  158 + case F_SHADER:
  159 + strcat(name, ".frag");
  160 + break;
  161 + default:
  162 + cout << "ERROR: unknown shader file type " << endl;
  163 + exit(1);
  164 + break;
  165 + }
  166 +
  167 + // Open the file, seek to the end to find its length (WIN32)
  168 + fd = _open(name, _O_RDONLY);
  169 + if (fd != -1)
  170 + {
  171 + count = _lseek(fd, 0, SEEK_END) + 1;
  172 + _close(fd);
  173 + }
  174 + return count;
  175 +}
  176 +
  177 +
  178 +int Shader::readShader(char *fileName, ShaderType shaderType, char *shaderText, int size)
  179 +{
  180 + // Reads a shader from the supplied file and returns the shader in the
  181 + // arrays passed in. Returns 1 if successful, 0 if an error occurred.
  182 + // The parameter size is an upper limit of the amount of bytes to read.
  183 + // It is ok for it to be too big.
  184 + FILE *fh;
  185 + char name[100];
  186 + int count;
  187 +
  188 + strcpy(name, fileName);
  189 +
  190 + switch (shaderType)
  191 + {
  192 + case V_SHADER:
  193 + strcat(name, ".vert");
  194 + break;
  195 + case F_SHADER:
  196 + strcat(name, ".frag");
  197 + break;
  198 + default:
  199 + printf("ERROR: unknown shader file type\n");
  200 + exit(1);
  201 + break;
  202 + }
  203 +
  204 + // Open the file
  205 + fh = fopen(name, "r");
  206 + if (!fh)
  207 + return -1;
  208 +
  209 + // Get the shader from a file.
  210 + fseek(fh, 0, SEEK_SET);
  211 + count = (int) fread(shaderText, 1, size, fh);
  212 + shaderText[count] = '\0';
  213 +
  214 + if (ferror(fh))
  215 + count = 0;
  216 +
  217 + fclose(fh);
  218 + return count;
  219 +}
  220 +
  221 +void Shader::enable()
  222 +{
  223 + glUseProgram(prog_);
  224 +}
  225 +void Shader::disable()
  226 +{
  227 + glUseProgram(0);
  228 +}
  229 +
  230 +GLuint Shader::getProg()
  231 +{
  232 + return prog_;
  233 +}
  234 +
... ...
SpaceShooterV4/src/SpaceShip.cpp
1 1  
  2 +#include <GL/glew.h>
2 3 #include "../include/SpaceShip.hpp"
3 4 #include "include/glm/glm.hpp"
4 5 #include "include/glm/gtc/matrix_transform.hpp"
5   -#include "Bullet.h"
  6 +#include "include/Bullet.h"
6 7 #include <iostream>
7 8  
8 9  
... ... @@ -37,11 +38,12 @@ SpaceShip::~SpaceShip()
37 38 void SpaceShip::privateInit()
38 39 {
39 40 matrix_ = glm::translate(matrix_, glm::vec3(0.0f, -15.0f, 0.0f));
  41 + red.initShaders("assets/shaders/red");
40 42 }
41 43  
42 44 void SpaceShip::privateRender()
43 45 {
44   -
  46 + red.enable();
45 47 //glColor3f(1.0f, 0.0f, 0.0f);
46 48 //float size = 5.0f;
47 49 //glBegin(GL_QUADS);
... ... @@ -57,13 +59,19 @@ void SpaceShip::privateRender()
57 59 glShadeModel(GL_SMOOTH);
58 60 //glClear(GL_COLOR_BUFFER_BIT);
59 61 glBegin(GL_TRIANGLES);
60   - glColor3f(size, 0.0f, 0.0f);
  62 + glTexCoord2f(0.0f, 0.0f);
  63 + //glColor3f(0.0f, 1.0f, 0.0f);
61 64 glVertex2f(-size, -size);
62   - glColor3f(0.0f, size, 0.0f);
  65 +
  66 + //glColor3f(0.0f, 1.0f, 0.0f);
  67 + glTexCoord2f(1.0f, 0.0f);
63 68 glVertex2f(size, -size);
64   - glColor3f(0.0f, 0.0f, size);
  69 +
  70 + //glColor3f(0.0f, 1.0f, 0.0f);
  71 + glTexCoord2f(0.0f, 1.0f);
65 72 glVertex2f(-size, size);
66 73 glEnd();
  74 + red.disable();
67 75 }
68 76  
69 77 void SpaceShip::privateUpdate()
... ... @@ -80,7 +88,7 @@ float SpaceShip::moveBackward()
80 88 if (matrix_[3][2] < 0)
81 89 {
82 90 matrix_ = glm::translate(matrix_, glm::vec3(0.0f, 0.0f, _speed * _deltaTime));
83   - return _speed * _deltaTime;
  91 + return _speed * float(_deltaTime);
84 92 }
85 93 return 0.0f;
86 94 }
... ... @@ -90,7 +98,7 @@ float SpaceShip::moveForward()
90 98 if (matrix_[3][2] > -320)
91 99 {
92 100 matrix_ = glm::translate(matrix_, glm::vec3(0.0f, 0.0f, -_speed * _deltaTime));
93   - return -_speed * _deltaTime;
  101 + return -_speed * float(_deltaTime);
94 102 }
95 103 return 0.0f;
96 104 }
... ... @@ -100,7 +108,7 @@ float SpaceShip::moveRight()
100 108 if (matrix_[3][0] < 160)
101 109 {
102 110 matrix_ = glm::translate(matrix_, glm::vec3(_speed * _deltaTime, 0.0f, 0.0f));
103   - return -(_speed * _deltaTime);
  111 + return -(_speed * float(_deltaTime));
104 112 }
105 113 return 0.0f;
106 114 }
... ... @@ -110,7 +118,7 @@ float SpaceShip::moveLeft()
110 118 if (matrix_[3][0] > -150)
111 119 {
112 120 matrix_ = glm::translate(matrix_, glm::vec3(-_speed * _deltaTime, 0.0f, 0.0f));
113   - return (_speed * _deltaTime);
  121 + return (_speed * float(_deltaTime));
114 122 }
115 123 return 0.0f;
116 124 }
... ...
SpaceShooterV4/Spotlight.cpp renamed to SpaceShooterV4/src/Spotlight.cpp
1 1 #include "include/glm/glm.hpp"
2 2 #include "include/glm/gtc/matrix_transform.hpp"
3   -#include "Spotlight.h"
  3 +#include "include/Spotlight.h"
4 4  
5 5 Spotlight::Spotlight()
6 6 {
... ... @@ -39,6 +39,7 @@ void Spotlight::privateRender()
39 39 //Positional light (lamp)
40 40 GLfloat light_pos[] = { 1.0, 1.0, 1.0, 1.0 };
41 41 glLightfv(GL_LIGHT0, GL_POSITION, light_pos);
  42 +
42 43 //glLightfv(GL_LIGHT1, GL_POSITION, light_sun_pos);
43 44 glLightf(GL_LIGHT0, GL_SPOT_CUTOFF, _angle);
44 45 glLightf(GL_LIGHT0, GL_SPOT_EXPONENT, _exponent);
... ...
SpaceShooterV4/x64/Debug/BattleField.obj
No preview for this file type
SpaceShooterV4/x64/Debug/Bullet.obj
No preview for this file type
SpaceShooterV4/x64/Debug/Camera.obj
No preview for this file type
SpaceShooterV4/x64/Debug/Collision.obj
No preview for this file type
SpaceShooterV4/x64/Debug/Enemy.obj
No preview for this file type
SpaceShooterV4/x64/Debug/GameManager.obj
No preview for this file type
SpaceShooterV4/x64/Debug/SceneObject.obj
No preview for this file type
SpaceShooterV4/x64/Debug/Shader.obj 0 → 100644
No preview for this file type
SpaceShooterV4/x64/Debug/SpaceShip.obj
No preview for this file type
SpaceShooterV4/x64/Debug/SpaceShooterV4.log
1   - main.cpp
2   -d:\csmaster\opengl projects\projects\spaceshooterv4\spaceshooterv4\spaceshooterv4\include\fpscounter.hpp(60): warning C4244: 'return': conversion from '__int64' to 'double', possible loss of data
3   -d:\csmaster\opengl projects\projects\spaceshooterv4\spaceshooterv4\spaceshooterv4\enemy.h(41): warning C4305: 'initializing': truncation from 'double' to 'float'
  1 + GameManager.cpp
4 2 SOIL.lib(SOIL.obj) : MSIL .netmodule or module compiled with /GL found; restarting link with /LTCG; add /LTCG to the link command line to improve linker performance
5 3 LINK : warning LNK4075: ignoring '/INCREMENTAL' due to '/LTCG' specification
6 4 LINK : warning LNK4098: defaultlib 'MSVCRT' conflicts with use of other libs; use /NODEFAULTLIB:library
... ...
SpaceShooterV4/x64/Debug/SpaceShooterV4.tlog/CL.command.1.tlog
1 1 Binary files a/SpaceShooterV4/x64/Debug/SpaceShooterV4.tlog/CL.command.1.tlog and b/SpaceShooterV4/x64/Debug/SpaceShooterV4.tlog/CL.command.1.tlog differ
... ...
SpaceShooterV4/x64/Debug/SpaceShooterV4.tlog/CL.read.1.tlog
1 1 Binary files a/SpaceShooterV4/x64/Debug/SpaceShooterV4.tlog/CL.read.1.tlog and b/SpaceShooterV4/x64/Debug/SpaceShooterV4.tlog/CL.read.1.tlog differ
... ...
SpaceShooterV4/x64/Debug/SpaceShooterV4.tlog/CL.write.1.tlog
1 1 Binary files a/SpaceShooterV4/x64/Debug/SpaceShooterV4.tlog/CL.write.1.tlog and b/SpaceShooterV4/x64/Debug/SpaceShooterV4.tlog/CL.write.1.tlog differ
... ...
SpaceShooterV4/x64/Debug/SpaceShooterV4.tlog/SpaceShooterV4.write.1u.tlog
1 1 Binary files a/SpaceShooterV4/x64/Debug/SpaceShooterV4.tlog/SpaceShooterV4.write.1u.tlog and b/SpaceShooterV4/x64/Debug/SpaceShooterV4.tlog/SpaceShooterV4.write.1u.tlog differ
... ...
SpaceShooterV4/x64/Debug/SpaceShooterV4.tlog/link.command.1.tlog
1 1 Binary files a/SpaceShooterV4/x64/Debug/SpaceShooterV4.tlog/link.command.1.tlog and b/SpaceShooterV4/x64/Debug/SpaceShooterV4.tlog/link.command.1.tlog differ
... ...
SpaceShooterV4/x64/Debug/SpaceShooterV4.tlog/link.read.1.tlog
1 1 Binary files a/SpaceShooterV4/x64/Debug/SpaceShooterV4.tlog/link.read.1.tlog and b/SpaceShooterV4/x64/Debug/SpaceShooterV4.tlog/link.read.1.tlog differ
... ...
SpaceShooterV4/x64/Debug/SpaceShooterV4.tlog/link.write.1.tlog
1 1 Binary files a/SpaceShooterV4/x64/Debug/SpaceShooterV4.tlog/link.write.1.tlog and b/SpaceShooterV4/x64/Debug/SpaceShooterV4.tlog/link.write.1.tlog differ
... ...
SpaceShooterV4/x64/Debug/Spotlight.obj
No preview for this file type
SpaceShooterV4/x64/Debug/Text.obj
No preview for this file type
SpaceShooterV4/x64/Debug/main.obj
No preview for this file type
SpaceShooterV4/x64/Debug/vc141.idb
No preview for this file type
SpaceShooterV4/x64/Debug/vc141.pdb
No preview for this file type
x64/Debug/SpaceShooterV4.exe
No preview for this file type
x64/Debug/SpaceShooterV4.pdb
No preview for this file type