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 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396
| #include <windows.h>
#include <GL/gl.h> // Header File For The OpenGL32 Library
#include <GL/glu.h> // Header File For The GLu32 Library
#include <GL/glut.h>
#include <math.h>
#include "jpeglib.h"
#include <iostream.h>
using namespace std;
#include <stdio.h>
#include <stdlib.h>
#pragma comment(lib, "lib/jpeg.lib")
#define SKYFRONT 0 // Give Front ID = 0
#define SKYBACK 1 // Give Back ID = 1
#define SKYLEFT 2 // Give Left ID = 2
#define SKYRIGHT 3 // Give Right ID = 3
#define SKYUP 4 // Give Up ID = 4
#define SKYDOWN 5 // Give Down ID = 5
UINT SkyboxTexture[6];
#define CAMERASPEED 0.03f // The Camera Speed
#define GL_CLAMP_TO_EDGE 0x812F // Define this for glTexParameteri
const int WINDOW_WIDTH = 1280;
const int WINDOW_HEIGHT = 720;
const char *WINDOW_TITLE = "Triangles ";
/////////////////////////////////////////////////////////////////////////////////////////////////
// JPEG TEXTURE LOADER
/////////////////////////////////////////////////////////////////////////////////////////////////
void JPEG_Skybox(UINT textureArray[], LPSTR strFileName, int ID)
{
if(!strFileName) return;
tImageJPG *pBitMap = Load_JPEG(strFileName);
if(pBitMap == NULL) exit(0);
glGenTextures(1, &textureArray[ID]);
glBindTexture(GL_TEXTURE_2D, textureArray[ID]);
gluBuild2DMipmaps(GL_TEXTURE_2D, 3, pBitMap->sizeX, pBitMap->sizeY, GL_RGB, GL_UNSIGNED_BYTE, pBitMap->data);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_CLAMP_TO_EDGE);
if (pBitMap)
{
if (pBitMap->data)
{
free(pBitMap->data);
}
free(pBitMap);
}
}
//////////////////////////////////////
//The tVector3 Struct
//////////////////////////////////////
typedef struct tVector3 // expanded 3D vector struct
{
tVector3() {} // constructor
tVector3(float new_x, float new_y, float new_z) // initialize constructor
{
x = new_x;
y = new_y;
z = new_z;
}
// overload + operator so that we easier can add vectors
tVector3 operator+(tVector3 vVector) {
return tVector3(vVector.x + x, vVector.y + y, vVector.z + z);
}
// overload - operator that we easier can subtract vectors
tVector3 operator-(tVector3 vVector) {
return tVector3(x - vVector.x, y - vVector.y, z - vVector.z);
}
// overload * operator that we easier can multiply by scalars
tVector3 operator*(float number){
return tVector3(x*number, y*number, z*number);
}
// overload / operator that we easier can divide by a scalar
tVector3 operator/(float number){
return tVector3(x / number, y / number, z / number);
}
float x, y, z; // 3D vector coordinates
}tVector3;
tVector3 mPos;
tVector3 mView;
tVector3 mUp;
int mWindowWidth;
int mWindowHeight;
/////////////////////////////////////////////////////////////////////////////////////////////////
// THE CCAMERA POSITION CAMERA
/////////////////////////////////////////////////////////////////////////////////////////////////
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)
{
tVector3 vPos = tVector3(pos_x, pos_y, pos_z);
tVector3 vView = tVector3(view_x, view_y, view_z);
tVector3 vUp = tVector3(up_x, up_y, up_z);
mPos = vPos; // set the position
mView = vView; // set the view
mUp = vUp; // set the up vector
}
/////////////////////////////////////////////////////////////////////////////////////////////////
// THE CCAMERA MOVE CAMERA
/////////////////////////////////////////////////////////////////////////////////////////////////
void Move_Camera(float cameraspeed)
{
tVector3 vVector = tVector3(0, 0, 0); // init a new view vector
vVector = mView - mPos; // Get the view vector
// forward positive cameraspeed and backward negative -cameraspeed.
mPos.x = mPos.x + vVector.x * cameraspeed;
mPos.z = mPos.z + vVector.z * cameraspeed;
mView.x = mView.x + vVector.x * cameraspeed;
mView.z = mView.z + vVector.z * cameraspeed;
}
/////////////////////////////////////////////////////////////////////////////////////////////////
// THE CCAMERA ROTATE VIEW
/////////////////////////////////////////////////////////////////////////////////////////////////
void Rotate_View(float x, float y, float z)
{
tVector3 vVector = mView - mPos;
if (x)
{
mView.z = (float)(mPos.z + sin(x)*vVector.y + cos(x)*vVector.z);
mView.y = (float)(mPos.y + cos(x)*vVector.y - sin(x)*vVector.z);
}
if (y)
{
mView.z = (float)(mPos.z + sin(y)*vVector.x + cos(y)*vVector.z);
mView.x = (float)(mPos.x + cos(y)*vVector.x - sin(y)*vVector.z);
}
if (z)
{
mView.x = (float)(mPos.x + sin(z)*vVector.y + cos(z)*vVector.x);
mView.y = (float)(mPos.y + cos(z)*vVector.y - sin(z)*vVector.x);
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////
// THE CCAMERA MOUSE MOVE
/////////////////////////////////////////////////////////////////////////////////////////////////
void Mouse_Move()
{
POINT mousePos;
int mid_x = mWindowWidth >> 1;
int mid_y = mWindowHeight >> 1;
float angle_y = 0.0f;
float angle_z = 0.0f;
GetCursorPos(&mousePos); // Get the 2D mouse cursor (x,y) position
if ((mousePos.x == mid_x) && (mousePos.y == mid_y)) return;
SetCursorPos(mid_x, mid_y); // Set the mouse cursor in the middle of the window
// Get the direction from the mouse, and bring the number down to a reasonable amount
angle_y = (float)((mid_x - mousePos.x)) / 1000;
angle_z = (float)((mid_y - mousePos.y)) / 1000;
// The higher the number(acceleration) is the faster the camera looks around.
mView.y += angle_z * 2;
// limit the rotation around the x-axis
if ((mView.y - mPos.y) > 8) mView.y = mPos.y + 8;
if ((mView.y - mPos.y) <-8) mView.y = mPos.y - 8;
Rotate_View(0, -angle_y, 0); // Rotate
}
/////////////////////////////////////////////////////////////////////////////////////////////////
// THE KEYBOARD INPUT
/////////////////////////////////////////////////////////////////////////////////////////////////
void keyboardT(int key, int x, int y){
switch (key)
{
case GLUT_KEY_UP:
Move_Camera(CAMERASPEED);
break;
case GLUT_KEY_DOWN:
Move_Camera(-CAMERASPEED);
break;
case GLUT_KEY_LEFT:
Rotate_View(0, -CAMERASPEED, 0);
break;
case GLUT_KEY_RIGHT:
Rotate_View(0, CAMERASPEED, 0);
break;
}
}
void Keyboard_Input()
{
if((GetKeyState(VK_UP) & 0x80) || (GetKeyState('W') & 0x80))
{
Move_Camera(CAMERASPEED);
}
if((GetKeyState(VK_DOWN) & 0x80) || (GetKeyState('S') & 0x80))
{
Move_Camera(-CAMERASPEED);
}
if((GetKeyState(VK_LEFT) & 0x80) || (GetKeyState('A') & 0x80))
{
Rotate_View(0, -CAMERASPEED, 0);
}
if((GetKeyState(VK_RIGHT) & 0x80) || (GetKeyState('D') & 0x80))
{
Rotate_View(0, CAMERASPEED, 0);
}
}
void Init(void)
{
glClearColor(0.0, 0.0, 0.0, 0.0);
glShadeModel(GL_SMOOTH);
// Position de la lampe en coordonnées homogènes
GLfloat light_position[] = { 1.0, 1.0, 1.0, 0.0 };
// Affecter à la source sa nouvelle position
glLightfv(GL_LIGHT0, GL_POSITION, light_position);
// Position View(target) Up
Position_Camera(0, 2.5f, 5, 0, 2.5f, 0, 0, 1, 0);
// Load the Skybox textures
JPEG_Skybox(SkyboxTexture,"Texture/front.jpg", SKYFRONT);
JPEG_Skybox(SkyboxTexture,"Texture/back.jpg", SKYBACK);
JPEG_Skybox(SkyboxTexture,"Texture/left.jpg", SKYLEFT);
JPEG_Skybox(SkyboxTexture,"Texture/right.jpg", SKYRIGHT);
JPEG_Skybox(SkyboxTexture,"Texture/up.jpg", SKYUP);
JPEG_Skybox(SkyboxTexture,"Texture/down.jpg", SKYDOWN);
}
void reshape(int w, int h)
{
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
mWindowWidth = w;
gluPerspective(45.0f, (GLfloat)w / (GLfloat)h, 0.1f, 1000.0f);
glMatrixMode(GL_MODELVIEW); // Select The Modelview Matrix
glLoadIdentity(); // Reset The Modelview Matrix
mWindowWidth = w;
mWindowHeight = h;
}
void 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 Draw_Skybox(float x, float y, float z, float width, float height, float length)
{
// Center the Skybox around the given x,y,z position
x = x - width / 2;
y = y - height / 2;
z = z - length / 2;
// Draw Front side
glBindTexture(GL_TEXTURE_2D, SkyboxTexture[SKYFRONT]);
glBegin(GL_QUADS);
glTexCoord2f(1.0f, 0.0f); glVertex3f(x, y, z+length);
glTexCoord2f(1.0f, 1.0f); glVertex3f(x, y+height, z+length);
glTexCoord2f(0.0f, 1.0f); glVertex3f(x+width, y+height, z+length);
glTexCoord2f(0.0f, 0.0f); glVertex3f(x+width, y, z+length);
glEnd();
// Draw Back side
glBindTexture(GL_TEXTURE_2D, SkyboxTexture[SKYBACK]);
glBegin(GL_QUADS);
glTexCoord2f(1.0f, 0.0f); glVertex3f(x+width, y, z);
glTexCoord2f(1.0f, 1.0f); glVertex3f(x+width, y+height, z);
glTexCoord2f(0.0f, 1.0f); glVertex3f(x, y+height, z);
glTexCoord2f(0.0f, 0.0f); glVertex3f(x, y, z);
glEnd();
// Draw Left side
glBindTexture(GL_TEXTURE_2D, SkyboxTexture[SKYLEFT]);
glBegin(GL_QUADS);
glTexCoord2f(1.0f, 1.0f); glVertex3f(x, y+height, z);
glTexCoord2f(0.0f, 1.0f); glVertex3f(x, y+height, z+length);
glTexCoord2f(0.0f, 0.0f); glVertex3f(x, y, z+length);
glTexCoord2f(1.0f, 0.0f); glVertex3f(x, y, z);
glEnd();
// Draw Right side
glBindTexture(GL_TEXTURE_2D, SkyboxTexture[SKYRIGHT]);
glBegin(GL_QUADS);
glTexCoord2f(0.0f, 0.0f); glVertex3f(x+width, y, z);
glTexCoord2f(1.0f, 0.0f); glVertex3f(x+width, y, z+length);
glTexCoord2f(1.0f, 1.0f); glVertex3f(x+width, y+height, z+length);
glTexCoord2f(0.0f, 1.0f); glVertex3f(x+width, y+height, z);
glEnd();
// Draw Up side
glBindTexture(GL_TEXTURE_2D, SkyboxTexture[SKYUP]);
glBegin(GL_QUADS);
glTexCoord2f(0.0f, 0.0f); glVertex3f(x+width, y+height, z);
glTexCoord2f(1.0f, 0.0f); glVertex3f(x+width, y+height, z+length);
glTexCoord2f(1.0f, 1.0f); glVertex3f(x, y+height, z+length);
glTexCoord2f(0.0f, 1.0f); glVertex3f(x, y+height, z);
glEnd();
// Draw Down side
glBindTexture(GL_TEXTURE_2D, SkyboxTexture[SKYDOWN]);
glBegin(GL_QUADS);
glTexCoord2f(0.0f, 0.0f); glVertex3f(x, y, z);
glTexCoord2f(1.0f, 0.0f); glVertex3f(x, y, z+length);
glTexCoord2f(1.0f, 1.0f); glVertex3f(x+width, y, z+length);
glTexCoord2f(0.0f, 1.0f); glVertex3f(x+width, y, z);
glEnd();
}
/////////////////////////////////////////////////////////////////////////////////////////////////
// THE OPENGL RENDER
/////////////////////////////////////////////////////////////////////////////////////////////////
int DrawGLScene(GLvoid) // Here's Where We Do All The Drawing
{
Keyboard_Input();
Mouse_Move();
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear Screen And Depth Buffer
glLoadIdentity(); // Reset The Current Modelview Matrix
//NEW//////////////////NEW//////////////////NEW//////////////////NEW////////////////
// use this function for opengl target camera
gluLookAt(mPos.x, mPos.y, mPos.z,
mView.x, mView.y, mView.z,
mUp.x, mUp.y, mUp.z);
Draw_Grid(); // Draw grid
Draw_Skybox(0,0,0,100,100,100); // Draw the Skybox
//NEW//////////////////NEW//////////////////NEW//////////////////NEW////////////////
return TRUE; // Keep Going
}
int main(int argc, char **argv)
{
glutInit(&argc, argv);
/* Definit le mode d'affichage: Couleur RGB - Double buffering [Pour optimiser l'affichage] */
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH);
glutInitWindowSize(WINDOW_WIDTH, WINDOW_HEIGHT);
glutInitWindowPosition(100, 100);
glutCreateWindow(WINDOW_TITLE);
/* Indique la fonction à utiliser lors de laffichage graphique.. */
glutDisplayFunc(DrawGLScene);
glutIdleFunc(DrawGLScene);
glutReshapeFunc(reshape);
glutKeyboardFunc(keyboardT);
//glutSpecialFunc(keyboardT);
glutMainLoop();
return 0;
} |
Partager