Skip to content

Instantly share code, notes, and snippets.

@ASxa86
Last active August 29, 2015 13:56
Show Gist options
  • Save ASxa86/9318482 to your computer and use it in GitHub Desktop.
Save ASxa86/9318482 to your computer and use it in GitHub Desktop.
CEG4510 asg2
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <glut.h>
#include <chrono>
#include <thread>
#include <ply/ply.h>
int width, height;
// ply vertices and faces
Vertex **vlist;
Face **flist;
int num_elems;
void init ()
{
// set the clear color (black)
glClearColor(0.0, 0.0, 0.0, 0.0);
// set the projection
glMatrixMode(GL_PROJECTION);
gluPerspective( 65.0, (GLfloat) 400 / 300,
1.0, 100.0 );
// set matrix mode back to modelview
glMatrixMode (GL_MODELVIEW);
}
void draw ()
{
// draw the entire geometry by looping through the faces which in
// turn reference the vertices
for (int j=0; j<num_elems; j++)
{
glBegin (GL_POLYGON);
for (int k = 0; k < flist[j]->nverts; k++)
{
glVertex3f (vlist[flist[j]->verts[k]]->x, vlist[flist[j]->verts[k]]->y, vlist[flist[j]->verts[k]]->z);
}
glEnd ();
}
}
void display()
{
static auto simTime = 0.0f;
// reset frame and depth buffer
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// we draw everything in red for now
glColor3f (1.0, 0.0, 0.0);
auto time = std::chrono::system_clock::now();
static auto prevTime = std::chrono::time_point<std::chrono::system_clock>();
auto deltaTime = time - prevTime;
prevTime = time;
// convert milliseconds to seconds for frame time
auto fpsInverse = std::chrono::duration_cast<std::chrono::milliseconds>(deltaTime).count() / 1000.0f;
auto fps = 1.0f / fpsInverse;
simTime += fpsInverse;
// reset simTime every 9 seconds
if(simTime >= 9.0f)
{
simTime = 0.0f;
}
printf("FPS: %f\n", fps);
glViewport(0, 0, width, height);
glLoadIdentity ();
// interpolate camera position based on simulation time
// AMS // gluLookAt
// AMS // (
// AMS // // camera position, divide by 4.0 to "zoom in"
// AMS // // throw in z axis interpolation for coordinate y for fun
// AMS // lInterpX(simTime) / 4.0, lInterpZ(simTime) / 4.0, lInterpZ(simTime) / 4.0,
// AMS // // position of model to look at
// AMS // 0.0, 0.0, 0.0,
// AMS // // set y as the up vector
// AMS // 0.0, 1.0, 0.0
// AMS // );
gluLookAt(1,0,2, 0,0,0, 0,1,0);
static auto posX = 0.0f;
posX += 0.1 * fpsInverse;
// This successfully moves model in the +x direction
// Thanks you /u/Aransentin
glPushMatrix();
glTranslatef(posX, 0, 0);
draw();
glPopMatrix();
// swap buffers since we are using double buffering
glutSwapBuffers ();
// flush just in case
glFlush ();
}
void parsekey (unsigned char key, int x, int y)
{
switch (key)
{
case 27:
exit(0);
default:
break;
}
}
void idle()
{
glutPostRedisplay();
}
int main (int argc, char **argv)
{
// read the ply file
if (argc == 2)
read_test(argv[1]);
else
read_test("D:/CEG4510/Assignment1/models/dragon.ply");
// initialize GLUT and create window
glutInit (&argc, argv);
glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize (800, 600);
glutCreateWindow ("Project 0");
// enable the depth testing
glEnable(GL_DEPTH_TEST);
init ();
// set display callback
glutDisplayFunc (display);
// set resize callback
glutReshapeFunc (reshape);
// set key-press callback
glutKeyboardFunc (parsekey);
// set idle callback
glutIdleFunc(idle);
// enter GLUT's main loop
glutMainLoop();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment