source: nanovis/branches/1.1/RenderVertexArray.cpp @ 4599

Last change on this file since 4599 was 3502, checked in by ldelgass, 7 years ago

Add basic VTK structured points reader to nanovis, update copyright dates.

  • Property svn:eol-style set to native
File size: 3.7 KB
Line 
1/* -*- mode: c++; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2/*
3 * ----------------------------------------------------------------------
4 *  Render to vertex array
5 *
6 *  This class implements "render to vertex array" functionality
7 *  using vertex and pixel buffer objects (VBO and PBO).
8 *
9 *  Operation:
10 *  1. A buffer object is created
11 *  2. The buffer object is bound to the pixel pack (destination) buffer
12 *  3. glReadPixels is used to read from the frame buffer to the buffer object
13 *  4. The buffer object is bound to the vertex array
14 *  5. Vertex array pointers are set
15 * 
16 *  Usage:
17 *  1. Create a floating point pbuffer
18 *  2. Create a RenderVertexArray object for each vertex attribute
19 *       you want to render to
20 *  3. Render vertex data to pbuffer using a fragment program (could
21 *       use multiple draw buffers here)
22 *  4. Call Read() method to read data from pbuffer to vertex array
23 *  5. Call SetPointer() method to set vertex array pointers
24 *  6. Set any other other static vertex array attribute pointers
25 *  7. Render geometry as usual using glDrawArrays or glDrawElements
26 *
27 * ======================================================================
28 *  AUTHOR:  Wei Qiao <qiaow@purdue.edu>
29 *           Purdue Rendering and Perceptualization Lab (PURPL)
30 *
31 *  Copyright (c) 2004-2013  HUBzero Foundation, LLC
32 *
33 *  See the file "license.terms" for information on usage and
34 *  redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
35 * ======================================================================
36 */
37#include <stdio.h>
38
39#include <GL/glew.h>
40#include <GL/gl.h>
41
42#include "RenderVertexArray.h"
43#include "Trace.h"
44
45RenderVertexArray::RenderVertexArray(int nverts, GLint size, GLenum type) :
46    _usage(GL_STREAM_COPY_ARB),
47    _nverts(nverts), 
48    _size(size), 
49    _type(type)
50{
51    switch (_type) {
52    case GL_HALF_FLOAT_NV:
53        _bytesPerComponent = 2;
54        break;
55    case GL_FLOAT:
56        _bytesPerComponent = sizeof(float);
57        break;
58    default:
59        ERROR("unsupported RenderVertexArray type");
60        return;
61    }
62
63    // create the buffer object
64    glGenBuffersARB(1, &_buffer);
65    glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, _buffer);
66    glBufferDataARB(GL_PIXEL_PACK_BUFFER_ARB, 
67                    _nverts*_size*_bytesPerComponent,
68                    0, _usage); // undefined data
69    glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, 0);
70
71    // set equivalent image format
72    switch(_size) {
73    case 1:
74        _format = GL_LUMINANCE;
75        break;
76    case 3:
77        _format = GL_RGB;
78        break;
79    case 4:
80        _format = GL_RGBA;
81        break;
82    default:
83        ERROR("unsupported RenderVertexArray size");
84        return;
85    }
86}
87
88RenderVertexArray::~RenderVertexArray()
89{
90    glDeleteBuffersARB(1, &_buffer);
91}
92
93void
94RenderVertexArray::loadData(void *data)
95{
96    // load data to buffer object
97    glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, _buffer);
98    glBufferDataARB(GL_PIXEL_PACK_BUFFER_ARB, 
99                    _nverts*_size*_bytesPerComponent,
100                    data, _usage);
101    glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, 0);
102}
103
104void
105RenderVertexArray::read(int w, int h)
106{
107    // bind buffer object to pixel pack buffer
108    glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, _buffer);
109
110    // read from frame buffer to buffer object
111    glReadPixels(0, 0, w, h, _format, _type, 0);
112
113    glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, 0);
114}
115
116void
117RenderVertexArray::setPointer(GLuint index)
118{
119    // bind buffer object to vertex array
120    glBindBufferARB(GL_ARRAY_BUFFER_ARB, _buffer);
121    //glVertexAttribPointerARB(index, _size, _type, GL_FALSE, 0, 0);          //doesn't work
122    glVertexPointer(_size, _type, 0, 0);
123
124    glBindBufferARB(GL_ARRAY_BUFFER_ARB, 0);
125}
Note: See TracBrowser for help on using the repository browser.