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

Last change on this file since 2825 was 2800, checked in by ldelgass, 13 years ago

Don't pollute namespace with using directives in headers, also prefer using
std:: prefix to 'using namespace std' in implementation files.

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