# HG changeset patch # User John Tsiombikas # Date 1409342879 -10800 # Node ID 76e75cbcb758f978d915dc518611a8631c871c18 # Parent affaad5fcd309e02e559042769c98b7ae79946e1 foo diff -r affaad5fcd30 -r 76e75cbcb758 .hgignore --- a/.hgignore Fri Aug 29 18:56:54 2014 +0300 +++ b/.hgignore Fri Aug 29 23:07:59 2014 +0300 @@ -1,3 +1,4 @@ \.o$ \.d$ \.swp$ +^vrmodel$ diff -r affaad5fcd30 -r 76e75cbcb758 Makefile --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Makefile Fri Aug 29 23:07:59 2014 +0300 @@ -0,0 +1,25 @@ +src = $(wildcard src/*.cc) +obj = $(src:.cc=.o) +dep = $(obj:.o=.d) +bin = vrmodel + +CFLAGS = -pedantic -Wall -g `pkg-config --cflags sdl2` +LDFLAGS = $(libgl) `pkg-config --libs sdl2` + +ifeq ($(shell uname -s), Darwin) + libgl = -framework OpenGL -lGLEW +else + libgl = -lGL -lGLU -lGLEW +endif + +$(bin): $(obj) + $(CC) -o $@ $(obj) $(LDFLAGS) + +-include $(dep) + +%.d: %.cc + @$(CPP) $(CFLAGS) $< -MM -MT $(@:.d=.o) >$@ + +.PHONY: clean +clean: + rm -f $(obj) $(bin) diff -r affaad5fcd30 -r 76e75cbcb758 src/main.cc --- a/src/main.cc Fri Aug 29 18:56:54 2014 +0300 +++ b/src/main.cc Fri Aug 29 23:07:59 2014 +0300 @@ -0,0 +1,101 @@ +#include +#include +#include +#include "opengl.h" +#include "vport.h" + +#define ANYPOS SDL_WINDOWPOS_UNDEFINED + +void display(); +bool handle_event(SDL_Event *ev); +bool handle_key(int key, bool pressed); +void reshape(int x, int y); + +static SDL_Window *win; +static SDL_GLContext ctx; + +int main(int argc, char **argv) +{ + if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER) == -1) { + fprintf(stderr, "failed to initialize SDL\n"); + return 1; + } + + unsigned int flags = SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE; + if(!(win = SDL_CreateWindow("vrmodel", ANYPOS, ANYPOS, 1280, 800, flags))) { + fprintf(stderr, "failed to create window\n"); + return 1; + } + if(!(ctx = SDL_GL_CreateContext(win))) { + fprintf(stderr, "failed to create OpenGL context\n"); + SDL_Quit(); + return 1; + } + + for(;;) { + SDL_Event ev; + SDL_WaitEvent(&ev); + + do { + if(!handle_event(&ev)) { + goto done; + } + } while(SDL_PollEvent(&ev)); + + display(); + } + +done: + SDL_Quit(); + return 0; +} + +void display() +{ + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + + int width, height; + SDL_GetWindowSize(win, &width, &height); + glViewport(0, 0, width, height); + + glMatrixMode(GL_PROJECTION); + glLoadIdentity(); + gluPerspective(50.0, (float)width / (float)height, 0.5, 1000.0); + + glMatrixMode(GL_MODELVIEW); + glLoadIdentity(); + glTranslatef(0, 0, -10); + glRotatef(25, 1, 0, 0); + + draw_vport(); + + SDL_GL_SwapWindow(win); +} + +bool handle_event(SDL_Event *ev) +{ + switch(ev->type) { + case SDL_KEYDOWN: + case SDL_KEYUP: + return handle_key(ev->key.keysym.sym, ev->key.state == SDL_PRESSED); + + default: + break; + } + + return true; +} + +bool handle_key(int key, bool pressed) +{ + if(pressed) { + switch(key) { + case 27: + return false; + + default: + break; + } + } + return true; +} diff -r affaad5fcd30 -r 76e75cbcb758 src/opengl.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/opengl.h Fri Aug 29 23:07:59 2014 +0300 @@ -0,0 +1,16 @@ +#ifndef OPENGL_H_ +#define OPENGL_H_ + +#include + +#ifdef WIN32 +#include +#endif + +#ifdef __APPLE__ +#include +#else +#include +#endif + +#endif /* OPENGL_H_ */ diff -r affaad5fcd30 -r 76e75cbcb758 src/vport.cc --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/vport.cc Fri Aug 29 23:07:59 2014 +0300 @@ -0,0 +1,49 @@ +#include "vport.h" +#include "opengl.h" + +static void draw_grid(int num_lines, float sep); + +bool init_vport() +{ + return true; +} + +void destroy_vport() +{ +} + +void draw_vport() +{ + draw_grid(10, 2.5); +} + +static void draw_grid(int num_lines, float sep) +{ + float size = num_lines * sep; + + glLineWidth(1); + + glBegin(GL_LINES); + glColor3f(0.3, 0.3, 0.3); + for(int i=1; i<=num_lines; i++) { + glVertex3f(-i * sep, 0, -size); + glVertex3f(-i * sep, 0, size); + glVertex3f(i * sep, 0, -size); + glVertex3f(i * sep, 0, size); + glVertex3f(-size, 0, -i * sep); + glVertex3f(size, 0, -i * sep); + glVertex3f(-size, 0, i * sep); + glVertex3f(size, 0, i * sep); + } + glEnd(); + + glLineWidth(2); + glBegin(GL_LINES); + glColor3f(1, 0.3, 0.3); + glVertex3f(-size, 0, 0); + glVertex3f(size, 0, 0); + glColor3f(0.3, 1, 0.3); + glVertex3f(0, 0, -size); + glVertex3f(0, 0, size); + glEnd(); +} diff -r affaad5fcd30 -r 76e75cbcb758 src/vport.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/vport.h Fri Aug 29 23:07:59 2014 +0300 @@ -0,0 +1,9 @@ +#ifndef VPORT_H_ +#define VPORT_H_ + +bool init_vport(); +void destroy_vport(); + +void draw_vport(); + +#endif // VPORT_H_