我在使用OpenGL时遇到了一个大问题。我试图得到一个简单的盒子来渲染纹理。但是,即使是这个应该只是绘制框的代码也会崩溃。如何使用 VBO 将纹理添加到 3D 框以及如何让此代码不崩溃?
class Box {
Location start, end;
...... More Code Here .....
public Location[] getVertices() {
return new Location[] {
start, new Location(start).add(width, 0, 0),
new Location(start).add(0, 0, depth), new Location(start).add(width, 0, depth),
end, new Location(end).subtract(width, 0, 0),
new Location(end).subtract(0, 0, depth), new Location(end).subtract(width, 0, depth)
};
}
public void draw() {
Location[] vertices = getVertices();
FloatBuffer verts = BufferUtils.createFloatBuffer(vertices.length * 3);
for(Location l : vertices) {
verts.put(l.toArray());
}
int vertHandle = glGenBuffers();
glBindBuffer(GL_ARRAY_BUFFER, vertHandle);
glBufferData(GL_ARRAY_BUFFER, verts, GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, false, 0, 0);
glDrawArrays(GL_QUADS, 0, 4);
glDisableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
}
}
编辑:这是位置类。
public class Location {
public float x, y, z;
//.......... Code here.......
public Location(Location l) {
this.x = l.x;
this.y = l.y;
this.z = l.z;
}
public Location(float x, float y, float z) {
this.x = x;
this.y = y;
this.z = z;
}
//... Code here.......
// Sets
public Location add(Location l) {
this.x += l.x;
this.y += l.y;
this.z += l.z;
return this;
}
public Location add(float x, float y, float z) {
this.x += x;
this.y += y;
this.z += z;
return this;
}
// Sets
public Location subtract(Location l) {
this.x -= l.x;
this.y -= l.y;
this.z -= l.z;
return this;
}
public Location subtract(float x, float y, float z) {
this.x -= x;
this.y -= y;
this.z -= z;
return this;
}
//..Code Here.....
public float[] toArray() {
return new float[] {
x, y, z
};
}
//... Code Here....
}
编辑:这是我的initGL()方法:
void initGL() {
// Initialize OpenGL
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glViewport(0, 0, Display.getWidth(), Display.getHeight());
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
// Enable Depth Testing
glEnable(GL_DEPTH_TEST);
// Enable client states
//glEnableClientState(GL_VERTEX_ARRAY); Do I need this???
//glEnableClientState(GL_COLOR_ARRAY);
// Are these right for drawing textures?
glEnable(GL_TEXTURE_2D);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
// enable alpha blending
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
}
这是我的渲染GL()方法:
public void renderGL() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear The Screen And The Depth Buffer
for(Box b : boxes) {
b.draw();
}
//System.out.println(player.midpoint());
Display.update();
// Camera stuff. This has been working and so I don't think it's causing an issue
glLoadIdentity();
player.lookThrough();
}
编辑:这就是我自己为堆栈跟踪而得到的全部内容:
#
# A fatal error has been detected by the Java Runtime Environment:
#
# EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x0000000013b5cda0, pid=5844, tid=5824
#
# JRE version: 7.0_11-b21
# Java VM: Java HotSpot(TM) 64-Bit Server VM (23.6-b04 mixed mode windows-amd64 compressed oops)
# Problematic frame:
# C [ig75icd64.dll+0x7cda0] RegisterProcTableCallback+0x74cd0
#
# Failed to write core dump. Minidumps are not enabled by default on client versions of Windows
#
# An error report file with more information is saved as:
# C:UsersChristianDocumentsJM3BasicGamehs_err_pid5844.log
#
# If you would like to submit a bug report, please visit:
# http://bugreport.sun.com/bugreport/crash.jsp
# The crash happened outside the Java Virtual Machine in native code.
# See problematic frame for where to report the bug.
#
Java Result: 1
您需要在渲染立方体之前绑定纹理。 此外,除了verts
浮点缓冲区外,您还需要一个来存储纹理坐标(0 到 1)。 对于纹理坐标,您几乎完全可以执行顶点,但glVertexAttribPointer
替换为glTexCoordAttribPointer
(就像您几乎vertex
替换为texCoord
的地方)。