39 #define PGL_MOUSE_BUTTON_LEFT 0 40 #define PGL_MOUSE_BUTTON_RIGHT 1 41 #define PGL_MOUSE_BUTTON_MIDDLE 2 137 for (
size_t ii=0; ii != children.size(); ++ii)
145 for (
size_t ii=0; ii != children.size(); ++ii)
146 children[ii]->
draw();
162 children.push_back(child);
185 Object() : transform({0, 0, 0}, {0, 0, 0}) { }
191 glMultMatrixd(transform.data);
209 Scene() : color(0, 0, 0) { }
214 glClearColor(color.x, color.y, color.z, 1);
215 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
239 Camera(
Scene *_scene,
double _fovy = 0.92) : scene(_scene), fovy(_fovy) { }
245 glGetIntegerv(GL_VIEWPORT, dims);
246 double aspect = dims[2]/(double)dims[3];
248 double f = 1/tan(fovy/2);
249 double near = 1, far = 100;
251 double matrix[] = {f/aspect, 0., 0., 0.,
253 0., 0., (far+near)/(near-far), -1.,
254 0., 0., 2*far*near/(near-far), 0.};
256 glMatrixMode(GL_PROJECTION);
257 glLoadMatrixd(matrix);
259 GLfloat pos[] = {0, 0, 1, 0};
260 glLightfv(GL_LIGHT0, GL_POSITION, pos);
262 glMatrixMode(GL_MODELVIEW);
263 glLoadMatrixd(transform.data);
284 virtual void click(
int button,
int action,
int mods,
double xpos,
double ypos) = 0;
287 virtual void scroll(
double xoffset,
double yoffset) = 0;
290 virtual void motion(
double xpos,
double ypos) = 0;
302 typedef std::shared_ptr<GLuint> GLuintPtr;
309 GLuintPtr texture_ = 0;
315 Texture(
int width,
int height,
unsigned char *data,
bool interpolate=
false)
317 make(width, height, data, interpolate);
321 Texture(
const std::string &file,
bool interpolate=
true)
323 std::ifstream ifs(file);
329 std::cerr << file <<
" is not a valid binary PPM" << std::endl;
333 int _width = readint(ifs);
334 int _height = readint(ifs);
335 int maxval = readint(ifs);
339 std::cerr << file <<
": pixel format not supported" << std::endl;
343 unsigned char *data =
new unsigned char[_width*_height*3];
345 ifs.read((
char*)data, _width*_height*3);
348 std::cerr << file <<
" is truncated" << std::endl;
352 make(_width, _height, data, interpolate);
360 if (texture_.use_count() == 1)
361 glDeleteTextures(1, texture_.get());
365 operator bool()
const 367 return texture_ != 0;
373 glBindTexture(GL_TEXTURE_2D, *texture_);
377 void make(
int _width,
int _height,
unsigned char *data,
bool interpolate)
381 texture_ = GLuintPtr(
new GLuint);
383 glGenTextures(1, texture_.get());
384 glBindTexture(GL_TEXTURE_2D, *texture_);
385 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
386 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, interpolate?GL_LINEAR:GL_NEAREST);
387 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, interpolate?GL_LINEAR:GL_NEAREST);
388 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
389 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
390 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, (
void*)data);
393 void eatcomments(std::ifstream &ifs)
402 ifs.getline(buffer, 256);
404 }
while (isspace(c) || c ==
'#');
428 const unsigned char m = 255;
429 unsigned char data[] = {0,0,0,m,m,m,0,0,0,m,m,m,
430 m,m,m,0,0,0,m,m,m,0,0,0,
431 0,0,0,m,m,m,0,0,0,m,m,m,
432 m,m,m,0,0,0,m,m,m,0,0,0};
434 make(4, 4, data,
false);
Camera(Scene *_scene, double _fovy=0.92)
Specifies the Scene to draw, as well as the vertical field of view.
Definition: pgl.h:239
Transform transform
Position and orientation.
Definition: pgl.h:177
Texture(int width, int height, unsigned char *data, bool interpolate=false)
Loads texture data from existing RGB array. Data can be discarded afterwards.
Definition: pgl.h:315
Vector3 color
Background color.
Definition: pgl.h:206
std::vector< Node * > children
Sub-objects.
Definition: pgl.h:132
virtual void draw()
Draw children relative to this object.
Definition: pgl.h:188
Object()
Default constructor.
Definition: pgl.h:185
Camera * camera
Camera to move.
Definition: pgl.h:277
Node in the scene graph.
Definition: pgl.h:129
Texture.
Definition: pgl.h:299
Definition: controller.h:27
virtual void draw()
Draw scene.
Definition: pgl.h:212
Transform transform
Camera position.
Definition: pgl.h:227
Camera controller.
Definition: pgl.h:274
virtual void draw()
Draw children.
Definition: pgl.h:143
3-component vector.
Definition: math.h:43
double fovy
Vertical field of view.
Definition: pgl.h:229
Checkerboard texture.
Definition: pgl.h:423
void bind() const
Use this texture as the current OpenGL 2D texture.
Definition: pgl.h:371
void draw()
Draw Scene from this camera's perspective.
Definition: pgl.h:242
int readint(std::ifstream &ifs)
Read integer value from PPM file, ignoring comments and whitespace.
Definition: pgl.h:410
Texture(const std::string &file, bool interpolate=true)
Loads texture from Portable Pixmap (PPM) file.
Definition: pgl.h:321
Root node of the scene graph.
Definition: pgl.h:203
Object in a scene.
Definition: pgl.h:174
T * attach(T *child)
Add child to list of sub-objects.
Definition: pgl.h:160
Scene * scene
Scene to draw.
Definition: pgl.h:228
Defines camera position and frustum.
Definition: pgl.h:224