Segmentation fault when cursor enters window
Salvador Girbau <[email protected]> Sun, 6 Oct 2013 21:55:47 +0100
| Newsgroups | gmane.comp.python.opengl.user |
|---|---|
| Message-ID | <CAP1-aDGo_EFK=BG043Q=6StsgyE1K-gQ5zXv54i5XxksKYQg+g@mail.gmail.com> |
Hello,
I have recently started to follow the PyOpenGL shaders & lighting tutorials
at
http://pyopengl.sourceforge.net/context/tutorials/index.xhtml
and, regarding tutorial #5, "Diffuse, Ambient, Directional Lighting",
http://pyopengl.sourceforge.net/context/tutorials/shader_5.xhtml
I copied&pasted the code from the web page to run it, and it seems to run
fine, but I'm getting a segmentation fault when I move the mouse cursor, at
the moment it enters the window.
In order to narrow the problem down, I copied the code for the simpler
tutorial #1, "First steps (Basic Geometry)",
http://pyopengl.sourceforge.net/context/tutorials/shader_1.xhtml
but, seeing that tutorial #5 has a phong_weightCalc function in the vertex
shader, I added a "dummy" function at the beginning of tutorial#1's vertex
shader:
float dummy(float x) {
return x;
}
and a call inside the void main() of the shader:
float a = dummy(1);
and the problem is reproduced. Without these additions, tutorial#1 runs
without problem. I am attaching both failing codes (tutorial#5, and the
modified tutorial#1).
If somebody could try and run these on their machine, and tell me if the
problem is NOT reproduced, then I will check the configuration in my
machine. Otherwise this is a bug report.
My uname -a says
Linux bartleby 3.2.0-31-generic #50-Ubuntu SMP Fri Sep 7 16:16:45 UTC 2012
x86_64 x86_64 x86_64 GNU/Linux
python --version
Python 2.7.3
I installed these today,
PyOpenGL==3.1.0a3
PyOpenGL-accelerate==3.1.0a3
OpenGLContext==2.2.0a2
PyVRML97==2.3.0a2
PyDispatcher==2.0.3
I am using CInnamon as the desktop manager, but I tried with Gnome Classic
and the problem is the same.
Input is welcome. Thanks!
S.
------------------------------------------------------------------------------
October Webinars: Code for Performance
Free Intel webinars can help you accelerate application performance.
Explore tips for MPI, OpenMP, advanced profiling, and more. Get the most from
the latest Intel processors and coprocessors. See abstracts and register >
http://pubads.g.doubleclick.net/gampad/clk?id=60134791&iu=/4140/ostg.clktrk
_______________________________________________
PyOpenGL Homepage
http://pyopengl.sourceforge.net
_______________________________________________
PyOpenGL-Users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/pyopengl-users
tutorial5.py
(application/octet-stream, 4.6 KB)
#!/usr/bin/python
from OpenGLContext import testingcontext
BaseContext = testingcontext.getInteractive()
from OpenGL.GL import *
from OpenGL.arrays import vbo
from OpenGLContext.arrays import *
from OpenGL.GL import shaders
from OpenGLContext.events.timer import Timer
class TestContext( BaseContext ):
"""Demonstrates use of attribute types in GLSL
"""
def OnInit( self ):
"""Initialize the context"""
phong_weightCalc = """
float phong_weightCalc(
in vec3 light_pos, // light position
in vec3 frag_normal // geometry normal
) {
// returns vec2( ambientMult, diffuseMult )
float n_dot_pos = max( 0.0, dot(
frag_normal, light_pos
));
return n_dot_pos;
}
"""
vertex = shaders.compileShader( phong_weightCalc +
"""
uniform vec4 Global_ambient;
uniform vec4 Light_ambient;
uniform vec4 Light_diffuse;
uniform vec3 Light_location;
uniform vec4 Material_ambient;
uniform vec4 Material_diffuse;
attribute vec3 Vertex_position;
attribute vec3 Vertex_normal;
varying vec4 baseColor;
void main() {
gl_Position = gl_ModelViewProjectionMatrix * vec4(
Vertex_position, 1.0
);
vec3 EC_Light_location = gl_NormalMatrix * Light_location;
float diffuse_weight = phong_weightCalc(
normalize(EC_Light_location),
normalize(gl_NormalMatrix * Vertex_normal)
);
baseColor = clamp(
(
// global component
(Global_ambient * Material_ambient)
// material's interaction with light's contribution
// to the ambient lighting...
+ (Light_ambient * Material_ambient)
// material's interaction with the direct light from
// the light.
+ (Light_diffuse * Material_diffuse * diffuse_weight)
), 0.0, 1.0);
}""", GL_VERTEX_SHADER)
fragment = shaders.compileShader("""
varying vec4 baseColor;
void main() {
gl_FragColor = baseColor;
}
""", GL_FRAGMENT_SHADER)
self.shader = shaders.compileProgram(vertex,fragment)
self.vbo = vbo.VBO(
array( [
[ -1, 0, 0, -1,0,1],
[ 0, 0, 1, -1,0,2],
[ 0, 1, 1, -1,0,2],
[ -1, 0, 0, -1,0,1],
[ 0, 1, 1, -1,0,2],
[ -1, 1, 0, -1,0,1],
[ 0, 0, 1, -1,0,2],
[ 1, 0, 1, 1,0,2],
[ 1, 1, 1, 1,0,2],
[ 0, 0, 1, -1,0,2],
[ 1, 1, 1, 1,0,2],
[ 0, 1, 1, -1,0,2],
[ 1, 0, 1, 1,0,2],
[ 2, 0, 0, 1,0,1],
[ 2, 1, 0, 1,0,1],
[ 1, 0, 1, 1,0,2],
[ 2, 1, 0, 1,0,1],
[ 1, 1, 1, 1,0,2],
],'f')
)
for uniform in (
'Global_ambient',
'Light_ambient','Light_diffuse','Light_location',
'Material_ambient','Material_diffuse',
):
location = glGetUniformLocation( self.shader, uniform )
if location in (None,-1):
print 'Warning, no uniform: %s'%( uniform )
setattr( self, uniform+ '_loc', location )
for attribute in (
'Vertex_position','Vertex_normal',
):
location = glGetAttribLocation( self.shader, attribute )
if location in (None,-1):
print 'Warning, no attribute: %s'%( uniform )
setattr( self, attribute+ '_loc', location )
def Render( self, mode = None):
"""Render the geometry for the scene."""
BaseContext.Render( self, mode )
glUseProgram(self.shader)
try:
self.vbo.bind()
try:
glUniform4f( self.Global_ambient_loc, .3,.05,.05,.1 )
glUniform4f( self.Light_ambient_loc, .2,.2,.2, 1.0 )
glUniform4f( self.Light_diffuse_loc, 1,1,1,1 )
glUniform3f( self.Light_location_loc, 2,2,10 )
glUniform4f( self.Material_ambient_loc, .2,.2,.2, 1.0 )
glUniform4f( self.Material_diffuse_loc, 1,1,1, 1 )
glEnableVertexAttribArray( self.Vertex_position_loc )
glEnableVertexAttribArray( self.Vertex_normal_loc )
stride = 6*4
glVertexAttribPointer(
self.Vertex_position_loc,
3, GL_FLOAT,False, stride, self.vbo
)
glVertexAttribPointer(
self.Vertex_normal_loc,
3, GL_FLOAT,False, stride, self.vbo+12
)
glDrawArrays(GL_TRIANGLES, 0, 18)
finally:
self.vbo.unbind()
glDisableVertexAttribArray( self.Vertex_position_loc )
glDisableVertexAttribArray( self.Vertex_normal_loc )
finally:
glUseProgram( 0 )
if __name__ == "__main__":
TestContext.ContextMainLoop()
tutorial1_plus_dummy.py
(application/octet-stream, 1.7 KB)
#!/usr/bin/python
from OpenGLContext import testingcontext
BaseContext = testingcontext.getInteractive()
from OpenGL.GL import *
from OpenGL.arrays import vbo
from OpenGLContext.arrays import *
from OpenGL.GL import shaders
class TestContext( BaseContext ):
"""Creates a simple vertex shader..."""
def OnInit( self ):
VERTEX_SHADER = shaders.compileShader("""#version 330
float dummy(float x) {
return x;
}
void main() {
float a = dummy(1);
gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
}""", GL_VERTEX_SHADER)
FRAGMENT_SHADER = shaders.compileShader("""#version 330
void main() {
gl_FragColor = vec4( 0, 1, 0, 1 );
}""", GL_FRAGMENT_SHADER)
self.shader = shaders.compileProgram(VERTEX_SHADER,FRAGMENT_SHADER)
self.vbo = vbo.VBO(
array( [
[ 0, 1, 0 ],
[ -1,-1, 0 ],
[ 1,-1, 0 ],
[ 2,-1, 0 ],
[ 4,-1, 0 ],
[ 4, 1, 0 ],
[ 2,-1, 0 ],
[ 4, 1, 0 ],
[ 2, 1, 0 ],
],'f')
)
def Render( self, mode):
"""Render the geometry for the scene."""
shaders.glUseProgram(self.shader)
try:
self.vbo.bind()
try:
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointerf( self.vbo )
glDrawArrays(GL_TRIANGLES, 0, 9)
finally:
self.vbo.unbind()
glDisableClientState(GL_VERTEX_ARRAY);
finally:
shaders.glUseProgram( 0 )
if __name__ == "__main__":
TestContext.ContextMainLoop()