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);
}
}
你好!
我正在尝试纹理多边形,但是当我运行此代码时,它向我显示了一个具有奇怪颜色的矩形,就像纹理坐标不适合矩形一样。我将纹理坐标绑定到VertexAttribArray然后我在主游戏循环中调用它。我观看了很多教程,但没有人像我对索引那样做,我没有将索引绑定到attribarray,我通常将它们放入VBO然后在glDrawElements之前调用它。提前致谢!
P.S:我在这个lwjgl章节中是新手,所以请带我一点:)
答案 0 :(得分:0)
我真的不知道OpenGL 2,因为它似乎是你正在使用的。我有很多事情要改变(如下所示),但我建议你切换到OpenGL 3和更新版本。这是更近期和灵活的方式。如果您需要相关教程,我可以推荐ThinMatrix(https://www.youtube.com/user/ThinMatrix)制作的教程。
由于我只知道OpenGL 3和更新版本,我告诉你的可能不正确,但请尝试:
new test().bind();
移到主循环之外