Jump to content

  • Log In with Google      Sign In   
  • Create Account

We're offering banner ads on our site from just $5!

1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


CirdanValen

Member Since 05 Jul 2009
Offline Last Active Yesterday, 04:30 PM

Topics I've Started

Problem with my projection or vertex buffer?

20 August 2014 - 01:57 PM

I'm having a strange problem, and I don't know what could be causing it. My current code is identical to how I've done this before. 

 

My results:

 

fu55D9z.png

 

What I expect:

3x3 rectangle in the top left corner

 

#include <stdio.h>
#include <GL\glew.h>
#include <GLFW\glfw3.h>
#include "lodepng.h"
 
static const int FALSE = 0;
static const int TRUE = 1;
 
static const char* VERT_SHADER =
    "#version 330\n"
 
    "layout(location=0) in vec4 VertexPosition; "
    "layout(location=1) in vec2 UV;"
    "uniform mat4 uProjectionMatrix;"
    /*"out vec2 TexCoords;"*/
 
    "void main(void) {"
    "    gl_Position = uProjectionMatrix*VertexPosition;"
    /*"    TexCoords = UV;"*/
    "}";
 
static const char* FRAG_SHADER =
    "#version 330\n"
 
    /*"uniform sampler2D uDiffuseTexture;"
    "uniform vec4 uColor;"
    "in vec2 TexCoords;"*/
    "out vec4 FragColor;"
 
    "void main(void) {"
   /* "    vec4 texel = texture2D(uDiffuseTexture, TexCoords);"
    "    if(texel.a <= 0) {"
    "         discard;"
    "    }"
    "    FragColor = texel;"*/
    "    FragColor = vec4(1.f);"
    "}";
 
static int g_running;
static GLFWwindow *gl_window;
static float gl_projectionMatrix[16];
 
/*
    Structures
*/
typedef struct _Vertex {
    float x, y, z, w;
    float u, v;
} Vertex;
 
typedef struct _Position {
    float x, y;
} Position;
 
typedef struct _Bitmap {
    unsigned char *pixels;
    unsigned int width, height;
} Bitmap;
 
typedef struct _Texture {
    GLuint id;
    unsigned int width, height;
} Texture;
 
typedef struct _VertexBuffer {
    GLuint bufferObj, vertexArray;
} VertexBuffer;
 
typedef struct _ShaderProgram {
    GLuint vertexShader, fragmentShader, program;
} ShaderProgram;
 
/*
  http://en.wikipedia.org/wiki/Orthographic_projection
*/
void createOrthoProjection(float *projection, float width, float height, float far, float near)  {
 
    const float left = 0;
    const float right = width;
    const float top = 0;
    const float bottom = height;
    
    projection[0] = 2.f / (right - left);
    projection[1] = 0.f;
    projection[2] = 0.f;
    projection[3] = -(right+left) / (right-left);
    projection[4] = 0.f;
    projection[5] = 2.f / (top - bottom);
    projection[6] = 0.f;
    projection[7] = -(top + bottom) / (top - bottom);
    projection[8] = 0.f;
    projection[9] = 0.f;
    projection[10] = -2.f / (far-near);
    projection[11] = (far+near)/(far-near);
    projection[12] = 0.f;
    projection[13] = 0.f;
    projection[14] = 0.f;
    projection[15] = 1.f;
}
 
/*
    Textures
*/
void loadBitmap(const char *filename, Bitmap *bitmap, int *success) {
    int error = lodepng_decode32_file(&bitmap->pixels, &bitmap->width, &bitmap->height, filename);
 
    if (error != 0) {
        printf("Failed to load bitmap. ");
        printf(lodepng_error_text(error));
        success = FALSE;
        return;
    }
}
 
void destroyBitmap(Bitmap *bitmap) {
    free(bitmap->pixels);
}
 
void createTexture(Texture *texture, const Bitmap *bitmap) {
    texture->id = 0;
    glGenTextures(1, &texture->id);
    glBindTexture(GL_TEXTURE_2D, texture);
 
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
 
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, bitmap->width, bitmap->height, 0, 
            GL_RGBA, GL_UNSIGNED_BYTE, bitmap->pixels);
 
    glBindTexture(GL_TEXTURE_2D, 0);
}
 
void destroyTexture(Texture *texture) {
    glDeleteTextures(1, &texture->id);
    texture->id = 0;
}
 
/*
    Vertex Buffer
*/
void createVertexBuffer(VertexBuffer *vertexBuffer, Vertex *vertices) {
    glGenBuffers(1, &vertexBuffer->bufferObj);
    glGenVertexArrays(1, &vertexBuffer->vertexArray);
    glBindVertexArray(vertexBuffer->vertexArray);
 
    glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer->bufferObj);
    glBufferData(GL_ARRAY_BUFFER, sizeof(Vertex) * 6, (const GLvoid*)vertices, GL_STATIC_DRAW);
 
    const unsigned int uvOffset = sizeof(float) * 4;
 
    glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, sizeof(Vertex), 0);
    glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (GLvoid*)uvOffset);
 
    glEnableVertexAttribArray(0);
    glEnableVertexAttribArray(1);
 
    glBindBuffer(GL_ARRAY_BUFFER, 0);
    glBindVertexArray(0);
}
 
void destroyVertexBuffer(VertexBuffer *vertexBuffer) {
    glDeleteBuffers(1, &vertexBuffer->bufferObj);
    glDeleteVertexArrays(1, &vertexBuffer->vertexArray);
}
 
void bindVertexBuffer(VertexBuffer *vertexBuffer) {
    glBindVertexArray(vertexBuffer->vertexArray);
    glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer->bufferObj);
}
 
void drawVertexBufferMode(GLenum mode) {
    glDrawArrays(mode, 0, 6);
}
 
void drawVertexBuffer() {
    drawVertexBufferMode(GL_TRIANGLES);
}
 
void unbindVertexBuffer() {
    glBindVertexArray(0);
    glBindBuffer(GL_ARRAY_BUFFER, 0);
}
 
/*
    Shaders
*/
void compileShader(ShaderProgram *shaderProgram, const char *vertexSrc, const char *fragSrc) {
    GLenum err;
    shaderProgram->vertexShader = glCreateShader(GL_VERTEX_SHADER);
    shaderProgram->fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
 
    if (shaderProgram->vertexShader == 0) {
        printf("Failed to create vertex shader.");
        return;
    }
 
    if (shaderProgram->fragmentShader == 0) {
        printf("Failed to create fragment shader.");
        return;
    }
 
    glShaderSource(shaderProgram->vertexShader, 1, &vertexSrc, NULL);
    glCompileShader(shaderProgram->vertexShader);
    glGetShaderiv(shaderProgram->vertexShader, GL_COMPILE_STATUS, &err);
 
    if (err != GL_TRUE) {
        printf("Failed to compile vertex shader.");
        return;
    }
 
    glShaderSource(shaderProgram->fragmentShader, 1, &fragSrc, NULL);
    glCompileShader(shaderProgram->fragmentShader);
    glGetShaderiv(shaderProgram->fragmentShader, GL_COMPILE_STATUS, &err);
 
    if (err != GL_TRUE) {
        printf("Failed to compile fragment shader.");
        return;
    }
 
    shaderProgram->program = glCreateProgram();
    glAttachShader(shaderProgram->program, shaderProgram->vertexShader);
    glAttachShader(shaderProgram->program, shaderProgram->fragmentShader);
    glLinkProgram(shaderProgram->program);
    
    glGetProgramiv(shaderProgram->program, GL_LINK_STATUS, &err);
 
    if (err != GL_TRUE) {
        printf("Failed to link shader.");
        return;
    }
}
 
void destroyShader(ShaderProgram *shaderProgram) {
    glDetachShader(shaderProgram->program, shaderProgram->vertexShader);
    glDetachShader(shaderProgram->program, shaderProgram->fragmentShader);
 
    glDeleteShader(shaderProgram->vertexShader);
    glDeleteShader(shaderProgram->fragmentShader);
 
    glDeleteProgram(shaderProgram->program);
}
 
GLuint getUniformLocation(const char *name, ShaderProgram *program) {
    GLuint result = 0;
    result = glGetUniformLocation(program->program, name);
 
    return result;
}
 
void setUniformMatrix(float *matrix, const char *name, ShaderProgram *program) {
    GLuint loc = getUniformLocation(name, program);
 
    if (loc == -1) {
        printf("Failed to get uniform location in setUniformMatrix.\n");
        return;
    }
 
    glUniformMatrix4fv(loc, 1, GL_FALSE, matrix);
}
 
/*
    General functions
*/
static int isRunning() {
    return g_running && !glfwWindowShouldClose(gl_window);
}
 
static void initializeGLFW(GLFWwindow **window, int width, int height, int *success) {
    if (!glfwInit()) {
        printf("Failed it inialize GLFW.");
        *success = FALSE;
   <span> </span>    return;
    }
    
    glfwWindowHint(GLFW_RESIZABLE, 0);
    *window = glfwCreateWindow(width, height, "Alignments", NULL, NULL);
    
    if (!*window) {
        printf("Failed to create window.");
        glfwTerminate();
        *success = FALSE;
        return;
    }
    
    glfwMakeContextCurrent(*window);
 
    GLenum glewErr = glewInit();
    if (glewErr != GLEW_OK) {
        printf("Failed to initialize GLEW.");
        printf(glewGetErrorString(glewErr));
        *success = FALSE;
        return;
    }
 
    glClearColor(0.f, 0.f, 0.f, 1.f);
    glViewport(0, 0, width, height);
    *success = TRUE;
}
 
int main(int argc, char **argv) {
    
    int err = FALSE;
    initializeGLFW(&gl_window, 480, 320, &err);
    glDisable(GL_DEPTH_TEST);
    if (err == FALSE) {
        return 1;
    }
    
    createOrthoProjection(gl_projectionMatrix, 480.f, 320.f, 0.f, 1.f);
    
    g_running = TRUE;
    
    ShaderProgram shader;
    compileShader(&shader, VERT_SHADER, FRAG_SHADER);
    glUseProgram(shader.program);
    setUniformMatrix(&gl_projectionMatrix, "uProjectionMatrix", &shader);
 
    Vertex rectangle[6];
    VertexBuffer vbo;
    rectangle[0] = (Vertex){0.f, 0.f, 0.f, 1.f, 0.f, 0.f}; // Top left
    rectangle[1] = (Vertex){3.f, 0.f, 0.f, 1.f, 1.f, 0.f}; // Top right
    rectangle[2] = (Vertex){0.f, 3.f, 0.f, 1.f, 0.f, 1.f}; // Bottom left
    rectangle[3] = (Vertex){3.f, 0.f, 0.f, 1.f, 1.f, 0.f}; // Top left
    rectangle[4] = (Vertex){0.f, 3.f, 0.f, 1.f, 0.f, 1.f}; // Bottom left
    rectangle[5] = (Vertex){3.f, 3.f, 0.f, 1.f, 1.f, 1.f}; // Bottom right
 
    createVertexBuffer(&vbo, &rectangle);
    
 
    bindVertexBuffer(&vbo);
    
    while (isRunning()) {
        glClear(GL_COLOR_BUFFER_BIT);
        glfwPollEvents();
 
        
        drawVertexBuffer();
        
 
        glfwSwapBuffers(gl_window);
    }
    
    unbindVertexBuffer(&vbo);
 
    glUseProgram(0);
    destroyShader(&shader);
    destroyVertexBuffer(&vbo);
    glfwTerminate();
    return 0;
}

Trouble getting a GBuffer up and running

13 May 2014 - 01:29 PM

I'm working towards implementing a deferred lighting system in my 2d game, and have run into trouble with my gbuffer.
 
GBuffer code: http://pastebin.com/4xrcJ5c3
 
Geometry pass shaders:
 

// VERTEX SHADER

#version 330

layout(location=0) in vec4 inPosition;
layout(location=1) in vec2 UV;
uniform mat4 transformMatrix;

out vec2 TexCoords;

void main(void) {
    gl_Position = transformMatrix*inPosition;
    TexCoords = UV;
}

// FRAGMENT SHADER

#version 330

uniform sampler2D diffuseTexture;
uniform sampler2D normalsTexture;
in vec2 TexCoords;

layout(location=0) out vec3 outDiffuse;
layout(location=1) out vec3 outNormals;

void main(void) {
    vec4 texel = texture2D(diffuseTexture, TexCoords);
    outDiffuse = vec3(1.0);
    outNormals = vec3(1.0, 0.0, 0.0);
}

Light pass shaders
 

// VERTEX SHADER

#version 330

layout(location=0) in vec4 inPosition;
layout(location=1) in vec2 UV;
uniform mat4 transformMatrix;

out vec2 TexCoords;

void main(void) {
    gl_Position = transformMatrix*inPosition;
    TexCoords = UV;
}

// FRAGMENT SHADER

#version 330

uniform sampler2D diffuseTexture;
uniform sampler2D normalsTexture;
in vec2 TexCoords;

out vec4 outColor;

void main(void) {
    outColor = vec4(texture2D(diffuseTexture, TexCoords).rgb, 1.0);
}

 
I am rendering out all of my geometry to the gbuffer/framebuffer through the geometry shader, which right now I have set to only output white pixels for testing. After rendering to the gbuffer, I render a rectangle the size of the screen using the light pass shader. What I expect to be happening is getting white rectangles from the first gbuffer pass, however all I am getting is a black screen. If I change outColor in the light pass to white, I get a white screen so I know that portion is working fine. So something is wrong with either reading or writing data to the gbuffer.
 
 

mGBuffer.writeBind(mRenderWindow.context());
ae::RenderState *state = mGBuffer.state();
state->pushShader(mDeferredGeomShader);
state->useOrthographicProj();
state->useCamera(mCamera);
mMap.draw(mRenderWindow.state());
mSpriteRenderSystem.renderSprites(mRenderWindow.state());
mGBuffer.writeUnbind(mRenderWindow.context());

mRenderWindow.state()->pushShader(mDeferredLightShader);
mDeferredLightShader->setUniform("diffuseTexture", (GLuint)0);
mDeferredLightShader->setUniform("normalsTexture", (GLuint)1);

mGBuffer.readBind();
mRenderWindow.state()->pushModelMatrix(glm::translate(glm::mat4(1.f), glm::vec3(1.f,
1.f, 0.f)));
mScreenRect->draw(mRenderWindow.state());
mRenderWindow.state()->popModelMatrix();
mGBuffer.readUnbind();
mRenderWindow.state()->popShader();

EDIT:

 

Okay, one of the problems was in my initialization code for the gbuffer, I was initializing texture 0 twice. Another problem was when I was rendering to the gbuffer, I was using the wrong state. It seems to be working now,  however not all my objects are showing up in the gbuffer (the sprite is, but the map is not)

 

EDIT2:

Okay, the map was not appearing because there is no alpha value in the gbuffer textures...so the map layers that had no tiles were overwriting the bottom layer.


GLSL vec4 multiplication not working?

24 June 2013 - 09:04 PM

I'm having a problem with my shader causing my texture to render transparent. As you can see in the top 2 windows, the text is rendered correctly when I just use the image data. However, in the bottom half, the text is not rendered at all when I try to multiply a color. No shader errors at all.

 

2vAKWfG.png


Why is this a function on distance and not angle?

13 January 2013 - 01:12 PM

I'm trying to create a billboard that faces the camera, following the Lighthouse3d tutorial and it really isn't working at all.
        glm::vec3 campos = state->getCamera()->getPosition();
        glm::vec3 objToCam = campos - mPosition;
        objToCam = glm::normalize(objToCam);

        glm::vec3 upAux = glm::cross(glm::vec3(0.f, 0.f, 1.f), objToCam);
        float angleCos = glm::dot(glm::vec3(0.f, 0.f, 1.f), objToCam);

        glm::vec3 rot;
        rot.y = glm::degrees(glm::acos(angleCos));
       
        // First translate so we rotate around the center of the billboard
        glm::mat4 rotMatrix = glm::translate(glm::mat4(), glm::vec3(-16.f, 0.f, 0.f));
        rotMatrix = glm::rotate(rotMatrix, rot.y, upAux);

This isn't working as expected.

A) The billboard will follow the camera as long as I am not within a few degrees of 0 on the x axis. As I start to close in to 0 the billboard rotates back to face down the X axis.

b) Rotation of the billboard becomes whacky when I only increase the distance. It starts rotating by large amounts, and faces away from the camera.

Moving camera in direction of rotation

05 January 2013 - 12:15 AM

I'm trying to get my camera to move in the direction of it's rotation. My code is mostly working, but I have one problem and I'm not sure how to fix it. I'm trying to make the camera move in any direction on the horizontal plane and up and down on the vertical plane. No rolling. My problem is, when the camera is facing straight up (or close to it) the camera continue to moves on the horizontal plane instead vertically. I don't know how to make it so as the pitch get's closer to 90/-90, the movement on the horizontal plane becomes less and less. Here is my code so far:

void ITransformable::moveInRotation(float distance) {
   
    float x = 0.f;
    float z = 0.f;
    float y = 0.f;

    
    if(mRotation.x < DEGREES_TO_RADIANS(45.f)) {
        y = std::sin(mRotation.x) * distance;
    } else {
        y = std::cos(mRotation.x) * distance;
    }

    if(mRotation.y >= DEGREES_TO_RADIANS(90.f)) {
        mRotation.y -= DEGREES_TO_RADIANS(90.f);
    }

    if(mRotation.y < DEGREES_TO_RADIANS(45.f)) {
        x = std::sin(mRotation.y) * distance;

        // Find the adjacent side, which is our distance on the x-axis
        z = std::cos(mRotation.y) * distance;
    } else {
        z = std::sin(mRotation.y) * distance;
        x = std::cos(mRotation.y) * distance;
    }

    translate(x, -y, z);
}

PARTNERS