Skip to content

Instantly share code, notes, and snippets.

@zrbecker
Last active December 14, 2015 09:08
Show Gist options
  • Select an option

  • Save zrbecker/5062299 to your computer and use it in GitHub Desktop.

Select an option

Save zrbecker/5062299 to your computer and use it in GitHub Desktop.
#include <GL/glfw.h>
#include <math.h>
#if defined(_WIN32)
#include <windows.h>
extern int main(int argc, char* argv[]);
int WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, INT)
{
return main(__argc, __argv);
}
#endif // _WIN32
float PI = 3.14159265359f;
float player_x = -7.0;
float player_y = 0.0;
float player_z = 7.0;
float player_viewangle = 45.0;
float box_rotation = 0.0;
void DrawCube() {
float tfl[3] = {-1, 1, 1};
float tfr[3] = {1, 1, 1};
float bfl[3] = {-1, -1, 1};
float bfr[3] = {1, -1, 1};
float tbl[3] = {-1, 1, -1};
float tbr[3] = {1, 1, -1};
float bbl[3] = {-1, -1, -1};
float bbr[3] = {1, -1, -1};
float red[3] = {1, 0, 0};
float green[3] = {0, 1, 0};
float blue[3] = {0, 0, 1};
float yellow[3] = {1, 1, 0};
float lightblue[3] = {0, 1, 1};
float magenta[3] = {1, 0, 1};
float *verts[24] = {
tfr, tfl, bfl, bfr, // front
tbr, bbr, bbl, tbl, // back
tfl, tbl, bbl, bfl, // left
tfr, bfr, bbr, tbr, // right
tfl, tfr, tbr, tbl, // top
bfl, bbl, bbr, bfr // bottom
};
float *colors[24] = {
red, red, red, red,
blue, blue, blue, blue,
green, green, green, green,
yellow, yellow, yellow, yellow,
lightblue, lightblue, lightblue, lightblue,
magenta, magenta, magenta, magenta
};
glBegin(GL_QUADS);
int i;
for (i = 0; i < 24; ++i) {
glColor3fv(colors[i]);
glVertex3fv(verts[i]);
}
glEnd();
}
void Terminate() {
}
void Update(double secs) {
if (glfwGetKey('A'))
player_viewangle -= 2.0f;
if (glfwGetKey('D'))
player_viewangle += 2.0f;
if (glfwGetKey('W')) {
player_x += sin(PI / 180 * player_viewangle) * 0.05f;
player_z += -cos(PI / 180 * player_viewangle) * 0.05f;
}
if (glfwGetKey('S')) {
player_x -= sin(PI / 180 * player_viewangle) * 0.05f;
player_z -= -cos(PI / 180 * player_viewangle) * 0.05f;
}
if (glfwGetKey('E')) {
player_x += cos(PI / 180 * player_viewangle) * 0.05f;
player_z += sin(PI / 180 * player_viewangle) * 0.05f;
}
if (glfwGetKey('Q')) {
player_x -= cos(PI / 180 * player_viewangle) * 0.05f;
player_z -= sin(PI / 180 * player_viewangle) * 0.05f;
}
box_rotation += 2.0f;
}
void Render() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glRotatef(player_viewangle, 0.0f, 1.0f, 0.0f);
glTranslatef(-player_x, -player_y, -player_z);
DrawCube();
glPushMatrix();
glRotatef(box_rotation, 0.0f, 1.0f, 0.0f);
glTranslatef(0.0f, 0.0f, 3.0f);
glScalef(0.2f, 0.2f, 0.2f);
DrawCube();
glPopMatrix();
glPushMatrix();
glRotatef(box_rotation, 1.0f, 1.0f, 1.0f);
glTranslatef(-2.1f, 0.0f, 2.1f);
glScalef(0.2f, 0.2f, 0.2f);
DrawCube();
glPopMatrix();
glPushMatrix();
glRotatef(box_rotation * 0.7f, 1.0f, -1.0f, 1.0f);
glTranslatef(-2.4f, -1.0f, 1.4f);
glScalef(0.2f, 0.2f, 0.2f);
DrawCube();
glPopMatrix();
glTranslatef(5.0f, 0.0f, 0.0f);
glRotatef(box_rotation * 1.3f, 1.0f, 1.0f, 1.0f);
DrawCube();
}
void Init(int w, int h) {
float aspect = (float) w / h;
glViewport(0, 0, w, h);
glEnable(GL_DEPTH_TEST);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glFrustum(-1.0 * 0.1, 1.0 * 0.1, -aspect * 0.1, aspect * 0.1, 1.5 * 0.1, 100.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
int main(int argc, char *argv[]) {
if (!glfwInit())
return 1;
int width = 500;
int height = 500;
if (!glfwOpenWindow(500, 500, 8, 8, 8, 8, 24, 8, GLFW_WINDOW)) {
glfwTerminate();
return 1;
}
glfwEnable(GLFW_STICKY_KEYS);
glfwEnable(GLFW_KEY_REPEAT);
Init(width, height);
double now = glfwGetTime();
double last = 0.0;
int running = 1;
while (running) {
now = glfwGetTime();
if (now - last > 1.0 / 60.0) {
Update(now - last);
last = now;
}
Render();
glfwSwapBuffers();
running = !glfwGetKey(GLFW_KEY_ESC) && glfwGetWindowParam(GLFW_OPENED);
}
Terminate();
glfwTerminate();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment