Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>First of all, your <code>draw_rect</code> function has an error. Don't call <code>glClientActiveTexture</code>, it is used for multi-texturing and you don't need it. Calling it with a texture object will either bind some really strange texture unit or, most likely, result in an error.</p> <p>And in the <code>drawTexture</code> function you are actually drawing the triangles in clockwise order. Assuming you didn't flip the y-direction in the projection matrix or something similar, if you have back-face culling enabled your whole geometry will get culled away. Try calling <code>glDisable(GL_CULL_FACE)</code>, although back-face culling should be disabled by default. Or even better, change your vertices to counter-clockwise ordering:</p> <pre><code>box[] = { x,y+h, x,y, x+w,y+h, x+w,y }; </code></pre> <p>You also have a mismatch of texture coordinates to vertices in your <code>drawTexture</code> function, but this shouldn't cause the texture not to be drawn, but rather just look a bit strange. Considering the changes to counter-clockwise ordering from the last paragraph, the texture coordinates should be:</p> <pre><code>tex[] = { 0.0f,1.0f, 0.0f,0.0f, 1.0f,1.0f, 1.0f, 0.0f }; </code></pre> <p><strong>EDIT:</strong> Your <code>draw_rect</code> function is also messing up the state, because you enable the vertex and color arrays, but then don't disable them again when you are finished with rendering. When you now want to draw something different without a color array (like in <code>drawTexture</code>), the color array is still enabled and uses some arbitrary data. So you should add</p> <pre><code>glDisableClientState(GL_COLOR_ARRAY); glDisableClientState(GL_VERTEX_ARRAY); </code></pre> <p>right after</p> <pre><code>glDisableClientState(GL_TEXTURE_COORD_ARRAY); </code></pre> <p>in <code>draw_rect</code>.</p> <p><strong>EDIT:</strong> And you should also wrap the drawTexture function in a pair of <code>glEnable(GL_TEXTURE_2D)</code> and <code>glDisable(GL_TEXTURE_2D)</code>. You enable texturing in the initialization code, which is wrong. You should set all neccessary state right before rendering, especially such highly object-dependent state like texturing. For example once you call <code>draw_rect</code> before <code>drawTexture</code>, you end up with disabled texturing, although you enabled it in the initialization code and thought it to be always enabled. Do you see that this is not a good idea?</p> <p><strong>EDIT:</strong> I just spotted another error. In <code>draw_rect</code> you call <code>glEnable</code> and <code>glDisable</code> with <code>GL_TEXTURE_COORD_ARRAY</code>, which is wrong. You have to use <code>glEnableClientState</code> and <code>glDisableClientState</code> for enabling/disabling vertex arrays, like you did int <code>drawTexture</code>.</p> <p>So as a little mid-way conclusion your functions should actually look like:</p> <pre><code>void drawTexture(GLuint texture, float x, float y, float w, float h) { glBindTexture(GL_TEXTURE_2D,texture); glEnable(GL_TEXTURE_2D); GLfloat box[] = {x,y+h, x+w,y+h, x,y, x+w,y}; GLfloat tex[] = {0,0, 1,0, 1,1, 0,1}; glTexCoordPointer(2, GL_FLOAT, 0, tex); glEnableClientState(GL_TEXTURE_COORD_ARRAY); glVertexPointer(2, GL_FLOAT, 0, box); glEnableClientState(GL_VERTEX_ARRAY); glDrawArrays(GL_TRIANGLE_STRIP,0,4); glDisableClientState(GL_VERTEX_ARRAY); glDisableClientState(GL_TEXTURE_COORD_ARRAY); glDisable(GL_TEXTURE_2D); } void draw_rect(RectObject* robj) { if ([robj texture] != -1) { glBindTexture(GL_TEXTURE_2D, [robj texture]); glEnable(GL_TEXTURE_2D); glTexCoordPointer(2, GL_FLOAT, 0, defaultTexCoord); glEnableClientState(GL_TEXTURE_COORD_ARRAY); } glColorPointer(4, GL_UNSIGNED_BYTE, 0, [robj colors]); glEnableClientState(GL_COLOR_ARRAY); glVertexPointer(2, GL_FLOAT, 0, [robj vertices]); glEnableClientState(GL_VERTEX_ARRAY); glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); glDisableClientState(GL_VERTEX_ARRAY); glDisableClientState(GL_COLOR_ARRAY); if ([robj texture] != -1) { glDisableClientState(GL_TEXTURE_COORD_ARRAY); glDisable(GL_TEXTURE_2D); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload