• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
CirdanValen

Problem with my projection or vertex buffer?

0 posts in this topic

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

 

[source]

#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;
       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;
}
[/source]
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0