vrmodel

changeset 1:76e75cbcb758

foo
author John Tsiombikas <nuclear@member.fsf.org>
date Fri, 29 Aug 2014 23:07:59 +0300
parents affaad5fcd30
children be91b72ce3f9
files .hgignore Makefile src/main.cc src/opengl.h src/vport.cc src/vport.h
diffstat 6 files changed, 201 insertions(+), 0 deletions(-) [+]
line diff
     1.1 --- a/.hgignore	Fri Aug 29 18:56:54 2014 +0300
     1.2 +++ b/.hgignore	Fri Aug 29 23:07:59 2014 +0300
     1.3 @@ -1,3 +1,4 @@
     1.4  \.o$
     1.5  \.d$
     1.6  \.swp$
     1.7 +^vrmodel$
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/Makefile	Fri Aug 29 23:07:59 2014 +0300
     2.3 @@ -0,0 +1,25 @@
     2.4 +src = $(wildcard src/*.cc)
     2.5 +obj = $(src:.cc=.o)
     2.6 +dep = $(obj:.o=.d)
     2.7 +bin = vrmodel
     2.8 +
     2.9 +CFLAGS = -pedantic -Wall -g `pkg-config --cflags sdl2`
    2.10 +LDFLAGS = $(libgl) `pkg-config --libs sdl2`
    2.11 +
    2.12 +ifeq ($(shell uname -s), Darwin)
    2.13 +	libgl = -framework OpenGL -lGLEW
    2.14 +else
    2.15 +	libgl = -lGL -lGLU -lGLEW
    2.16 +endif
    2.17 +
    2.18 +$(bin): $(obj)
    2.19 +	$(CC) -o $@ $(obj) $(LDFLAGS)
    2.20 +
    2.21 +-include $(dep)
    2.22 +
    2.23 +%.d: %.cc
    2.24 +	@$(CPP) $(CFLAGS) $< -MM -MT $(@:.d=.o) >$@
    2.25 +
    2.26 +.PHONY: clean
    2.27 +clean:
    2.28 +	rm -f $(obj) $(bin)
     3.1 --- a/src/main.cc	Fri Aug 29 18:56:54 2014 +0300
     3.2 +++ b/src/main.cc	Fri Aug 29 23:07:59 2014 +0300
     3.3 @@ -0,0 +1,101 @@
     3.4 +#include <stdio.h>
     3.5 +#include <stdlib.h>
     3.6 +#include <SDL2/SDL.h>
     3.7 +#include "opengl.h"
     3.8 +#include "vport.h"
     3.9 +
    3.10 +#define ANYPOS	SDL_WINDOWPOS_UNDEFINED
    3.11 +
    3.12 +void display();
    3.13 +bool handle_event(SDL_Event *ev);
    3.14 +bool handle_key(int key, bool pressed);
    3.15 +void reshape(int x, int y);
    3.16 +
    3.17 +static SDL_Window *win;
    3.18 +static SDL_GLContext ctx;
    3.19 +
    3.20 +int main(int argc, char **argv)
    3.21 +{
    3.22 +	if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER) == -1) {
    3.23 +		fprintf(stderr, "failed to initialize SDL\n");
    3.24 +		return 1;
    3.25 +	}
    3.26 +
    3.27 +	unsigned int flags = SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE;
    3.28 +	if(!(win = SDL_CreateWindow("vrmodel", ANYPOS, ANYPOS, 1280, 800, flags))) {
    3.29 +		fprintf(stderr, "failed to create window\n");
    3.30 +		return 1;
    3.31 +	}
    3.32 +	if(!(ctx = SDL_GL_CreateContext(win))) {
    3.33 +		fprintf(stderr, "failed to create OpenGL context\n");
    3.34 +		SDL_Quit();
    3.35 +		return 1;
    3.36 +	}
    3.37 +
    3.38 +	for(;;) {
    3.39 +		SDL_Event ev;
    3.40 +		SDL_WaitEvent(&ev);
    3.41 +
    3.42 +		do {
    3.43 +			if(!handle_event(&ev)) {
    3.44 +				goto done;
    3.45 +			}
    3.46 +		} while(SDL_PollEvent(&ev));
    3.47 +
    3.48 +		display();
    3.49 +	}
    3.50 +
    3.51 +done:
    3.52 +	SDL_Quit();
    3.53 +	return 0;
    3.54 +}
    3.55 +
    3.56 +void display()
    3.57 +{
    3.58 +	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    3.59 +
    3.60 +	int width, height;
    3.61 +	SDL_GetWindowSize(win, &width, &height);
    3.62 +	glViewport(0, 0, width, height);
    3.63 +
    3.64 +	glMatrixMode(GL_PROJECTION);
    3.65 +	glLoadIdentity();
    3.66 +	gluPerspective(50.0, (float)width / (float)height, 0.5, 1000.0);
    3.67 +
    3.68 +	glMatrixMode(GL_MODELVIEW);
    3.69 +	glLoadIdentity();
    3.70 +	glTranslatef(0, 0, -10);
    3.71 +	glRotatef(25, 1, 0, 0);
    3.72 +
    3.73 +	draw_vport();
    3.74 +
    3.75 +	SDL_GL_SwapWindow(win);
    3.76 +}
    3.77 +
    3.78 +bool handle_event(SDL_Event *ev)
    3.79 +{
    3.80 +	switch(ev->type) {
    3.81 +	case SDL_KEYDOWN:
    3.82 +	case SDL_KEYUP:
    3.83 +		return handle_key(ev->key.keysym.sym, ev->key.state == SDL_PRESSED);
    3.84 +
    3.85 +	default:
    3.86 +		break;
    3.87 +	}
    3.88 +
    3.89 +	return true;
    3.90 +}
    3.91 +
    3.92 +bool handle_key(int key, bool pressed)
    3.93 +{
    3.94 +	if(pressed) {
    3.95 +		switch(key) {
    3.96 +		case 27:
    3.97 +			return false;
    3.98 +
    3.99 +		default:
   3.100 +			break;
   3.101 +		}
   3.102 +	}
   3.103 +	return true;
   3.104 +}
     4.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.2 +++ b/src/opengl.h	Fri Aug 29 23:07:59 2014 +0300
     4.3 @@ -0,0 +1,16 @@
     4.4 +#ifndef OPENGL_H_
     4.5 +#define OPENGL_H_
     4.6 +
     4.7 +#include <GL/glew.h>
     4.8 +
     4.9 +#ifdef WIN32
    4.10 +#include <windows.h>
    4.11 +#endif
    4.12 +
    4.13 +#ifdef __APPLE__
    4.14 +#include <OpenGL/gl.h>
    4.15 +#else
    4.16 +#include <GL/gl.h>
    4.17 +#endif
    4.18 +
    4.19 +#endif	/* OPENGL_H_ */
     5.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     5.2 +++ b/src/vport.cc	Fri Aug 29 23:07:59 2014 +0300
     5.3 @@ -0,0 +1,49 @@
     5.4 +#include "vport.h"
     5.5 +#include "opengl.h"
     5.6 +
     5.7 +static void draw_grid(int num_lines, float sep);
     5.8 +
     5.9 +bool init_vport()
    5.10 +{
    5.11 +	return true;
    5.12 +}
    5.13 +
    5.14 +void destroy_vport()
    5.15 +{
    5.16 +}
    5.17 +
    5.18 +void draw_vport()
    5.19 +{
    5.20 +	draw_grid(10, 2.5);
    5.21 +}
    5.22 +
    5.23 +static void draw_grid(int num_lines, float sep)
    5.24 +{
    5.25 +	float size = num_lines * sep;
    5.26 +
    5.27 +	glLineWidth(1);
    5.28 +
    5.29 +	glBegin(GL_LINES);
    5.30 +	glColor3f(0.3, 0.3, 0.3);
    5.31 +	for(int i=1; i<=num_lines; i++) {
    5.32 +		glVertex3f(-i * sep, 0, -size);
    5.33 +		glVertex3f(-i * sep, 0, size);
    5.34 +		glVertex3f(i * sep, 0, -size);
    5.35 +		glVertex3f(i * sep, 0, size);
    5.36 +		glVertex3f(-size, 0, -i * sep);
    5.37 +		glVertex3f(size, 0, -i * sep);
    5.38 +		glVertex3f(-size, 0, i * sep);
    5.39 +		glVertex3f(size, 0, i * sep);
    5.40 +	}
    5.41 +	glEnd();
    5.42 +
    5.43 +	glLineWidth(2);
    5.44 +	glBegin(GL_LINES);
    5.45 +	glColor3f(1, 0.3, 0.3);
    5.46 +	glVertex3f(-size, 0, 0);
    5.47 +	glVertex3f(size, 0, 0);
    5.48 +	glColor3f(0.3, 1, 0.3);
    5.49 +	glVertex3f(0, 0, -size);
    5.50 +	glVertex3f(0, 0, size);
    5.51 +	glEnd();
    5.52 +}
     6.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     6.2 +++ b/src/vport.h	Fri Aug 29 23:07:59 2014 +0300
     6.3 @@ -0,0 +1,9 @@
     6.4 +#ifndef VPORT_H_
     6.5 +#define VPORT_H_
     6.6 +
     6.7 +bool init_vport();
     6.8 +void destroy_vport();
     6.9 +
    6.10 +void draw_vport();
    6.11 +
    6.12 +#endif	// VPORT_H_