0 Replies - 184 Views - Last Post: 29 December 2013 - 09:48 AM Rate Topic: -----

#1 lowpowerobjective  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 29-December 13

OpenGL rendering trouble?

Posted 29 December 2013 - 09:48 AM

Hello gentlemen, I've recently started a project. I have never used C for anything above Project Euler degree challenges but I decided on using it to make a game with OpenGL. The problem here is that even though I believe that I've done everything correctly and set up everything as they should be set up; I still have trouble getting anything rendered on the screen! I realize that I'm asking for a code review but being very confused about this issue, this is my last possible resort

The dependencies (GLEW, GLFW and SOIL) are all linked correctly and the program compiles without issue, so I believe that this might be a mistake on some sort of initialization process

http://pastebin.com/MCcGuC7m
#define GLEW_STATIC

#include <stdio.h>
#include <math.h>
#include <glew.h>
#include <glfw3.h>
#include <SOIL.h>

#define SCREEN_WIDTH 192
#define SCREEN_HEIGHT 224
#define SCREEN_HORIZONTAL_SCALE 4
#define SCREEN_VERTICAL_SCALE 2
#define WINDOW_WIDTH SCREEN_WIDTH * SCREEN_HORIZONTAL_SCALE
#define WINDOW_HEIGHT SCREEN_HEIGHT * SCREEN_VERTICAL_SCALE

/* Common vertex shader */
const GLchar* vertexSource =
    "#version 150 core\n"
    "uniform mat4 uniModel;"
    "uniform mat4 uniView;"
    "uniform mat4 uniProj;"
    "in vec3 inPosition;"
    "in vec3 inColor;"
    "in vec2 inTexCoord;"
    "out vec3 fragColor;"
    "out vec2 fragTexCoord;"
    "void main() {"
    "   fragColor = inColor;"
    "   fragTexCoord = inTexCoord;"
    "   gl_Position = model * view * proj * vec4(inPosition, 1.0);"
    "}";
	
/* Common fragment shader */
const GLchar* fragmentSource =
    "#version 150 core\n"
    "uniform sampler2D fragSampler;"
    "in vec3 fragColor;"
    "in vec2 fragTexCoord;"
    "out vec4 outColor;"
    "void main() {"
    "   outColor = vec4(fragColor, 1.0) * texture(fragSampler, fragTexCoord);"
    "}";

/* Identity matrix constant */
const GLfloat identityMatrix4[] = {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1};

GLfloat modelMatrix[] = {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1};
GLfloat viewMatrix[] = {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1};
GLfloat projMatrix[] = {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1};

/* Matrix functions */
void gfxUploadMatrix4(GLint location, GLfloat* value) {
	glUniformMatrix4fv(location, 1, GL_FALSE, value);
}

void gfxIdentityMatrix4(GLint location) {
	glUniformMatrix4fv(location, 1, GL_FALSE, identityMatrix4);
}

void gfxReady2D(GLint modelLocation, GLint viewLocation, GLint projLocation, GLint width, GLint height) {
	/* Calculate the ortho matrix and set variables */
	projMatrix[0] = 2 / width;
	projMatrix[1] = 0;
	projMatrix[2] =	0;
	projMatrix[3] = -1;
	projMatrix[4] = 0;
	projMatrix[5] = 2 / height;
	projMatrix[6] = 0;
	projMatrix[7] = -1;
	projMatrix[8] = 0;
	projMatrix[9] = 0;
	projMatrix[10] = -1;
	projMatrix[11] = 0;
	projMatrix[12] = 0;
	projMatrix[13] = 0;
	projMatrix[14] = 0;
	projMatrix[15] = 1;
	
	/* Set viewport and upload matrices */
	glViewport(0, 0, width, height);
	
	gfxIdentityMatrix4(modelLocation);
	gfxIdentityMatrix4(viewLocation);

	gfxUploadMatrix4(projLocation, projMatrix);

	/* Disable depth testing for 2D */
    glDisable(GL_DEPTH_TEST);
}

void gfxReady3D(GLint modelLocation, GLint viewLocation, GLint projLocation, GLint width, GLint height) {
	/* Calculate the frustum matrix and set variables */
	float aspect = ((float) width) / height, fH = 1.73205081, fW = fH * aspect;
	
	projMatrix[0] = 2 / (2 * fW);
	projMatrix[1] = 0;
	projMatrix[2] = 0;
	projMatrix[3] = 0;
	projMatrix[4] = 0;
	projMatrix[5] = 2 / (2 * fH);
	projMatrix[6] = 0;
	projMatrix[7] = 0;
	projMatrix[8] = 0;
	projMatrix[9] = 0;
	projMatrix[10] = -100.0f / 99;
	projMatrix[11] = -200.0f / 99;
	projMatrix[12] = 0;
	projMatrix[13] = 0;
	projMatrix[14] = -1;
	projMatrix[15] = 1;
	
	/* Set viewport and upload matrices */
	glViewport(0, 0, width, height);
	
	gfxIdentityMatrix4(modelLocation);
	gfxIdentityMatrix4(viewLocation);
	
    gfxUploadMatrix4(projLocation, projMatrix);

	/* Enable depth testing for 3D */
    glEnable(GL_DEPTH_TEST);
}

void gfxBindBufferSet(GLint vao, GLint vbo, GLint ebo) {
	/* Bind all necessary buffers */
    glBindVertexArray(vao);
    glBindBuffer(GL_ARRAY_BUFFER, vbo);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo);
}

GLint main(void) {
	/* Declaring variables */
    GLFWwindow* window;
	
    /* Initialize the library */
    if (!glfwInit())
        return -1;
		
	/* Setup window hints */
	glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);

    /* Create a windowed mode window and its OpenGL context */
    window = glfwCreateWindow(WINDOW_WIDTH, WINDOW_HEIGHT, "Project", NULL, NULL);
    if (!window) {
        glfwTerminate();
        return -1;
    }
	
	/* Center window */
	const GLFWvidmode* windowMode = glfwGetVideoMode(glfwGetPrimaryMonitor());
	GLint windowWidth, windowHeight;
	glfwGetWindowSize(window, &windowWidth, &windowHeight);
	glfwSetWindowPos(window, (windowMode->width - windowWidth) / 2, (windowMode->height - windowHeight) / 2);
	
    /* Make the window's context current */
    glfwMakeContextCurrent(window);
	
	/* Initialize GLEW (after contexting) */
	GLenum err = glewInit();
	if (GLEW_OK != err)
		return -1;
	
	/* Setup OpenGL hints and stuff */
	glEnable(GL_TEXTURE_2D);
	glDepthFunc(GL_LEQUAL);
	
	/* Setup vertex and fragment shaders */
    GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER);
    glShaderSource(vertexShader, 1, &vertexSource, NULL);
    glCompileShader(vertexShader);

    GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
    glShaderSource(fragmentShader, 1, &fragmentSource, NULL);
    glCompileShader(fragmentShader);

    /* Link vertex and fragment shaders into a program */
    GLuint shaderProgram = glCreateProgram();
    glAttachShader(shaderProgram, vertexShader);
    glAttachShader(shaderProgram, fragmentShader);
    glBindFragDataLocation(shaderProgram, 0, "outColor");
    glLinkProgram(shaderProgram);
    glUseProgram(shaderProgram);

	/* Get matrix locations */
	GLint modelUniform = glGetUniformLocation(shaderProgram, "uniModel");
	GLint viewUniform = glGetUniformLocation(shaderProgram, "uniView");
	GLint projUniform = glGetUniformLocation(shaderProgram, "uniProj");
	
    /* Specify vertex data layout */
    GLint positionAttrib = glGetAttribLocation(shaderProgram, "inPosition");
    glEnableVertexAttribArray(positionAttrib);
    glVertexAttribPointer(positionAttrib, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(GLfloat), 0);

    GLint colorAttrib = glGetAttribLocation(shaderProgram, "inColor");
    glEnableVertexAttribArray(colorAttrib);
    glVertexAttribPointer(colorAttrib, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(GLfloat), (void*)(3 * sizeof(GLfloat)));

    GLint texCoordAttrib = glGetAttribLocation(shaderProgram, "inTexCoord");
    glEnableVertexAttribArray(texCoordAttrib);
    glVertexAttribPointer(texCoordAttrib, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(GLfloat), (void*)(6 * sizeof(GLfloat)));
	
	/* Test rendering buffer set */
	
	/* Create Vertex Array Object */
    GLuint vao;
    glGenVertexArrays(1, &vao);
    glBindVertexArray(vao);

    /* Create a Vertex Buffer Object and copy the vertex data to it */
    GLuint vbo;
    glGenBuffers(1, &vbo);

    GLfloat vertices[] = {
    /*  Position (3) Color (3) Texcoords (2) */
        20, 80, 0, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, /* Top-left */
        80, 80, 0, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, /* Top-right */
        80, 20, 0, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f, /* Bottom-right */
        20, 20, 0, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f  /* Bottom-left */
    };

    glBindBuffer(GL_ARRAY_BUFFER, vbo);
    glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);

    /* Create an element array */
    GLuint ebo;
    glGenBuffers(1, &ebo);

    GLuint elements[] = {
        0, 1, 2,
        2, 3, 0
    };

    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(elements), elements, GL_STATIC_DRAW);
	
	/* FBO rendering buffer set */
	
	/* Create Vertex Array Object */
    GLuint fboVao;
    glGenVertexArrays(1, &fboVao);
    glBindVertexArray(fboVao);

    /* Create a Vertex Buffer Object and copy the vertex data to it */
    GLuint fboVbo;
    glGenBuffers(1, &fboVbo);

    GLfloat fboVertices[] = {
    /*  Position (3) Color (3) Texcoords (2) */
        0, 0, 0,						0, 0, 0, 0, 0,
        0, WINDOW_HEIGHT, 0,			0, 0, 0, 0, 1,
        WINDOW_WIDTH, WINDOW_HEIGHT, 0,	0, 0, 0, 1, 1,
        WINDOW_WIDTH, 0, 0,				0, 0, 0, 1, 0
    };

    glBindBuffer(GL_ARRAY_BUFFER, fboVbo);
    glBufferData(GL_ARRAY_BUFFER, sizeof(fboVertices), fboVertices, GL_STATIC_DRAW);

    /* Create an element array */
    GLuint fboEbo;
    glGenBuffers(1, &fboEbo);

    GLuint fboElements[] = {
        0, 1, 2,
        2, 3, 0
    };

    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, fboEbo);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(fboElements), fboElements, GL_STATIC_DRAW);
	
	/* Prepare an FBO for unscaled rendering */
	GLuint unscaledBuffer;
	glGenFramebuffers(1, &unscaledBuffer);
	glBindFramebuffer(GL_FRAMEBUFFER, unscaledBuffer);
	
	/* Setup the FBO texture */
	GLuint texColorBuffer;
	glGenTextures(1, &texColorBuffer);
	glBindTexture(GL_TEXTURE_2D, texColorBuffer);

	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, SCREEN_WIDTH, SCREEN_HEIGHT, 0, GL_RGB, GL_UNSIGNED_BYTE, NULL);

	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
	
	glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texColorBuffer, 0);
	
	/* Setup the FBO depth buffer */
	GLuint rboDepth;
	glGenRenderbuffers(1, &rboDepth);
	glBindRenderbuffer(GL_RENDERBUFFER, rboDepth);
	glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, SCREEN_WIDTH, SCREEN_HEIGHT);
	
	glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, rboDepth);
	
	/* Load textures */
    GLuint textures[2];
    glGenTextures(2, textures);
    
    int width, height;
    unsigned char* image;

    glBindTexture(GL_TEXTURE_2D, textures[0]);

	image = SOIL_load_image("img/blank.png", &width, &height, 0, SOIL_LOAD_RGB);
	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, image);
	SOIL_free_image_data(image);

    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);

    /* Loop until the user closes the window */
    while (!glfwWindowShouldClose(window)) {
		/* Unbind texture */
		glBindTexture(GL_TEXTURE_2D, 0);
		
		/* Switch to unscaled FBO */
		glBindFramebuffer(GL_FRAMEBUFFER, unscaledBuffer);
		glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
		
		/* Start 2D rendering */
        gfxReady2D(modelUniform, viewUniform, projUniform, SCREEN_WIDTH, SCREEN_HEIGHT);
		
		/* Random drawing stuffs */
		glBindTexture(GL_TEXTURE_2D, textures[0]);
		gfxBindBufferSet(vao, vbo, ebo);
		glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
		
		/* Start 3D rendering */
        gfxReady3D(modelUniform, viewUniform, projUniform, SCREEN_WIDTH, SCREEN_HEIGHT);
		
		/* Random drawing stuffs */
		
		// NOTHING
		
		/* Switch to default FBO */
		glBindFramebuffer(GL_FRAMEBUFFER, 0);
		glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
        gfxReady2D(modelUniform, viewUniform, projUniform, WINDOW_WIDTH, WINDOW_HEIGHT);
		
		/* Render unscaled FBO to default FBO */
		glBindTexture(GL_TEXTURE_2D, texColorBuffer);
		gfxBindBufferSet(fboVao, fboVbo, fboEbo);
		glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);

        /* Swap front and back buffers */
        glfwSwapBuffers(window);

        /* Poll for and process events */
        glfwPollEvents();
    }

	/* Terminate program and GLFW */
    glfwTerminate();
    return 0;
}


This post has been edited by Atli: 29 December 2013 - 09:50 AM
Reason for edit:: Added code to the post. Please post relevant code in the thread, rather than links to external sources.


Is This A Good Question/Topic? 0
  • +

Page 1 of 1