[2798] | 1 | /* -*- mode: c++; c-basic-offset: 4; indent-tabs-mode: nil -*- */ |
---|
[953] | 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 | * |
---|
[3502] | 31 | * Copyright (c) 2004-2013 HUBzero Foundation, LLC |
---|
[953] | 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 | */ |
---|
[2837] | 37 | #include <stdio.h> |
---|
| 38 | |
---|
| 39 | #include <GL/glew.h> |
---|
| 40 | #include <GL/gl.h> |
---|
| 41 | |
---|
[953] | 42 | #include "RenderVertexArray.h" |
---|
[1984] | 43 | #include "Trace.h" |
---|
[953] | 44 | |
---|
[4889] | 45 | using namespace nv; |
---|
| 46 | |
---|
[953] | 47 | RenderVertexArray::RenderVertexArray(int nverts, GLint size, GLenum type) : |
---|
[3362] | 48 | _usage(GL_STREAM_COPY_ARB), |
---|
[2837] | 49 | _nverts(nverts), |
---|
| 50 | _size(size), |
---|
| 51 | _type(type) |
---|
[953] | 52 | { |
---|
[2837] | 53 | switch (_type) { |
---|
[953] | 54 | case GL_HALF_FLOAT_NV: |
---|
[2953] | 55 | _bytesPerComponent = 2; |
---|
| 56 | break; |
---|
[953] | 57 | case GL_FLOAT: |
---|
[2953] | 58 | _bytesPerComponent = sizeof(float); |
---|
| 59 | break; |
---|
[953] | 60 | default: |
---|
[3452] | 61 | ERROR("unsupported RenderVertexArray type"); |
---|
[953] | 62 | return; |
---|
| 63 | } |
---|
| 64 | |
---|
| 65 | // create the buffer object |
---|
[2837] | 66 | glGenBuffersARB(1, &_buffer); |
---|
[2921] | 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); |
---|
[953] | 72 | |
---|
| 73 | // set equivalent image format |
---|
[2837] | 74 | switch(_size) { |
---|
[953] | 75 | case 1: |
---|
[2953] | 76 | _format = GL_LUMINANCE; |
---|
| 77 | break; |
---|
[953] | 78 | case 3: |
---|
[2953] | 79 | _format = GL_RGB; |
---|
| 80 | break; |
---|
[953] | 81 | case 4: |
---|
[2953] | 82 | _format = GL_RGBA; |
---|
| 83 | break; |
---|
[953] | 84 | default: |
---|
[3452] | 85 | ERROR("unsupported RenderVertexArray size"); |
---|
[953] | 86 | return; |
---|
| 87 | } |
---|
| 88 | } |
---|
| 89 | |
---|
| 90 | RenderVertexArray::~RenderVertexArray() |
---|
| 91 | { |
---|
[2837] | 92 | glDeleteBuffersARB(1, &_buffer); |
---|
[953] | 93 | } |
---|
| 94 | |
---|
| 95 | void |
---|
[2921] | 96 | RenderVertexArray::loadData(void *data) |
---|
[953] | 97 | { |
---|
| 98 | // load data to buffer object |
---|
[2921] | 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); |
---|
[953] | 104 | } |
---|
| 105 | |
---|
| 106 | void |
---|
[2921] | 107 | RenderVertexArray::read(int w, int h) |
---|
[953] | 108 | { |
---|
| 109 | // bind buffer object to pixel pack buffer |
---|
[2921] | 110 | glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, _buffer); |
---|
| 111 | |
---|
[953] | 112 | // read from frame buffer to buffer object |
---|
[2837] | 113 | glReadPixels(0, 0, w, h, _format, _type, 0); |
---|
[953] | 114 | |
---|
[2921] | 115 | glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, 0); |
---|
[953] | 116 | } |
---|
| 117 | |
---|
| 118 | void |
---|
[2921] | 119 | RenderVertexArray::setPointer(GLuint index) |
---|
[953] | 120 | { |
---|
| 121 | // bind buffer object to vertex array |
---|
[2921] | 122 | glBindBufferARB(GL_ARRAY_BUFFER_ARB, _buffer); |
---|
[2837] | 123 | //glVertexAttribPointerARB(index, _size, _type, GL_FALSE, 0, 0); //doesn't work |
---|
| 124 | glVertexPointer(_size, _type, 0, 0); |
---|
[953] | 125 | |
---|
[2921] | 126 | glBindBufferARB(GL_ARRAY_BUFFER_ARB, 0); |
---|
[953] | 127 | } |
---|