gremlin/src/main.cpp

212 lines
5.3 KiB
C++
Raw Normal View History

2011-01-04 17:11:59 +00:00
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <GL/glfw.h>
#define MAX_PLAYERS_PER_TEAM 8
#define MAX_TEAMS 8
typedef struct _player {
unsigned int session;
double x, y, z;
double rx, ry, rz, rw;
double vx, vy, vz;
} player;
typedef struct _team {
player players[MAX_PLAYERS_PER_TEAM];
double x, y, z;
GLfloat color[4];
} team;
GLUquadricObj *quadratic;
2011-01-04 21:24:18 +00:00
void setup_opengl() {
2011-01-04 17:11:59 +00:00
// Initialise GLFW
2011-01-04 21:24:18 +00:00
if (!glfwInit()) {
fprintf(stderr, "Failed to initialize GLFW\n");
exit(EXIT_FAILURE);
2011-01-04 17:11:59 +00:00
}
// Open OpenGL window
2011-01-04 21:24:18 +00:00
if (!glfwOpenWindow(640, 480, 0, 0, 0, 0, 0, 0, GLFW_FULLSCREEN)) {
fprintf(stderr, "Failed to open GLFW window\n");
2011-01-04 17:11:59 +00:00
glfwTerminate();
2011-01-04 21:24:18 +00:00
exit(EXIT_FAILURE);
2011-01-04 17:11:59 +00:00
}
glfwDisable(GLFW_MOUSE_CURSOR);
2011-01-04 21:24:18 +00:00
glfwSetWindowTitle("Trilinear interpolation");
2011-01-04 17:11:59 +00:00
// Enable sticky keys
2011-01-04 21:24:18 +00:00
glfwEnable(GLFW_STICKY_KEYS);
2011-01-04 17:11:59 +00:00
// general settings
2011-01-04 21:24:18 +00:00
glShadeModel(GL_SMOOTH);
2011-01-04 17:11:59 +00:00
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
glEnable(GL_CULL_FACE);
// setup depth buffer
glClearDepth(1.0f);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
2011-01-04 21:24:18 +00:00
GLfloat LightAmbient[] = { 0.1f, 0.1f, 0.1f, 1.0f };
GLfloat LightDiffuse[] = { 1.0f, 1.0f, 1.0f, 1.0f };
GLfloat LightPosition[] = { 0.0f, 0.0f, 2.0f, 1.0f };
2011-01-04 17:11:59 +00:00
// setup directional light
glLightfv(GL_LIGHT1, GL_AMBIENT, LightAmbient);
2011-01-04 21:24:18 +00:00
glLightfv(GL_LIGHT1, GL_DIFFUSE, LightDiffuse);
glLightfv(GL_LIGHT1, GL_POSITION, LightPosition);
2011-01-04 17:11:59 +00:00
glEnable(GL_LIGHT1);
glEnable(GL_LIGHTING);
// Enable vertical sync (on cards that support it)
2011-01-04 21:24:18 +00:00
glfwSwapInterval(1);
2011-01-04 17:11:59 +00:00
}
2011-01-04 21:24:18 +00:00
void setup_team(team *team, size_t id) {
2011-01-04 21:26:37 +00:00
team->color[0] = .5f + .5f * (id & (1 << 0));
team->color[1] = .5f + .5f * (id & (1 << 1));
team->color[2] = .5f + .5f * (id & (1 << 2));
2011-01-04 17:11:59 +00:00
team->color[3] = 1.0f;
team->x = 2000.0 * (id & (1 << 0));
team->y = 2000.0 * (id & (1 << 1));
team->z = 2000.0 * (id & (1 << 2));
size_t i = 0;
2011-01-04 21:24:18 +00:00
for (i = 0; i < MAX_PLAYERS_PER_TEAM; i++) {
2011-01-04 17:11:59 +00:00
team->players[i].session = 0;
team->players[i].x = team->x + i * 50.;
team->players[i].y = team->y;
team->players[i].z = team->z;
team->players[i].vx = i * 5.;
team->players[i].vy = i * 5.;
team->players[i].vz = i * 5.;
}
}
2011-01-04 21:24:18 +00:00
void update_team(team *team, double dt) {
2011-01-04 17:11:59 +00:00
size_t i;
2011-01-04 21:24:18 +00:00
for (i = 0; i < MAX_PLAYERS_PER_TEAM; i++) {
2011-01-04 17:11:59 +00:00
if (team->players[i].session == 0)
continue;
team->players[i].x += team->players[i].vx * dt;
team->players[i].y += team->players[i].vy * dt;
team->players[i].z += team->players[i].vz * dt;
}
}
2011-01-04 21:24:18 +00:00
void draw_team(team *team) {
2011-01-04 17:11:59 +00:00
size_t i = 0;
glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, team->color);
2011-01-04 21:24:18 +00:00
glMatrixMode(GL_MODELVIEW);
2011-01-04 17:11:59 +00:00
glPushMatrix();
glTranslated(team->x, team->y, team->z);
2011-01-04 21:24:18 +00:00
gluSphere(quadratic, 50.f, 32, 32);
2011-01-04 17:11:59 +00:00
glPopMatrix();
2011-01-04 21:24:18 +00:00
for (i = 0; i < MAX_PLAYERS_PER_TEAM; i++) {
2011-01-04 17:11:59 +00:00
if (team->players[i].session == 0)
continue;
glPushMatrix();
glTranslated(team->players[i].x, team->players[i].y, team->players[i].z);
2011-01-04 21:24:18 +00:00
gluSphere(quadratic, 10.f, 32, 32);
2011-01-04 17:11:59 +00:00
glPopMatrix();
}
}
2011-01-04 21:24:18 +00:00
int main(void) {
2011-01-04 17:11:59 +00:00
int width, height, x, y, last_x, last_y;
double time, last_time, phi = 0.0, theta = 0.0;
GLboolean running;
quadratic = gluNewQuadric();
gluQuadricNormals(quadratic, GLU_SMOOTH);
gluQuadricTexture(quadratic, GL_TRUE);
team teams[MAX_TEAMS];
setup_opengl();
size_t i;
for (i = 0; i < MAX_TEAMS; i++)
setup_team(&teams[i], i);
running = GL_TRUE;
last_time = glfwGetTime();
2011-01-04 21:24:18 +00:00
glfwGetMousePos(&last_x, &last_y);
while (running) {
2011-01-04 17:11:59 +00:00
// Get time and mouse position
time = glfwGetTime();
2011-01-04 21:24:18 +00:00
glfwGetMousePos(&x, &y);
phi += (x - last_x) * 0.001;
theta += (y - last_y) * -0.001;
2011-01-04 17:11:59 +00:00
if (theta > 1.5)
theta = 1.5;
if (theta < -1.5)
theta = -1.5;
last_x = x;
last_y = y;
// Get window size (may be different than the requested size)
2011-01-04 21:24:18 +00:00
glfwGetWindowSize(&width, &height);
2011-01-04 17:11:59 +00:00
height = height > 0 ? height : 1;
// Set viewport
2011-01-04 21:24:18 +00:00
glViewport(0, 0, width, height);
2011-01-04 17:11:59 +00:00
// Clear color buffer
2011-01-04 21:24:18 +00:00
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
2011-01-04 17:11:59 +00:00
// Select and setup the projection matrix
2011-01-04 21:24:18 +00:00
glMatrixMode(GL_PROJECTION);
2011-01-04 17:11:59 +00:00
glLoadIdentity();
2011-01-04 21:24:18 +00:00
gluPerspective(60.0f, (GLfloat) width / (GLfloat) height, 1.0f,
10000.0f);
2011-01-04 17:11:59 +00:00
// Select and setup the modelview matrix
2011-01-04 21:24:18 +00:00
glMatrixMode(GL_MODELVIEW);
2011-01-04 17:11:59 +00:00
glLoadIdentity();
2011-01-04 21:24:18 +00:00
gluLookAt(100.0f, 100.0f, 100.0f, // Eye-position
100.0f + cos(phi) * cos(theta) * 10.0f, 100.0f + sin(theta)
* 10.0f, 100.0f + sin(phi) * cos(theta) * 10.0f, // View-point
0.0f, 1.0f, 0.0f); // Up-vector
2011-01-04 17:11:59 +00:00
2011-01-04 21:24:18 +00:00
glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, teams[0].color);
2011-01-04 17:11:59 +00:00
// Draw a textured quad
2011-01-04 21:24:18 +00:00
glBegin(GL_QUADS);
glVertex3f(-5000.0f, 5000.0f, -5000.0f);
glVertex3f(5000.0f, 5000.0f, -5000.0f);
glVertex3f(5000.0f, 5000.0f, 5000.0f);
glVertex3f(-5000.0f, 5000.0f, 5000.0f);
2011-01-04 17:11:59 +00:00
glEnd();
for (i = 0; i < MAX_TEAMS; i++)
2011-01-04 21:24:18 +00:00
update_team(&teams[i], time - last_time);
2011-01-04 17:11:59 +00:00
for (i = 0; i < MAX_TEAMS; i++)
draw_team(&teams[i]);
// Swap buffers
glfwSwapBuffers();
// Check if the ESC key was pressed or the window was closed
2011-01-04 21:24:18 +00:00
running = !glfwGetKey(GLFW_KEY_ESC) && glfwGetWindowParam(GLFW_OPENED);
2011-01-04 17:11:59 +00:00
last_time = time;
}
2011-01-04 21:24:18 +00:00
gluDeleteQuadric(quadratic);
2011-01-04 17:11:59 +00:00
// Close OpenGL window and terminate GLFW
glfwTerminate();
2011-01-04 21:24:18 +00:00
exit(EXIT_SUCCESS);
2011-01-04 17:11:59 +00:00
}