Skip to main content
Notice removed Improve details by Cilenco
Bounty Ended with tubberd's answer chosen by Cilenco
Notice added Improve details by Cilenco
Bounty Started worth 50 reputation by Cilenco
added 49 characters in body
Source Link
Cilenco
  • 113
  • 1
  • 8
float vertices [] = {-1,1,1,1,1,1,-1,-1,1,1,-1,1,-1,1,-1,1,1,-1,-1,-1,-1,1,-1,-1};
byte drawOrder[] = {1,3,0,0,3,2,4,6,5,5,6,7,0,2,4,4,2,6,5,7,1,1,7,3,5,1,4,4,1,0,6,2,7,7,2,3};

private final void bindTexture()
{
    int shaderTextureUnit = GLES20.glGetUniformLocation(shaderProgram, "u_TextureUnit");
        
    GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
    GLES20.glBindTexture(GLES20.GL_TEXTURE_CUBE_MAP, texture);
    GLES20.glUniform1i(shaderTextureUnit, 0);
}

public void draw(float[] viewMatrix) // viewMatrix = Camera = data array
{
    GLES20.glUseProgram(shaderProgram);
    bindTexture(); // Load Texture
                
    shaderPosition = GLES20.glGetAttribLocation(shaderProgram, "a_Position");
    shaderMatrix = GLES20.glGetUniformLocation(shaderProgram, "u_Matrix");
        
    // get handle to shape's transformation matrix
    GLES20.glUniformMatrix4fv(shaderMatrix, 1, false, vpMatrix, 0);
        
    // Prepare the triangle coordinate data
    GLES20.glEnableVertexAttribArray(shaderPosition);
    GLES20.glVertexAttribPointer(shaderPosition, 3, GLES20.GL_FLOAT, false, 0, vertexBuffer);
        
    // Draw the object to the screen GL_UNSIGNED_BYTE
    GLES20.glDrawElements(GLES20.GL_TRIANGLES, orderBuffer.capacity(), GLES20.GL_UNSIGNED_BYTE, orderBuffer);
    GLES20.glDisableVertexAttribArray(shaderPosition); // Free the position in shader
}
private float angle = 90;

Matrix.setLookAtM(data, 0,   0, 0, 0,   0, 0, -1,   0, 1, 0); // the viewMatrix
// Z+ out of screen     Z- in screen

public void onTouch()
{
    angle += 2;
        
    float s = (float) Math.sin(angle * Math.PI / 180);
    float c = (float) Math.cos(angle * Math.PI / 180);

    Matrix.setLookAtM(data, 0,   0, 0, 0,   c, 0, -s,   0, 1,  0); // the viewMatrix
}
float vertices [] = {-1,1,1,1,1,1,-1,-1,1,1,-1,1,-1,1,-1,1,1,-1,-1,-1,-1,1,-1,-1};
byte drawOrder[] = {1,3,0,0,3,2,4,6,5,5,6,7,0,2,4,4,2,6,5,7,1,1,7,3,5,1,4,4,1,0,6,2,7,7,2,3};

private final void bindTexture()
{
    int shaderTextureUnit = GLES20.glGetUniformLocation(shaderProgram, "u_TextureUnit");
        
    GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
    GLES20.glBindTexture(GLES20.GL_TEXTURE_CUBE_MAP, texture);
    GLES20.glUniform1i(shaderTextureUnit, 0);
}

public void draw(float[] viewMatrix) // viewMatrix = Camera
{
    GLES20.glUseProgram(shaderProgram);
    bindTexture(); // Load Texture
                
    shaderPosition = GLES20.glGetAttribLocation(shaderProgram, "a_Position");
    shaderMatrix = GLES20.glGetUniformLocation(shaderProgram, "u_Matrix");
        
    // get handle to shape's transformation matrix
    GLES20.glUniformMatrix4fv(shaderMatrix, 1, false, vpMatrix, 0);
        
    // Prepare the triangle coordinate data
    GLES20.glEnableVertexAttribArray(shaderPosition);
    GLES20.glVertexAttribPointer(shaderPosition, 3, GLES20.GL_FLOAT, false, 0, vertexBuffer);
        
    // Draw the object to the screen GL_UNSIGNED_BYTE
    GLES20.glDrawElements(GLES20.GL_TRIANGLES, orderBuffer.capacity(), GLES20.GL_UNSIGNED_BYTE, orderBuffer);
    GLES20.glDisableVertexAttribArray(shaderPosition); // Free the position in shader
}
private float angle = 90;

Matrix.setLookAtM(data, 0,   0, 0, 0,   0, 0, -1,   0, 1, 0);
// Z+ out of screen     Z- in screen

public void onTouch()
{
    angle += 2;
        
    float s = (float) Math.sin(angle * Math.PI / 180);
    float c = (float) Math.cos(angle * Math.PI / 180);

    Matrix.setLookAtM(data, 0,   0, 0, 0,   c, 0, -s,   0, 1,  0);
}
float vertices [] = {-1,1,1,1,1,1,-1,-1,1,1,-1,1,-1,1,-1,1,1,-1,-1,-1,-1,1,-1,-1};
byte drawOrder[] = {1,3,0,0,3,2,4,6,5,5,6,7,0,2,4,4,2,6,5,7,1,1,7,3,5,1,4,4,1,0,6,2,7,7,2,3};

private final void bindTexture()
{
    int shaderTextureUnit = GLES20.glGetUniformLocation(shaderProgram, "u_TextureUnit");
        
    GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
    GLES20.glBindTexture(GLES20.GL_TEXTURE_CUBE_MAP, texture);
    GLES20.glUniform1i(shaderTextureUnit, 0);
}

public void draw(float[] viewMatrix) // viewMatrix = Camera = data array
{
    GLES20.glUseProgram(shaderProgram);
    bindTexture(); // Load Texture
                
    shaderPosition = GLES20.glGetAttribLocation(shaderProgram, "a_Position");
    shaderMatrix = GLES20.glGetUniformLocation(shaderProgram, "u_Matrix");
        
    // get handle to shape's transformation matrix
    GLES20.glUniformMatrix4fv(shaderMatrix, 1, false, vpMatrix, 0);
        
    // Prepare the triangle coordinate data
    GLES20.glEnableVertexAttribArray(shaderPosition);
    GLES20.glVertexAttribPointer(shaderPosition, 3, GLES20.GL_FLOAT, false, 0, vertexBuffer);
        
    // Draw the object to the screen GL_UNSIGNED_BYTE
    GLES20.glDrawElements(GLES20.GL_TRIANGLES, orderBuffer.capacity(), GLES20.GL_UNSIGNED_BYTE, orderBuffer);
    GLES20.glDisableVertexAttribArray(shaderPosition); // Free the position in shader
}
private float angle = 90;

Matrix.setLookAtM(data, 0,   0, 0, 0,   0, 0, -1,   0, 1, 0); // the viewMatrix
// Z+ out of screen     Z- in screen

public void onTouch()
{
    angle += 2;
        
    float s = (float) Math.sin(angle * Math.PI / 180);
    float c = (float) Math.cos(angle * Math.PI / 180);

    Matrix.setLookAtM(data, 0,   0, 0, 0,   c, 0, -s,   0, 1,  0); // the viewMatrix
}
Source Link
Cilenco
  • 113
  • 1
  • 8

Problems with rendering a SkyBox

At the moment I'm writing an Android OpenGL ES 2.0 game but now I get stuck on rendering a SkyBox. Here is my (a bit simplified) code for the SkyBox:

float vertices [] = {-1,1,1,1,1,1,-1,-1,1,1,-1,1,-1,1,-1,1,1,-1,-1,-1,-1,1,-1,-1};
byte drawOrder[] = {1,3,0,0,3,2,4,6,5,5,6,7,0,2,4,4,2,6,5,7,1,1,7,3,5,1,4,4,1,0,6,2,7,7,2,3};

private final void bindTexture()
{
    int shaderTextureUnit = GLES20.glGetUniformLocation(shaderProgram, "u_TextureUnit");
        
    GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
    GLES20.glBindTexture(GLES20.GL_TEXTURE_CUBE_MAP, texture);
    GLES20.glUniform1i(shaderTextureUnit, 0);
}

public void draw(float[] viewMatrix) // viewMatrix = Camera
{
    GLES20.glUseProgram(shaderProgram);
    bindTexture(); // Load Texture
                
    shaderPosition = GLES20.glGetAttribLocation(shaderProgram, "a_Position");
    shaderMatrix = GLES20.glGetUniformLocation(shaderProgram, "u_Matrix");
        
    // get handle to shape's transformation matrix
    GLES20.glUniformMatrix4fv(shaderMatrix, 1, false, vpMatrix, 0);
        
    // Prepare the triangle coordinate data
    GLES20.glEnableVertexAttribArray(shaderPosition);
    GLES20.glVertexAttribPointer(shaderPosition, 3, GLES20.GL_FLOAT, false, 0, vertexBuffer);
        
    // Draw the object to the screen GL_UNSIGNED_BYTE
    GLES20.glDrawElements(GLES20.GL_TRIANGLES, orderBuffer.capacity(), GLES20.GL_UNSIGNED_BYTE, orderBuffer);
    GLES20.glDisableVertexAttribArray(shaderPosition); // Free the position in shader
}

I think this code should be perfect because I got it from an OpenGL for beginners book. My problem now is that it looks like this if I render the SkyBox to the screen. I initialize my Camera with this code and here you have also the code for my touch event. If my considerations are right this should produce a circle around the origin so I can see all sides of the SkyBox.

private float angle = 90;

Matrix.setLookAtM(data, 0,   0, 0, 0,   0, 0, -1,   0, 1, 0);
// Z+ out of screen     Z- in screen

public void onTouch()
{
    angle += 2;
        
    float s = (float) Math.sin(angle * Math.PI / 180);
    float c = (float) Math.cos(angle * Math.PI / 180);

    Matrix.setLookAtM(data, 0,   0, 0, 0,   c, 0, -s,   0, 1,  0);
}

The draw Method from the SkyBox is called with the data Matrix from the camera (ViewMatrix) and with disabled depthBuffer. Do you have any ideas why the SkyBox is rendered so strange? What is wrong with my code? Only to be sure that all is right with my shaders here is the code from them:

FragmentShader

precision mediump float; 

uniform samplerCube u_TextureUnit;
varying vec3 v_Position;
                                            
void main()                         
{
    gl_FragColor = textureCube(u_TextureUnit, v_Position);    
}

VertexShader

uniform mat4 u_Matrix;
attribute vec3 a_Position;  
varying vec3 v_Position;

void main()                    
{                                                 
    v_Position = a_Position;    
    v_Position.z = -v_Position.z; 
               
    gl_Position = u_Matrix * vec4(a_Position, 1.0);
    gl_Position = gl_Position.xyww;
}