small fixes
This commit is contained in:
parent
cb7368c138
commit
436c5ad643
125
src/main.cpp
125
src/main.cpp
@ -20,35 +20,29 @@ typedef struct _team {
|
|||||||
GLfloat color[4];
|
GLfloat color[4];
|
||||||
} team;
|
} team;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
GLUquadricObj *quadratic;
|
GLUquadricObj *quadratic;
|
||||||
|
|
||||||
void setup_opengl()
|
void setup_opengl() {
|
||||||
{
|
|
||||||
// Initialise GLFW
|
// Initialise GLFW
|
||||||
if( !glfwInit() )
|
if (!glfwInit()) {
|
||||||
{
|
fprintf(stderr, "Failed to initialize GLFW\n");
|
||||||
fprintf( stderr, "Failed to initialize GLFW\n" );
|
exit(EXIT_FAILURE);
|
||||||
exit( EXIT_FAILURE );
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Open OpenGL window
|
// Open OpenGL window
|
||||||
if( !glfwOpenWindow( 640, 480, 0,0,0,0, 0,0, GLFW_FULLSCREEN ) )
|
if (!glfwOpenWindow(640, 480, 0, 0, 0, 0, 0, 0, GLFW_FULLSCREEN)) {
|
||||||
{
|
fprintf(stderr, "Failed to open GLFW window\n");
|
||||||
fprintf( stderr, "Failed to open GLFW window\n" );
|
|
||||||
glfwTerminate();
|
glfwTerminate();
|
||||||
exit( EXIT_FAILURE );
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
glfwDisable(GLFW_MOUSE_CURSOR);
|
glfwDisable(GLFW_MOUSE_CURSOR);
|
||||||
glfwSetWindowTitle( "Trilinear interpolation" );
|
glfwSetWindowTitle("Trilinear interpolation");
|
||||||
|
|
||||||
// Enable sticky keys
|
// Enable sticky keys
|
||||||
glfwEnable( GLFW_STICKY_KEYS );
|
glfwEnable(GLFW_STICKY_KEYS);
|
||||||
|
|
||||||
// general settings
|
// general settings
|
||||||
glShadeModel(GL_SMOOTH);
|
glShadeModel(GL_SMOOTH);
|
||||||
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
|
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
|
||||||
glEnable(GL_CULL_FACE);
|
glEnable(GL_CULL_FACE);
|
||||||
|
|
||||||
@ -57,26 +51,25 @@ void setup_opengl()
|
|||||||
glEnable(GL_DEPTH_TEST);
|
glEnable(GL_DEPTH_TEST);
|
||||||
glDepthFunc(GL_LEQUAL);
|
glDepthFunc(GL_LEQUAL);
|
||||||
|
|
||||||
GLfloat LightAmbient[]= { 0.1f, 0.1f, 0.1f, 1.0f };
|
GLfloat LightAmbient[] = { 0.1f, 0.1f, 0.1f, 1.0f };
|
||||||
GLfloat LightDiffuse[]= { 1.0f, 1.0f, 1.0f, 1.0f };
|
GLfloat LightDiffuse[] = { 1.0f, 1.0f, 1.0f, 1.0f };
|
||||||
GLfloat LightPosition[]= { 0.0f, 0.0f, 2.0f, 0.0f };
|
GLfloat LightPosition[] = { 0.0f, 0.0f, 2.0f, 1.0f };
|
||||||
|
|
||||||
// setup directional light
|
// setup directional light
|
||||||
glLightfv(GL_LIGHT1, GL_AMBIENT, LightAmbient);
|
glLightfv(GL_LIGHT1, GL_AMBIENT, LightAmbient);
|
||||||
glLightfv(GL_LIGHT1, GL_DIFFUSE, LightDiffuse);
|
glLightfv(GL_LIGHT1, GL_DIFFUSE, LightDiffuse);
|
||||||
glLightfv(GL_LIGHT1, GL_POSITION,LightPosition);
|
glLightfv(GL_LIGHT1, GL_POSITION, LightPosition);
|
||||||
glEnable(GL_LIGHT1);
|
glEnable(GL_LIGHT1);
|
||||||
glEnable(GL_LIGHTING);
|
glEnable(GL_LIGHTING);
|
||||||
|
|
||||||
// Enable vertical sync (on cards that support it)
|
// Enable vertical sync (on cards that support it)
|
||||||
glfwSwapInterval( 1 );
|
glfwSwapInterval(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
void setup_team(team *team, size_t id)
|
void setup_team(team *team, size_t id) {
|
||||||
{
|
team->color[0] = 1.0f * (id + 1 & (1 << 0));
|
||||||
team->color[0] = 1.0f * (id+1 & (1 << 0));
|
team->color[1] = 1.0f * (id + 1 & (1 << 1));
|
||||||
team->color[1] = 1.0f * (id+1 & (1 << 1));
|
team->color[2] = 1.0f * (id + 1 & (1 << 2));
|
||||||
team->color[2] = 1.0f * (id+1 & (1 << 2));
|
|
||||||
team->color[3] = 1.0f;
|
team->color[3] = 1.0f;
|
||||||
|
|
||||||
team->x = 2000.0 * (id & (1 << 0));
|
team->x = 2000.0 * (id & (1 << 0));
|
||||||
@ -84,8 +77,7 @@ void setup_team(team *team, size_t id)
|
|||||||
team->z = 2000.0 * (id & (1 << 2));
|
team->z = 2000.0 * (id & (1 << 2));
|
||||||
|
|
||||||
size_t i = 0;
|
size_t i = 0;
|
||||||
for (i = 0; i < MAX_PLAYERS_PER_TEAM; i++)
|
for (i = 0; i < MAX_PLAYERS_PER_TEAM; i++) {
|
||||||
{
|
|
||||||
team->players[i].session = 0;
|
team->players[i].session = 0;
|
||||||
team->players[i].x = team->x + i * 50.;
|
team->players[i].x = team->x + i * 50.;
|
||||||
team->players[i].y = team->y;
|
team->players[i].y = team->y;
|
||||||
@ -97,11 +89,9 @@ void setup_team(team *team, size_t id)
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void update_team(team *team, double dt)
|
void update_team(team *team, double dt) {
|
||||||
{
|
|
||||||
size_t i;
|
size_t i;
|
||||||
for (i = 0; i < MAX_PLAYERS_PER_TEAM; i++)
|
for (i = 0; i < MAX_PLAYERS_PER_TEAM; i++) {
|
||||||
{
|
|
||||||
if (team->players[i].session == 0)
|
if (team->players[i].session == 0)
|
||||||
continue;
|
continue;
|
||||||
team->players[i].x += team->players[i].vx * dt;
|
team->players[i].x += team->players[i].vx * dt;
|
||||||
@ -110,30 +100,27 @@ void update_team(team *team, double dt)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void draw_team(team *team)
|
void draw_team(team *team) {
|
||||||
{
|
|
||||||
size_t i = 0;
|
size_t i = 0;
|
||||||
glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, team->color);
|
glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, team->color);
|
||||||
glMatrixMode( GL_MODELVIEW );
|
glMatrixMode(GL_MODELVIEW);
|
||||||
|
|
||||||
glPushMatrix();
|
glPushMatrix();
|
||||||
glTranslated(team->x, team->y, team->z);
|
glTranslated(team->x, team->y, team->z);
|
||||||
gluSphere(quadratic,50.f,32,32);
|
gluSphere(quadratic, 50.f, 32, 32);
|
||||||
glPopMatrix();
|
glPopMatrix();
|
||||||
|
|
||||||
for (i = 0; i < MAX_PLAYERS_PER_TEAM; i++)
|
for (i = 0; i < MAX_PLAYERS_PER_TEAM; i++) {
|
||||||
{
|
|
||||||
if (team->players[i].session == 0)
|
if (team->players[i].session == 0)
|
||||||
continue;
|
continue;
|
||||||
glPushMatrix();
|
glPushMatrix();
|
||||||
glTranslated(team->players[i].x, team->players[i].y, team->players[i].z);
|
glTranslated(team->players[i].x, team->players[i].y, team->players[i].z);
|
||||||
gluSphere(quadratic,10.f,32,32);
|
gluSphere(quadratic, 10.f, 32, 32);
|
||||||
glPopMatrix();
|
glPopMatrix();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int main( void )
|
int main(void) {
|
||||||
{
|
|
||||||
int width, height, x, y, last_x, last_y;
|
int width, height, x, y, last_x, last_y;
|
||||||
double time, last_time, phi = 0.0, theta = 0.0;
|
double time, last_time, phi = 0.0, theta = 0.0;
|
||||||
GLboolean running;
|
GLboolean running;
|
||||||
@ -152,14 +139,13 @@ int main( void )
|
|||||||
|
|
||||||
running = GL_TRUE;
|
running = GL_TRUE;
|
||||||
last_time = glfwGetTime();
|
last_time = glfwGetTime();
|
||||||
glfwGetMousePos( &last_x, &last_y );
|
glfwGetMousePos(&last_x, &last_y);
|
||||||
while( running )
|
while (running) {
|
||||||
{
|
|
||||||
// Get time and mouse position
|
// Get time and mouse position
|
||||||
time = glfwGetTime();
|
time = glfwGetTime();
|
||||||
glfwGetMousePos( &x, &y );
|
glfwGetMousePos(&x, &y);
|
||||||
phi += (x - last_x) * -0.001;
|
phi += (x - last_x) * 0.001;
|
||||||
theta += (y - last_y) * 0.001;
|
theta += (y - last_y) * -0.001;
|
||||||
if (theta > 1.5)
|
if (theta > 1.5)
|
||||||
theta = 1.5;
|
theta = 1.5;
|
||||||
if (theta < -1.5)
|
if (theta < -1.5)
|
||||||
@ -168,39 +154,41 @@ int main( void )
|
|||||||
last_y = y;
|
last_y = y;
|
||||||
|
|
||||||
// Get window size (may be different than the requested size)
|
// Get window size (may be different than the requested size)
|
||||||
glfwGetWindowSize( &width, &height );
|
glfwGetWindowSize(&width, &height);
|
||||||
height = height > 0 ? height : 1;
|
height = height > 0 ? height : 1;
|
||||||
|
|
||||||
// Set viewport
|
// Set viewport
|
||||||
glViewport( 0, 0, width, height );
|
glViewport(0, 0, width, height);
|
||||||
|
|
||||||
// Clear color buffer
|
// Clear color buffer
|
||||||
glClearColor( 0.0f, 0.0f, 0.0f, 0.0f);
|
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
|
||||||
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||||
|
|
||||||
// Select and setup the projection matrix
|
// Select and setup the projection matrix
|
||||||
glMatrixMode( GL_PROJECTION );
|
glMatrixMode(GL_PROJECTION);
|
||||||
glLoadIdentity();
|
glLoadIdentity();
|
||||||
gluPerspective( 60.0f, (GLfloat)width / (GLfloat)height, 1.0f,
|
gluPerspective(60.0f, (GLfloat) width / (GLfloat) height, 1.0f,
|
||||||
10000.0f );
|
10000.0f);
|
||||||
|
|
||||||
// Select and setup the modelview matrix
|
// Select and setup the modelview matrix
|
||||||
glMatrixMode( GL_MODELVIEW );
|
glMatrixMode(GL_MODELVIEW);
|
||||||
glLoadIdentity();
|
glLoadIdentity();
|
||||||
gluLookAt( 100.0f, 100.0f, 100.0f, // Eye-position
|
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
|
100.0f + cos(phi) * cos(theta) * 10.0f, 100.0f + sin(theta)
|
||||||
0.0f, 1.0f, 0.0f ); // Up-vector
|
* 10.0f, 100.0f + sin(phi) * cos(theta) * 10.0f, // View-point
|
||||||
|
0.0f, 1.0f, 0.0f); // Up-vector
|
||||||
|
|
||||||
|
glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, teams[0].color);
|
||||||
// Draw a textured quad
|
// Draw a textured quad
|
||||||
glBegin( GL_QUADS );
|
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);
|
||||||
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);
|
||||||
glEnd();
|
glEnd();
|
||||||
|
|
||||||
for (i = 0; i < MAX_TEAMS; i++)
|
for (i = 0; i < MAX_TEAMS; i++)
|
||||||
update_team(&teams[i], time-last_time);
|
update_team(&teams[i], time - last_time);
|
||||||
|
|
||||||
for (i = 0; i < MAX_TEAMS; i++)
|
for (i = 0; i < MAX_TEAMS; i++)
|
||||||
draw_team(&teams[i]);
|
draw_team(&teams[i]);
|
||||||
@ -209,17 +197,16 @@ int main( void )
|
|||||||
glfwSwapBuffers();
|
glfwSwapBuffers();
|
||||||
|
|
||||||
// Check if the ESC key was pressed or the window was closed
|
// Check if the ESC key was pressed or the window was closed
|
||||||
running = !glfwGetKey( GLFW_KEY_ESC ) &&
|
running = !glfwGetKey(GLFW_KEY_ESC) && glfwGetWindowParam(GLFW_OPENED);
|
||||||
glfwGetWindowParam( GLFW_OPENED );
|
|
||||||
|
|
||||||
last_time = time;
|
last_time = time;
|
||||||
}
|
}
|
||||||
|
|
||||||
gluDeleteQuadric(quadratic);
|
gluDeleteQuadric(quadratic);
|
||||||
|
|
||||||
// Close OpenGL window and terminate GLFW
|
// Close OpenGL window and terminate GLFW
|
||||||
glfwTerminate();
|
glfwTerminate();
|
||||||
|
|
||||||
exit( EXIT_SUCCESS );
|
exit(EXIT_SUCCESS);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user