Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "Player.h"
- Player::Player(ObjMesh * mesh, Texture2D * texture, float x, float y, float z) : ObjSceneObject(mesh, texture)
- {
- worldX = x;
- worldY = y;
- worldZ = z;
- }
- void Player::Keyboard(unsigned key, int x, int y)
- {
- if (key == 's')
- {
- movePlayer -= 1.0f;
- worldX = movePlayer;
- }
- }
- void Player::Update()
- {
- glutKeyboardFunc(Keyboard(unsigned key, int x, int y));
- }
- void Player::Draw()
- {
- if (_mesh->textureEnabled == true)
- {
- glBindTexture(GL_TEXTURE_2D, _texture->GetID());
- glEnableClientState(GL_NORMAL_ARRAY);
- glEnableClientState(GL_VERTEX_ARRAY);
- glEnableClientState(GL_TEXTURE_COORD_ARRAY);
- glVertexPointer(3, GL_FLOAT, 0, &_mesh->vertices[0]);
- glNormalPointer(GL_FLOAT, 0, &_mesh->normals[0]);
- glTexCoordPointer(2, GL_FLOAT, 0, &_mesh->textures[0]);
- glPushMatrix();
- glTranslatef(worldX, worldY, worldZ);
- glDrawArrays(GL_TRIANGLES, 0, _mesh->vertices.size());
- glPopMatrix();
- glDisableClientState(GL_VERTEX_ARRAY);
- glDisableClientState(GL_NORMAL_ARRAY);
- glDisableClientState(GL_TEXTURE_COORD_ARRAY);
- }
- else
- {
- glEnableClientState(GL_NORMAL_ARRAY);
- glEnableClientState(GL_VERTEX_ARRAY);
- glVertexPointer(3, GL_FLOAT, 0, &_mesh->vertices[0]);
- glNormalPointer(GL_FLOAT, 0, &_mesh->normals[0]);
- glPushMatrix();
- glTranslatef(-4.0f, -0.0f, -6.0f);
- glDrawArrays(GL_TRIANGLES, 0, _mesh->vertices.size());
- glPopMatrix();
- glDisableClientState(GL_VERTEX_ARRAY);
- glDisableClientState(GL_NORMAL_ARRAY);
- }
- }
- //My glut call backs
- #include "helloGl.h"
- #include "GLUTCallback.h"
- #include "Player.h"
- namespace GLUTCallbacks
- {
- namespace
- {
- helloGl* helloGL = nullptr;
- }
- void Init(helloGl * gl)
- {
- helloGL = gl;
- }
- void Display()
- {
- if (helloGL != nullptr)
- helloGL->Display();
- }
- void Timer(int preferredRefresh)
- {
- int updateTime = glutGet(GLUT_ELAPSED_TIME);
- helloGL->Update();
- updateTime = glutGet(GLUT_ELAPSED_TIME) - updateTime;
- glutTimerFunc(preferredRefresh - updateTime, GLUTCallbacks::Timer, preferredRefresh);
- }
- void Keyboard(unsigned char key, int x, int y)
- {
- helloGL->Keyboard(key, x, y);
- }
- }
- //My main class where everything is drawn and updated
- #include "helloGl.h"
- //https://www.opengl.org/discussion_boards/showthread.php/142675-Making-the-camera-follow-the-object //camera movement with the player
- helloGl::helloGl(int argc, char* argv[])
- {
- InitGL(argc, argv);
- InitObjects();
- //glLoadIdentity(); // Reset Matrix
- glutMainLoop();
- }
- void helloGl::LightingForGame()
- {
- _lightPosition = new Vector4();
- _lightPosition->x = 0.0;
- _lightPosition->y = 0.0;
- _lightPosition->z = 1.0;
- _lightPosition->w = 0.0;
- _lightData = new Lighting();
- _lightData->Ambient.x = 0.2;
- _lightData->Ambient.y = 0.2;
- _lightData->Ambient.z = 0.2;
- _lightData->Ambient.w = 1.0;
- _lightData->Diffuse.x = 0.8;
- _lightData->Diffuse.y = 0.8;
- _lightData->Diffuse.z = 0.8;
- _lightData->Diffuse.w = 1.0;
- _lightData->Specular.x = 0.2;
- _lightData->Specular.y = 0.2;
- _lightData->Specular.z = 0.2;
- _lightData->Specular.w = 1.0;
- glLightfv(GL_LIGHT0, GL_AMBIENT, &(_lightData->Ambient.x));
- glLightfv(GL_LIGHT0, GL_AMBIENT, &(_lightData->Ambient.y));
- glLightfv(GL_LIGHT0, GL_AMBIENT, &(_lightData->Ambient.z));
- glLightfv(GL_LIGHT0, GL_DIFFUSE, &(_lightData->Diffuse.x));
- glLightfv(GL_LIGHT0, GL_DIFFUSE, &(_lightData->Diffuse.y));
- glLightfv(GL_LIGHT0, GL_DIFFUSE, &(_lightData->Diffuse.z));
- glLightfv(GL_LIGHT0, GL_SPECULAR, &(_lightData->Specular.x));
- glLightfv(GL_LIGHT0, GL_SPECULAR, &(_lightData->Specular.y));
- glLightfv(GL_LIGHT0, GL_SPECULAR, &(_lightData->Specular.z));
- glLightfv(GL_LIGHT0, GL_POSITION, &(_lightPosition->x));
- glLightfv(GL_LIGHT0, GL_POSITION, &(_lightPosition->y));
- glLightfv(GL_LIGHT0, GL_POSITION, &(_lightPosition->z));
- glLightfv(GL_LIGHT0, GL_POSITION, &(_lightPosition->w));
- _material = new Material();
- _material->Ambient.x = 0.8;
- _material->Ambient.y = 0.05;
- _material->Ambient.z = 0.05;
- _material->Ambient.w = 1.0;
- _material->Diffuse.x = 0.8;
- _material->Diffuse.y = 0.05;
- _material->Diffuse.z = 0.05;
- _material->Diffuse.w = 1.0;
- _material->Specular.x = 1.0;
- _material->Specular.y = 1.0;
- _material->Specular.z = 1.0;
- _material->Specular.w = 1.0;
- _material->Shininess = 100.0f;
- glMaterialfv(GL_FRONT, GL_AMBIENT, &(_material->Ambient.x));
- glMaterialfv(GL_FRONT, GL_AMBIENT, &(_material->Ambient.y));
- glMaterialfv(GL_FRONT, GL_AMBIENT, &(_material->Ambient.z));
- glMaterialfv(GL_FRONT, GL_DIFFUSE, &(_material->Diffuse.x));
- glMaterialfv(GL_FRONT, GL_DIFFUSE, &(_material->Diffuse.y));
- glMaterialfv(GL_FRONT, GL_DIFFUSE, &(_material->Diffuse.z));
- glMaterialfv(GL_FRONT, GL_SPECULAR, &(_material->Specular.x));
- glMaterialfv(GL_FRONT, GL_SPECULAR, &(_material->Specular.y));
- glMaterialfv(GL_FRONT, GL_SPECULAR, &(_material->Specular.z));
- glMaterialf(GL_FRONT, GL_SHININESS, _material->Shininess);
- }
- void helloGl::InitObjects()
- {
- Texture2D* texture = new Texture2D();
- texture->LoadTextureTGA("GDT.tga");
- ObjMesh* objMesh = ModelLoader::Load("gunShip1.obj");
- ObjTest = new Player(objMesh, texture, -2, 0, -10);
- cameraMove = 5.0f;
- camera = new Camera();
- camera->eye.x = 5.0f; camera->eye.y = 0.0f; camera->eye.z = cameraMove;
- camera->center.x = 0.0f; camera->center.y = 0.0f; camera->center.z = 0.0f;
- camera->up.x = 0.0f; camera->up.y = 1.0f; camera->up.z = 0.0f;
- }
- void helloGl::InitGL(int argc, char* argv[])
- {
- GLUTCallbacks::Init(this);
- glutInit(&argc, argv);
- glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH);
- glutInitWindowSize(1920, 1080);
- glutInitDisplayMode(GLUT_DOUBLE);
- glutCreateWindow("Simple OpenGL Program");
- glutDisplayFunc(GLUTCallbacks::Display);
- glEnable(GL_DEPTH_TEST);
- glutTimerFunc(REFRESHRATE, GLUTCallbacks::Timer, REFRESHRATE);
- glutKeyboardFunc(GLUTCallbacks::Keyboard);
- glEnable(GL_TEXTURE_2D);
- glEnable(GL_CULL_FACE);
- glCullFace(GL_BACK);
- // Use the Projection Matrix
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- // Set the viewport to be the entire window
- glViewport(0, 0, 800, 800);
- gluPerspective(45, 1, 0.1, 1000);
- glEnable(GL_DEPTH_TEST);
- glEnable(GL_TEXTURE_2D);
- glEnable(GL_LIGHTING);
- glEnable(GL_LIGHT0);
- }
- void helloGl::Display()
- {
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); //This clears the scene so we start a fresh each time
- glPushMatrix();
- ObjTest->Draw();
- glEnd(); //Ends the polygon draw
- glPopMatrix();
- glFlush(); //Flushes the scene we just drew to the graphics card
- glutSwapBuffers();
- }
- void helloGl::Update()
- {
- glMatrixMode(GL_MODELVIEW);
- glLoadIdentity();
- gluLookAt(camera->eye.x, camera->eye.y, camera->eye.z, camera->center.x, camera->center.y, camera->center.z, camera->up.x, camera->up.y, camera->up.z);
- ObjTest->Update();
- glutPostRedisplay();
- }
- void helloGl::Keyboard(unsigned char key, int x, int y)
- {
- //if (key == 'w')
- //{
- // direction -= 2.0f;
- // camera->eye.z = direction;
- //
- //}
- //if (key == 'a')
- //{
- // direction -= 2.0f;
- // camera->eye.x = direction;
- //}
- //if (key == 's')
- //{
- // direction += 2.0f;
- // camera->eye.z = direction;
- //}
- //if (key == 'd')
- //{
- // direction += 2.0f;
- // camera->eye.x = direction;
- //}
- if (key == 'w')
- {
- direction -= 2.0f;
- playerZ = direction;
- }
- if (key == 's')
- {
- direction += 2.0f;
- playerZ = direction;
- }
- }
- helloGl::~helloGl()
- {
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement