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