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 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371
|
#include <stdio.h>
#include <stdlib.h>
//*
// Include GLEW. Always include it before gl.h and glfw.h
#ifdef _WIN32
#include <GL\glew.h>
#else
#include <GL/glew.h>
#endif
// Include GLFW
#ifdef _WIN32
#include <GLFW\glfw3.h>
#else
#include <GLFW/glfw3.h>
#endif
// Include GLM
#ifdef _WIN32
#include <glm\glm.hpp>
#include <glm\gtx\transform.hpp>
#else
#include <glm/glm.hpp>
#include <glm/gtx/transform.hpp>
#endif
#ifdef _WIN32
#include <Seed\Graphics\camera.hpp>
#include <Seed\Graphics\texture.hpp>
#include <Seed\Graphics\model.hpp>
#include <Seed\Graphics\shader.hpp>
#include <Seed\Graphics\control.hpp>
#include <Seed\Graphics\Constant.hpp>
#include <Seed\Graphics\Outils.hpp>
#include <Seed\Graphics\scene.hpp>
#else
#include <Seed/Graphics/camera.hpp>
#include <Seed/Graphics/texture.hpp>
#include <Seed/Graphics/model.hpp>
#include <Seed/Graphics/shader.hpp>
#include <Seed/Graphics/control.hpp>
#include <Seed/Graphics/Constant.hpp>
#include <Seed/Graphics/Outils.hpp>
#include <Seed/Graphics/scene.hpp>
#endif
#include <time.h>
// Open a window and create its OpenGL context
GLFWwindow* window; // (In the accompanying source code, this variable is global)
int Initialisation();
int main()
{
//position of the camera
glm::vec3 position = glm::vec3(0.0, 0.0, 5.0);
//horizontal and vertical angle
float WAngle = 3.14f;
float HAngle = 0.0f;
//field of view
const float initFoV = 45.0f;
float near = (float)0.1;
float far = (float)100.0;
//speed move direction (keyboard)
float speed = 3.0f;
//speed view direction (mouse)
float mouseSpeed = 0.05f;
//initialisation systeme
if (Initialisation() != 0)
return -1;
//captur keys
glfwSetInputMode(window, GLFW_STICKY_KEYS, GL_TRUE);
//create scene
Scene scene;
//import model
scene.importModelFromFile(pathToModels + "UVsphere.obj");
//create camera
Camera camera(position, glm::vec3(0.0,0.0,0.0), glm::vec3(0.0,1.0,0.0), initFoV, WIDTH, HEIGHT, near, far);
// Create and compile our GLSL program from the shaders
GLuint programID = loadShaders(pathToShaders + "shader1\\VertexShader.hlsl",pathToShaders + "shader1\\FragmentShader.hlsl");//(pathToShaders + "\FragmentShader.hlsl").c_str());
//enable texturing
glEnable(GL_TEXTURE_2D);
// Dark blue background
glClearColor(0.0f, 0.0f, 0.4f, 0.0f);
/*GLuint VertexArrayID;
glGenVertexArrays(1, &VertexArrayID);
glBindVertexArray(VertexArrayID);
// Our vertices. Tree consecutive floats give a 3D vertex; Three consecutive vertices give a triangle.
// A cube has 6 faces with 2 triangles each, so this makes 6*2=12 triangles, and 12*3 vertices
static const GLfloat vertex_buffer_data[] = {
-1.0f, -1.0f, -1.0f, // triangle 1 : begin
-1.0f, -1.0f, 1.0f,
-1.0f, 1.0f, 1.0f, // triangle 1 : end
1.0f, 1.0f, -1.0f, // triangle 2 : begin
-1.0f, -1.0f, -1.0f,
-1.0f, 1.0f, -1.0f, // triangle 2 : end
1.0f, -1.0f, 1.0f,
-1.0f, -1.0f, -1.0f,
1.0f, -1.0f, -1.0f,
1.0f, 1.0f, -1.0f,
1.0f, -1.0f, -1.0f,
-1.0f, -1.0f, -1.0f,
-1.0f, -1.0f, -1.0f,
-1.0f, 1.0f, 1.0f,
-1.0f, 1.0f, -1.0f,
1.0f, -1.0f, 1.0f,
-1.0f, -1.0f, 1.0f,
-1.0f, -1.0f, -1.0f,
-1.0f, 1.0f, 1.0f,
-1.0f, -1.0f, 1.0f,
1.0f, -1.0f, 1.0f,
1.0f, 1.0f, 1.0f,
1.0f, -1.0f, -1.0f,
1.0f, 1.0f, -1.0f,
1.0f, -1.0f, -1.0f,
1.0f, 1.0f, 1.0f,
1.0f, -1.0f, 1.0f,
1.0f, 1.0f, 1.0f,
1.0f, 1.0f, -1.0f,
-1.0f, 1.0f, -1.0f,
1.0f, 1.0f, 1.0f,
-1.0f, 1.0f, -1.0f,
-1.0f, 1.0f, 1.0f,
1.0f, 1.0f, 1.0f,
-1.0f, 1.0f, 1.0f,
1.0f, -1.0f, 1.0f
};
// Two UV coordinatesfor each vertex. They were created with Blender. You'll learn shortly how to do this yourself.
static const GLfloat uv_buffer_data[] = {
0.000059f, 1.0f - 0.000004f,
0.000103f, 1.0f - 0.336048f,
0.335973f, 1.0f - 0.335903f,
1.000023f, 1.0f - 0.000013f,
0.667979f, 1.0f - 0.335851f,
0.999958f, 1.0f - 0.336064f,
0.667979f, 1.0f - 0.335851f,
0.336024f, 1.0f - 0.671877f,
0.667969f, 1.0f - 0.671889f,
1.000023f, 1.0f - 0.000013f,
0.668104f, 1.0f - 0.000013f,
0.667979f, 1.0f - 0.335851f,
0.000059f, 1.0f - 0.000004f,
0.335973f, 1.0f - 0.335903f,
0.336098f, 1.0f - 0.000071f,
0.667979f, 1.0f - 0.335851f,
0.335973f, 1.0f - 0.335903f,
0.336024f, 1.0f - 0.671877f,
1.000004f, 1.0f - 0.671847f,
0.999958f, 1.0f - 0.336064f,
0.667979f, 1.0f - 0.335851f,
0.668104f, 1.0f - 0.000013f,
0.335973f, 1.0f - 0.335903f,
0.667979f, 1.0f - 0.335851f,
0.335973f, 1.0f - 0.335903f,
0.668104f, 1.0f - 0.000013f,
0.336098f, 1.0f - 0.000071f,
0.000103f, 1.0f - 0.336048f,
0.000004f, 1.0f - 0.671870f,
0.336024f, 1.0f - 0.671877f,
0.000103f, 1.0f - 0.336048f,
0.336024f, 1.0f - 0.671877f,
0.335973f, 1.0f - 0.335903f,
0.667969f, 1.0f - 0.671889f,
1.000004f, 1.0f - 0.671847f,
0.667979f, 1.0f - 0.335851f
};
srand(time(NULL));
static GLfloat color_buffer_data[12 * 3 * 3];
for (int i = 0; i < 12*3; i++)
{
color_buffer_data[i * 3] = (float)(rand() % 100) / 100.0;
color_buffer_data[i * 3 + 1] = (float)(rand() % 100) / 100.0;
color_buffer_data[i * 3 + 2] = (float)(rand() % 100) / 100.0;
}
//id vertex VertexArrayID
GLuint VertexBuffer;
//on génère une adresse pointant vers un emplacement dans la mémoire du GPU
//on génère 1 emplacement
glGenBuffers(1, &VertexBuffer);
//on bind l'emplacement
glBindBuffer(GL_ARRAY_BUFFER, VertexBuffer);
//on donne les vertex à openGL
glBufferData(GL_ARRAY_BUFFER, sizeof(vertex_buffer_data), vertex_buffer_data, GL_STATIC_DRAW);
//id du color vertexArrayID
GLuint ColorBuffer;
glGenBuffers(1, &ColorBuffer);
glBindBuffer(GL_ARRAY_BUFFER, ColorBuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(color_buffer_data), color_buffer_data, GL_STATIC_DRAW);
//id UV vertexArrayID
GLuint UVBuffer;
glGenBuffers(1, &UVBuffer);
glBindBuffer(GL_ARRAY_BUFFER, UVBuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(uv_buffer_data), uv_buffer_data, GL_STATIC_DRAW);
*/
glm::mat4 M = glm::mat4(1.0);
//M = glm::translate(glm::mat4(1.0), glm::vec3(1.0, 0.0, 0.0));
glm::mat4 MVP = camera.getProjectionMatrix() * camera.getViewMatrix() * M;
// Get a handle for our "MVP" uniform.
// Only at initialisation time.
GLuint MatrixID = glGetUniformLocation(programID, "MVP");
GLuint TextureID = glGetUniformLocation(programID, "samplerTexture");
//chargement texture
Texture t(pathToTextures + "texture2.bmp");
//Model model("Models/axe.obj");
double currentTime = 0, lastTime = 0;
float deltaTime = 0;
float FoV = initFoV;
glm::vec3 direction;
glm::vec3 up;
//main loop to render
do
{
//get current time
currentTime = glfwGetTime();
//update mouse control and keyboard control
updateControl(window, WAngle, HAngle, mouseSpeed, deltaTime, speed, position , direction, up, initFoV, FoV);
//update ViewMatrix
camera.setViewMatrix(position, direction, up);
//update Projection Matrix
camera.setProjectionMatrix(FoV, WIDTH, HEIGHT, near, far);
//update MVP matrix
MVP = camera.getProjectionMatrix() * camera.getViewMatrix() * M;
// Clear the screen
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
if(programID)
{
// Use our shader
glUseProgram(programID);
}
// Send our transformation to the currently bound shader,
// in the "MVP" uniform
// For each model you render, since the MVP will be different (at least the M part)
glUniformMatrix4fv(MatrixID, 1, GL_FALSE, &MVP[0][0]);
/*
//vertexs
//on se place sur les vertices
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, VertexBuffer);
glVertexAttribPointer(
0,//canal 0
3,//la taille du vertex
GL_FLOAT,//le type de données
GL_FALSE,//est-ce que les données sont normalisée
0,//stride
(void*)0//offset du buffer
);
//couleurs
//on se place sur les couleurs
glEnableVertexAttribArray(1);
//on bind le buffer
glBindBuffer(GL_ARRAY_BUFFER, ColorBuffer);
//on attribue le buffer sur le canal 1
glVertexAttribPointer(
1,//canal 1
3,//la taille de la couleur
GL_FLOAT,//le type de données
GL_FALSE,//est-ce que les données sont normalisée
0,//stride
(void*)0//offset du buffer
);
//UV
//on se place sur le canal pour les UV
glEnableVertexAttribArray(2);
glBindBuffer(GL_ARRAY_BUFFER, UVBuffer);
glVertexAttribPointer(
2,//canal 0
2,//la taille du vertex
GL_FLOAT,//le type de données
GL_FALSE,//est-ce que les données sont normalisée
0,//stride
(void*)0//offset du buffer
);
*/
//Enable culling triangles which normal is not towards the camera
glEnable(GL_CULL_FACE);
// Enable depth test
glEnable(GL_DEPTH_TEST);
// Accept fragment if it closer to the camera than the former one
glDepthFunc(GL_LESS);
scene.getRootNode()->render();
//t.bind();
//glUniform1i(TextureID, 0);
//draw Arrays!
//glDrawArrays(GL_TRIANGLES, 0, 12*3);//on part du vertex0 et il y en a trois
//t.release();
/*glBindBuffer(GL_ARRAY_BUFFER, 0);
glDisableVertexAttribArray(0);
glDisableVertexAttribArray(1);
glDisableVertexAttribArray(2);*/
//on nettoie les buffers
glfwSwapBuffers(window);
glfwPollEvents();
//get current time
lastTime = glfwGetTime();
//time between 2 frames
deltaTime = float(lastTime - currentTime);
} while (glfwGetKey(window, GLFW_KEY_ESCAPE) != GLFW_PRESS && glfwWindowShouldClose(window) == 0);
// Cleanup VBO
/*glDeleteBuffers(1, &VertexBuffer);
glDeleteBuffers(1, &ColorBuffer);
glDeleteVertexArrays(1, &VertexArrayID);*/
glDeleteProgram(programID);
// Close OpenGL window and terminate GLFW
glfwTerminate();
return 0;
}
int Initialisation()
{
// Initialise GLFW
if (!glfwInit())
{
fprintf(stderr, "Failed to initialize GLFW\n");
return -1;
}
glfwWindowHint(GLFW_SAMPLES, 4); // 4x antialiasing
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4); // We want OpenGL 4.3
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); // To make MacOS happy; should not be needed
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); //We don't want the old OpenGL
window = glfwCreateWindow(WIDTH, HEIGHT, "Moteur3d", NULL, NULL);
if (window == NULL){
fprintf(stderr, "Failed to open GLFW window. If you have an Intel GPU, they are not 3.3 compatible. Try the 2.1 version of the tutorials.\n");
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window); // Initialize GLEW
glewExperimental = true; // Needed in core profile
if (glewInit() != GLEW_OK) {
fprintf(stderr, "Failed to initialize GLEW\n");
return -1;
}
return 0;
} |
Partager