I have been using LWJGL 3 and a tutorial at LearnOpenGL.com in order to create a game engine. However, after following the tutorial, I have discovered that the use of glVertexAttribPointer is not working. It works when uploading vertex positions and indices, but when I try to upload color and texture coordinates, nothing's happening, and I'm just getting a white box. I have attached the source code of the render engine, main class, and shaders:
public class Mesh {
public float[] vertices;
public float[] colors;
public float[] textureCoords;
public int[] indices;
public Mesh(float[] vertices, float[] colors, float[] textureCoords, int[] indices) {
this.vertices = vertices;
this.colors = colors;
this.textureCoords = textureCoords;
this.indices = indices;
}
}
public class MasterRenderer {
private int vbo;
private int vao;
private int ebo;
private Mesh mesh;
private ShaderProgram program;
public void Init(Mesh mesh) {
this.mesh = mesh;
program = new ShaderProgram();
vao = GL30.glGenVertexArrays();
vbo = GL20.glGenBuffers();
ebo = GL20.glGenBuffers();
}
public void Render(Color clearColor) {
// Clear Screen
GL11.glClearColor(clearColor.getRed(), clearColor.getGreen(), clearColor.getBlue(), clearColor.getAlpha());
GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
// Bind vao
GL30.glBindVertexArray(vao);
// Bind Buffers
GL20.glBindBuffer(GL20.GL_ARRAY_BUFFER, vbo);
GL20.glBufferData(GL20.GL_ARRAY_BUFFER, mesh.vertices, GL20.GL_STATIC_DRAW);
GL20.glBindBuffer(GL20.GL_ELEMENT_ARRAY_BUFFER, ebo);
GL20.glBufferData(GL20.GL_ELEMENT_ARRAY_BUFFER, mesh.indices, GL20.GL_STATIC_DRAW);
// Enable vertex attribPointers
GL20.glVertexAttribPointer(0, 3, GL11.GL_FLOAT, false, 0, 0);
GL20.glEnableVertexAttribArray(0);
// Handle Textures
Texture texture = Utils.LoadTexture("resources//textures//test.png");
// Uniforms
// Use Program
program.UseProgram();
// Draw the shape
GL20.glActiveTexture(GL20.GL_TEXTURE0);
GL20.glBindTexture(GL20.GL_TEXTURE_2D, texture.getId());
GL30.glBindVertexArray(vao);
GL20.glDrawElements(GL20.GL_TRIANGLES, 6, GL20.GL_UNSIGNED_INT, 0);
}
}
public class ShaderProgram {
public int programID;
public int vertexShaderID;
public int fragmentShaderID;
public ShaderProgram() {
programID = GL20.glCreateProgram();
try {
CompileVertexShader(Utils.LoadResource("resources/shaders/default/defaultShader.vs"));
CompileFragmentShader(Utils.LoadResource("resources/shaders/default/defaultShader.fs"));
GL20.glAttachShader(programID, vertexShaderID);
GL20.glAttachShader(programID, fragmentShaderID);
if(GL20.glGetShaderi(vertexShaderID, GL20.GL_COMPILE_STATUS) == 0) {
Logger.LogErrorClose("Error compiling vertex shader code: " + GL20.glGetShaderInfoLog(vertexShaderID, 1024));
}
if(GL20.glGetShaderi(fragmentShaderID, GL20.GL_COMPILE_STATUS) == 0) {
Logger.LogErrorClose("Error compiling fragment shader code: " + GL20.glGetShaderInfoLog(fragmentShaderID, 1024));
}
GL20.glLinkProgram(programID);
} catch (Exception e) {
e.printStackTrace();
}
}
public void UseProgram() {
GL20.glUseProgram(programID);
GL20.glDeleteShader(vertexShaderID);
GL20.glDeleteShader(fragmentShaderID);
}
public void CompileVertexShader(String shaderCode) {
vertexShaderID = GL20.glCreateShader(GL20.GL_VERTEX_SHADER);
GL20.glShaderSource(vertexShaderID, shaderCode);
GL20.glCompileShader(vertexShaderID);
}
public void CompileFragmentShader(String shaderCode) {
fragmentShaderID = GL20.glCreateShader(GL20.GL_FRAGMENT_SHADER);
GL20.glShaderSource(fragmentShaderID, shaderCode);
GL20.glCompileShader(fragmentShaderID);
}
}
public class Texture {
private int id;
public Texture(int id){
this.id = id;
}
public int getId(){
return id;
}
}
public class FoodFight {
public static void main(String[] args) {
Logger.LogMessage("Running on LWJGL Version: " + Version.getVersion());
Window w = new Window();
long window = w.CreateWindow(640, 360, "Food Fight");
MasterRenderer renderer = new MasterRenderer();
float[] vertices = {
0.5f, 0.5f, 0f,
0.5f, -0.5f, 0f,
-0.5f, -0.5f, 0f,
-0.5f, 0.5f, 0f
};
float[] colors = {
1f, 0f, 0f,
0f, 1f, 0f,
0f, 0f, 1f,
1f, 1f, 0f
};
float[] textureCoords = {
1f, 1f,
1f, 0f,
0f, 0f,
0f, 1f
};
int[] indices = {
0, 1, 3,
1, 2, 3
};
Mesh m = new Mesh(vertices, colors, textureCoords, indices);
renderer.Init(m);
while(!w.ShouldClose(window)) {
Input.ProcessInput(window);
renderer.Render(Color.black);
w.HandleWindow(window);
}
w.DestroyWindow(window);
}
}
Fragment:
#version 330 core
in vec3 color;
in vec2 texCoords;
out vec4 fragColor;
uniform sampler2D inputTexture;
void main() {
fragColor = texture(inputTexture, texCoords) * vec4(color, 1.0);
}
Vertex:
#version 330 core
in vec3 aPos;
in vec3 aColor;
in vec2 aTexCoord;
out vec3 color;
out vec2 texCoord;
void main() {
gl_Position = vec4(aPos, 1.0);
color = aColor;
texCoord = aTexCoord;
}
Since you have separate arrays for the vertex coordinates, colors and texture coordinates, you have specify separat buffers for the attributes:
vbo_ver = GL20.glGenBuffers();
vbo_col = GL20.glGenBuffers();
vbo_tex = GL20.glGenBuffers();
Creates and initializes the buffer object's data store
GL20.glBindBuffer(GL20.GL_ARRAY_BUFFER, vbo_ver);
GL20.glBufferData(GL20.GL_ARRAY_BUFFER, mesh.vertices, GL20.GL_STATIC_DRAW);
GL20.glBindBuffer(GL20.GL_ARRAY_BUFFER, vbo_col);
GL20.glBufferData(GL20.GL_ARRAY_BUFFER, mesh.colors, GL20.GL_STATIC_DRAW);
GL20.glBindBuffer(GL20.GL_ARRAY_BUFFER, vbo_tex);
GL20.glBufferData(GL20.GL_ARRAY_BUFFER, mesh.textureCoords, GL20.GL_STATIC_DRAW);
Specify the attribute indices, in the vertex shader by layout qualifiers:
#version 330 core
layout(location = 0) in vec3 aPos;
layout(location = 1) in vec3 aColor;
layout(location = 2) in vec2 aTexCoord;
glVertexAttribPointer associates the buffer which is currently bound to the ARRAY_BUFFER target, to the specified attribute (index), in the state vector of the currently bound Vertex Array Object. (See Vertex Specification).
Hence, the proper buffer and the VAO has to be bound before glVertexAttribPointer.
(The ELEMENT_ARRAY_BUFFER) is stated in the VAO, thus the VAO has to be bound before specifying the index buffer):
GL30.glBindVertexArray(vao);
GL20.glBindBuffer(GL20.GL_ELEMENT_ARRAY_BUFFER, ebo);
GL20.glBufferData(GL20.GL_ELEMENT_ARRAY_BUFFER, mesh.indices, GL20.GL_STATIC_DRAW);
GL20.glEnableVertexAttribArray(0);
GL20.glEnableVertexAttribArray(1);
GL20.glEnableVertexAttribArray(2);
GL20.glBindBuffer(GL20.GL_ARRAY_BUFFER, vbo_ver);
GL20.glVertexAttribPointer(0, 3, GL11.GL_FLOAT, false, 0, 0);
GL20.glBindBuffer(GL20.GL_ARRAY_BUFFER, vbo_col);
GL20.glVertexAttribPointer(1, 3, GL11.GL_FLOAT, false, 0, 0);
GL20.glBindBuffer(GL20.GL_ARRAY_BUFFER, vbo_tex);
GL20.glVertexAttribPointer(2, 2, GL11.GL_FLOAT, false, 0, 0);
Alternatively you can put the vertices and attributes to 1 array:
float[] attributes = {
// x y z r g b u v
0.5f, 0.5f, 0f, 1f, 0f, 0f, 1f, 1f,
0.5f, -0.5f, 0f, 0f, 1f, 0f, 1f, 0f,
-0.5f, -0.5f, 0f, 0f, 0f, 1f, 0f, 0f,
-0.5f, 0.5f, 0f, 1f, 1f, 0f 0f, 1f
};
vbo = GL20.glGenBuffers();
GL20.glBindBuffer(GL20.GL_ARRAY_BUFFER, vbo);
GL20.glBufferData(GL20.GL_ARRAY_BUFFER, mesh.attributes, GL20.GL_STATIC_DRAW);
The stride and the offset have to be specified in bates:
GL20.glBindBuffer(GL20.GL_ARRAY_BUFFER, vbo);
int stride = 8 * 4; // the tuple size is 8 (x y z r g b u v), sizeof float is 4
GL20.glVertexAttribPointer(0, 3, GL11.GL_FLOAT, false, stride, 0);
GL20.glVertexAttribPointer(1, 3, GL11.GL_FLOAT, false, stride, 3*4);
GL20.glVertexAttribPointer(2, 2, GL11.GL_FLOAT, false, stride, 6*4);
Related
I have run into this error and i have no Idea what it could be.
So iam initalizing GLFW, creating the Capability for OpenGL, loading the Shader and the VAO into VRAM.
I really have no Idea what it could be at this point.
public static void main(String[] args){
GLFWErrorCallback.createPrint(System.err).set();
if(!glfwInit()) {
assert false : "failed to inialize GLFW";
}
glfwDefaultWindowHints();
glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE);
glfwWindowHint(GLFW_RESIZABLE, GLFW_TRUE);
long windowID = glfwCreateWindow(800, 600, "title", NULL, NULL);
if(windowID == NULL) {
assert false : "Failed to create GLFW-Window";
}
glfwMakeContextCurrent(windowID);
glfwSwapInterval(0);
glfwShowWindow(windowID);
GL.createCapabilities();
Shader shader = new Shader(
FileReader.readRaw("assets/shaders/basicMesh.vs"),
FileReader.readRaw("assets/shaders/basicMesh.fs"));
int vaoID = glGenVertexArrays();
glBindVertexArray(vaoID);
int vbo = glGenBuffers();
glBindBuffer(GL_VERTEX_ARRAY, vbo);
glBufferData(GL_VERTEX_ARRAY, new float[] {
0.0f, 0.0f, 0.0f,
0.0f, 1.0f, 0.0f,
1.0f, 1.0f, 0.0f,
1.0f, 0.0f, 0.0f
}, GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, false, 0, 0);
glBindBuffer(GL_VERTEX_ARRAY, 0);
int ebo = glGenBuffers();
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, new int[] {
0,1,2,
2,3,0
}, GL_STATIC_DRAW);
glBindVertexArray(0);
while(!glfwWindowShouldClose(windowID)) {
glfwPollEvents();
glUseProgram(shader.getID());
glBindVertexArray(vaoID);
//crahes here!!!
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
glBindVertexArray(0);
glUseProgram(0);
glfwSwapBuffers(windowID);
}
}
Shader code is pretty simple for the Moment. Just to answer any questions here is the Code.
basicMesh.vs
#version 420 core
layout(location=0) in vec3 position;
void main() {
gl_Position = vec4(position, 1.0);
}
basicMesh.fs
#version 420 core
out vec4 color;
void main(){
color = vec4(1.0f, 1.0f, 0.0f, 1.0f);
}
GL_VERTEX_ARRAY is not a valid buffer binding point for e.g. glBindBuffer. The reason why it crashes is because you enabled generic vertex attribute 0 without having specified any vertex data for that, since you bind to an illegal binding point. glVertexAttribPointer looks for a buffer bound to GL_ARRAY_BUFFER, not GL_VERTEX_ARRAY.
What you meant to bind to is not GL_VERTEX_ARRAY but GL_ARRAY_BUFFER.
Sorry for my English. I'm beginner in OpenglES. I wrote this code to draw simple table for airhockey. The code was taken from book OpenglES2 for Android and i'm a bit to modified it. When i launched my app, it's crashed. Wherein do not appear an errors.
The code from ShaderHandler.java
public class ShaderHandler {
private static int program;
private static final String TAG = "TAG";
public static int loadShader(int type, String shaderCode){
final int shader = GLES20.glCreateShader(type);
if(shader == 0){
if(LoggerConfig.ON) {
Log.w(TAG, "Could not create new shader");
}
}
GLES20.glShaderSource(shader, shaderCode);
GLES20.glCompileShader(shader);
//----------------------------------------------------------------------------------------
//check compile status. This is pattern
final int[] compileStatus = new int[1];
GLES20.glGetShaderiv(shader, GLES20.GL_COMPILE_STATUS, compileStatus, 0);
if (LoggerConfig.ON) {
Log.v(TAG, "Results of compiling source: " + "\n" + shaderCode + "\n:"
+ GLES20.glGetShaderInfoLog(shader));
}
if (compileStatus[0] == 0){
//if it failed, delete the shader object
GLES20.glDeleteShader(shader);
if (LoggerConfig.ON) Log.w(TAG, "Compilation of shader failed");
}
//----------------------------------------------------------------------------------------
return shader;
}
public static int makeProgram(int vertexShader, int fragmentShader) {
program = GLES20.glCreateProgram();
if (program == 0){
if (LoggerConfig.ON){
Log.w(TAG, "Could not create a program");
}
}
GLES20.glAttachShader(program, vertexShader);
GLES20.glAttachShader(program, fragmentShader);
GLES20.glLinkProgram(program);
if(LoggerConfig.ON) validateProgram(program);
GLES20.glUseProgram(program);
return program;
}
}
And the code from myRenderer.java
public class MyRenderer implements GLSurfaceView.Renderer {
private static final int POSITION_COMPONENT_COUNT = 2;
private static final int BYTES_PER_FLOAT = 4;
private FloatBuffer vertexData;
private Context context;
private ShaderHandler shaderHandler;
private static final String A_POSITION = "a_Position";
private int aPositionLocation;
private static final String U_COLOR = "u_Color";
private int uColorLocation;
private final String vertexShaderCode =
"attribute vec4 a_Position;" +
"void main() {" +
" gl_Position = a_Position;" +
"}";
private final String fragmentShaderCode =
"precision mediump float;" +
"uniform vec4 u_Color;" +
"void main() {" +
" gl_FragColor = u_Color;" +
"}";
float[] tableVertices = {
0f, 0f,
0f, 14f,
9f, 14f,
9f, 0f
};
float[] tableVerticesTriangles = {
//Triangle1
0f, 0f,
9f, 14f,
0f, 14f,
//Triangle2
0f, 0f,
9f, 0f,
9f, 14f,
//Line1
0f, 7f,
9f, 7f,
//Mallets
4.5f, 2f,
4.5f, 12f
};
#Override
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
GLES20.glClearColor(1.0f, 0.0f, 0.0f, 0.0f);
vertexData = ByteBuffer
.allocateDirect(tableVerticesTriangles.length * BYTES_PER_FLOAT)
.order(ByteOrder.nativeOrder())
.asFloatBuffer();
vertexData.put(tableVertices);
int compiledVertShader = shaderHandler.loadShader(GLES20.GL_VERTEX_SHADER, vertexShaderCode);
int compiledFragShader = shaderHandler.loadShader(GLES20.GL_FRAGMENT_SHADER, fragmentShaderCode);
int program = shaderHandler.makeProgram(compiledVertShader, compiledFragShader);
aPositionLocation = GLES20.glGetAttribLocation(program, "a_Position");
uColorLocation = GLES20.glGetUniformLocation(program, "u_Color");
vertexData.position(0);
GLES20.glVertexAttribPointer(aPositionLocation, POSITION_COMPONENT_COUNT, GLES20.GL_FLOAT,
false, 0, vertexData);
GLES20.glEnableVertexAttribArray(aPositionLocation);
}
#Override
public void onSurfaceChanged(GL10 gl, int width, int height) {
GLES20.glViewport(0, 0, width, height);
}
#Override
public void onDrawFrame(GL10 gl) {
GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT);
GLES20.glUniform4f(uColorLocation, 1.0f, 1.0f, 1.0f, 1.0f);
GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, 6);
GLES20.glUniform4f(uColorLocation, 1.0f, 0.0f, 0.0f, 1.0f);
GLES20.glDrawArrays(GLES20.GL_LINES, 6, 2);
GLES20.glUniform4f(uColorLocation, 0.0f, 0.0f, 1.0f, 1.0f);
GLES20.glDrawArrays(GLES20.GL_POINTS, 8, 1);
GLES20.glUniform4f(uColorLocation, 1.0f, 0.0f, 0.0f, 1.0f);
GLES20.glDrawArrays(GLES20.GL_POINTS, 9, 1);
}
}
After that as app crashes, i see this message in logcat
A/libc: Fatal signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr 0x0 in tid 17311 (GLThread 891), pid 17264 (ockey.airhockey)
Please help. I've been trying to overcome this problem all day, but I just can’t figure out what to do.
I've been trying to resolve this problem for the past three days, but in vain. Therefore, I'd be very grateful for any help. I am currently learning how to draw triangles with VBOs and VAOs, so all of the code is included in one single "test" class.
package quad;
import java.nio.FloatBuffer;
import org.lwjgl.BufferUtils;
import org.lwjgl.glfw.GLFWVidMode;
import org.lwjgl.opengl.GL;
import static org.lwjgl.system.MemoryUtil.*;
import static org.lwjgl.glfw.GLFW.*;
import static org.lwjgl.opengl.GL11.GL_FALSE;
import static org.lwjgl.opengl.GL11.GL_TRUE;
import static org.lwjgl.opengl.GL.*;
import static org.lwjgl.opengl.GL11.*;
import static org.lwjgl.opengl.GL15.*;
import static org.lwjgl.opengl.GL20.*;
import static org.lwjgl.opengl.GL30.*;
public class Quad {
private long window;
private final String WINDOW_TITLE = "The Quad: glDrawArrays";
private final int WIDTH = 1920;
private final int HEIGHT = 1080;
// Quad variables
private int vaoId = 0;
private int vboId = 0;
private int vertexCount = 0;
private boolean running = false;
public static void main(String[] args) {
new Quad().start();
}
public void start() {
running = true;
setupOpenGL();
setupQuad();
while (running) {
loopCycle();
update();
if(glfwWindowShouldClose(window)) {
running = false;
}
}
destroyOpenGL();
}
public void setupOpenGL() {
if(!glfwInit()) {
throw new IllegalStateException("Unable to initialize GLFW!");
}
glfwDefaultWindowHints();
glfwWindowHint(GLFW_VISIBLE, GL_FALSE);
glfwWindowHint(GLFW_RESIZABLE, GL_TRUE);
window = glfwCreateWindow(WIDTH, HEIGHT, WINDOW_TITLE, NULL, NULL);
if (window == NULL) {
throw new RuntimeException("Cannot create window!");
}
glfwMakeContextCurrent(window);
GLFWVidMode vidmode = glfwGetVideoMode(glfwGetPrimaryMonitor());
glfwSetWindowPos(window, (vidmode.width() - WIDTH) / 2, (vidmode.height() - HEIGHT) / 2);
glfwShowWindow(window);
GL.createCapabilities();
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
}
public void setupQuad() {
float[] vertices = {
// Left bottom triangle
-0.5f, 0.5f, 0f,
-0.5f, -0.5f, 0f,
0.5f, -0.5f, 0f,
// Right top triangle
0.5f, -0.5f, 0f,
0.5f, 0.5f, 0f,
-0.5f, 0.5f, 0f
};
FloatBuffer verticesBuffer = BufferUtils.createFloatBuffer(vertices.length);
verticesBuffer.put(vertices);
verticesBuffer.flip();
vertexCount = 6;
// Create a new Vertex Array Object in memory and select it (bind)
// A VAO can have up to 16 attributes (VBO's) assigned to it by default
vaoId = glGenVertexArrays();
glBindVertexArray(vaoId);
// Create a new Vertex Buffer Object in memory and select it (bind)
// A VBO is a collection of Vectors which in this case resemble the location of each vertex.
vboId = glGenBuffers();
glBindBuffer(GL_ARRAY_BUFFER, vboId);
glBufferData(GL_ARRAY_BUFFER, verticesBuffer, GL_STATIC_DRAW);
// Put the VBO in the attributes list at index 0
glVertexAttribPointer(0, 3, GL_FLOAT, false, 0, 0);
// Deselect (bind to 0) the VBO
glBindBuffer(GL_ARRAY_BUFFER, 0);
// Deselect (bind to 0) the VAO
glBindVertexArray(0);
}
public void loopCycle() {
glClear(GL_COLOR_BUFFER_BIT);
// Bind to the VAO that has all the information about the quad vertices
glBindVertexArray(vaoId);
glEnableVertexAttribArray(0);
// Draw the vertices
glDrawArrays(GL_TRIANGLES, 0, vertexCount);
// Put everything back to default (deselect)
glDisableVertexAttribArray(0);
glBindVertexArray(0);
}
public void destroyOpenGL() {
// Disable the VBO index from the VAO attributes list
glDisableVertexAttribArray(0);
// Delete the VBO
glBindBuffer(GL_ARRAY_BUFFER, 0);
glDeleteBuffers(vboId);
// Delete the VAO
glBindVertexArray(0);
glDeleteVertexArrays(vaoId);
glfwDestroyWindow(window);
glfwTerminate();
}
public void update() {
glfwSwapBuffers(window);
glfwPollEvents();
}
}
Note: I've modernised this tutorial code example so LWJGL3 is used (none of the old Displays etc).
Everything functions correctly at the first glance, but when the code is run, only a window with black colour (which is set by glClearColor(0.0f, 0.0f, 0.0f, 0.0f) inside the setupOpengl() method.
Why are the quads not showing up?
I began to try to draw VBO using JOGL. Prior to that, I drew with the help of glBegin and glEnd, and everything worked. And then I see only a black screen. What could be the problem? I read somewhere that using VBO for drawing requires shaders. Is it so?
Code:
public class Main implements GLEventListener {
public static DisplayMode dm, dm_old;
private GLU glu = new GLU();
private float xrot,yrot,zrot;
private int texture;
Texture t;
#Override
public void display(GLAutoDrawable drawable) {
final GL2 gl = drawable.getGL().getGL2();
gl.glClear(GL2.GL_COLOR_BUFFER_BIT | GL2.GL_DEPTH_BUFFER_BIT);
gl.glLoadIdentity(); // Reset The View
gl.glTranslatef(0f, 0f, -5.0f);
gl.glBindTexture(GL2.GL_TEXTURE_2D, texture);
final float[] coordData = {
0, 0, //
1, 0, //
0, 1, //
};
final float[] vertices = {
-1.0f, -1.0f, 1.0f,
1.0f, -1.0f, 1.0f,
-1.0f, 1.0f, 1.0f,
};
// Setup the vertices into the buffer
FloatBuffer verts = Buffers.newDirectFloatBuffer(vertices.length);
verts.put(vertices).position(0);
// Setup the texture coordinates
FloatBuffer coords = Buffers.newDirectFloatBuffer(coordData.length);
coords.put(coordData).position(0);
gl.glVertexPointer(3, GL2.GL_FLOAT, 0, verts);
gl.glTexCoordPointer(2, GL2.GL_FLOAT, 0, coords);
gl.glDrawArrays(GL2.GL_TRIANGLES, 0, 3);
//change the speeds here
xrot += 5f;
}
#Override
public void init(GLAutoDrawable drawable) {
final GL2 gl = drawable.getGL().getGL2();
gl.glShadeModel(GL2.GL_SMOOTH);
gl.glClearColor(0f, 0f, 0f, 0f);
gl.glClearDepth(1.0f);
gl.glEnable(GL2.GL_DEPTH_TEST);
gl.glDepthFunc(GL2.GL_LEQUAL);
gl.glHint(GL2.GL_PERSPECTIVE_CORRECTION_HINT, GL2.GL_NICEST);
gl.glEnable(GL2.GL_TEXTURE_2D);
try {
File im = new File("/home/congard/pic/t.jpeg");
t = TextureIO.newTexture(im, true);
texture= t.getTextureObject(gl);
}catch(IOException e){
e.printStackTrace();
}
}
#Override
public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) {
// TODO Auto-generated method stub
}
public static void main(String[] args) {
// some code
}
}
You didn't enable the client-side capabilities for vertex and texture coordinates. See Client-Side Vertex Arrays
and glEnableClientState.
Add the following to your code:
gl.glEnableClientState(GL2.GL_VERTEX_ARRAY);
gl.glEnableClientState(GL2.GL_TEXTURE_COORD_ARRAY);
gl.glVertexPointer(3, GL2.GL_FLOAT, 0, verts);
gl.glTexCoordPointer(2, GL2.GL_FLOAT, 0, coords);
I am trying to put a texture on a mesh, but failing.
I am trying to render something like this, where texture can be seen but I get this, where I can barely see the triangle. I am running the following code:
public class Game implements ApplicationListener{
Mesh mesh;
ShaderProgram shader;
Texture texture;
public static void main(String[] args) {
LwjglApplication app = new LwjglApplication(new Game(), "Mesh Tutorial 1", 800, 600, true);
}
protected static ShaderProgram createMeshShader() {
String vert = Gdx.files.internal("data/VertexShader.txt").readString();
String frag = Gdx.files.internal("data/FragmentShader.txt").readString();
ShaderProgram.pedantic = false;
ShaderProgram shader = new ShaderProgram(vert, frag);
return shader;
}
#Override
public void create() {
if (mesh == null) {
mesh = new Mesh(true, 3, 3,
new VertexAttribute(Usage.Position, 3, ShaderProgram.POSITION_ATTRIBUTE),
new VertexAttribute(Usage.Color, 4, ShaderProgram.COLOR_ATTRIBUTE),
new VertexAttribute(Usage.TextureCoordinates, 2, ShaderProgram.TEXCOORD_ATTRIBUTE));
mesh.setVertices(new float[] { -0.5f, -0.5f, 0, 0.2f, 0.3f, 0.4f, 1f, 0, 1,
0.5f, -0.5f, 0, 0.1f, 0.2f, 0.1f, 1f, 1, 1,
0, 0.5f, 0, 0, 0.4f, 0.5f, 0.5f, 1f, 0 });
mesh.setIndices(new short[] { 0, 1, 2 });
texture = new Texture(Gdx.files.internal("data/badlogic.png"));
}
shader = createMeshShader();
}
#Override
public void render() {
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
Gdx.gl.glEnable(GL20.GL_TEXTURE_2D);
texture.bind();
mesh.render(shader, GL20.GL_TRIANGLES);
}
#Override
public void resize(int width, int height) {}
#Override
public void pause() {}
#Override
public void resume() {}
#Override
public void dispose() {}
The Vertexshader is:
attribute vec4 a_position;
attribute vec4 a_color;
attribute vec2 a_texCoords;
uniform mat4 u_projTrans;
varying vec4 vColor;
varying vec2 vTexCoord;
void main() {
vColor = a_color;
vTexCoord = a_texCoords;
gl_Position = u_projTrans * a_position;
}
Fragmentshader:
#ifdef GL_ES
precision mediump float;
#endif
varying vec4 v_color;
varying vec2 v_texCoords;
uniform sampler2D u_texture;
void main(){
vec4 texColor = texture2D(u_texture, v_texCoords);
gl_FragColor = v_color * texColor;
}
Correct me if I am wrong, but aren't you missing the part where you set your uniforms?
See the following as an example: OpenGlShader