Jump to content
  • Advertisement
Sign in to follow this  
Darragh

OpenGL Texure / Color Array Problems

This topic is 4950 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello, I'm just after implementing an MD2 loader into my gane engine (for java/jogl) and need some help with OpenGL's arrays. I first tried displaying the test MD2 model using immediate mode rendering, but as you can imagine it sucked speed wise. The test model is the 'hobgoblin' from gametutorials.com- which has around 800 or so polys. With immediate mode rendering it took a pretty hefty 500,000 nanoseconds to render.. So instead i've decided to use vertex arrays to try and reduce this figure. And so now i'm down to about 100,000. But.... i'm having problems: The actual geometry of the model displays correctly, but textures and colors do not. I've tried enabling texture coordinate arrays and color arrays but these have no effect on the problem (see code). Any ideas what I am doing wrong? Here is the code which draws the model:

import net.java.games.jogl.GL;
import net.java.games.jogl.util.BufferUtils;
import java.io.*;
import java.nio.*;
import java.util.*;

class ModelMD2
{

// GENERAL VARIABLES //
String modelName;

public boolean loadedOK;
public boolean validTexture;

public int currentTriangle;
public int currentFramePointer;
public int glTextureNumber;	

// MODEL DATA //
public int numTriangles;		
public int numFrames;
public int numVertices;
public int numTexCoords;						

public FloatBuffer 	vertexBuffer;
public FloatBuffer 	uvBuffer;
public IntBuffer 	lightBuffer;				

public String skinName;					
public String frameNames[];				

// ANIMATION ///
public short currentAnimStartFrame;
public short currentAnimEndFrame;
public short currentFrame;
public float currentAnimFPS;
public long  lastFrameMS;

public boolean isPlaying=false;

public ModelMD2(String fileName)
{

modelName = fileName;

MD2Loader.readMD2(fileName);

if 	( !MD2Loader.readError )
	{
	// NO PROBLEM LOADING MD2 MODEL	
	loadedOK = true;
	numTriangles 	= MD2Loader.numTriangles;		
	numFrames 	= MD2Loader.numFrames;
	numVertices	= MD2Loader.numVertices;
	numTexCoords	= MD2Loader.numTexCoords;
	
	// GENERATE VERTEX,LIGHT,AND UV BUFFERS
	generateBuffers();
	
	if	( MD2Loader.numSkins > 0 )
		{
		// ADD SKIN NAME TO THE TEXTURE LIST (IF IT EXISTS)
		skinName 		= MD2Loader.skinNames[0];
		glTextureNumber = Texture_list.textureNames.size();
		Texture_list.textureNames.add(skinName);			
		validTexture = true;
		}
	else
		{
		// NO SKIN EXISTS	
		validTexture = false;
		}
		
	MD2Loader.cleanUpMemory();
	
	}
else
	{
	// ERROR LOADING THE MODEL
		
	MD2Loader.cleanUpMemory();	
	loadedOK = false;
	}
	
}

public void generateBuffers()
{
// CREATE THE VERTEX BUFFER FOR EACH FRAME
vertexBuffer = BufferUtils.newFloatBuffer(numFrames*numTriangles*9);
	
for ( int x = 0 ; x < numFrames 	; x++ )	
for ( int y = 0 ; y < numTriangles 	; y++ )
    {
    vertexBuffer.put(MD2Loader.vertices_x[x][MD2Loader.triangles_v1[y]]);
    vertexBuffer.put(MD2Loader.vertices_y[x][MD2Loader.triangles_v1[y]]);
    vertexBuffer.put(MD2Loader.vertices_z[x][MD2Loader.triangles_v1[y]]);
    vertexBuffer.put(MD2Loader.vertices_x[x][MD2Loader.triangles_v3[y]]);
    vertexBuffer.put(MD2Loader.vertices_y[x][MD2Loader.triangles_v3[y]]);
    vertexBuffer.put(MD2Loader.vertices_z[x][MD2Loader.triangles_v3[y]]);
    vertexBuffer.put(MD2Loader.vertices_x[x][MD2Loader.triangles_v2[y]]);
    vertexBuffer.put(MD2Loader.vertices_y[x][MD2Loader.triangles_v2[y]]);
    vertexBuffer.put(MD2Loader.vertices_z[x][MD2Loader.triangles_v2[y]]);	
    }

// CREATE THE UV COORDINATES BUFFER
uvBuffer = BufferUtils.newFloatBuffer(numTriangles*3*2);

for ( int x = 0 ; x < numTriangles ; x++ )
    {
    uvBuffer.put(MD2Loader.textureCoordinates_u[MD2Loader.triangles_t1[x]]);
    uvBuffer.put(MD2Loader.textureCoordinates_v[MD2Loader.triangles_t1[x]]);
    uvBuffer.put(MD2Loader.textureCoordinates_u[MD2Loader.triangles_t3[x]]);
    uvBuffer.put(MD2Loader.textureCoordinates_v[MD2Loader.triangles_t3[x]]);
    uvBuffer.put(MD2Loader.textureCoordinates_u[MD2Loader.triangles_t2[x]]);
    uvBuffer.put(MD2Loader.textureCoordinates_v[MD2Loader.triangles_t2[x]]);
    }

// CREATE THE LIGHTING BUFFER
lightBuffer = BufferUtils.newIntBuffer(numFrames*numTriangles*9);

for ( int x = 0 ; x < numFrames 	; x++ )	
for ( int y = 0 ; y < numTriangles  ; y++ )
  {
  lightBuffer.put(MD2Loader.vertices_normalIndex[x][MD2Loader.triangles_v1[y]]);
  lightBuffer.put(MD2Loader.vertices_normalIndex[x][MD2Loader.triangles_v1[y]]);
  lightBuffer.put(MD2Loader.vertices_normalIndex[x][MD2Loader.triangles_v1[y]]);
  lightBuffer.put(MD2Loader.vertices_normalIndex[x][MD2Loader.triangles_v3[y]]);
  lightBuffer.put(MD2Loader.vertices_normalIndex[x][MD2Loader.triangles_v3[y]]);
  lightBuffer.put(MD2Loader.vertices_normalIndex[x][MD2Loader.triangles_v3[y]]);
  lightBuffer.put(MD2Loader.vertices_normalIndex[x][MD2Loader.triangles_v2[y]]);
  lightBuffer.put(MD2Loader.vertices_normalIndex[x][MD2Loader.triangles_v2[y]]);
  lightBuffer.put(MD2Loader.vertices_normalIndex[x][MD2Loader.triangles_v2[y]]);
  }
	
}


public void renderStatic()
{

if 	( loadedOK	)
	{
	// FIND CURRENT FRAME POSITION WITHIN THE VERTEX BUFFER	
	currentFrame = 20;
	currentFramePointer = currentFrame*numTriangles*3;		
		
	// SELECT THE MODELS CURRENT TEXTURE
	GL_Drawing.myGL.glBindTexture(GL.GL_TEXTURE_2D,this.glTextureNumber);
	
	// GL ENABLE STATEMENTS: NONE OF THESE HELP THE PROBLEM!
	
	//GL_Drawing.myGL.glEnableClientState(GL.GL_VERTEX_ARRAY);
	//GL_Drawing.myGL.glEnableClientState(GL.GL_COLOR_ARRAY);
	//GL_Drawing.myGL.glEnableClientState(GL.GL_TEXTURE_COORD_ARRAY);
		
	// SET BUFFER POINTERS	
   	GL_Drawing.myGL.glVertexPointer(3,GL.GL_FLOAT, 0, vertexBuffer);	
	GL_Drawing.myGL.glColorPointer(3,GL.GL_INT,0,lightBuffer);
	GL_Drawing.myGL.glTexCoordPointer(2,GL.GL_FLOAT, 0,uvBuffer);
	
	// DRAW THE MODEL: ONLY VERTEXS APPEAR TO WORK CORRECTLY	
	GL_Drawing.myGL.glDrawArrays(GL.GL_TRIANGLES,currentFramePointer,numTriangles*3);
	}
	
}

}





By the way, if any of you have any suggestions about how to speed up the rendering of the model, then please by all means let me know. I'm wondering if it would be better to use the glCommands stored in the MD2 to draw the model? Probably not though- since this brings with it a lot of state changes between GL_TRIANGLE_STRIP and GL_TRIANGLE_FAN... Is there any other way this code could be improved? How do you guys render models like these?

Share this post


Link to post
Share on other sites
Advertisement
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!