-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
135 lines (105 loc) · 3.03 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#include <stdexcept>
#include <Windows.h>
#include <sstream>
#include <GL\glew.h>
#include <glm\glm.hpp>
#include <glm\gtc\constants.hpp>
#include <glm\gtc\matrix_transform.hpp>
#include "Shader.h"
#include "Window.h"
#include "Texture.h"
#include "bth_image.h"
using namespace glm;
void createTriangData();
GLuint gVertexBuffer = 0;
GLuint gVertexAttribute = 0;
const mat4 I;
const vec3 X(1, 0, 0);
const vec3 Y(0, 1, 0);
const vec3 Z(0, 0, 1);
const vec3 O(0, 0, 0);
int main(int argc, char ** argv)
{
try
{
// Create the window
Window window = { "DV1541 OpenGL SDL", 800, 600 };
// Initialize Glew
glewExperimental = GL_TRUE;
glewInit();
glClearColor(0, 0, 0, 1);
glEnable(GL_DEPTH_TEST);
createTriangData();
Shader shader = {
"VertexShader.glsl",
"FragmentShader.glsl",
"GeometryShader.glsl" };
shader.use();
Texture texture = {
(GLsizei)BTH_IMAGE_WIDTH,
(GLsizei)BTH_IMAGE_HEIGHT,
(GLubyte*)BTH_IMAGE_DATA };
texture.use();
shader.setUniform("view", lookAt(vec3(0, 0, -2), O, Y));
shader.setUniform("proj", perspective(pi<float>() * 0.45f, 8.f / 6.f, 0.1f, 20.f));
shader.setUniform("offset", vec3(0, 0, 0.33f));
shader.setUniform("lightPosition", vec3(0, 0, -10));
shader.setUniform("lightColor", vec3(1));
shader.setUniform("ambientLight", vec3(0));
vec2 mousePos;
mat4 world;
while (true)
{
window.handleEvents();
window.clear();
mousePos = window.getMouseCoords();
world = rotate(I, half_pi<float>() * mousePos.x, Y);
world = rotate(world, -0.5f * half_pi<float>() * mousePos.y, X);
shader.setUniform("world", world);
glBindVertexArray(gVertexAttribute);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
window.draw();
}
}
catch (const std::runtime_error &error)
{
std::wostringstream msg;
msg << error.what() << std::endl;
OutputDebugStringW(msg.str().c_str());
throw;
}
return 0;
}
void createTriangData()
{
struct Vertex
{
vec3 postion;
vec3 color;
vec2 uv;
};
Vertex vertices[] = {
{{ -1, 1, 0 }, { 1, 0, 0 }, { 1, 0 }},
{{ 1, 1, 0 }, { 0, 1, 0 }, { 0, 0 }},
{{ -1, -1, 0 }, { 0, 0, 1 }, { 1, 1 }},
{{ 1, -1, 0 }, { 1, 1, 1 }, { 0, 1 }}
};
// Create an internal Vertex Array Object (VAO) and bind it for manipulation
glGenVertexArrays(1, &gVertexAttribute);
glBindVertexArray(gVertexAttribute);
// Create an internal Vertex Buffer Object (VBO) and bind it for manipulation
// "maybe" copy the vertices to the gpu
glGenBuffers(1, &gVertexBuffer);
glBindBuffer(GL_ARRAY_BUFFER, gVertexBuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
// write vertex data to memory
// location 0 position
// location 1 color
// location 2 texture coordinate
glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1);
glEnableVertexAttribArray(2);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), 0);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (GLvoid*)(sizeof(vec3)));
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (GLvoid*)(2 * sizeof(vec3)));
}