我正在与JOGL合作。我有:Intel Corporation Device 22b1(rev 35) 我只是将mesa更新到17.0版。我的英特尔驱动器是i915,根据我所读的内容,它似乎是正确的。
当指示#version 430时,我仍然无法编译着色器我对使用GLSL“ES”的嵌入版本不感兴趣。
glxinfo的输出| grep“opengl”:
OpenGL vendor string: Intel Open Source Technology Center
OpenGL renderer string: Mesa DRI Intel(R) HD Graphics 405 (Braswell)
OpenGL core profile version string: 4.5 (Core Profile) Mesa 17.1.2
OpenGL core profile shading language version string: 4.50
OpenGL core profile context flags: (none)
OpenGL core profile profile mask: core profile
OpenGL core profile extensions:
OpenGL version string: 3.0 Mesa 17.1.2
OpenGL shading language version string: 1.30
OpenGL context flags: (none)
OpenGL extensions:
OpenGL ES profile version string: OpenGL ES 3.1 Mesa 17.1.2
OpenGL ES profile shading language version string: OpenGL ES GLSL ES 3.10
OpenGL ES profile extensions:
如何使用版本430?我很确定我的显卡会支持它。
lspci -nn |egrep "VGA|Display"
00:02.0 VGA compatible controller [0300]: Intel Corporation Device [8086:22b1] (rev 35)
着色器:
#version 400 core
layout (location=0) in vec3 position;
uniform mat4 mv_matrix;
uniform mat4 proj_matrix;
void main(void)
{
gl_Position = proj_matrix * mv_matrix * vec4(position,1.0);
}
createshader():
GL4 gl = (GL4) GLContext.getCurrentGL();
int[] vertCompiled = new int[1];
int[] fragCompiled = new int[1];
int[] linked = new int[1];
String[] vshaderSource;
String[] fshaderSource;
vshaderSource = readShaderSource("resources/vert.shader");
fshaderSource = readShaderSource("resources/frag.shader");
int vShader = gl.glCreateShader(GL_VERTEX_SHADER);
gl.glShaderSource(vShader,vshaderSource.length,vshaderSource, null, 0);
gl.glCompileShader(vShader);
全:
public class FirstApp extends JFrame implements GLEventListener {
private int vao[] = new int[1];
private int vbo[] = new int[2];
private int rendering_program;
private GLCanvas myCanvas;
private float cameraX, cameraY, cameraZ;
private float cubeLocX, cubeLocY, cubeLocZ;
private GLSLUtils util = new GLSLUtils();
private Matrix3D pMat;
public FirstApp(){
setSize(600,600);
setLocation(200,200);
myCanvas = new GLCanvas();
myCanvas.addGLEventListener(this);
this.add(myCanvas);
setVisible(true);
// FPSAnimator animator = new FPSAnimator(myCanvas, 50);
// animator.start();
}
public void init(GLAutoDrawable draw){
GL4 gl = (GL4) GLContext.getCurrentGL();
rendering_program = createShaderProgram();
setupVertices();
cameraX = 0.0f; cameraY = 0.0f; cameraZ = 8.0f;
cubeLocX = 0.0f; cubeLocY = -2.0f; cubeLocZ = 0.0f;
}
private void setupVertices(){
GL4 gl = (GL4) GLContext.getCurrentGL();
float[] vertex_positions = {
-1.0f, 1.0f, -1.0f, -1.0f, -1.0f, -1.0f, 1.0f, -1.0f, -1.0f,
1.0f, -1.0f, -1.0f, 1.0f, 1.0f, -1.0f, -1.0f, 1.0f, -1.0f,
1.0f, -1.0f, -1.0f, 1.0f, -1.0f, 1.0f, 1.0f, 1.0f, -1.0f,
1.0f, -1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, -1.0f,
1.0f, -1.0f, 1.0f, -1.0f, -1.0f, 1.0f, 1.0f, 1.0f, 1.0f,
-1.0f, -1.0f, 1.0f, -1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f,
-1.0f, -1.0f, 1.0f, -1.0f, -1.0f, -1.0f, -1.0f, 1.0f, 1.0f,
-1.0f, -1.0f, -1.0f, -1.0f, 1.0f, -1.0f, -1.0f, 1.0f, 1.0f,
-1.0f, -1.0f, 1.0f, 1.0f, -1.0f, 1.0f, 1.0f, -1.0f, -1.0f,
1.0f, -1.0f, -1.0f, -1.0f, -1.0f, -1.0f, -1.0f, -1.0f, 1.0f,
-1.0f, 1.0f, -1.0f, 1.0f, 1.0f, -1.0f, 1.0f, 1.0f, 1.0f,
1.0f, 1.0f, 1.0f, -1.0f, 1.0f, 1.0f, -1.0f, 1.0f, -1.0f
};
gl.glGenVertexArrays(vao.length, vao, 0);
gl.glBindVertexArray(vao[0]);
gl.glGenBuffers(vbo.length, vbo, 0);
gl.glBindBuffer(GL_ARRAY_BUFFER, vbo[0]);
FloatBuffer vertBuf = Buffers.newDirectFloatBuffer(vertex_positions);
gl.glBufferData(GL_ARRAY_BUFFER, vertBuf.limit() * 4, vertBuf, GL_STATIC_DRAW);
}
private Matrix3D perspective(float fovy, float aspect, float n, float f){
float q = 1.0f / ((float) Math.tan(Math.toRadians(0.5f * fovy)));
float A = q / aspect;
float B = (n + f) / (n-f);
float C = (2.0f * n * f) / (n-f);
Matrix3D r = new Matrix3D();
r.setElementAt(0,0,A);
r.setElementAt(1,1,q);
r.setElementAt(0,0,B);
r.setElementAt(3,2,-1.0f);
r.setElementAt(2,3,C);
r.setElementAt(3,3,0.0f);
return r;
}
private int createShaderProgram(){
//compiles and links fragment shader and vertex shader
GL4 gl = (GL4) GLContext.getCurrentGL();
int[] vertCompiled = new int[1];
int[] fragCompiled = new int[1];
int[] linked = new int[1];
String[] vshaderSource;
String[] fshaderSource;
vshaderSource = readShaderSource("resources/vert.shader");
fshaderSource = readShaderSource("resources/frag.shader");
int vShader = gl.glCreateShader(GL_VERTEX_SHADER);
gl.glShaderSource(vShader,vshaderSource.length,vshaderSource, null, 0);
gl.glCompileShader(vShader);
checkOpenGLError();
gl.glGetShaderiv(vShader, GL_COMPILE_STATUS, vertCompiled, 0);
if(vertCompiled[0] == 1){
System.out.println("comilation is a success");
} else {
System.out.println("compilation failed...");
printShaderLog(vShader);
}
int fShader = gl.glCreateShader(GL_FRAGMENT_SHADER);
gl.glShaderSource(fShader, fshaderSource.length, fshaderSource,null, 0);
gl.glCompileShader(fShader);
checkOpenGLError();
gl.glGetShaderiv(fShader, GL_COMPILE_STATUS, fragCompiled, 0);
if(fragCompiled[0] == 1){
System.out.println("fragment comilation is a success");
} else {
System.out.println("fragment compilation failed...");
printShaderLog(fShader);
}
if((vertCompiled[0] != 1) || (fragCompiled[0] != 1)){
System.out.println("\nCompilation error; return-flags: ");
System.out.println("vercompiled = "+ vertCompiled[0] + " frag compiled = " +fragCompiled[0]);
} else {
System.out.println("Succesful compilation");
}
int vfprogram = gl.glCreateProgram();
gl.glAttachShader(vfprogram, vShader);
gl.glAttachShader(vfprogram, fShader);
gl.glBindAttribLocation(vfprogram,0,"position");
gl.glLinkProgram(vfprogram);
checkOpenGLError();
gl.glGetProgramiv(vfprogram, GL_LINK_STATUS, linked, 0);
if(linked[0] == 1){
System.out.println("linking is a success");
} else {
System.out.println("link failed...");
printProgramLog(vfprogram);
}
gl.glDeleteShader(vShader);
gl.glDeleteShader(fShader);
return vfprogram;
}
public void display(GLAutoDrawable drawable){
GL4 gl = (GL4) GLContext.getCurrentGL();
gl.glClear(GL_DEPTH_BUFFER_BIT);
gl.glUseProgram(rendering_program);
int mv_loc = gl.glGetUniformLocation(rendering_program,"mv_matrix");
int proj_loc = gl.glGetUniformLocation(rendering_program, "proj_matrix");
float aspect = (float) myCanvas.getWidth() / (float) myCanvas.getHeight();
Matrix3D pMat = perspective(60.0f, aspect, 0.1f, 1000.0f);
Matrix3D vMat = new Matrix3D();
vMat.translate(-cameraX, -cameraY, -cameraZ);
Matrix3D mMat = new Matrix3D();
mMat.translate(cubeLocX, cubeLocY, cubeLocZ);
Matrix3D mvMat = new Matrix3D();
mvMat.concatenate(vMat);
mvMat.concatenate(mMat);
gl.glUniformMatrix4fv(mv_loc, 1, false, mvMat.getFloatValues(), 0);
gl.glUniformMatrix4fv(proj_loc, 1, false, pMat.getFloatValues(), 0);
gl.glBindBuffer(GL_ARRAY_BUFFER, vbo[0]);
gl.glVertexAttribPointer(0,3,GL_FLOAT, false, 0,0);
gl.glEnableVertexAttribArray(0);
gl.glEnable(GL_DEPTH_TEST);
gl.glDepthFunc(GL_LEQUAL);
gl.glDrawArrays(GL_TRIANGLES, 0, 36);
}
private void printShaderLog(int shader){
GL4 gl = (GL4) GLContext.getCurrentGL();
int[] len = new int[1];
int[] chWritten = new int[1];
byte[] log = null;
gl.glGetShaderiv(shader,GL_INFO_LOG_LENGTH,len,0);
if(len[0] > 0){
log = new byte[len[0]];
gl.glGetShaderInfoLog(shader,len[0], chWritten, 0, log, 0);
System.out.println("Shader info log: ");
for( int i =0; i < log.length; i++){
System.out.print((char) log[i]);
}
}
}
void printProgramLog(int prog){
GL4 gl = (GL4) GLContext.getCurrentGL();
int[] len = new int[1];
int[] chWritten = new int[1];
byte[] log = null;
gl.glGetProgramiv(prog,GL_INFO_LOG_LENGTH,len,0);
if(len[0] > 0){
log = new byte[len[0]];
gl.glGetProgramInfoLog(prog,len[0], chWritten, 0, log, 0);
System.out.println("Program info log: ");
for( int i =0; i < log.length; i++){
System.out.print((char) log[i]);
}
}
}
boolean checkOpenGLError(){
GL4 gl = (GL4) GLContext.getCurrentGL();
boolean foundError = false;
GLU glu = new GLU();
int glErr = gl.glGetError();
while(glErr != GL_NO_ERROR){
System.err.println("glError: "+glu.gluErrorString(glErr));
foundError = true;
glErr = gl.glGetError();
}
return foundError;
}
public static void main(String[] args) {
new FirstApp();
}
public String[] readShaderSource(String path) {
//reads in shader programs
ArrayList<String> builder = new ArrayList();
try (InputStream in = new FileInputStream(path);
BufferedReader reader = new BufferedReader(new InputStreamReader(in))) {
String line;
while ((line = reader.readLine()) != null) {
builder.add(line+"\n");
}
} catch (IOException ex) {
throw new RuntimeException("Failed to load a shader file!"
+ System.lineSeparator() + ex.getMessage());
}
CharSequence source = builder.toString();
return builder.toArray(new String[1]);
}
public void reshape(GLAutoDrawable draw, int x, int y, int width, int height){ }
public void dispose(GLAutoDrawable draw){ }
}