使用纹理通过glUniform1i
来源:互联网 发布:淘宝紫砂壶哪家靠谱 编辑:程序博客网 时间:2024/06/09 17:45
问题 (Question)
in my texturing with GLSL program,just one texture bind to texture unit 0.such as following:
....................... glActiveTexture(GL_TEXTURE0); GLuint tid; glGenTextures(1, &tid); glBindTexture(GL_TEXTURE_2D, tid); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, img.width, img.height, 0, img.format, GL_UNSIGNED_BYTE, img.data); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
and then inform to frag shader through a uniformvariable(as setUniform does),here's name is "texture0" corresponding to my frag shader:
void CShader::setUniform( const char *name, int val ) { int loc = getUniformLocation(name); glAssert(); //note!,my own assertion wrapping around glGetError. if( loc >= 0 ) { glUniform1i(loc, val); } else { printf("Uniform: %s not found.\n",name); } glAssert(); //note!,my own assertion wrapping around glGetError. }
it is out of my expectation, it assert failed at last glAssert()
line,and giving me GL_INVALID_OPERATION
.what was wrong,doesn't my display card is supported for this operation?,it can't be,my OpenGL
version is 3.x. after few hours struggle, did a try commented the lastglAssert()
,but another assertion failed at my gldrawelements call. obviously, i absolutely think that cause gldrawelements
failed is glUniform1i
failed. can someone give me suggestion.
EDIT:
from datenwolf suggestion,here 's code for glAssert, it's very simple,just call glGetError once.
void _glAssert(const char * info, int line){ char const* sz_GL_INVALID_ENUM="GL_INVALID_ENUM"; char const* sz_GL_INVALID_VALUE="GL_INVALID_VALUE"; char const* sz_GL_INVALID_OPERATION="GL_INVALID_OPERATION"; char const* sz_GL_OUT_OF_MEMORY ="GL_OUT_OF_MEMORY";GLenum result; if((result=glGetError()) != GL_NO_ERROR ) { const char * sz_result=NULL; switch(result) { case GL_INVALID_ENUM: sz_result = sz_GL_INVALID_ENUM; break; case GL_INVALID_VALUE: sz_result = sz_GL_INVALID_VALUE; break; case GL_INVALID_OPERATION: sz_result = sz_GL_INVALID_OPERATION; break; case GL_OUT_OF_MEMORY: sz_result = sz_GL_OUT_OF_MEMORY; break; } _assert(sz_result,info,line); }}#define glAssert() \ _glAssert(__FILE__, __LINE__);
BTW,before i use glUniform1i call,everything works fine with my shader
attribute vec3 VertexPosition;varying vec3 Color;void main(){Color = vec3(0.5,0,0);gl_TexCoord[0] = gl_MultiTexCoord0;gl_Position = gl_ModelViewProjectionMatrix * vec4( VertexPosition, 1.0 );}uniform sampler2D texture0;varying vec3 Color;void main() { vec4 texColor = texture2D( texture0, gl_TexCoord[0].st);gl_FragColor= vec4(Color, 1.0)*texColor;}
i have stripped out many code in shader to simplify theproblem .
i'm Sorry that I don't describe this in detail. actually,my wrapper around opengl is very simple,no complex things with it, no multiple threads.
this is constructor used for loading texture and shader script into GPU,memory separately, then call "compileandlink" method to compile the shader script , and call setUniform immediately.
IEntity(const char * szvs,const char * szfs): m_shader(new CShader(szvs,szfs)){ ...........................}CCube::CCube():IEntity("v_simple.glsl","f_simple.glsl") { ................... IMAGE img; img.Load("texture.bmp"); img.ExpandPalette(); glActiveTexture(GL_TEXTURE0); GLuint tid; glGenTextures(1, &tid); glBindTexture(GL_TEXTURE_2D, tid); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, img.width, img.height, 0, img.format, GL_UNSIGNED_BYTE, img.data); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); m_shader->compileandlink(); m_shader->setUniform("texture0", 0);}
CShader::compileandlink method
bool CShader::compileandlink(){ if(m_vsscript == NULL || m_fsscript == NULL) return false; if(strlen(m_vsscript) <= 0 || strlen(m_fsscript) <= 0) return false; glShaderSource(m_vs, 1, (const GLchar **) &m_vsscript, NULL); glCompileShader(m_vs); glShaderSource(m_fs, 1, (const GLchar **) &m_fsscript, NULL); glCompileShader(m_fs); glAttachShader(m_program, m_vs); glAttachShader(m_program, m_fs); //glBindFragDataLocation(m_program, 0, "FragColor"); glLinkProgram(m_program); glAssert(); GLint status; glGetProgramiv( m_program, GL_LINK_STATUS, &status ); if( GL_FALSE == status ) { fprintf( stderr, "Failed to link shader program!\n" ); GLint logLen; glGetProgramiv(m_program, GL_INFO_LOG_LENGTH, &logLen); if( logLen > 0 ) { char * log = (char *)malloc(logLen); GLsizei written; glGetProgramInfoLog(m_program, logLen, &written, log); printf("Program log: \n%s", log); free(log); } } return true; }
CShader::setUniform method, as i say before,it have GL_INVALID_OPERATION result of "glGetError" after glGetUniformiv returned inside this method.
void CShader::setUniform( const char *name, int val ) { int loc = getUniformLocation(name); glAssert(); if( loc >= 0 ) { GLint outval=-1; glUniform1i(loc, val); /*int tmploc =glGetUniformLocation(m_program, name); glGetUniformiv(m_program, tmploc, &outval); printf("Value is %d\n", outval);*/ } else { printf("Uniform: %s not found.\n",name); } glAssert(); }
问题 (Question)
in my texturing with GLSL program,just one texture bind to texture unit 0.such as following:
....................... glActiveTexture(GL_TEXTURE0); GLuint tid; glGenTextures(1, &tid); glBindTexture(GL_TEXTURE_2D, tid); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, img.width, img.height, 0, img.format, GL_UNSIGNED_BYTE, img.data); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
and then inform to frag shader through a uniformvariable(as setUniform does),here's name is "texture0" corresponding to my frag shader:
void CShader::setUniform( const char *name, int val ) { int loc = getUniformLocation(name); glAssert(); //note!,my own assertion wrapping around glGetError. if( loc >= 0 ) { glUniform1i(loc, val); } else { printf("Uniform: %s not found.\n",name); } glAssert(); //note!,my own assertion wrapping around glGetError. }
it is out of my expectation, it assert failed at last
glAssert()
line,and giving meGL_INVALID_OPERATION
.what was wrong,doesn't my display card is supported for this operation?,it can't be,myOpenGL
version is 3.x. after few hours struggle, did a try commented the lastglAssert()
,but another assertion failed at my gldrawelements call. obviously, i absolutely think that causegldrawelements
failed isglUniform1i
failed. can someone give me suggestion.EDIT:
from datenwolf suggestion,here 's code for glAssert, it's very simple,just call glGetError once.
void _glAssert(const char * info, int line){ char const* sz_GL_INVALID_ENUM="GL_INVALID_ENUM"; char const* sz_GL_INVALID_VALUE="GL_INVALID_VALUE"; char const* sz_GL_INVALID_OPERATION="GL_INVALID_OPERATION"; char const* sz_GL_OUT_OF_MEMORY ="GL_OUT_OF_MEMORY";GLenum result; if((result=glGetError()) != GL_NO_ERROR ) { const char * sz_result=NULL; switch(result) { case GL_INVALID_ENUM: sz_result = sz_GL_INVALID_ENUM; break; case GL_INVALID_VALUE: sz_result = sz_GL_INVALID_VALUE; break; case GL_INVALID_OPERATION: sz_result = sz_GL_INVALID_OPERATION; break; case GL_OUT_OF_MEMORY: sz_result = sz_GL_OUT_OF_MEMORY; break; } _assert(sz_result,info,line); }}#define glAssert() \ _glAssert(__FILE__, __LINE__);
BTW,before i use glUniform1i call,everything works fine with my shader
attribute vec3 VertexPosition;varying vec3 Color;void main(){Color = vec3(0.5,0,0);gl_TexCoord[0] = gl_MultiTexCoord0;gl_Position = gl_ModelViewProjectionMatrix * vec4( VertexPosition, 1.0 );}uniform sampler2D texture0;varying vec3 Color;void main() { vec4 texColor = texture2D( texture0, gl_TexCoord[0].st);gl_FragColor= vec4(Color, 1.0)*texColor;}
i have stripped out many code in shader to simplify theproblem .
i'm Sorry that I don't describe this in detail. actually,my wrapper around opengl is very simple,no complex things with it, no multiple threads.
this is constructor used for loading texture and shader script into GPU,memory separately, then call "compileandlink" method to compile the shader script , and call setUniform immediately.
IEntity(const char * szvs,const char * szfs): m_shader(new CShader(szvs,szfs)){ ...........................}CCube::CCube():IEntity("v_simple.glsl","f_simple.glsl") { ................... IMAGE img; img.Load("texture.bmp"); img.ExpandPalette(); glActiveTexture(GL_TEXTURE0); GLuint tid; glGenTextures(1, &tid); glBindTexture(GL_TEXTURE_2D, tid); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, img.width, img.height, 0, img.format, GL_UNSIGNED_BYTE, img.data); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); m_shader->compileandlink(); m_shader->setUniform("texture0", 0);}
CShader::compileandlink method
bool CShader::compileandlink(){ if(m_vsscript == NULL || m_fsscript == NULL) return false; if(strlen(m_vsscript) <= 0 || strlen(m_fsscript) <= 0) return false; glShaderSource(m_vs, 1, (const GLchar **) &m_vsscript, NULL); glCompileShader(m_vs); glShaderSource(m_fs, 1, (const GLchar **) &m_fsscript, NULL); glCompileShader(m_fs); glAttachShader(m_program, m_vs); glAttachShader(m_program, m_fs); //glBindFragDataLocation(m_program, 0, "FragColor"); glLinkProgram(m_program); glAssert(); GLint status; glGetProgramiv( m_program, GL_LINK_STATUS, &status ); if( GL_FALSE == status ) { fprintf( stderr, "Failed to link shader program!\n" ); GLint logLen; glGetProgramiv(m_program, GL_INFO_LOG_LENGTH, &logLen); if( logLen > 0 ) { char * log = (char *)malloc(logLen); GLsizei written; glGetProgramInfoLog(m_program, logLen, &written, log); printf("Program log: \n%s", log); free(log); } } return true; }
CShader::setUniform method, as i say before,it have GL_INVALID_OPERATION result of "glGetError" after glGetUniformiv returned inside this method.
void CShader::setUniform( const char *name, int val ) { int loc = getUniformLocation(name); glAssert(); if( loc >= 0 ) { GLint outval=-1; glUniform1i(loc, val); /*int tmploc =glGetUniformLocation(m_program, name); glGetUniformiv(m_program, tmploc, &outval); printf("Value is %d\n", outval);*/ } else { printf("Uniform: %s not found.\n",name); } glAssert(); }
- 使用纹理通过glUniform1i
- OpenGL——glUniform1i 用法
- glActiveTexture glBindTexture glUniform1i关系图
- 怎样使用深度纹理
- Direct3D中使用纹理
- DirecX中使用纹理
- CUDA使用LinearMemory纹理
- 使用两个纹理
- CUDA 纹理的使用
- CUDA使用纹理内存
- opengl纹理的使用
- Direct3D 纹理使用
- Qt OpenGL 使用纹理
- 怎样使用深度纹理
- 深度纹理的使用
- OpenGL纹理使用
- CUDA使用纹理内存
- cuda纹理内存使用
- PHP静态成员变量和非静态成员变量
- Unity3D & Java 基于 Protobuf 通信实现
- Android——ViewPager多页面滑动切换以及动画效果
- Doxygen - 分析代码结构的好帮手
- PDU编码(非常经典)
- 使用纹理通过glUniform1i
- Android 进阶 教你打造 Android 中的 IOC 框架 【ViewInject】 (上)
- 2015中国“互联网+”千人论坛活动圆满闭幕
- python—BeautifulSoup学习总结
- 黑马程序员--网络编程知识点总结
- [DFS]部分和问题
- 杂谈:安哥拉 与 TypeScript拥抱在一起
- 图解TCP建立连接全过程
- 螺旋数组