我的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);
}
这是我的renderGL()方法:
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:\Users\Christian\Documents\JM3\BasicGame\hs_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
答案 0 :(得分:0)
您需要在渲染多维数据集之前绑定纹理。此外,除了verts
浮动缓冲区,您还需要一个存储纹理坐标(0到1)。对于纹理co-ords来说,你做了很多令人兴奋的事情,你用glVertexAttribPointer
代替glTexCoordAttribPointer
(只要你vertex
替换texCoord
几乎)。