PDA

View Full Version : OpenGL Volume Rendering of 3D Array



luckymath
07-12-2017, 05:28 PM
I am trying to do some volume rendering based on the code at this website https://www.codeproject.com/Articles/352270/Getting-started-with-Volume-Rendering

What I am doing right now is that I have some sample 3D data (the array chBuffer is essentially a 4*4*4 test data array) and I am converting that into an OpenGL 3D Texture. After that, I am attempting to render this texture using other OpenGL commands. However, when I run the application, the screen is completely black with something showing up. Is there anything specific I am doing wrong that should fix this problem? My code is attached at the bottom.


#include <glut.h>
#include <GL/gl.h>
#include <gl/glext.h>
#include <stdio.h>
#include <Windows.h>
GLfloat dOrthoSize = 1.0f;
GLuint mu3DTex;
#define MAP_3DTEXT( TexIndex ) \
glTexCoord3f(0.0f, 0.0f, ((float)TexIndex+1.0f)/2.0f); \
glVertex3f(-dOrthoSize,-dOrthoSize,TexIndex);\
glTexCoord3f(1.0f, 0.0f, ((float)TexIndex+1.0f)/2.0f); \
glVertex3f(dOrthoSize,-dOrthoSize,TexIndex);\
glTexCoord3f(1.0f, 1.0f, ((float)TexIndex+1.0f)/2.0f); \
glVertex3f(dOrthoSize,dOrthoSize,TexIndex);\
glTexCoord3f(0.0f, 1.0f, ((float)TexIndex+1.0f)/2.0f); \
glVertex3f(-dOrthoSize,dOrthoSize,TexIndex);
void main(int argc, char **argv)
{

glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(500, 500);
glutCreateWindow("Name");


char* chBuffer = new char[64];
for (int i = 0; i < 64; ++i)
chBuffer[i] = ((i + (i / 8)) % 2) * 128 + 127;


char* chRGBABuffer = new char[256];
glGenTextures(1, (GLuint*)&mu3DTex);



glBindTexture(GL_TEXTURE_3D, mu3DTex);
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_BORDER);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);



for (int nIndx = 0; nIndx < 64; ++nIndx)
{
chRGBABuffer[nIndx * 4] = chBuffer[nIndx];
chRGBABuffer[nIndx * 4 + 1] = chBuffer[nIndx];
chRGBABuffer[nIndx * 4 + 2] = chBuffer[nIndx];
chRGBABuffer[nIndx * 4 + 3] = chBuffer[nIndx];
}

glTexImage3D(GL_TEXTURE_3D, 0, GL_RGBA, 4, 4, 4, 0,
GL_RGBA, GL_UNSIGNED_BYTE, (GLvoid *)chRGBABuffer);
glBindTexture(GL_TEXTURE_3D, 0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glEnable(GL_ALPHA_TEST);
glAlphaFunc(GL_GREATER, 0.03f);

glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

glMatrixMode(GL_TEXTURE);
glLoadIdentity();


glEnable(GL_TEXTURE_3D);
glBindTexture(GL_TEXTURE_3D, mu3DTex);

for (float fIndx = -1.0f; fIndx <= 1.0f; fIndx += 0.003f)
{
glBegin(GL_QUADS);
MAP_3DTEXT(fIndx);
glEnd();
}
system("pause");


}