2012-12-05 38 views
0

我正在學習opengl-tutorial.org,我使用的是QT Too,當我移動鼠標時,MVP矩陣被更新,它運行良好。但是,按下Alt鍵之前,屏幕並未顯示更改。我不知道爲什麼,這是QT還是OpenGL的問題?OpenGL - QT:鼠標移動事件效果沒有顯示

InitGL功能:

programId = this->createPorgram(":/glsl/VertexShader.glsl",":/glsl/FragmentShader.glsl"); 
if(programId==-1){ 
    emit blewUpApplication(); 
    return; 
} 

glClearColor(0.0,0.0,0.0,1.0); 
glEnable(GL_CULL_FACE); 
glEnable(GL_MULTISAMPLE); // Enables multisampling for a better look 
//glEnable(GL_BLEND); // Enable textures with alpha channel and alpha channels colors 
glEnable(GL_TEXTURE_2D); 
glEnable(GL_ALPHA_TEST); 
glEnable(GL_DEPTH_TEST); 
glDepthFunc(GL_LESS); 
// Bla bla bla, the rest does not matters. 

PaintGL功能:

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clears the screen and paint the bg with the color specified on glClearColor 

float currT = float(tmr.elapsed()/1000.0f); 
if((currT-lastTime)>=1.0f){ 
    lastTime+=1.0f; 
} 
deltaTime = (float)(currT-lastTime); 

glm::mat4 Project = this->getMatrixProjection(); 
glm::mat4 View = this->getMatrixView(); 
glm::mat4 Model = glm::mat4(1.0f); 
glm::mat4 MVP = Project * View * Model; 

glUseProgram(programId); 

GLuint MatrixId = glGetUniformLocation(programId,"MVP"); 
glUniformMatrix4fv(MatrixId,1,GL_FALSE,glm::value_ptr(MVP)); 

glActiveTexture(cursor_texture); 
glBindTexture(GL_TEXTURE_2D,cursor_texture); 

glUniform1i(glGetUniformLocation(programId,"textura"),cursor_texture); 
// Draw or triangles 

glEnableVertexAttribArray(0); 
glEnableVertexAttribArray(1); 

glBindBuffer(GL_ARRAY_BUFFER,trianglebuffer[0]); 
glVertexAttribPointer(0,3,GL_FLOAT,GL_FALSE,0,(void*)0); 


glBindBuffer(GL_ARRAY_BUFFER,trianglebuffer[1]); 
glVertexAttribPointer(1,2,GL_FLOAT,GL_FALSE,0,(void*)0); 


glDrawArrays(GL_TRIANGLES,0,12*3); 

glDisableVertexAttribArray(0); 
glDisableVertexAttribArray(1); 

glUseProgram(0); 

鼠標移動功能:

mousepos[0] = evt->x(); 
    mousepos[1] = evt->y(); 
    horzAn += mouseSpeed * deltaTime * float(this->width()/2 - mousepos[0]); 
    vertAn += mouseSpeed * deltaTime * float(this->height()/2 - mousepos[1]); 

    CmDirect = glm::vec3(cos(horzAn) * sin(vertAn), sin(vertAn), cos(horzAn) * cos(vertAn)); 
    glm::vec3 RgtVc = glm::vec3(sin(horzAn - 3.14f/2.0f),0,cos(horzAn - 3.14f/2.0f)); 
    UpVc = glm::cross(RgtVc,CmDirect); 

    Proj = glm::perspective(Zoom,4.0f/3.0f,0.1f,100.0f); 
    Vw = glm::lookAt(PosCm,PosCm+CmDirect,UpVc); 
    this->cursor().setPos(suppose_x,suppose_y); 

着色器:

// Fragment 
#version 330 core 
out vec3 color; 
in vec2 thcoord; 
in vec2 UV; 
uniform sampler2D d_textura; 
void main(){ 
color = texture(d_textura,thcoord).rgb; 
} 
// Vertex 
#version 330 core 
layout(location = 0) in vec3 vertexPosition_modelspace; 
layout(location = 1) in vec2 tcoord; 
out vec2 thcoord; 
uniform mat4 MVP; 
void main(){ 
vec4 v = vec4(vertexPosition_modelspace,1); 
thcoord=tcoord; 
gl_Position = MVP * v; 
} 

我知道,矩陣運算工作好,因爲效果改變,當我按下Alt鍵(僅一次發生)。

+0

是否有處理alt按鍵的代碼?或者它只是隨機的Alt鍵? – Xymostech

+0

我不知道,我只是運行應用程序,我沒有在應用程序的任何部分編寫一個alt鍵事件。 alt鍵並不重要,更重要的是爲什麼不採取更新MVP矩陣的效果。 – Spamdark

+0

OT:這是Qt(一個C++框架),而不是QT(QuickTime)。 – herophuong

回答

2

您應該將更新請求添加到您的mouseMoveEvent處理程序的末尾,以便知道該小部件需要重繪。更新()或repaint()應該做的伎倆。

+0

非常感謝你=) – Spamdark