I am using the following java code
package SPAGE.engine.gui;
import java.io.File;
import java.io.FileNotFoundException;
import java.nio.ByteBuffer;
import java.nio.FloatBuffer;
import java.nio.IntBuffer;
import java.util.Random;
import java.util.Scanner;
import javafx.application.Platform;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import org.lwjgl.BufferUtils;
import org.lwjgl.glfw.Callbacks;
import org.lwjgl.glfw.GLFWErrorCallback;
import org.lwjgl.glfw.GLFWKeyCallback;
import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.GL13;
import org.lwjgl.opengl.GL20;
import org.lwjgl.opengl.GLContext;
import static org.lwjgl.glfw.GLFW.*;
import static org.lwjgl.opengl.GL11.*;
import static org.lwjgl.opengl.GL15.*;
import static org.lwjgl.opengl.GL20.*;
import static org.lwjgl.opengl.GL30.*;
import static org.lwjgl.opengl.GL31.*;
import static org.lwjgl.opengl.GL32.*;
import static org.lwjgl.opengl.GL33.*;
import static org.lwjgl.system.MemoryUtil.*;
public class glembedtest {
private GLFWErrorCallback errorCallback = Callbacks.errorCallbackPrint(System.err);
long window;
private GLFWKeyCallback keyCallback = new GLFWKeyCallback() {
@Override
public void invoke(long window, int key, int scancode, int action, int mods) {
if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS) {
glfwSetWindowShouldClose(window, GL_TRUE);
}
}
};
glStagePlatform plat = new glStagePlatform();
int vbo;
int tcb;
int tex;
int program;
public void go()
{
glfwSetErrorCallback(errorCallback);
if (glfwInit() != GL_TRUE) {
throw new IllegalStateException("Unable to initialize GLFW");
}
glfwDefaultWindowHints();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
window = glfwCreateWindow(640, 480, "Simple example", NULL, NULL);
if (window == NULL) {
glfwTerminate();
throw new RuntimeException("Failed to create the GLFW window");
}
glfwSetKeyCallback(window, keyCallback);
glfwMakeContextCurrent(window);
GLContext.createFromCurrent();
/*
Platform.runLater(new Runnable() {
@Override
public void run() {
plat.setScene(createScene());
}
});
plat.setsize(512, 512);
*/
ByteBuffer buf= ByteBuffer.allocateDirect(512*512*3);
Random r = new Random();
for(int x=0;x<buf.capacity();x++)
{
buf.put((byte) r.nextInt(255));
}
buf.flip();
tex=glGenTextures();
glBindTexture(GL_TEXTURE_2D, tex);
GL11.glTexImage2D(GL_TEXTURE_2D,0,GL_RGB,512,512,0,GL_RGB,GL_UNSIGNED_BYTE,buf);
int vao = glGenVertexArrays();
glBindVertexArray(vao);
//set up vbo
float[] vbodata = {
-0.9f, -0.9f, 0.0f,
0.9f, -0.9f, 0.0f,
-0.9f, 0.9f,0.0f,
0.9f, 0.9f, 0.0f,
};
FloatBuffer fb= BufferUtils.createFloatBuffer(vbodata.length);
fb.put(vbodata);
fb.flip();
vbo=glGenBuffers();
glBindBuffer(GL_ARRAY_BUFFER,vbo);
glBufferData(GL_ARRAY_BUFFER,fb, GL_STATIC_DRAW);
float[] tcs={0f,0f,
0f,1f,
1f,1f,
1f,0f};
fb= BufferUtils.createFloatBuffer(tcs.length);
fb.put(tcs);
fb.flip();
tcb=glGenBuffers();
glBindBuffer(GL_ARRAY_BUFFER,tcb);
glBufferData(GL_ARRAY_BUFFER,fb, GL_STATIC_DRAW);
program=createshaders("shaders/shader.vtex","shaders/shader.frag");
glClearColor(0.0f, 0.0f, 0.4f, 0.0f);
while (glfwWindowShouldClose(window) != GL_TRUE)
{
mainloop();
}
glfwDestroyWindow(window);
keyCallback.release();
glfwTerminate();
errorCallback.release();
}
public static void main(String[] args)
{
new glembedtest().go();
}
public void mainloop()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glUseProgram(program);
glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
GL20.glVertexAttribPointer(0, 3, GL_FLOAT, false, 0, 0);
glBindBuffer(GL_ARRAY_BUFFER, tcb);
GL20.glVertexAttribPointer(1, 2, GL_FLOAT, false, 0, 0);
GL13.glActiveTexture(GL13.GL_TEXTURE0);
int texloc=glGetUniformLocation(program, "mytex");
glUniform1i(texloc, 0);
glBindTexture(GL_TEXTURE_2D, tex);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
glfwSwapBuffers(window);
glfwPollEvents();
}
private static Scene createScene() {
Group root = new Group();
Scene scene = new Scene(root, Color.BLUE);
Text text = new Text();
text.setX(40);
text.setY(100);
text.setFont(new Font(25));
text.setText("Welcome JavaFX!");
root.getChildren().add(text);
return (scene);
}
int createshaders(String vert, String frag)
{
int VertexShaderID = glCreateShader(GL_VERTEX_SHADER);
int FragmentShaderID = glCreateShader(GL_FRAGMENT_SHADER);
String code="";
try {
Scanner scan = new Scanner(new File(vert));
while(scan.hasNext())
{
code+=scan.nextLine()+"\n";
}
scan.close();
glShaderSource(VertexShaderID,code);
glCompileShader(VertexShaderID);
int result=glGetShaderi(VertexShaderID, GL_COMPILE_STATUS);
String log=glGetShaderInfoLog(VertexShaderID);
System.out.println("vtx shader returned: "+log);
scan = new Scanner(new File(frag));
code="";
while(scan.hasNext())
{
code+=scan.nextLine()+"\n";
}
scan.close();
glShaderSource(FragmentShaderID,code);
glCompileShader(FragmentShaderID);
result=glGetShaderi(FragmentShaderID, GL_COMPILE_STATUS);
log=glGetShaderInfoLog(FragmentShaderID);
System.out.println("frag shader returned: "+log);
int ProgramID = glCreateProgram();
glAttachShader(ProgramID, VertexShaderID);
glAttachShader(ProgramID, FragmentShaderID);
glLinkProgram(ProgramID);
result=glGetProgrami(ProgramID, GL_LINK_STATUS);
log=glGetProgramInfoLog(ProgramID);
System.out.println("program link returned: "+log);
return ProgramID;
} catch (FileNotFoundException ex) {
ex.printStackTrace();
}
return -1;
}
}
and shaders
// vertex shader
#version 330 core
// Input vertex data, different for all executions of this shader.
layout(location = 0) in vec3 vertexPosition_modelspace;
layout(location = 1) in vec2 vertexUV;
// Output data ; will be interpolated for each fragment.
out vec2 UV;
void main(){
// Output position of the vertex, in clip space : MVP * position
gl_Position = vec4(vertexPosition_modelspace,1);
// UV of the vertex. No special space for this one.
UV = vertexUV;
}
//fragment shader
#version 330 core
// Interpolated values from the vertex shaders
in vec2 UV;
// Ouput data
out vec3 color;
// Values that stay constant for the whole mesh.
uniform sampler2D mytex;
void main(){
// Output color = color of the texture at the specified UV
color = texture( mytex, UV ).rgb;
}
I’m expecting a rectangle with random noise in it (i’m actually trying to make a gui, but i’m just loading noise into my texture to test the shader), but all i get is a black rectangle. any ideas what i’m doing wrong here?