[英]OpenGL - render 3D texture
Idea: I have been trying to develop code which can take several (ten) slices of 2D images and render them as a 3D texture. 想法:我一直在尝试开发代码,这些代码可以拍摄几(十)张2D图像并将它们渲染为3D纹理。 I have so far used
glTexImage3D
and glTexSubImage3D
upon suggestions from my previous post here: OpenGL - 'glTexSubImage3D': identifier not found 到目前为止,根据我以前的帖子在这里的建议,我已经使用
glTexImage3D
和glTexSubImage3D
: OpenGL-'glTexSubImage3D':找不到标识符
I have been based my work off of NeHe's texture mapping tutorial here: http://nehe.gamedev.net/tutorial/lesson_06_texturing_update/47002/ 我的工作基于NeHe的纹理贴图教程,网址为: http : //nehe.gamedev.net/tutorial/lesson_06_texturing_update/47002/
The flow of function calls in the NeHe's tutorial in main()
proceeds like this: CreateGLWindow()
, InitGL()
, LoadGLTextures()
, DrawGLScene()
. NeHe的
main()
教程中的函数调用流程如下: CreateGLWindow()
, InitGL()
, LoadGLTextures()
, DrawGLScene()
。 I have only made changes to the code commencing upwards from DrawGLScene()
while, everything below that function is the same as in my code. 我仅对从
DrawGLScene()
开始向上的代码进行了更改,而该函数下方的所有内容与我的代码相同。
Problem: Everything in the code seemingly looks right but, there is nothing being rendered on the screen. 问题:代码中的所有内容看似正确,但是屏幕上没有呈现任何内容。 I have spent two days on this trying to get it to work to no avail.
我花了两天时间试图使它无济于事。 What am I missing?
我想念什么? Is there something that I am doing incorrectly?
我做错了什么吗?
EDITED CODE 编辑代码
#include "windows.h"
#include "stdio.h"
#include "gl\gl.h"
#include "gl\glu.h"
#include "GLext.h"
#include "SOIL.h"
HDC hDC=NULL;
HGLRC hRC=NULL;
HWND hWnd=NULL;
HINSTANCE hInstance;
bool keys[256];
bool active=TRUE;
bool fullscreen=TRUE;
GLfloat xrot;
GLfloat yrot;
GLfloat zrot;
GLuint m_nTexId;
unsigned char tex;
int h = 1024;
int w = 256;
int slices = 10;
GLfloat dOrthoSize = 1.0f;
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
// EDIT HERE
PFNGLTEXIMAGE3DPROC TexImage3D;
PFNGLTEXSUBIMAGE3DPROC TexSubImage3D;
PFNGLCOPYTEXSUBIMAGE3DPROC CopyTexSubImage3D;
int LoadGLTextures()
{
glGenTextures(1,(GLuint*)&m_nTexId );
if(m_nTexId == 0)
return false;
glBindTexture( GL_TEXTURE_3D, m_nTexId );
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);
return true;
}
GLvoid ReSizeGLScene(GLsizei width, GLsizei height)
{
if (height==0)
{
height=1;
}
glViewport(0,0,width,height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0f,(GLfloat)width/(GLfloat)height,0.1f,100.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
int InitGL(GLvoid)
{
if (!LoadGLTextures())
{
return FALSE;
}
glEnable(GL_TEXTURE_3D);
glShadeModel(GL_SMOOTH);
glClearColor(0.0f, 0.0f, 0.0f, 0.5f);
glClearDepth(1.0f);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
// EDIT HERE
TexImage3D = (PFNGLTEXIMAGE3DPROC) wglGetProcAddress("glTexImage3D");
TexSubImage3D = (PFNGLTEXSUBIMAGE3DPROC) wglGetProcAddress("glTexSubImage3D");
CopyTexSubImage3D = (PFNGLCOPYTEXSUBIMAGE3DPROC) wglGetProcAddress("glCopyTexSubImage3D");
return TRUE;
}
#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);
int DrawGLScene(GLvoid)
{
PFNGLTEXIMAGE3DPROC glTexImage3D = (PFNGLTEXIMAGE3DPROC) wglGetProcAddress("glTexImage3D");
PFNGLTEXSUBIMAGE3DPROC glTexSubImage3D = (PFNGLTEXSUBIMAGE3DPROC) wglGetProcAddress("glTexSubImage3D");
PFNGLCOPYTEXSUBIMAGE3DPROC glCopyTexSubImage3D = (PFNGLCOPYTEXSUBIMAGE3DPROC) wglGetProcAddress("glCopyTexSubImage3D");
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
//glEnable( GL_ALPHA_TEST );
//glAlphaFunc( GL_GREATER, 0.2f );
//glEnable(GL_BLEND);
//glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
gluLookAt(0,0,-300,0,0,1,0,1,0);
glEnable(GL_TEXTURE_3D);
glBindTexture( GL_TEXTURE_3D, m_nTexId );
TexImage3D(GL_TEXTURE_3D, 0, GL_RGBA, w, h , slices, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL );
glTranslatef( 0.0f, 0.0f, 100.0f );
glBegin(GL_QUADS);
tex = (unsigned char)SOIL_load_image("Data/PA_170090.png", &w, &h, NULL, 0);
TexSubImage3D(GL_TEXTURE_3D, 0, 0, 0, 1, w, h, 1, GL_RGBA, GL_UNSIGNED_BYTE, (GLvoid*) tex);
MAP_3DTEXT( 0.0f );
glEnd();
glBegin(GL_QUADS);
tex = (unsigned char) SOIL_load_image("Data/PA_170091.png", &w, &h, NULL, 0);
TexSubImage3D(GL_TEXTURE_3D, 0, 0, 0, 2, w, h, 1, GL_RGBA, GL_UNSIGNED_BYTE, (GLvoid*) tex);
MAP_3DTEXT( 0.1f );
glEnd();
glBegin(GL_QUADS);
tex = (unsigned char)SOIL_load_image("Data/PA_170092.png", &w, &h, NULL, 0);
TexSubImage3D(GL_TEXTURE_3D, 0, 0, 0, 3, w, h, 1, GL_RGBA, GL_UNSIGNED_BYTE, (GLvoid*) tex);
MAP_3DTEXT( 0.2f );
glEnd();
glBegin(GL_QUADS);
tex = (unsigned char)SOIL_load_image("Data/PA_170093.png", &w, &h, NULL, 0);
TexSubImage3D(GL_TEXTURE_3D, 0, 0, 0, 4, w, h, 1, GL_RGBA, GL_UNSIGNED_BYTE, (GLvoid*) tex);
MAP_3DTEXT( 0.3f );
glEnd();
glBegin(GL_QUADS);
tex = (unsigned char)SOIL_load_image("Data/PA_170094.png", &w, &h, NULL, 0);
TexSubImage3D(GL_TEXTURE_3D, 0, 0, 0, 5, w, h, 1, GL_RGBA, GL_UNSIGNED_BYTE, (GLvoid*) tex);
MAP_3DTEXT( 0.4f );
glEnd();
glBegin(GL_QUADS);
tex = (unsigned char)SOIL_load_image("Data/PA_170095.png", &w, &h, NULL, 0);
TexSubImage3D(GL_TEXTURE_3D, 0, 0, 0, 6, w, h, 1, GL_RGBA, GL_UNSIGNED_BYTE, (GLvoid*) tex);
MAP_3DTEXT( 0.5f );
glEnd();
glBegin(GL_QUADS);
tex = (unsigned char) SOIL_load_image("Data/PA_170096.png", &w, &h, NULL, 0);
TexSubImage3D(GL_TEXTURE_3D, 0, 0, 0, 7, w, h, 1, GL_RGBA, GL_UNSIGNED_BYTE, (GLvoid*) tex);
MAP_3DTEXT( 0.6f );
glEnd();
glBegin(GL_QUADS);
tex = (unsigned char) SOIL_load_image("Data/PA_170097.png", &w, &h, NULL, 0);
TexSubImage3D(GL_TEXTURE_3D, 0, 0, 0, 8, w, h, 1, GL_RGBA, GL_UNSIGNED_BYTE, (GLvoid*) tex);
MAP_3DTEXT( 0.7f );
glEnd();
glBegin(GL_QUADS);
tex = (unsigned char) SOIL_load_image("Data/PA_170098.png", &w, &h, NULL, 0);
TexSubImage3D(GL_TEXTURE_3D, 0, 0, 0, 9, w, h, 1, GL_RGBA, GL_UNSIGNED_BYTE, (GLvoid*) tex);
MAP_3DTEXT( 0.8f );
glEnd();
glBegin(GL_QUADS);
tex = (unsigned char) SOIL_load_image("Data/PA_170099.png", &w, &h, NULL, 0);
TexSubImage3D(GL_TEXTURE_3D, 0, 0, 0, 10, w, h, 1, GL_RGBA, GL_UNSIGNED_BYTE, (GLvoid*) tex);
MAP_3DTEXT( 0.9f );
glEnd();
return TRUE;
}
Look at the function call TexSubImage3D, the argument depth is always 1, and you are incrementing only zoffset. 查看函数调用TexSubImage3D,参数深度始终为1,并且仅递增zoffset。 Please keep the zoffset 0 and provide the depth/layer values from 8th argument (depth).
请保持zoffset为0,并提供第8个参数(depth)的深度/图层值。
TexSubImage3D( enum target, int level, int xoffset, int yoffset, int zoffset, sizei width, sizei height, sizei depth, enum format, enum type, const void *data );
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.