OpenGL functions give FunctionNotSupported error - java

I am trying to make a 2D OpenGL project using LWJGL 2, and I am having trouble with basic rendering.
Main class:
public class ScratchCord {
private Renderer renderer;
public TextureManager manager;
private int i = 0;
private int i2 = 0;
private ScratchCord() {
renderer = new Renderer(this);
manager = new TextureManager();
ContextAttribs attribs = new ContextAttribs(3, 2).withForwardCompatible(true).withProfileCore(true);
try {
Display.setDisplayMode(new DisplayMode(1240, 740));
Display.create(new PixelFormat(), attribs);
Display.setTitle("ScratchCord");
} catch (LWJGLException e) {
e.printStackTrace();
}
GL11.glViewport(0, 0, 1240, 740);
GL11.glEnable(GL11.GL_TEXTURE_2D);
GL11.glDisable(GL11.GL_DEPTH_TEST);
GL11.glMatrixMode(GL11.GL_PROJECTION);
GL11.glLoadIdentity();
GL11.glOrtho(0, 1240, 740, 0, -1, 1);
}
private void start() {
while (!Display.isCloseRequested()) {
GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
GL11.glMatrixMode(GL11.GL_MODELVIEW);
GL11.glLoadIdentity();
renderer.draw(i, i2, "ScratchCord/src/main/resources/textures/download.jpg", 200, 200);
Display.sync(60);
Display.update();
i += 1;
i2 += 1;
}
}
public static void main(String[] args) {
System.setProperty("org.lwjgl.util.NoChecks","true");
ScratchCord scratchCord = new ScratchCord();
scratchCord.start();
}
}
For some reason, I get a FunctionNotSupported error, but the code works in http://www.cokeandcode.com/info/tut2d-4.html.
I tried setting the OpenGl version to 2.0, and I got an error in Display.create and the OpenGL context was not created.
I've heard that sometimes graphics drivers can break LWJGL, but mine are updated and other projects I have works.
Also some people say that the function just isn't in 3.2, and if so how would I do this?

Your machine likely does not support Legacy GL, making it a "Core" context. Core contexts have deprecated legacy functions, like matrix operations, unavailable.
Find a tutorial that is for modern core opengl.

Related

libGDX 3D Shadows

Im using libGDX and this is a Desktop project.
I have 2 models, one is the character and the other is the map, as you can see in the image below:
My question is:
How do I project the shadow of the character on the floor?
As you can see the character doesn't have a shadow, thus the Ambience Light. What do I have to use or how do I achieve this? Should I fake a shadow or is there a real way to project shadows?
Any comments or sugestions are welcome.
You can use following code:
Environment environment;
DirectionalShadowLight shadowLight;
#Override
public void show() {
modelBatch = new ModelBatch();
environment = new Environment();
environment.set(new ColorAttribute(ColorAttribute.AmbientLight, 1.0f, 1f, .6f, 1f));
environment.add((shadowLight = new DirectionalShadowLight(1024, 1024, 60f, 60f, .1f, 50f))
.set(1f, 1f, 1f, 40.0f, -35f, -35f));
environment.shadowMap = shadowLight;
shadowBatch = new ModelBatch(new DepthShaderProvider());
}
#Override
public void render(float delta) {
//create shadow texture
shadowLight.begin(Vector3.Zero, camera.direction);
shadowBatch.begin(shadowLight.getCamera());
shadowBatch.render(instances);
shadowBatch.end();
shadowLight.end();
//render scene
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT | GL20.GL_DEPTH_BUFFER_BIT);
modelBatch.begin(cam);
modelBatch.render(instances, environment); //environment has shadowMap!
modelBatch.end();
}
Sometime in 2015, a better shadow system was added to the libGDX testbed, separately from the "experimental, do not use" stuff mentioned in #Nolesh's answer.
The code is in the libGDX tests, so you'd need to copy it to your own project. A short description of how it works was posted on the libGDX forums by the author, realitix. Note that there doesn't seem to be a fully runnable example, but the JavaDoc for the ShadowSystem interface contains the following:
// Init system:
Array<ModelBatch> passBatches = new Array<ModelBatch>();
ModelBatch mainBatch;
ShadowSystem system = new XXXShadowSystem();
system.init();
for (int i = 0; i < system.getPassQuantity(); i++) {
passBatches.add(new ModelBatch(system.getPassShaderProvider(i)));
}
mainBatch = new ModelBatch(system.getShaderProvider());
// Render scene with shadows:
system.begin(camera, instances);
system.update();
for (int i = 0; i < system.getPassQuantity(); i++) {
system.begin(i);
Camera camera;
while ((camera = system.next()) != null) {
passBatches.get(i).begin(camera);
passBatches.get(i).render(instances, environment);
passBatches.get(i).end();
}
camera = null;
system.end(i);
}
system.end();
HdpiUtils.glViewport(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT | GL20.GL_DEPTH_BUFFER_BIT);
mainBatch.begin(cam);
mainBatch.render(instances, environment);
mainBatch.end();
It's not as good as having a proper API for shadowing, but better than having to implement the entire thing from scratch.

Running LWJGL on a Raspberry Pi

I need to port something written in Java with LWJGL to a Raspberry. Im using Raspbian and tried oracles java version and some called "openjdk" or something like that.
With both versions I get this Exception:
java.lang.UnsatisfiedLinkError: org.lwjgl.opengl.GLContext.nLoadOpenGLLibrary()V
when creating the "Display".
I already searched for some solution, but they refer to OpenGLES and I did never use OpenGLES nor I found any download to use it.
I have no clue what to do and what information you might need, just comment if you need some more input.
edit:
Well you might want my current sourcecode too:
import java.io.File;
import org.lwjgl.LWJGLException;
import org.lwjgl.Sys;
import org.lwjgl.input.Keyboard;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.PixelFormat;
public class Main {
private int fps;
private long lastFPS, lastTime;
Main() {
createWindow();
init2D();
loop();
}
private void init2D() {
GL11.glMatrixMode(GL11.GL_PROJECTION);
GL11.glLoadIdentity();
GL11.glOrtho(0, 500, 400, 0, -1, 1);
GL11.glMatrixMode(GL11.GL_MODELVIEW);
GL11.glLoadIdentity();
GL11.glDisable(GL11.GL_DEPTH_TEST);
lastFPS = getTime();
getDelta();
}
private long getTime() {
return (Sys.getTime() * 1000) / Sys.getTimerResolution();
}
public void updateFPS() {
if (getTime() - lastFPS > 1000) {
Display.setTitle(Integer.toString(fps));
fps = 0;
lastFPS += 1000;
}
fps++;
}
private int getDelta() {
long time = getTime();
int delta = (int) (time - lastTime);
lastTime = time;
return delta;
}
private void loop() {
float rot = 0;
while(!Display.isCloseRequested()&&!Keyboard.isKeyDown(Keyboard.KEY_ESCAPE)) {
GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
GL11.glLoadIdentity();
int x = Display.getDisplayMode().getWidth()/2;
int y = Display.getDisplayMode().getHeight()/2;
GL11.glTranslated(x, y, 0);
GL11.glRotated(rot, 0, 0, 1);
if(Keyboard.isKeyDown(Keyboard.KEY_SPACE)) {
GL11.glColor3f(1, 0, 0);
} else {
GL11.glColor3f(1, 1, 1);
}
GL11.glBegin(GL11.GL_QUADS);
GL11.glVertex2d(-50, -50);
GL11.glVertex2d(50, -50);
GL11.glVertex2d(50, 50);
GL11.glVertex2d(-50, 50);
GL11.glEnd();
rot+=0.05*getDelta();
Display.update();
updateFPS();
}
}
private void createWindow() {
try {
Display.setDisplayMode(new DisplayMode(500, 400));
Display.create();
} catch (LWJGLException e) {
e.printStackTrace();
System.exit(0);
} // Window is created in this try-catch
try {
Assetloader.load();
} catch (Exception e) {
} // Load Assets
}
public static void main(String args[]) {
System.setProperty("org.lwjgl.librarypath", new File("native").getAbsolutePath());
new Main();
}
}
Raspberry Pi's only support OpenGL ES 2.0 and they're isn't any way around that.
OpenGL ES has no immediate mode rendering (glBegin, glVertex, glColor). You'll have to load your geometry into VBO's and render that way.
You'll also need to write basic shaders and pass your projection and modelview matrices in via glUniformMatrix.
Immediate mode rendering is pretty much only available on the desktop, iPhone's, Android's and Blackberry's use only OpenGL ES and pretty much any game console will use an API very similiar to OpenGL ES.
Bottom line: You'll need to ditch the immediate mode rendering and learn to use the ES spec.
These links should help...
http://www.khronos.org/registry/gles/specs/2.0/es_full_spec_2.0.25.pdf
http://www.khronos.org/files/opengles_shading_language.pdf

Java LWJGL / Slick-Util texture.bind() binds the same texture everytime

I've been programming for a few years now and wanted to try Java LWJGL, and people recommended using the Slick-Util library so I set all of it up, etc. and I've been working on several projects, never encountering this problem. However, I've encountered a problem where when binding a texture to openGL, the same incorrect texture is instead bound every time. I ran into this problem first with a project with a bunch of classes etc, and chose to try to condense the problem into one jar file.
So before I show the code, I wanna explain how the example file works. Basically, there are four PNG's within my project. They are contained in res/textures/character/texturesHere. The names of the PNG's consist as: char_d.png, char_u.png, char_l.png, char_r.png. I've checked all four and all four have different textures of the character's direction, as it should be. This is to prove that the resources of the project are NOT the problem. Here is the entire problem condensed into a "Main" class file.
package main_p;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import org.lwjgl.LWJGLException;
import org.lwjgl.opengl.*;
import org.newdawn.slick.opengl.Texture;
import org.newdawn.slick.opengl.TextureLoader;
import static org.lwjgl.opengl.GL11.*;
public class Main
{
public final int srcWidth = 1280;
public final int srcHeight = 720;
public float x = 150;
public float y = 150;
public float w = 40;
public float h = 40;
public Texture char_down;
public Texture char_right;
public Texture char_left;
public Texture char_up;
public Main()
{
try
{
Display.setTitle("Escape Alpha v0.0.1");
Display.setDisplayMode(new DisplayMode(srcWidth, srcHeight));
Display.create();
}
catch (LWJGLException e)
{
e.printStackTrace();
}
initGL();
initTextures();
loop();
}
public void loop()
{
glClear(GL_COLOR_BUFFER_BIT);
while (!Display.isCloseRequested())
{
glBegin(GL_QUADS);
char_left.bind();
glTexCoord2f(0, 0);
glVertex2f(x, y);
glTexCoord2f(1, 0);
glVertex2f(x + w, y);
glTexCoord2f(1, 1);
glVertex2f(x + w, y + h);
glTexCoord2f(0, 1);
glVertex2f(x, y + h);
glEnd();
Display.update();
Display.sync(60);
}
Display.destroy();
}
public void initGL()
{
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, srcWidth, srcHeight, 0, 1, -1);
glMatrixMode(GL_MODELVIEW);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
}
public void initTextures()
{
char_down = loadTextureFrom("character/char_d.png");
char_up = loadTextureFrom("character/char_u.png");
char_left = loadTextureFrom("character/char_l.png");
char_right = loadTextureFrom("character/char_r.png");
}
public Texture loadTextureFrom(String path)
{
try
{
return TextureLoader.getTexture("PNG", new FileInputStream(new File("res/textures/" + path)));
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
return null;
}
public static void write(Object ob)
{
System.out.println(ob);
}
public static void main(String[] args)
{
new Main();
}
}
One thing I've noticed is that within the initTextures() method, if I change the LAST loaded texture, ANY texture I bind will end up rendering the last texture loaded in initTextures()
In this example I bind Texture char_left, but instead it uses char_right, because it was the last one loaded in my initTextures() method. I can prove this because if I change whichever texture is LAST loaded in my initTextures() method, it ends up rendering that one instead, again disregarding the texture I tell it to bind. Like I said, I've checked all of my PNG's multiple times, and they all look good, and show the character facing the correct direction. My loadTextureFrom(String path) method is as any other texture loader. Nothing special about it really. I actually am using this exact method in 90% of my 2D projects for Java LWJGL / Slick-Util, and I've only recently started running into this problem. I'm at a loss for words, and have never ran into this problem before. Any help is greatly appreciated. :)
Edit 1:
I just did an important test. I checked the HashCode of all four textures, AFTER loading them within the initTextures() method.
public void initTextures()
{
char_left = loadTextureFrom("character/char_l.png");
char_right = loadTextureFrom("character/char_r.png");
char_down = loadTextureFrom("character/char_d.png");
char_up = loadTextureFrom("character/char_u.png");
write(System.identityHashCode(char_left));
write(System.identityHashCode(char_right));
write(System.identityHashCode(char_down));
write(System.identityHashCode(char_up));
}
It returned this:
1364345882
1878339755
1246651385
1619367563
I think this truly proves that the initTextures() method isn't the problem. Once again, any help is appreciated! This kind of 100% holds me back from doing any more work on any projects :/ Thanks! :)
I know this is an old post but I don't want anyone who sees this to go answerless. Basically, the texture bind call has to be outsde the "glBegin". That's all the problem is. You cannot bind textures inside of the glBegin call. If you don't do this, gl will just bind the last texture loaded.
I dont know if this will actually help but you have a problem in your loop method. Basically you are not clearing you screen. You do glClear(GL_COLOR_BUFFER_BIT); before your loop. Another thing that might cause this sort of problem is that you actually have to enable and disable GL_BLEND in a loop. Also almost forgot you didn't enable glEnable(GL_TEXTURE_2D); in your initGl() method.
public void loop()
{
while (!Display.isCloseRequested())
{
glClear(GL_COLOR_BUFFER_BIT);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glBegin(GL_QUADS);
char_left.bind();
glTexCoord2f(0, 0);
glVertex2f(x, y);
glTexCoord2f(1, 0);
glVertex2f(x + w, y);
glTexCoord2f(1, 1);
glVertex2f(x + w, y + h);
glTexCoord2f(0, 1);
glVertex2f(x, y + h);
glEnd();
glDisable(GL_BLEND);
Display.update();
Display.sync(60);
}
Display.destroy();
}
Hmm try this and see if it works. I use it in my 2d game it works there.
package game.utils;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import org.lwjgl.opengl.GL11;
import org.newdawn.slick.opengl.Texture;
import org.newdawn.slick.util.ResourceLoader;
public class TextureLoader {
private static Map<URL, Texture> texMap = new HashMap<>();
public static void bindTexture(URL tex) {
try {
Texture texture = texMap.get(tex);
if (texture == null) {
texture = loadTexture(tex);
}
texture.bind();
GL11.glTexParameterf(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_NEAREST);
GL11.glTexParameterf(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_NEAREST);
} catch (IOException e) {
// TODO Auto-generated catch block.
e.printStackTrace();
}
}
public static void bindTexture(String tex) {
try {
bindTexture(ResourceLoader.getResource(tex));
} catch (Exception e) {
bindTexture("res/other/white.png");
}
}
private static Texture loadTexture(URL tex) throws FileNotFoundException, IOException {
System.out.printf("Loading texture %s (%s)%n", texMap.size(), tex.getFile());
Texture texture = org.newdawn.slick.opengl.TextureLoader.getTexture("PNG", tex.openStream());
texMap.put(tex, texture);
return texture;
}
}
You can change the "res/other/white.png" to whatever your missing texture file is. Also the glTexParameterf lines are for disabling mipmap/filtering/whatever it's called.

Unable to display SVG in angengine

I am trying to render a SVG to the screen using andengine, but unfortunately it is not being displayed. Also, I am not getting any exceptions or errors while running the project, thus I'm finding it very hard to debug.
I've had a look at this post but I guess BlackPawnTextureBuilder is not available on GLES2.
I've included part of my code below,
public void onCreateResources(OnCreateResourcesCallback pOnCreateResourcesCallback) throws Exception
{
BuildableBitmapTextureAtlas buildableBitmapTextureAtlas = new BuildableBitmapTextureAtlas(this.getTextureManager(), 2048, 2048, TextureOptions.BILINEAR);
logoSplashITextureRegion = SVGBitmapTextureAtlasTextureRegionFactory.createFromAsset(buildableBitmapTextureAtlas, this, "gfx/BrickRed.svg", 80, 40);
buildableBitmapTextureAtlas.load();
pOnCreateResourcesCallback.onCreateResourcesFinished();
}
public void onCreateScene(OnCreateSceneCallback pOnCreateSceneCallback) throws Exception
{
logoSplashScene = new Scene();
logoSplashSprite = new Sprite(0, 0, logoSplashITextureRegion,mEngine.getVertexBufferObjectManager())
{
#Override
protected void preDraw(GLState pGLState, Camera pCamera)
{
super.preDraw(pGLState, pCamera);
pGLState.enableDither();
}
};
logoSplashSprite.setPosition((CAMERA_WIDTH - logoSplashSprite.getWidth()) * 0.5f, (CAMERA_HEIGHT - logoSplashSprite.getHeight()) * 0.5f);
logoSplashScene.attachChild(logoSplashSprite);
pOnCreateSceneCallback.onCreateSceneFinished(BrickActivity.logoSplashScene);
}
Am I forgetting something in the code?
Thanks in advance for you help.
I finally managed to make it work.
It seems like as I'm using GLES2 I had to include the following line just before I load my textureAtlas.
buildableBitmapTextureAtlas.build(new BlackPawnTextureAtlasBuilder<IBitmapTextureAtlasSource, BitmapTextureAtlas>(0, 1, 0));
Now my onCreateResources looks like this,
try
{
buildableBitmapTextureAtlas = new BuildableBitmapTextureAtlas(textureManager, 2048, 2048, TextureOptions.BILINEAR);
SVG redBrick = SVGParser.parseSVGFromAsset(assertManager, "gfx/BrickRed.svg");
iTextureRegion = SVGBitmapTextureAtlasTextureRegionFactory.createFromSVG(buildableBitmapTextureAtlas, redBrick, 80, 40);
buildableBitmapTextureAtlas.build(new BlackPawnTextureAtlasBuilder<IBitmapTextureAtlasSource, BitmapTextureAtlas>(0, 1, 0));
buildableBitmapTextureAtlas.load();
} catch (TextureAtlasBuilderException e)
{
e.printStackTrace();
}

Java LWJGL Slick UnicodeFont acting up

I'm having some problems displaying text on the screen, in the past I have just used sprite based text, however this time I want to use UnicodeFont. TrueTypeFonts draw perfectly however its deprecated.
When I try to draw the UnicodeFont it seems to be affected by the characters I use. for example If I draw the string "stackoverflow" the text and the box will draw, if I try "stackoverflowcom" the box will not draw.
A barebones version of my source code is below. On line ~74 I call uniFont.drawString(0, 0,"stackoverflow"); , if the com (or anything really) the box will not be drawn.
edit. > You can use the boolean tryUnicode to swap between true and unicode.
Ive tried sticking them in to seperate display lists but it made no difference.
Could anyone offer an insight in to why this is happening?
Thanks
import java.awt.Font;
import org.lwjgl.LWJGLException;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.opengl.GL11;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.TrueTypeFont;
import org.newdawn.slick.UnicodeFont;
import org.newdawn.slick.font.effects.ColorEffect;
import static org.lwjgl.opengl.GL11.*;
public class Game {
private UnicodeFont uniFont;
private TrueTypeFont truFont;
public static void main(String[] argv) {
Game game = new Game();
game.start();
}
public void start()
{
initGL(600, 600);
initFonts();
while(!Display.isCloseRequested()) //display not closed
{
render();
Display.update();
Display.sync(60);
}
Display.destroy();
System.exit(0);
}
private void render()
{
GL11.glClear(GL11.GL_COLOR_BUFFER_BIT);
GL11.glClear(GL11.GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glTranslatef(-0.25f,0.7f,0);
glScalef(0.001f,-0.001f,0.001f);
glEnable(GL_BLEND);
boolean tryUnicode = false;
if(tryUnicode)
{
uniFont.drawString(0, 0,"stackoverflow");
//EDIT.. glDisable texture is required here.
}else
{
glScalef(1.1f,1.1f,1f);
truFont.drawString(0, 0, "stackoverflow truFont");
}
glDisable(GL_BLEND);
glPopMatrix();
glPushMatrix();
glTranslatef(-0.25f,0,0);
glColor3f(0.5f, 0f, 0f);
glBegin(GL_TRIANGLE_STRIP);
glVertex3f(0, 0,0.0f);
glVertex3f(0.5f, 0,0f);
glVertex3f(0f,0.5f,0f);
glVertex3f(0.5f, 0.5f,0f);
glEnd();
glPopMatrix();
}
private void initGL(int width, int height) {
try {
Display.setDisplayMode(new DisplayMode(width,height));
Display.create();
//Display.setVSyncEnabled(true);
} catch (LWJGLException e) {
e.printStackTrace();
System.exit(0);
}
glEnable(GL11.GL_TEXTURE_2D);
glShadeModel(GL11.GL_SMOOTH);
glEnable(GL11.GL_DEPTH_TEST);
glDisable(GL11.GL_LIGHTING);
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glClearDepth(1);
glEnable(GL_BLEND);
glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
glDisable(GL_BLEND);
glMatrixMode(GL11.GL_PROJECTION);
glLoadIdentity();
glOrtho(-1, 1, -1, 1, -1, 1);
glMatrixMode(GL11.GL_MODELVIEW);
}
private void initFonts() {
Font awtFont = new Font("", Font.PLAIN,55);
truFont = new TrueTypeFont(awtFont, true);
uniFont = new UnicodeFont(awtFont, 128, false, false);
uniFont.addAsciiGlyphs();
uniFont.addGlyphs(400,600); // Setting the unicode Range
uniFont.getEffects().add(new ColorEffect(java.awt.Color.white));
try {
uniFont.loadGlyphs();
} catch (SlickException e) {};
}
}
It seems I have this error now, it will only draw the outlined rectangle before using the font. This seems to be a Slick error, and is a major problem so they should fix it before adding new features.
The only work-around is to render fonts last on your frame.
EDIT: Problem Solved!
add this line after you render your font:
GL11.glDisable(GL11.GL_TEXTURE_2D);
The Slick people should really add that line in as it causes so many bugs!
This is how I create fonts:
Font font = ...; //Your loaded font
float size = 20.0F;
UnicodeFont f = new UnicodeFont(font.deriveFont(0 /*normal*/, size));
f.addAsciiGlyphs();
ColorEffect e = new ColorEffect();
e.setColor(java.awt.Color.white);
f.getEffects().add(e);
try {
f.loadGlyphs();
} catch (SlickException e1) {
e1.printStackTrace();
}
return f;
Hope this helped!

Categories