source: nanovis/trunk/RenderVertexArray.cpp @ 4897

Last change on this file since 4897 was 3611, checked in by ldelgass, 11 years ago

Use nv namespace for classes in nanovis rather than prefixing class names with
Nv (still need to convert shader classes).

  • 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
45using namespace nv;
46
47RenderVertexArray::RenderVertexArray(int nverts, GLint size, GLenum type) :
48    _usage(GL_STREAM_COPY_ARB),
49    _nverts(nverts),
50    _size(size),
51    _type(type)
52{
53    switch (_type) {
54    case GL_HALF_FLOAT_NV:
55        _bytesPerComponent = 2;
56        break;
57    case GL_FLOAT:
58        _bytesPerComponent = sizeof(float);
59        break;
60    default:
61        ERROR("unsupported RenderVertexArray type");
62        return;
63    }
64
65    // create the buffer object
66    glGenBuffersARB(1, &_buffer);
67    glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, _buffer);
68    glBufferDataARB(GL_PIXEL_PACK_BUFFER_ARB,
69                    _nverts*_size*_bytesPerComponent,
70                    0, _usage); // undefined data
71    glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, 0);
72
73    // set equivalent image format
74    switch(_size) {
75    case 1:
76        _format = GL_LUMINANCE;
77        break;
78    case 3:
79        _format = GL_RGB;
80        break;
81    case 4:
82        _format = GL_RGBA;
83        break;
84    default:
85        ERROR("unsupported RenderVertexArray size");
86        return;
87    }
88}
89
90RenderVertexArray::~RenderVertexArray()
91{
92    glDeleteBuffersARB(1, &_buffer);
93}
94
95void
96RenderVertexArray::loadData(void *data)
97{
98    // load data to buffer object
99    glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, _buffer);
100    glBufferDataARB(GL_PIXEL_PACK_BUFFER_ARB,
101                    _nverts*_size*_bytesPerComponent,
102                    data, _usage);
103    glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, 0);
104}
105
106void
107RenderVertexArray::read(int w, int h)
108{
109    // bind buffer object to pixel pack buffer
110    glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, _buffer);
111
112    // read from frame buffer to buffer object
113    glReadPixels(0, 0, w, h, _format, _type, 0);
114
115    glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, 0);
116}
117
118void
119RenderVertexArray::setPointer(GLuint index)
120{
121    // bind buffer object to vertex array
122    glBindBufferARB(GL_ARRAY_BUFFER_ARB, _buffer);
123    //glVertexAttribPointerARB(index, _size, _type, GL_FALSE, 0, 0);          //doesn't work
124    glVertexPointer(_size, _type, 0, 0);
125
126    glBindBufferARB(GL_ARRAY_BUFFER_ARB, 0);
127}
Note: See TracBrowser for help on using the repository browser.