我正在将OpenGl与Java配合使用,并且目前正在尝试将2个不同的着色器程序(每个要渲染的对象一个)与我的代码连接。尽管如此,最后只有一个着色器有效连接,但我认为(虽然不确定)它会覆盖第一个着色器。我的代码很长,所以我不确定应该提供多少信息,所以让我知道是否也应该发布着色器的代码:
public class Aufgabe3undFolgende extends AbstractOpenGLBase {
private ShaderProgram shaderProgram;
private ShaderProgram shaderProgram2;
private Matrix4 matrix = new Matrix4();
private Matrix4 secondMatrix = new Matrix4();
private Matrix4 projection = new Matrix4(0.8f,500f);
public static void main(String[] args) {
new Aufgabe3undFolgende().start("CG Aufgabe 3", 700, 700);
}
private int matrixID;
private int secondMatrixID;
private int snake;
private int greenSnake;
private GLFWKeyCallback input;
//cube normals
float[] normals = {..};
//cube vertices
float[] cube = {..};
//pyramid vertices
float[] pyramid = {..};
//uv coordinates for pyramid
float[] uv = {..};
//uv coordinates for cube
float[] uvCoords = {..};
@Override
protected void init() {
glfwSetKeyCallback(super.returnWindow(), input = new
MouseAndKeyboardControl());
shaderProgram = new ShaderProgram("aufgabe3");
//cube
glUseProgram(shaderProgram.getId());
matrixID = glGenVertexArrays();
glBindVertexArray(matrixID);
System.out.println(matrixID);
//pass data in the buffer
VBO(0, 3,cube);
VBO(1, 3, normals);
VBO( 2, 2, uvCoords);
Texture texture = new Texture("snake.jpg", 8);
snake = texture.getId();
glBindTexture(GL_TEXTURE_2D, snake);
//pyramid
shaderProgram2 = new ShaderProgram("render_pyramid");
glUseProgram(shaderProgram2.getId());
secondMatrixID = glGenVertexArrays();
glBindVertexArray(secondMatrixID);
System.out.println(secondMatrixID);
//pass data in the buffer
VBO(0, 3, pyramid);
VBO(1, 2,uv);
Texture texture2 = new Texture("greensnake.jpg", 8);
greenSnake = texture2.getId();
glBindTexture(GL_TEXTURE_2D, greenSnake);
glEnable(GL_DEPTH_TEST); // z-Buffer aktivieren
glEnable(GL_CULL_FACE); // backface culling aktivieren
int loc = glGetUniformLocation(shaderProgram.getId(),
"projectionMatrix");
glUniformMatrix4fv(loc, false,projection.getValuesAsArray());
}
public void VBO(int index, int size, float[] name) {
int vbold = glGenBuffers();
glBindBuffer(GL_ARRAY_BUFFER, vbold);
glBufferData(GL_ARRAY_BUFFER, name, GL_STATIC_DRAW);
glVertexAttribPointer(index,size, GL_FLOAT, false, 0,0);
glEnableVertexAttribArray(index);
}
@Override
public void update() {
matrix = new Matrix4();
matrix.translate(2, 2, -4);
secondMatrix = new Matrix4();
secondMatrix.translate(-2, -1, -4);
if (MouseAndKeyboardControl.keyPressed(49)) {
secondMatrix.rotateX(0.3f);
}
if (MouseAndKeyboardControl.keyPressed(50)) {
matrix.rotateY(0.3f);
}
}
@Override
protected void render() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
int loc1 = glGetUniformLocation(shaderProgram.getId(), "viewMatrix");
glUniformMatrix4fv(loc1, false, matrix.getValuesAsArray());
glBindTexture(GL_TEXTURE_2D, snake);
glBindVertexArray(matrixID);
glDrawArrays(GL_TRIANGLES, 0, 36);
int loc2 = glGetUniformLocation(shaderProgram2.getId(), "viewMatrix2");
glUniformMatrix4fv(loc2, false, secondMatrix.getValuesAsArray());
glBindTexture(GL_TEXTURE_2D, greenSnake);
glBindVertexArray(secondMatrixID);
glDrawArrays(GL_TRIANGLES, 0, 12);
}
@Override
public void destroy() {
}
我尝试使用 init()方法将着色器与程序连接。第一个着色器的名称为 aufgabe3 ,第二个着色器的名称为 render_pyramid 。感谢您提供的任何帮助!