MoustacheSpy MoustacheSpy - 1 year ago 64
C++ Question

Opengl Rectangle wont render

Either I am stupid or incredibly inexperienced ( probably both ) but I got somehow stuck on drawing a simple colored rectangle using opengl. It shouldnt be a problem since I just finished a simple opengl model loader ( however it was my first project so i only did everything once with reference ). Anyways nothing is showing up except the blue background.

Ive tried to make simple classes for basic shapes. Here is the rectangle class:

class AVRect{
AVcolor Color;
GLuint indices[6] = { // Note that we start from 0!
0, 1, 3, // First Triangle
1, 2, 3 // Second Triangle
GLfloat geometry[12];
AVRect(int Drawmode, GLfloat x, GLfloat y, GLfloat x2, GLfloat y2,GLfloat x3, GLfloat y3, GLfloat x4, GLfloat y4,GLfloat r,GLfloat b,GLfloat g,GLfloat a);
void Draw(Shader shader);

Dont worry about color. Ive only set it in the source but its not used anywhere else atm. I am going to implement it as soon as ive got the rectangle working with a fixed color.

Source of the rect class:

#include "stdafx.h"
#include "Shapes.h"

AVRect::AVRect(int Drawmode,GLfloat x, GLfloat y, GLfloat x2, GLfloat y2, GLfloat x3, GLfloat y3, GLfloat x4, GLfloat y4, GLfloat r, GLfloat b, GLfloat g, GLfloat a) {
geometry[0] = x;
geometry[1] = y;
geometry[2] = 0.0f;
geometry[3] = x2;
geometry[4] = y2;
geometry[5] = 0.0f;
geometry[6] = x3;
geometry[7] = y3;
geometry[8] = 0.0f;
geometry[9] = x4;
geometry[10] = y4;
geometry[11] = 0.0f;
Color.r = r;
Color.b = b;
Color.g = g;
Color.a = a;
glGenVertexArrays(1, &VAO);
glGenBuffers(1, &VBO);
glGenBuffers(1, &EBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(geometry), geometry, Drawmode);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, Drawmode);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), (GLvoid*)0);
glBindBuffer(GL_ARRAY_BUFFER, 0);


void AVRect::Draw(Shader shader) {
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);

The main is pretty self explanatory:

// Bullethell.cpp : Defines the entry point for the console application.

#include "stdafx.h"
#include <GL/glew.h>
#include <SDL.h>
#include <SDL_opengl.h>
#include <GLTextures.h>
#include <Shaders.h>
#include <ctime>
#include "Shapes.h"
#define HEIGHT 600
#define WIDTH 600

int main(int argc, char* args[])
SDL_Window *window;
SDL_Renderer *renderer;
window = SDL_CreateWindow("BulletHell", 100, 100, HEIGHT, WIDTH, SDL_WINDOW_OPENGL);
SDL_GLContext glcontext = SDL_GL_CreateContext(window);

glewExperimental = GL_TRUE;
if (GLEW_OK != glewInit())
// GLEW failed!
printf("GLEW INIT FAILED!");
Shader basic("./shaders/colorTest/colorTest.vs", "./shaders/colorTest/colorTest.frag");
AVRect test(GL_STATIC_DRAW,0.5f,0.5f,0.5f,-0.5f,-0.5f,-0.5f,0.5f,-0.5f,0.5f,0.9f,0.2f,0.5f);
int error = 0;
while (SDL_PollEvent(NULL))
clock_t start = clock();
glClearColor(0.2, 0.2, 0.5, 1);
error = glGetError();
if (error != GL_NO_ERROR)
printf("GL ERROR:%d ", error);
while (((clock() - start) * 1000 / CLOCKS_PER_SEC) <16)

float MS = (float)((clock() - start) * 1000 / CLOCKS_PER_SEC);
// Frames per seconds
float FPS = 1000.0f / MS;
//printf("%f \n",FPS);
// printf( "Status: Using GLEW %s\n", glewGetString(GLEW_VERSION));
return 1;

The Shader class is tested and is working. The shader is simple and should also work ( only passes on values and assigns a fixed color ).

I am probably just blind because i cant see the error. Please help me on this one.

Here is the Shader source for completeness sake:

#include "stdafx.h"
#include "Shaders.h"

Shader::Shader(const GLchar* vertexPath, const GLchar* fragmentPath)
// 1. Retrieve the vertex/fragment source code from filePath
std::string vertexCode;
std::string fragmentCode;
std::ifstream vShaderFile;
std::ifstream fShaderFile;

// ensures ifstream objects can throw exceptions:
vShaderFile.exceptions (std::ifstream::badbit);
fShaderFile.exceptions (std::ifstream::badbit);
// Open files;;
std::stringstream vShaderStream, fShaderStream;
// Read file's buffer contents into streams
vShaderStream << vShaderFile.rdbuf();
fShaderStream << fShaderFile.rdbuf();
// close file handlers
// Convert stream into string
vertexCode = vShaderStream.str();
fragmentCode = fShaderStream.str();
catch (std::ifstream::failure e)
std::cout << "ERROR::SHADER::FILE_NOT_SUCCESFULLY_READ" << std::endl;
const GLchar* vShaderCode = vertexCode.c_str();
const GLchar * fShaderCode = fragmentCode.c_str();
// 2. Compile shaders
GLuint vertex, fragment;
GLint success;
GLchar infoLog[512];
// Vertex Shader
vertex = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertex, 1, &vShaderCode, NULL);
// Print compile errors if any
glGetShaderiv(vertex, GL_COMPILE_STATUS, &success);
if (!success)
glGetShaderInfoLog(vertex, 512, NULL, infoLog);
std::cout << "ERROR::SHADER::VERTEX::COMPILATION_FAILED\n" << infoLog << std::endl;
// Fragment Shader
fragment = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragment, 1, &fShaderCode, NULL);
// Print compile errors if any
glGetShaderiv(fragment, GL_COMPILE_STATUS, &success);
if (!success)
glGetShaderInfoLog(fragment, 512, NULL, infoLog);
std::cout << "ERROR::SHADER::FRAGMENT::COMPILATION_FAILED\n" << infoLog << std::endl;
// Shader Program
this->Program = glCreateProgram();
glAttachShader(this->Program, vertex);
glAttachShader(this->Program, fragment);
// Print linking errors if any
glGetProgramiv(this->Program, GL_LINK_STATUS, &success);
if (!success)
glGetProgramInfoLog(this->Program, 512, NULL, infoLog);
std::cout << "ERROR::SHADER::PROGRAM::LINKING_FAILED\n" << infoLog << std::endl;
// Delete the shaders as they're linked into our program now and no longer necessery

// Uses the current shader
void Shader::Use()

Answer Source

You're drawing degenerate triangles, so no pixels will be rendered. If you look at the coordinates of your "rectangle", spaced for easier readability:

 0.5f,  0.5f,
 0.5f, -0.5f,
-0.5f, -0.5f,
 0.5f, -0.5f

you can see that the 2nd and 4th vertex have the same coordinates. The arrangement of your vertices looks like this:


2     1/3

Compare that with your indices:

0, 1, 3,   // First Triangle
1, 2, 3    // Second Triangle

and you will see that both triangles are degenerate, because two of their vertices are the same.

The coordinates you probably meant to use are:

 0.5f,  0.5f,
 0.5f, -0.5f,
-0.5f, -0.5f,
-0.5f,  0.5f

I would actually recommend to use counter-clockwise orientation for your geometry, since this is the default winding order in OpenGL. Even though it does not really matter unless you plan to enable culling.