Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (109)
games submitted by our members
Games in WIP (536)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Texture not binding sometimes  (Read 199 times)
0 Members and 1 Guest are viewing this topic.
Offline opiop65

JGO Kernel


Medals: 153
Projects: 7
Exp: 3 years


JumpButton Studios


« Posted 2014-01-08 02:31:22 »

I have a 3D project where I need to bind two textures. One for the font, and one for the game textures. Sometimes, at start up, the game texture doesn't bind, but the font texture does. Other times both of the textures bind. But the font texture always seems to bind correctly. My main concern is my Text class, where I tell OpenGL to render my text. The class is sloppily coded, and I did take code from other people, but here it is:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
public static void renderString(Font font, String string, float x, float y, float scale, Color4f color) {
      renderString(font, string, 16, x, y, 0.1f, 0.1f, scale, color);
   }
   
   public static void renderString(Font font, String string, int gridSize, float x, float y, float charWidth, float charHeight, float scale, Color4f color) {
      glPushAttrib(GL_TEXTURE_BIT | GL_ENABLE_BIT | GL_COLOR_BUFFER_BIT);
      glEnable(GL_CULL_FACE);
      glEnable(GL_TEXTURE_2D);
      font.bind();
     
      glEnable(GL_BLEND);
      glBlendFunc(GL_ONE, GL_ONE);
     
      glColor4f(color.r, color.g, color.b, color.a);
     
      glPushMatrix();
      glScalef(scale, scale, 0f);
      glTranslatef(x, y, 0);
      glBegin(GL_QUADS);
     
      for(int i = 0; i < string.length(); i++){
         int code = (int) string.charAt(i);
         float cellSize = 1.0f / gridSize;
         float cellX = ((int) code % gridSize) * cellSize;
         float cellY = ((int) code / gridSize) * cellSize;
         glTexCoord2f(cellX, cellY + cellSize);
         glVertex2f(i * charWidth / 3, y);
         
         glTexCoord2f(cellX + cellSize, cellY + cellSize);
         glVertex2f(i * charWidth / 3 + charWidth / 2, y);
         
         glTexCoord2f(cellX + cellSize, cellY);
         glVertex2f(i * charWidth / 3 + charWidth / 2, y + charHeight);
         
         glTexCoord2f(cellX, cellY);
         glVertex2f(i * charWidth / 3, y + charHeight);
      }
      glEnd();
      glPopMatrix();
      glPopAttrib();
   }

The method that renders both the world and the text is simple:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
@Override
   public void render() {
      render3D();
     
      worldManager.render();
      glLoadIdentity();
     
      if (renderText) {
         render2D();
         renderText();
      }
   }

WorldManager.render() binds my world texture spritesheet, and then renders my chunks (voxel engine). It's this:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
public void render() {
      Constants.chunksFrustum = 0;
      Spritesheet.tiles.bind();
      getMobManager().getPlayer().getCamera().applyTranslations();
      for (int i = 0; i < activeChunks.size(); i++) {
         if (Math.abs(activeChunks.get(i).getCenter().x - (int) mobManager.getPlayer().getX()) < 64 && activeChunks.get(i).getCenter().y - (int) mobManager.getPlayer().getY() < 64 && Math.abs(activeChunks.get(i).getCenter().z - (int) mobManager.getPlayer().getZ()) < 64) {
            if (Frustum.getFrustum().cubeInFrustum(activeChunks.get(i).getPos().x, activeChunks.get(i).getPos().y, activeChunks.get(i).getPos().z, activeChunks.get(i).getPos().x + Constants.CHUNKSIZE, activeChunks.get(i).getPos().y + Constants.CHUNKSIZE, activeChunks.get(i).getPos().z + Constants.CHUNKSIZE)) {
               Constants.chunksFrustum++;
               activeChunks.get(i).render();
            }
         }
      }
      mobManager.render();
   }

My chunks render method is very simple, it just calls glCallList. The strange this is, is that if I move the camera around, the tiles spritesheet will bind, and suddenly everything is fine. What could be the problem here? I feel like this is something to do with the way I push attributes in the Text class, but I don't know.

Edit: I figured out it came from my Frustum class:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
115  
116  
117  
118  
119  
120  
121  
122  
123  
124  
125  
126  
127  
128  
129  
130  
131  
132  
133  
134  
135  
136  
137  
138  
139  
140  
141  
142  
143  
144  
145  
146  
147  
148  
149  
150  
151  
152  
153  
154  
155  
156  
157  
158  
159  
160  
161  
162  
163  
164  
165  
166  
167  
168  
169  
170  
171  
package com.nishu.voxel.utilites;

import java.nio.FloatBuffer;

import org.lwjgl.BufferUtils;
import org.lwjgl.opengl.GL11;

public class Frustum {
   public float[][] m_Frustum = new float[6][4];
   public static final int RIGHT = 0;
   public static final int LEFT = 1;
   public static final int BOTTOM = 2;
   public static final int TOP = 3;
   public static final int BACK = 4;
   public static final int FRONT = 5;
   public static final int A = 0;
   public static final int B = 1;
   public static final int C = 2;
   public static final int D = 3;
   private static Frustum frustum = new Frustum();

   private FloatBuffer _proj = BufferUtils.createFloatBuffer(16);
   private FloatBuffer _modl = BufferUtils.createFloatBuffer(16);
   private FloatBuffer _clip = BufferUtils.createFloatBuffer(16);
   float[] proj = new float[16];
   float[] modl = new float[16];
   float[] clip = new float[16];

   public static Frustum getFrustum() {
      frustum.calculateFrustum();
      return frustum;
   }

   private void normalizePlane(float[][] frustum, int side) {
      float magnitude = (float) Math.sqrt(frustum[side][0] * frustum[side][0] + frustum[side][1] * frustum[side][1] + frustum[side][2] * frustum[side][2]);

      frustum[side][0] /= magnitude;
      frustum[side][1] /= magnitude;
      frustum[side][2] /= magnitude;
      frustum[side][3] /= magnitude;
   }

   private void calculateFrustum() {
      this._proj.clear();
      this._modl.clear();
      this._clip.clear();

      GL11.glGetFloat(2983, this._proj);

      GL11.glGetFloat(2982, this._modl);

      this._proj.flip().limit(16);
      this._proj.get(this.proj);
      this._modl.flip().limit(16);
      this._modl.get(this.modl);

      this.clip[0] = (this.modl[0] * this.proj[0] + this.modl[1] * this.proj[4] + this.modl[2] * this.proj[8] + this.modl[3] * this.proj[12]);
      this.clip[1] = (this.modl[0] * this.proj[1] + this.modl[1] * this.proj[5] + this.modl[2] * this.proj[9] + this.modl[3] * this.proj[13]);
      this.clip[2] = (this.modl[0] * this.proj[2] + this.modl[1] * this.proj[6] + this.modl[2] * this.proj[10] + this.modl[3] * this.proj[14]);
      this.clip[3] = (this.modl[0] * this.proj[3] + this.modl[1] * this.proj[7] + this.modl[2] * this.proj[11] + this.modl[3] * this.proj[15]);

      this.clip[4] = (this.modl[4] * this.proj[0] + this.modl[5] * this.proj[4] + this.modl[6] * this.proj[8] + this.modl[7] * this.proj[12]);
      this.clip[5] = (this.modl[4] * this.proj[1] + this.modl[5] * this.proj[5] + this.modl[6] * this.proj[9] + this.modl[7] * this.proj[13]);
      this.clip[6] = (this.modl[4] * this.proj[2] + this.modl[5] * this.proj[6] + this.modl[6] * this.proj[10] + this.modl[7] * this.proj[14]);
      this.clip[7] = (this.modl[4] * this.proj[3] + this.modl[5] * this.proj[7] + this.modl[6] * this.proj[11] + this.modl[7] * this.proj[15]);

      this.clip[8] = (this.modl[8] * this.proj[0] + this.modl[9] * this.proj[4] + this.modl[10] * this.proj[8] + this.modl[11] * this.proj[12]);
      this.clip[9] = (this.modl[8] * this.proj[1] + this.modl[9] * this.proj[5] + this.modl[10] * this.proj[9] + this.modl[11] * this.proj[13]);
      this.clip[10] = (this.modl[8] * this.proj[2] + this.modl[9] * this.proj[6] + this.modl[10] * this.proj[10] + this.modl[11] * this.proj[14]);
      this.clip[11] = (this.modl[8] * this.proj[3] + this.modl[9] * this.proj[7] + this.modl[10] * this.proj[11] + this.modl[11] * this.proj[15]);

      this.clip[12] = (this.modl[12] * this.proj[0] + this.modl[13] * this.proj[4] + this.modl[14] * this.proj[8] + this.modl[15] * this.proj[12]);
      this.clip[13] = (this.modl[12] * this.proj[1] + this.modl[13] * this.proj[5] + this.modl[14] * this.proj[9] + this.modl[15] * this.proj[13]);
      this.clip[14] = (this.modl[12] * this.proj[2] + this.modl[13] * this.proj[6] + this.modl[14] * this.proj[10] + this.modl[15] * this.proj[14]);
      this.clip[15] = (this.modl[12] * this.proj[3] + this.modl[13] * this.proj[7] + this.modl[14] * this.proj[11] + this.modl[15] * this.proj[15]);

      this.m_Frustum[0][0] = (this.clip[3] - this.clip[0]);
      this.m_Frustum[0][1] = (this.clip[7] - this.clip[4]);
      this.m_Frustum[0][2] = (this.clip[11] - this.clip[8]);
      this.m_Frustum[0][3] = (this.clip[15] - this.clip[12]);

      normalizePlane(this.m_Frustum, 0);

      this.m_Frustum[1][0] = (this.clip[3] + this.clip[0]);
      this.m_Frustum[1][1] = (this.clip[7] + this.clip[4]);
      this.m_Frustum[1][2] = (this.clip[11] + this.clip[8]);
      this.m_Frustum[1][3] = (this.clip[15] + this.clip[12]);

      normalizePlane(this.m_Frustum, 1);

      this.m_Frustum[2][0] = (this.clip[3] + this.clip[1]);
      this.m_Frustum[2][1] = (this.clip[7] + this.clip[5]);
      this.m_Frustum[2][2] = (this.clip[11] + this.clip[9]);
      this.m_Frustum[2][3] = (this.clip[15] + this.clip[13]);

      normalizePlane(this.m_Frustum, 2);

      this.m_Frustum[3][0] = (this.clip[3] - this.clip[1]);
      this.m_Frustum[3][1] = (this.clip[7] - this.clip[5]);
      this.m_Frustum[3][2] = (this.clip[11] - this.clip[9]);
      this.m_Frustum[3][3] = (this.clip[15] - this.clip[13]);

      normalizePlane(this.m_Frustum, 3);

      this.m_Frustum[4][0] = (this.clip[3] - this.clip[2]);
      this.m_Frustum[4][1] = (this.clip[7] - this.clip[6]);
      this.m_Frustum[4][2] = (this.clip[11] - this.clip[10]);
      this.m_Frustum[4][3] = (this.clip[15] - this.clip[14]);

      normalizePlane(this.m_Frustum, 4);

      this.m_Frustum[5][0] = (this.clip[3] + this.clip[2]);
      this.m_Frustum[5][1] = (this.clip[7] + this.clip[6]);
      this.m_Frustum[5][2] = (this.clip[11] + this.clip[10]);
      this.m_Frustum[5][3] = (this.clip[15] + this.clip[14]);

      normalizePlane(this.m_Frustum, 5);
   }

   public boolean pointInFrustum(float x, float y, float z) {
      for (int i = 0; i < 6; i++) {
         if (this.m_Frustum[i][0] * x + this.m_Frustum[i][1] * y + this.m_Frustum[i][2] * z + this.m_Frustum[i][3] <= 0.0F) {
            return false;
         }
      }

      return true;
   }

   public boolean sphereInFrustum(float x, float y, float z, float radius) {
      for (int i = 0; i < 6; i++) {
         if (this.m_Frustum[i][0] * x + this.m_Frustum[i][1] * y + this.m_Frustum[i][2] * z + this.m_Frustum[i][3] <= -radius) {
            return false;
         }
      }

      return true;
   }

   public boolean cubeFullyInFrustum(float x1, float y1, float z1, float x2, float y2, float z2) {
      for (int i = 0; i < 6; i++) {
         if (this.m_Frustum[i][0] * x1 + this.m_Frustum[i][1] * y1 + this.m_Frustum[i][2] * z1 + this.m_Frustum[i][3] <= 0.0F)
            return false;
         if (this.m_Frustum[i][0] * x2 + this.m_Frustum[i][1] * y1 + this.m_Frustum[i][2] * z1 + this.m_Frustum[i][3] <= 0.0F)
            return false;
         if (this.m_Frustum[i][0] * x1 + this.m_Frustum[i][1] * y2 + this.m_Frustum[i][2] * z1 + this.m_Frustum[i][3] <= 0.0F)
            return false;
         if (this.m_Frustum[i][0] * x2 + this.m_Frustum[i][1] * y2 + this.m_Frustum[i][2] * z1 + this.m_Frustum[i][3] <= 0.0F)
            return false;
         if (this.m_Frustum[i][0] * x1 + this.m_Frustum[i][1] * y1 + this.m_Frustum[i][2] * z2 + this.m_Frustum[i][3] <= 0.0F)
            return false;
         if (this.m_Frustum[i][0] * x2 + this.m_Frustum[i][1] * y1 + this.m_Frustum[i][2] * z2 + this.m_Frustum[i][3] <= 0.0F)
            return false;
         if (this.m_Frustum[i][0] * x1 + this.m_Frustum[i][1] * y2 + this.m_Frustum[i][2] * z2 + this.m_Frustum[i][3] <= 0.0F)
            return false;
         if (this.m_Frustum[i][0] * x2 + this.m_Frustum[i][1] * y2 + this.m_Frustum[i][2] * z2 + this.m_Frustum[i][3] <= 0.0F)
            return false;
      }

      return true;
   }

   public boolean cubeInFrustum(float x1, float y1, float z1, float x2, float y2, float z2) {
      for (int i = 0; i < 6; i++) {
         if ((this.m_Frustum[i][0] * x1 + this.m_Frustum[i][1] * y1 + this.m_Frustum[i][2] * z1 + this.m_Frustum[i][3] <= 0.0F) && (this.m_Frustum[i][0] * x2 + this.m_Frustum[i][1] * y1 + this.m_Frustum[i][2] * z1 + this.m_Frustum[i][3] <= 0.0F) && (this.m_Frustum[i][0] * x1 + this.m_Frustum[i][1] * y2 + this.m_Frustum[i][2] * z1 + this.m_Frustum[i][3] <= 0.0F) && (this.m_Frustum[i][0] * x2 + this.m_Frustum[i][1] * y2 + this.m_Frustum[i][2] * z1 + this.m_Frustum[i][3] <= 0.0F) && (this.m_Frustum[i][0] * x1 + this.m_Frustum[i][1] * y1 + this.m_Frustum[i][2] * z2 + this.m_Frustum[i][3] <= 0.0F) && (this.m_Frustum[i][0] * x2 + this.m_Frustum[i][1] * y1 + this.m_Frustum[i][2] * z2 + this.m_Frustum[i][3] <= 0.0F) && (this.m_Frustum[i][0] * x1 + this.m_Frustum[i][1] * y2 + this.m_Frustum[i][2] * z2 + this.m_Frustum[i][3] <= 0.0F) && (this.m_Frustum[i][0] * x2 + this.m_Frustum[i][1] * y2 + this.m_Frustum[i][2] * z2 + this.m_Frustum[i][3] <= 0.0F)) {
            return false;
         }
      }
      return true;
   }
}

Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

CogWheelz (18 views)
2014-07-30 21:08:39

Riven (26 views)
2014-07-29 18:09:19

Riven (15 views)
2014-07-29 18:08:52

Dwinin (13 views)
2014-07-29 10:59:34

E.R. Fleming (34 views)
2014-07-29 03:07:13

E.R. Fleming (12 views)
2014-07-29 03:06:25

pw (43 views)
2014-07-24 01:59:36

Riven (44 views)
2014-07-23 21:16:32

Riven (30 views)
2014-07-23 21:07:15

Riven (31 views)
2014-07-23 20:56:16
List of Learning Resources
by SilverTiger
2014-07-31 18:29:50

List of Learning Resources
by SilverTiger
2014-07-31 18:26:06

List of Learning Resources
by SilverTiger
2014-07-31 13:54:12

HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!