Jump to content
  • Advertisement
Sign in to follow this  
riekhof

Lesson 6 newbie question

This topic is 4859 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 went thru lesson 6, and after recovering from a texture induced headache, I proceeded to do as suggested and make the cube have six different textures on my own (using JOGL). I settled on just 3 textures, each texture would be on two sides. To my suprise, I was able to do this fairly quickly, but I noticed the CPU usage skyrocketed from practically nothing with one texture to sucking up over 50% with 3. I had moved a lot of the texture specific code out of the init() method and into the display method, and I assumed that was the reason. I tried moving code back into init(), as much as I thought I could get away with. However, its still sucking up over 50% cpu. The only texture method calls I left in display() are the ones to gl.glTexParameteri() and gl.glTexImage2D() Am I doing something stupid to hurt the performance? My display method is below. tx, Darrel ============================ public void display(GLDrawable gLDrawable) { final GL gl = gLDrawable.getGL(); gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT); gl.glLoadIdentity(); // Reset The View gl.glTranslatef(0.0f,0.0f,-5.0f); gl.glRotatef(xrot,1.0f,0.0f,0.0f); gl.glRotatef(yrot,0.0f,1.0f,0.0f); gl.glRotatef(zrot,0.0f,0.0f,1.0f); gl.glTexParameteri(GL.GL_TEXTURE_2D,GL.GL_TEXTURE_MIN_FILTER,GL.GL_LINEAR); gl.glTexParameteri(GL.GL_TEXTURE_2D,GL.GL_TEXTURE_MAG_FILTER,GL.GL_LINEAR); // select texture gl.glTexImage2D( GL.GL_TEXTURE_2D, 0, GL.GL_RGB, textureBeans[0].getWidth(), textureBeans[0].getHeight(), 0, GL.GL_RGB, GL.GL_UNSIGNED_BYTE, textureBeans[0].getByteBuffer() ); gl.glBegin(GL.GL_QUADS); // Front Face gl.glTexCoord2f(0.0f, 0.0f); gl.glVertex3f(-1.0f, -1.0f, 1.0f); gl.glTexCoord2f(1.0f, 0.0f); gl.glVertex3f( 1.0f, -1.0f, 1.0f); gl.glTexCoord2f(1.0f, 1.0f); gl.glVertex3f( 1.0f, 1.0f, 1.0f); gl.glTexCoord2f(0.0f, 1.0f); gl.glVertex3f(-1.0f, 1.0f, 1.0f); // Back Face gl.glTexCoord2f(1.0f, 0.0f); gl.glVertex3f(-1.0f, -1.0f, -1.0f); gl.glTexCoord2f(1.0f, 1.0f); gl.glVertex3f(-1.0f, 1.0f, -1.0f); gl.glTexCoord2f(0.0f, 1.0f); gl.glVertex3f( 1.0f, 1.0f, -1.0f); gl.glTexCoord2f(0.0f, 0.0f); gl.glVertex3f( 1.0f, -1.0f, -1.0f); gl.glEnd(); // select texture gl.glTexImage2D( GL.GL_TEXTURE_2D, 0, GL.GL_RGB, textureBeans[1].getWidth(), textureBeans[1].getHeight(), 0, GL.GL_RGB, GL.GL_UNSIGNED_BYTE, textureBeans[1].getByteBuffer() ); // gl.glTexParameteri(GL.GL_TEXTURE_2D,GL.GL_TEXTURE_MIN_FILTER,GL.GL_LINEAR); // gl.glTexParameteri(GL.GL_TEXTURE_2D,GL.GL_TEXTURE_MAG_FILTER,GL.GL_LINEAR); gl.glBegin(GL.GL_QUADS); // Top Face gl.glTexCoord2f(0.0f, 1.0f); gl.glVertex3f(-1.0f, 1.0f, -1.0f); gl.glTexCoord2f(0.0f, 0.0f); gl.glVertex3f(-1.0f, 1.0f, 1.0f); gl.glTexCoord2f(1.0f, 0.0f); gl.glVertex3f( 1.0f, 1.0f, 1.0f); gl.glTexCoord2f(1.0f, 1.0f); gl.glVertex3f( 1.0f, 1.0f, -1.0f); // Bottom Face gl.glTexCoord2f(1.0f, 1.0f); gl.glVertex3f(-1.0f, -1.0f, -1.0f); gl.glTexCoord2f(0.0f, 1.0f); gl.glVertex3f( 1.0f, -1.0f, -1.0f); gl.glTexCoord2f(0.0f, 0.0f); gl.glVertex3f( 1.0f, -1.0f, 1.0f); gl.glTexCoord2f(1.0f, 0.0f); gl.glVertex3f(-1.0f, -1.0f, 1.0f); gl.glEnd(); // select texture gl.glTexImage2D( GL.GL_TEXTURE_2D, 0, GL.GL_RGB, textureBeans[2].getWidth(), textureBeans[2].getHeight(), 0, GL.GL_RGB, GL.GL_UNSIGNED_BYTE, textureBeans[2].getByteBuffer() ); // gl.glTexParameteri(GL.GL_TEXTURE_2D,GL.GL_TEXTURE_MIN_FILTER,GL.GL_LINEAR); // gl.glTexParameteri(GL.GL_TEXTURE_2D,GL.GL_TEXTURE_MAG_FILTER,GL.GL_LINEAR); gl.glBegin(GL.GL_QUADS); // Right face gl.glTexCoord2f(1.0f, 0.0f); gl.glVertex3f( 1.0f, -1.0f, -1.0f); gl.glTexCoord2f(1.0f, 1.0f); gl.glVertex3f( 1.0f, 1.0f, -1.0f); gl.glTexCoord2f(0.0f, 1.0f); gl.glVertex3f( 1.0f, 1.0f, 1.0f); gl.glTexCoord2f(0.0f, 0.0f); gl.glVertex3f( 1.0f, -1.0f, 1.0f); // Left Face gl.glTexCoord2f(0.0f, 0.0f); gl.glVertex3f(-1.0f, -1.0f, -1.0f); gl.glTexCoord2f(1.0f, 0.0f); gl.glVertex3f(-1.0f, -1.0f, 1.0f); gl.glTexCoord2f(1.0f, 1.0f); gl.glVertex3f(-1.0f, 1.0f, 1.0f); gl.glTexCoord2f(0.0f, 1.0f); gl.glVertex3f(-1.0f, 1.0f, -1.0f); gl.glEnd(); xrot+=0.3f; yrot+=0.2f; zrot+=0.4f; }

Share this post


Link to post
Share on other sites
Advertisement
Yes, you are definitely doing something to hurt performance: Do not call glTexImage2D everytime you'd like to select a texture. Instead, when you load you texture, first tell opengl to give you a handle to the texture. This is accomplished via a call to glGenTextures. Then tell opengl that that's the texture you want to use by calling glBindTexture with the ID you get from glGenTexture. Then cram all the data into the texture using glTexImage2D. When it comes to time render things, simply set a texture by calling glBindTexture with the ID corresponding to the texture you want to use. Your display method should look something like:
<source>
public void display(GLDrawable gLDrawable)
{
final GL gl = gLDrawable.getGL();
gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
gl.glLoadIdentity(); // Reset The View
gl.glTranslatef(0.0f,0.0f,-5.0f);

gl.glRotatef(xrot,1.0f,0.0f,0.0f);
gl.glRotatef(yrot,0.0f,1.0f,0.0f);
gl.glRotatef(zrot,0.0f,0.0f,1.0f);

// select texture
gl.glBindTexture(GL.GL_TEXTURE_2D, firstTextureID);
gl.glBegin(GL.GL_QUADS);

// Front Face
gl.glTexCoord2f(0.0f, 0.0f); gl.glVertex3f(-1.0f, -1.0f, 1.0f);
gl.glTexCoord2f(1.0f, 0.0f); gl.glVertex3f( 1.0f, -1.0f, 1.0f);
gl.glTexCoord2f(1.0f, 1.0f); gl.glVertex3f( 1.0f, 1.0f, 1.0f);
gl.glTexCoord2f(0.0f, 1.0f); gl.glVertex3f(-1.0f, 1.0f, 1.0f);
// Back Face
gl.glTexCoord2f(1.0f, 0.0f); gl.glVertex3f(-1.0f, -1.0f, -1.0f);
gl.glTexCoord2f(1.0f, 1.0f); gl.glVertex3f(-1.0f, 1.0f, -1.0f);
gl.glTexCoord2f(0.0f, 1.0f); gl.glVertex3f( 1.0f, 1.0f, -1.0f);
gl.glTexCoord2f(0.0f, 0.0f); gl.glVertex3f( 1.0f, -1.0f, -1.0f);
gl.glEnd();

// select texture
gl.glBindTexture(GL.GL_TEXTURE_2D, secondTextureID);
gl.glBegin(GL.GL_QUADS);
// Top Face
gl.glTexCoord2f(0.0f, 1.0f); gl.glVertex3f(-1.0f, 1.0f, -1.0f);
gl.glTexCoord2f(0.0f, 0.0f); gl.glVertex3f(-1.0f, 1.0f, 1.0f);
gl.glTexCoord2f(1.0f, 0.0f); gl.glVertex3f( 1.0f, 1.0f, 1.0f);
gl.glTexCoord2f(1.0f, 1.0f); gl.glVertex3f( 1.0f, 1.0f, -1.0f);
// Bottom Face
gl.glTexCoord2f(1.0f, 1.0f); gl.glVertex3f(-1.0f, -1.0f, -1.0f);
gl.glTexCoord2f(0.0f, 1.0f); gl.glVertex3f( 1.0f, -1.0f, -1.0f);
gl.glTexCoord2f(0.0f, 0.0f); gl.glVertex3f( 1.0f, -1.0f, 1.0f);
gl.glTexCoord2f(1.0f, 0.0f); gl.glVertex3f(-1.0f, -1.0f, 1.0f);
gl.glEnd();


// select texture
gl.glBindTexture(GL.GL_TEXTURE_2D, thirdTextureID);
gl.glBegin(GL.GL_QUADS);
// Right face
gl.glTexCoord2f(1.0f, 0.0f); gl.glVertex3f( 1.0f, -1.0f, -1.0f);
gl.glTexCoord2f(1.0f, 1.0f); gl.glVertex3f( 1.0f, 1.0f, -1.0f);
gl.glTexCoord2f(0.0f, 1.0f); gl.glVertex3f( 1.0f, 1.0f, 1.0f);
gl.glTexCoord2f(0.0f, 0.0f); gl.glVertex3f( 1.0f, -1.0f, 1.0f);
// Left Face
gl.glTexCoord2f(0.0f, 0.0f); gl.glVertex3f(-1.0f, -1.0f, -1.0f);
gl.glTexCoord2f(1.0f, 0.0f); gl.glVertex3f(-1.0f, -1.0f, 1.0f);
gl.glTexCoord2f(1.0f, 1.0f); gl.glVertex3f(-1.0f, 1.0f, 1.0f);
gl.glTexCoord2f(0.0f, 1.0f); gl.glVertex3f(-1.0f, 1.0f, -1.0f);
gl.glEnd();

xrot+=0.3f;
yrot+=0.2f;
zrot+=0.4f;
}
</source>

Share this post


Link to post
Share on other sites
Thank you tool_2046! It runs now using no CPU, and I almost feel like I understand texture fundamentals.

Share this post


Link to post
Share on other sites
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!