簡體   English   中英

協助調試OpenGL glsl Shader或使用它的代碼

[英]Assistance in Debug OpenGL glsl Shader or Code using it

我正在為我的工作程序添加phong着色器。 基本上,在實現新的着色器之后,我的代碼將在以下過程中獲得“ Segmentation Fault:11”。

glDrawArrays(GL_TRIANGLES, 0, mCubes.getArrayNumberOfElements());

我知道元素的數量是正確的,因為它適用於我以前的簡單着色器。

這是我的頂點着色器:

// vertex shader
attribute vec4 vPosition;
attribute vec3 vNormal;
varying vec4 color; //vertex shader
// light and material properties
uniform vec4 AmbientProduct, DiffuseProduct, SpecularProduct; 
uniform mat4 ModelView;
//uniform mat4 Projection;
uniform vec4 LightPosition;
uniform float Shininess;
vec3 L, H, N, pos, E;
vec4 diffuse, specular, ambient;
float Kd, Ks;

void main()
{
// Transform vertex position into eye coordinates 
pos = (ModelView * vPosition).xyz; 
L = normalize( LightPosition.xyz - pos ); 
E = normalize( -pos );
H = normalize( L + E );
// Transform vertex normal into eye coordinates
N = normalize( ModelView*vec4(vNormal, 0.0) ).xyz;
// Compute terms in the illumination equation 
ambient = AmbientProduct;
Kd = max( dot(L, N), 0.0 );
diffuse = Kd*DiffuseProduct;
Ks = pow( max(dot(N, H), 0.0), Shininess );
specular = Ks * SpecularProduct;
if( dot(L, N) < 0.0 ) 
specular = vec4(0.0, 0.0, 0.0, 1.0); 

gl_Position = ModelView * vPosition;
color = ambient + diffuse + specular;
color.a = 1.0; 
        }

這是我的顯示函數,其中的代碼最終會收到錯誤:

void display(void) {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );

vector<float> cell = mCubes.getCell();

mat4 matrix = rot * scale(1.0/cell[0], 1.0/cell[1], 1.0/cell[2]) * translate(-cell[0]/2.0, -cell[1]/2.0, -cell[2]/2.0);
glUniformMatrix4fv(vShaderModelView, 1, GL_TRUE, matrix);

glDrawArrays(GL_TRIANGLES, 0, mCubes.getArrayNumberOfElements());


glutSwapBuffers();
glFlush();
}

這是我的初始化函數,主要用於設置着色器並與着色器進行交互:

void init() {
// Create a vertex array object
GLuint vao;
#ifdef __APPLE__
glGenVertexArraysAPPLE( 1, &vao );
glBindVertexArrayAPPLE( vao );
#else
glGenVertexArrays( 1, &vao );
glBindVertexArray( vao );
#endif

// Create and initialize a buffer object
GLuint buffer;

glGenBuffers(1, &buffer);
glBindBuffer(GL_ARRAY_BUFFER, buffer);
glBufferData( GL_ARRAY_BUFFER,
             mCubes.getDisplayArraySize() + mCubes.getDisplayArraySize()*3,                NULL, GL_STATIC_DRAW );
GLintptr offset = 0;
glBufferSubData(GL_ARRAY_BUFFER, offset, mCubes.getDisplayArraySize(), mCubes.getDisplayArray());
offset+= mCubes.getDisplayArraySize();

glBufferSubData(GL_ARRAY_BUFFER, offset, mCubes.getDisplayArraySize(), mCubes.getNormalVector());

// Load shaders and use the resulting shader program
string evname = "PROTCAD3DIR";
string path = PCGeneralIO::getEnvironmentVariable(evname);

path += "/data/shaders/";
#ifdef __APPLE__
string vshadername = path + "kw_vshader1_mac.glsl";
string fshadername = path + "kw_fshader1_mac.glsl";
//#else
//  string vshadername = path + "kw_vshader1.glsl";
//  string fshadername = path + "kw_fshader1.glsl";
#endif

GLuint program = InitShader( vshadername.c_str(), fshadername.c_str() );

glUseProgram(program);
// Initialize the vertex position attribute from the vertex shader    
GLuint vShaderPosition = glGetAttribLocation(program, "vPosition");
glEnableVertexAttribArray(vShaderPosition);
glVertexAttribPointer(vShaderPosition, 4, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(0));

GLuint vShaderNormal = glGetAttribLocation(program, "vNormal");
glEnableVertexAttribArray(vShaderNormal);
//glVertexAttribPointer(vShaderPosition, 4, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(offset)); //this was the ORIGINAL PROBLEM, now commented out and below is solution
  glVertexAttribPointer(vShaderNormal, 4, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(offset));


vShaderModelView = glGetUniformLocation(program, "ModelView");
vShaderLightPosition = glGetUniformLocation(program, "LightPosition");

vShaderAmbientProduct = glGetUniformLocation(program, "AmbientProduct");
vShaderDiffuseProduct = glGetUniformLocation(program, "DiffuseProduct");
vShaderSpecularProduct = glGetUniformLocation(program, "SpecularProduct");
vShaderShininess = glGetUniformLocation(program, "SpecularProduct");
glEnable( GL_DEPTH_TEST );

vec4 light = vec4(0.5,1.5,1.0,0.0);
glUniform4fv(vShaderLightPosition, 1, light);
vec4 amb = vec4(1.0f,0.0f,0.20f,1.0f);
glUniform4fv(vShaderAmbientProduct, 1, amb);
vec4 diff = vec4(0.5f,0.5f,0.5f,1.0f);
glUniform4fv(vShaderDiffuseProduct, 1, diff);
vec4 spec = vec4(0.80f,0.80f,0.80f,1.0f);
glUniform4fv(vShaderSpecularProduct, 1, spec);
float shin = 6.0f;
glUniform1f(vShaderShininess,shin);


glClearColor(.2, .2, .2, 1); /* Grey background */
}

如有任何疑問,請隨時提出,我將詳細說明。 我覺得頂點着色器本身有問題,或者我與着色器交互的方式有些奇怪。 任何幫助或建議都可以接受!

編輯:::(已編輯代碼以反映解決方案)問題出在第二個問題中:

glVertexAttribPointer(vShaderPosition, 4, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(offset));

內容應為:

glVertexAttribPointer(vShaderNormal, 4, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(offset));

並且是一個愚蠢的復制/粘貼錯誤。 但是,成品仍然看起來不正確:

![在旋轉0時,它似乎是全彩色的] [1]

http://i.stack.imgur.com/CKJ3f.png

![一點點旋轉揭示了一些奇怪的行為] [2]

http://i.stack.imgur.com/kyRfI.png

![甚至更多的旋轉會導致您拉出頭發] [3]

i.stack.imgur.com/lYOzK.png

![然后變白了,你知道我搞砸了!] [4]

i.stack.imgur.com/FZcqF.png

因此,當您旋轉時,顏色會擰緊並變成白色,黑色,有圖案的東西,但這顯然是不正確的。

編輯:::這是我試圖“糾正”通過vNormal傳遞錯誤數量的值的問題:

void init() {
// Create a vertex array object
GLuint vao;
#ifdef __APPLE__
glGenVertexArraysAPPLE( 1, &vao );
glBindVertexArrayAPPLE( vao );
#else
glGenVertexArrays( 1, &vao );
glBindVertexArray( vao );
#endif

// Create and initialize a buffer object
GLuint buffer;
realVec *normArray = new realVec[mCubes.getNormalArraySize()];//vec4 array compared to vec3 array     
normArray = mCubes.getNormalVector(); // new array of normals

for(int i=0; i<mCubes.getArrayNumberOfElements();i++){
    printf("Normal at %d  is %f \n",i,normArray[i][0]); //to print normals
    printf("Normal at %d  is %f \n",i,normArray[i][1]); //to print normals
    printf("Normal at %d  is %f \n",i,normArray[i][2]); //to print normals
}

glGenBuffers(1, &buffer);
glBindBuffer(GL_ARRAY_BUFFER, buffer);
glBufferData( GL_ARRAY_BUFFER,
             mCubes.getDisplayArraySize() + mCubes.getNormalArraySize(),                NULL, GL_STATIC_DRAW ); //Changed size for vec3 array of normals
GLintptr offset = 0;
glBufferSubData(GL_ARRAY_BUFFER, offset, mCubes.getDisplayArraySize(), mCubes.getDisplayArray());
offset+= mCubes.getDisplayArraySize();

glBufferSubData(GL_ARRAY_BUFFER, offset, mCubes.getNormalArraySize(), normArray);

// Load shaders and use the resulting shader program
string evname = "PROTCAD3DIR";
string path = PCGeneralIO::getEnvironmentVariable(evname);

path += "/data/shaders/";
#ifdef __APPLE__
string vshadername = path + "kw_vshader1_mac.glsl";
string fshadername = path + "kw_fshader1_mac.glsl";
//#else
//  string vshadername = path + "kw_vshader1.glsl";
//  string fshadername = path + "kw_fshader1.glsl";
#endif

GLuint program = InitShader( vshadername.c_str(), fshadername.c_str() );

glUseProgram(program);
//offset =0;
// Initialize the vertex position attribute from the vertex shader    
GLuint vShaderPosition = glGetAttribLocation(program, "vPosition");
glEnableVertexAttribArray(vShaderPosition);
glVertexAttribPointer(vShaderPosition, 4, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(0));

GLuint vShaderNormal = glGetAttribLocation(program, "vNormal");
glEnableVertexAttribArray(vShaderNormal);
glVertexAttribPointer(vShaderNormal, 3, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(offset));


//vShaderMatrix = glGetUniformLocation(program, "vMatrix");
//vShaderColor = glGetUniformLocation(program, "vColor")
vShaderModelView = glGetUniformLocation(program, "ModelView");
vShaderLightPosition = glGetUniformLocation(program, "LightPosition");
//vShaderProjection = glGetUniformLocation(program, "Projection");

vShaderAmbientProduct = glGetUniformLocation(program, "AmbientProduct");
vShaderDiffuseProduct = glGetUniformLocation(program, "DiffuseProduct");
vShaderSpecularProduct = glGetUniformLocation(program, "SpecularProduct");
vShaderShininess = glGetUniformLocation(program, "SpecularProduct");
glEnable( GL_DEPTH_TEST );

vec4 light = vec4(0.5,1.5,1.0,0.0);
glUniform4fv(vShaderLightPosition, 1, light);
vec4 amb = vec4(1.0f,0.0f,0.20f,1.0f);
glUniform4fv(vShaderAmbientProduct, 1, amb);
vec4 diff = vec4(0.5f,0.5f,0.5f,1.0f);
glUniform4fv(vShaderDiffuseProduct, 1, diff);
vec4 spec = vec4(0.80f,0.80f,0.80f,1.0f);
glUniform4fv(vShaderSpecularProduct, 1, spec);
float shin = 6.0f;
glUniform1f(vShaderShininess,shin);


glClearColor(.2, .2, .2, 1); /* Grey background */
}

我是否應該更改光,環境,鏡面和漫反射屬性? 我不確定是什么問題。

您使用以下代碼傳遞vNormal屬性數據

glVertexAttribPointer(vShaderNormal, 4, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(offset));

這表明法線具有4個分量,而在頂點着色器中將其聲明為

attribute vec3 vNormal;

如果法線被誤解,則可能與您的問題有關。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM