4 색 (노란색, 파란색, 녹색, 빨간색) 4 개의 사각형으로 구성된 512 x 512 텍스처로 쿼드를 그립니다. 나는 모든 노란색하지만 0에서 1로 텍스 좌표 공간 그리고 내가 여기에 코드의 ... 그들이 올바르게 매핑하고 꽤 확신 이미지를 볼 때 :LWJGL 텍스처로드/매핑 문제
private void loadTexture(String textureFile) {
TexHandle = glGenTextures();
BufferedImage TextureBuffer = null;
try {
TextureBuffer = ImageIO.read(new File(textureFile));
} catch (IOException ex) {
System.err.println("Error while reading the texture: " + textureFile);
System.exit(1);
}
Width = TextureBuffer.getWidth();
Height = TextureBuffer.getHeight();
AlphaChannel = TextureBuffer.getColorModel().hasAlpha();
int pixel;
int[] pixels = TextureBuffer.getRGB(0, 0, Width, Height, null, 0, Width);
ByteBuffer Buffer = BufferUtils.createByteBuffer(Width * Height * 4);
for(int i = 0; i < Height; i++){
for(int j = 0; j < Width; j++){
pixel = pixels[i * Width + j];
Buffer.put((byte)(pixel & 0xff));
Buffer.put((byte)((pixel >> 8) & 0xff));
Buffer.put((byte)((pixel >> 16) & 0xff));
if(AlphaChannel)
Buffer.put((byte)((pixel >> 24) & 0xff));
else
Buffer.put((byte)0xff);
}
}
glBindTexture(GL_TEXTURE_2D, TexHandle);
Buffer.flip();
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, Width, Height, 0, GL_BGRA, GL_UNSIGNED_BYTE, Buffer);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glBindTexture(GL_TEXTURE_2D, 0);
}
public void initOpenGL() {
glClearColor(0f, 0f, 0.4f, 1f);
glFrontFace(GL_CCW);
glCullFace(GL_BACK);
glEnable(GL_CULL_FACE);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_BLEND);
glDepthFunc(GL_LEQUAL);
glEnable(GL_DEPTH_TEST);
glEnable(GL_DEPTH_CLAMP);
glEnable(GL_TEXTURE_2D);
}
주 :
while(!window.isClosing()){
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
program.updateUniform("TextureIndex", 0);
glActiveTexture(GL_TEXTURE0 + 0);
glBindTexture(GL_TEXTURE_2D, TexHandle);
glBegin(GL_QUADS);
glNormal3f(0, 0, 0);
glTexCoord2f(0, 0);
glVertex3f(-.5f, -.5f, -2f);
glTexCoord2f(1, 0);
glVertex3f(.5f, -.5f, -2f);
glTexCoord2f(1, 1);
glVertex3f(.5f, .5f, -2f);
glTexCoord2f(0, 1);
glVertex3f(-.5f, .5f, -2f);
glEnd();
window.update();
}
정점 :
layout(location = 0) in vec3 in_position;
layout(location = 1) in vec2 in_texture_coord;
layout(location = 2) in vec3 in_normal;
out vec2 out_texture_coord;
void main(){
gl_Position = vec4(in_position, 1.0);
out_texture_coord = in_texture_coord;
}
단편
out vec4 out_color;
in vec2 in_texture_coord; // passed from the vertex shader
uniform sampler2D TextureIndex;
void main(){
out_color = texture2D(TextureIndex, in_texture_coord);
}
,536,913,632 10
누락 된 한 가지 사실은 그리기 전에 창을 지우는'glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)'호출입니다.그 외에도, 다음 추측은 버텍스 쉐이더에서 문제가 될 것입니다. –
함수를 추가했지만 여전히 오류가 발생합니다. 또한 vertex shader를 추가했습니다. – Mk3Y