import static org.lwjgl.glfw.GLFW.*;
import static org.lwjgl.opengl.GL11.*;
import static org.lwjgl.opengl.GL20.*;
import static org.lwjgl.opengl.GL30.*;
import java.awt.image.BufferedImageFilter;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.nio.DoubleBuffer;
import java.nio.FloatBuffer;
import java.nio.IntBuffer;
import static org.lwjgl.opengl.GL15.*;
import org.lwjgl.BufferUtils;
import org.lwjgl.glfw.GLFWCursorPosCallback;
import org.lwjgl.glfw.GLFWKeyCallback;
import org.lwjgl.opengl.GL;
public class test {
public static int program;
public static int vid,aid,iid,count,tid;
public static void main(String args[]) throws IOException {
glfwInit();
long window = glfwCreateWindow(1920, 1080, "HI", 0 , 0);
glfwShowWindow(window);
glfwMakeContextCurrent(window);
GL.createCapabilities();
TextureLoader texture = new TextureLoader(new File("D:/work/opengl2/src/opengl2/no.png"));
while (!glfwWindowShouldClose(window))
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable(GL_DEPTH_TEST);
glEnable(GL_TEXTURE_2D);
new test().bind();
glBindVertexArray(aid);
glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, iid);
texture.bind();
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT,0);
glDisableVertexAttribArray(0);
glDisableVertexAttribArray(1);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
glBindVertexArray(0);
glfwSwapBuffers(window);
glfwPollEvents();
}
}
//bind the data
public void bind()
{
float[] vertices = {
-0.5f, 0.5f, 0f, // Left top ID: 0
-0.5f, -0.5f, 0f, // Left bottom ID: 1
0.5f, -0.5f, 0f, // Right bottom ID: 2
0.5f, 0.5f, 0f // Right left ID: 3
};
int[] indices = new int[]
{
0, 1, 2,
2, 3, 0
};
float[] coords = new float[]{
0,0,
0,1,
1,1,
1,0
};
count = indices.length;
//create buffers
FloatBuffer texture = BufferUtils.createFloatBuffer(coords.length);
texture.put(coords);
texture.flip();
FloatBuffer vert = BufferUtils.createFloatBuffer(vertices.length);
vert.put(vertices);
vert.flip();
IntBuffer indic = BufferUtils.createIntBuffer(indices.length);
indic.put(indices);
indic.flip();
//bind the VAO
aid = glGenVertexArrays();
glBindVertexArray(aid);
vid = glGenBuffers();
glBindBuffer(GL_ARRAY_BUFFER, vid);
glBufferData(GL_ARRAY_BUFFER, vert, GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, false, 0, 0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
tid = glGenBuffers();
glBindBuffer(GL_ARRAY_BUFFER, tid);
glBufferData(GL_ARRAY_BUFFER, texture, GL_STATIC_DRAW);
glVertexAttribPointer(1, 2, GL_FLOAT, false, 0, 0);
glBindBuffer(GL_ARRAY_BUFFER,0);
glBindVertexArray(0);
iid = glGenBuffers();
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, iid);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, indic, GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
}
}
Ciao ragazzi! Sto provando a tessere un poligono ma quando eseguo questo codice mi mostra un rettangolo con uno strano colore, come quando le coordinate della trama non sono buone per il rettangolo. Lego le coordinate della texture al VertexAttribArray e quindi lo chiamo nel ciclo di gioco principale. Ho guardato un sacco di tutorial ma nessuno ha fatto come ho fatto con gli indici, non lego gli indici in un attribarray, di solito li metto in un VBO e poi lo chiamo prima di glDrawElements. Grazie in anticipo! P.S: Sono nuovo in questo capitolo di lwjgl quindi prendimi facile :)
risposte:
0 per risposta № 1Non conosco veramente OpenGL 2, come sembracosa stai usando. Ci sono più cose che dovrei cambiare (come elencato sotto), ma ti consiglio di passare a OpenGL 3 e successivi. È molto più recente e flessibile. Se hai bisogno di tutorial su questo, posso raccomandare quelli creati da ThinMatrix (https://www.youtube.com/user/ThinMatrix).
Dato che conosco solo OpenGL 3 e successivi, quello che sto dicendo potrebbe non essere corretto, ma prova:
- In movimento
new test().bind();
al di fuori del tuo ciclo principale - Prova a non legare il tuo VAO dopo il tuo buffer di indice è stato riempito con dati