Hello Guest

[UNRESOLVED]Some help with rendering tiles

  • 12 Replies
  • 5245 Views
*

Offline @

  • *
  • 41
[UNRESOLVED]Some help with rendering tiles
« on: May 15, 2014, 09:30:42 »
I have this problem: Right now, i'm displaying around 20 lists of tiles, each one 16x16. The biggest list has around 64X64 tiles, and each subsequent list has less tiles. When displaying every list, fps drops from 60 to almost 40, and cpu consume is around 90%. After some testing i have to conclude that the problem resides on rendering, but i do nothing special besides iterating throughout every entity object and drawing using its list id.

Maybe is due to my inexperience, but 20 lists does not seems too much for me. However, for every list i render, things start to get hairy.
I'm using opengl1 (old notebook).

Now, the question is, what can i employ to replace lists? Or how can i display them more efficiently?
« Last Edit: May 18, 2014, 19:24:51 by @ »

*

Offline Cornix

  • *****
  • 487
Re: Some help with rendering tiles
« Reply #1 on: May 15, 2014, 09:48:38 »
You are talking about deprecated Display-List, right?

Maybe show us the important parts of your code, we might see if you made any big mistakes there.

Other then that, try to use VBO's since they are the only not-deprecated way for rendering today. Even if your graphics card does not support OpenGL 1.5 it might still support the VBO extension.

*

Offline @

  • *
  • 41
Re: Some help with rendering tiles
« Reply #2 on: May 15, 2014, 10:06:32 »
You are talking about deprecated Display-List, right?

Maybe show us the important parts of your code, we might see if you made any big mistakes there.

Other then that, try to use VBO's since they are the only not-deprecated way for rendering today. Even if your graphics card does not support OpenGL 1.5 it might still support the VBO extension.

That's interesting, how can i test if VBO are supported? and how do i obtain that extension? This guy seems to gain more fps with display lists http://lwjgl.org/forum/index.php/topic,3118.msg17211.html#msg17211

*

Offline Cornix

  • *****
  • 487
Re: Some help with rendering tiles
« Reply #3 on: May 15, 2014, 13:07:36 »
You query the supported extensions.

Here is sample code for a Utility class that I once wrote. I use it to check for the OpenGL-Version and supported extensions:
Code: [Select]
public class GLVersion {

private static GLVersion instance;

public static int getMajorVersion() {
return get().majorVersion;
}

public static int getMinorVersion() {
return get().minorVersion;
}

public static double getVersion() {
return get().version;
}

public static String getVersionString() {
return get().versionString;
}

public static boolean isExtensionSupported(GLExtension extension) {
return isExtensionSupported(extension.asString());
}

public static boolean isExtensionSupported(String extension) {
return get().extensionMap.containsKey(extension);
}

public static Collection<String> getSupportedExtensions() {
return get().extensionMap.keySet();
}

private static GLVersion get() {
if (instance == null) {
instance = new GLVersion();
}
return instance;
}

private final String versionString;
private final int majorVersion;
private final int minorVersion;
private final double version;
private final Map<String, Boolean> extensionMap;

private GLVersion() {
versionString = GL11.glGetString(GL11.GL_VERSION);

int majorVersionIndex = versionString.indexOf('.');
int minorVersionIndex = majorVersionIndex + 1;
while (minorVersionIndex < versionString.length() && Character.isDigit(minorVersionIndex)) {
minorVersionIndex++;
}
minorVersionIndex++;

majorVersion = Integer.parseInt(versionString.substring(0, majorVersionIndex));
minorVersion = Integer.parseInt(versionString.substring(majorVersionIndex + 1, minorVersionIndex));
version = Double.parseDouble(versionString.substring(0, minorVersionIndex));

String[] supportedExtensions;
if (majorVersion >= 3) {
int numExtensions = GL11.glGetInteger(GL30.GL_NUM_EXTENSIONS);
supportedExtensions = new String[numExtensions];
for (int i = 0; i < numExtensions; i++) {
supportedExtensions[i] = GL30.glGetStringi(GL11.GL_EXTENSIONS, i);
}
} else {
String extensionsAsString = GL11.glGetString(GL11.GL_EXTENSIONS);
supportedExtensions = extensionsAsString.split(" ");
}

extensionMap = new HashMap<>();
for (String extension : supportedExtensions) {
extensionMap.put(extension, Boolean.TRUE);
}
}

}
You may use it or you may just copy the important bits, but I think it should be enough to get the idea across, even without comments.

*

Offline @

  • *
  • 41
Re: Some help with rendering tiles
« Reply #4 on: May 15, 2014, 15:10:05 »
You query the supported extensions.

Here is sample code for a Utility class that I once wrote. I use it to check for the OpenGL-Version and supported extensions:
Code: [Select]
public class GLVersion {

private static GLVersion instance;

public static int getMajorVersion() {
return get().majorVersion;
}

public static int getMinorVersion() {
return get().minorVersion;
}

public static double getVersion() {
return get().version;
}

public static String getVersionString() {
return get().versionString;
}

public static boolean isExtensionSupported(GLExtension extension) {
return isExtensionSupported(extension.asString());
}

public static boolean isExtensionSupported(String extension) {
return get().extensionMap.containsKey(extension);
}

public static Collection<String> getSupportedExtensions() {
return get().extensionMap.keySet();
}

private static GLVersion get() {
if (instance == null) {
instance = new GLVersion();
}
return instance;
}

private final String versionString;
private final int majorVersion;
private final int minorVersion;
private final double version;
private final Map<String, Boolean> extensionMap;

private GLVersion() {
versionString = GL11.glGetString(GL11.GL_VERSION);

int majorVersionIndex = versionString.indexOf('.');
int minorVersionIndex = majorVersionIndex + 1;
while (minorVersionIndex < versionString.length() && Character.isDigit(minorVersionIndex)) {
minorVersionIndex++;
}
minorVersionIndex++;

majorVersion = Integer.parseInt(versionString.substring(0, majorVersionIndex));
minorVersion = Integer.parseInt(versionString.substring(majorVersionIndex + 1, minorVersionIndex));
version = Double.parseDouble(versionString.substring(0, minorVersionIndex));

String[] supportedExtensions;
if (majorVersion >= 3) {
int numExtensions = GL11.glGetInteger(GL30.GL_NUM_EXTENSIONS);
supportedExtensions = new String[numExtensions];
for (int i = 0; i < numExtensions; i++) {
supportedExtensions[i] = GL30.glGetStringi(GL11.GL_EXTENSIONS, i);
}
} else {
String extensionsAsString = GL11.glGetString(GL11.GL_EXTENSIONS);
supportedExtensions = extensionsAsString.split(" ");
}

extensionMap = new HashMap<>();
for (String extension : supportedExtensions) {
extensionMap.put(extension, Boolean.TRUE);
}
}

}
You may use it or you may just copy the important bits, but I think it should be enough to get the idea across, even without comments.

Thanks (once again) Cornix. I'll check it out soon and tell you what happened. Last time i used a program to check which version was supported on my computer and it only reached to 1.4 if i recall well.

*

Offline @

  • *
  • 41
Re: Some help with rendering tiles
« Reply #5 on: May 16, 2014, 17:34:16 »
After some modifications, i got this strange error (Similar to that one i got from openAL). I have no idea why i get now this, because i did little to modify the rendering behaviour. Please, someone give me a hand: http://pastebin.com/bfSiBd4E

Happens here:
Code: [Select]
glPushMatrix(); {
glTranslatef(x, y, 0.f);
glCallList(id);
glTranslatef(-x, -y, 0.f);
} glPopMatrix();

*

Offline Cornix

  • *****
  • 487
Re: [UNRESOLVED]Some help with rendering tiles
« Reply #6 on: May 19, 2014, 17:35:14 »
Are you using any buffers at any point?
Because the DirectBuffers can very well cause these kinds of errors if they are not used correctly.

*

Offline @

  • *
  • 41
Re: [UNRESOLVED]Some help with rendering tiles
« Reply #7 on: May 20, 2014, 08:16:19 »
Are you using any buffers at any point?
Because the DirectBuffers can very well cause these kinds of errors if they are not used correctly.

Yes, i use this to bind imagess, but did not give me any problems in the past:

Code: [Select]
public static int bindTexture(ByteBuffer buffer, int size) {
buffer.flip();

glEnable(GL_TEXTURE_2D);

final int id = glGenTextures();

glBindTexture(GL_TEXTURE_2D, id);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, size, size, 0, GL_RGBA, GL_UNSIGNED_BYTE, buffer);

return id;
}

public static ByteBuffer imageToBuffer(BufferedImage bufferedImage) {
return imageToBuffer(bufferedImage, true);
}

public static ByteBuffer imageToBuffer(BufferedImage bufferedImage, final boolean argbToRGBA) {
final int width = bufferedImage.getWidth();
final int height = bufferedImage.getHeight();
final ByteBuffer buffer = ByteBuffer.allocateDirect(width * height * 4);

for( int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
final int argb = bufferedImage.getRGB(x, y);
final byte red = (byte) ((argb >>> 16) & 0xFF);
final byte green = (byte) ((argb >>>  8) & 0xFF);
final byte blue = (byte) ((argb >>>  0) & 0xFF);
final byte alpha = (byte) ((argb >>> 24) & 0xFF);

if (argbToRGBA) buffer.put(red).put(green).put(blue).put(alpha);
else buffer.putInt(argb);
}
}

return buffer;
}

And here i create a new list:

Code: [Select]
@Override public int prepareSet(Runnable r) {
finishDrawingTextures();
final int listID = glGenLists(1);
glNewList(listID, GL_COMPILE);

startDrawingTextures();
batching = true;
r.run();
batching = false;
finishDrawingTextures();  
glEndList();

startDrawingTextures();

return listID;
}

Previous runnable is not a thread, i use it as a lambda.

*

Offline @

  • *
  • 41
Re: [UNRESOLVED]Some help with rendering tiles
« Reply #8 on: May 23, 2014, 16:21:17 »
Silly of me, i was displaying the list on update time instead of draw time, so i did not called glbegin. Now i can start the addon thing.

*

Offline @

  • *
  • 41
Re: Some help with rendering tiles
« Reply #9 on: May 23, 2014, 16:36:12 »
You query the supported extensions.

Here is sample code for a Utility class that I once wrote. I use it to check for the OpenGL-Version and supported extensions:
Code: [Select]
public class GLVersion {

private static GLVersion instance;

public static int getMajorVersion() {
return get().majorVersion;
}

public static int getMinorVersion() {
return get().minorVersion;
}

public static double getVersion() {
return get().version;
}

public static String getVersionString() {
return get().versionString;
}

public static boolean isExtensionSupported(GLExtension extension) {
return isExtensionSupported(extension.asString());
}

public static boolean isExtensionSupported(String extension) {
return get().extensionMap.containsKey(extension);
}

public static Collection<String> getSupportedExtensions() {
return get().extensionMap.keySet();
}

private static GLVersion get() {
if (instance == null) {
instance = new GLVersion();
}
return instance;
}

private final String versionString;
private final int majorVersion;
private final int minorVersion;
private final double version;
private final Map<String, Boolean> extensionMap;

private GLVersion() {
versionString = GL11.glGetString(GL11.GL_VERSION);

int majorVersionIndex = versionString.indexOf('.');
int minorVersionIndex = majorVersionIndex + 1;
while (minorVersionIndex < versionString.length() && Character.isDigit(minorVersionIndex)) {
minorVersionIndex++;
}
minorVersionIndex++;

majorVersion = Integer.parseInt(versionString.substring(0, majorVersionIndex));
minorVersion = Integer.parseInt(versionString.substring(majorVersionIndex + 1, minorVersionIndex));
version = Double.parseDouble(versionString.substring(0, minorVersionIndex));

String[] supportedExtensions;
if (majorVersion >= 3) {
int numExtensions = GL11.glGetInteger(GL30.GL_NUM_EXTENSIONS);
supportedExtensions = new String[numExtensions];
for (int i = 0; i < numExtensions; i++) {
supportedExtensions[i] = GL30.glGetStringi(GL11.GL_EXTENSIONS, i);
}
} else {
String extensionsAsString = GL11.glGetString(GL11.GL_EXTENSIONS);
supportedExtensions = extensionsAsString.split(" ");
}

extensionMap = new HashMap<>();
for (String extension : supportedExtensions) {
extensionMap.put(extension, Boolean.TRUE);
}
}

}
You may use it or you may just copy the important bits, but I think it should be enough to get the idea across, even without comments.

I cannot locate GLExtension class, can you give me a hand?

*

Offline Cornix

  • *****
  • 487
Re: [UNRESOLVED]Some help with rendering tiles
« Reply #10 on: May 23, 2014, 19:42:33 »
That was just an Enum that I created for myself. I just forgot to remove it from the code.
I just orefer to use enums instead of regular OpenGL-Integer-Enums because then I can be sure to never use the wrong value by mistake.

*

Offline @

  • *
  • 41
Re: [UNRESOLVED]Some help with rendering tiles
« Reply #11 on: May 26, 2014, 09:07:45 »
That was just an Enum that I created for myself. I just forgot to remove it from the code.
I just orefer to use enums instead of regular OpenGL-Integer-Enums because then I can be sure to never use the wrong value by mistake.

Then, where can i get those integers and its correspondences? (or maybe your enum)
« Last Edit: May 26, 2014, 09:13:36 by @ »

*

Offline Cornix

  • *****
  • 487
Re: [UNRESOLVED]Some help with rendering tiles
« Reply #12 on: May 26, 2014, 12:41:16 »
The gl-constants are actually Strings. You check whether those Strings are in the SupportedExtensions collection.
If you are looking for any particular extension just google it. You will find all of the strings in the opengl reference pages.