source: trunk/packages/vizservers/nanovis/RenderVertexArray.cpp @ 1984

Last change on this file since 1984 was 1984, checked in by gah, 13 years ago

Clean up debugging/printing traces

File size: 3.6 KB
Line 
1
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-2006  Purdue Research Foundation
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 "RenderVertexArray.h"
38#include "Trace.h"
39#include <GL/gl.h>
40#include <stdio.h>
41
42using namespace std;
43
44RenderVertexArray::RenderVertexArray(int nverts, GLint size, GLenum type) :
45    m_usage(GL_STREAM_COPY),
46    m_nverts(nverts),
47    m_size(size),
48    m_type(type)
49{
50    switch(m_type) {
51    case GL_HALF_FLOAT_NV:
52        m_bytes_per_component = 2; break;
53    case GL_FLOAT:
54        m_bytes_per_component = sizeof(float); break;
55    default:
56        ERROR("unsupported RenderVertexArray type\n");
57        return;
58    }
59
60    // create the buffer object
61    glGenBuffersARB(1, &m_buffer);
62    glBindBufferARB(GL_PIXEL_PACK_BUFFER_EXT, m_buffer);
63    glBufferDataARB(GL_PIXEL_PACK_BUFFER_EXT,
64        m_nverts*m_size*m_bytes_per_component, 0, m_usage); // undefined data
65    glBindBufferARB(GL_PIXEL_PACK_BUFFER_EXT, 0);
66
67    // set equivalent image format
68    switch(m_size) {
69    case 1:
70        m_format = GL_LUMINANCE; break;
71    case 3:
72        m_format = GL_RGB; break;
73    case 4:
74        m_format = GL_RGBA; break;
75    default:
76        ERROR("unsupported RenderVertexArray size\n");
77        return;
78    }
79}
80
81RenderVertexArray::~RenderVertexArray()
82{
83    glDeleteBuffersARB(1, &m_buffer);
84}
85
86void
87RenderVertexArray::LoadData(void *data)
88{
89    // load data to buffer object
90    glBindBufferARB(GL_PIXEL_PACK_BUFFER_EXT, m_buffer);
91    glBufferDataARB(GL_PIXEL_PACK_BUFFER_EXT,
92                    m_nverts*m_size*m_bytes_per_component, data, m_usage);
93    glBindBufferARB(GL_PIXEL_PACK_BUFFER_EXT, 0);
94}
95
96void
97RenderVertexArray::Read(/*GLenum buffer,*/ int w, int h)
98{
99    // bind buffer object to pixel pack buffer
100    glBindBufferARB(GL_PIXEL_PACK_BUFFER_EXT, m_buffer);
101    // read from frame buffer to buffer object
102    //glReadBuffer(buffer); //crash
103    glReadPixels(0, 0, w, h, m_format, m_type, 0);
104
105    glBindBufferARB(GL_PIXEL_PACK_BUFFER_EXT, 0);
106}
107
108void
109RenderVertexArray::SetPointer(GLuint index)
110{
111    // bind buffer object to vertex array
112    glBindBufferARB(GL_ARRAY_BUFFER, m_buffer);
113    //glVertexAttribPointerARB(index, m_size, m_type, GL_FALSE, 0, 0);          //doesn't work
114    glVertexPointer(m_size, m_type, 0, 0);
115
116    glBindBufferARB(GL_ARRAY_BUFFER, 0);
117}
Note: See TracBrowser for help on using the repository browser.