PDA

View Full Version : Program not rendering in color



AdamVO
12-06-2013, 02:20 AM
Hello.

I have been trying really hard to figure out why my openGL application isn't rendering my square in the colors I specified.
Can someone please help me i'm still very new to openGL.

Here is my main.cpp file:


#include "main.h" //g++ main.cpp -lGLEW -lGL -lglfw -lGLU -lglut -o triangle
#define BUFFER_OFFSET(offset) ((void *)(offset))
using namespace std;

enum VAO_IDs {Triangles, NumVAOs};
enum Buffer_IDs {ArrayBuffer, cBuffer, NumBuffers};
enum Attrib_IDs {vPosition = 0, colorPosition = 1};
GLuint VAOs[NumVAOs];
GLuint ColourObjects[NumVAOs];
GLuint Buffers[NumBuffers];
const GLuint NumVertices = 36;

//assigning vertex data to buffer objects and preparing to send to
void init() //Vertex Shaders.
{
glewInit();
glGenVertexArrays(NumVAOs, VAOs);
glBindVertexArray(VAOs[Triangles]);

GLfloat vertices [NumVertices] [3] =
{
{ 0.30, 0.00, -0.30 }, //Triangle 1
{ 0.30, 0.00, 0.30 },
{ -0.30, 0.00, -0.30 },
{ -0.30, 0.00, -0.30 }, //Triangle 2
{ -0.30, 0.00, 0.30 },
{ 0.30, 0.00, -0.30 },
{ -0.30, 0.00, 0.30 }, //Triangle 3
{ 0.30, 0.00, 0.30 },
{ 0.30, 0.60, 0.30 },
{ -0.30, 0.60, 0.30 }, //Triangle 4
{ 0.30, 0.60, 0.30 },
{ -0.30, 0.00, 0.30 },
};

glGenBuffers(NumBuffers, Buffers);
glBindBuffer(GL_ARRAY_BUFFER, Buffers[ArrayBuffer]);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);

glVertexAttribPointer(vPosition, 3, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(0));
glEnableVertexAttribArray(vPosition );


GLfloat g_color_buffer_data[NumVertices][3] =
{
{ 0.583f, 0.771f, 0.014f },
{ 0.609f, 0.115f, 0.436f },
{ 0.327f, 0.483f, 0.844f },
{ 0.822f, 0.569f, 0.201f },
{ 0.435f, 0.602f, 0.223f },
{ 0.310f, 0.747f, 0.185f },
{ 0.597f, 0.770f, 0.761f },
{ 0.559f, 0.436f, 0.730f },
{ 0.359f, 0.583f, 0.152f },
{ 0.483f, 0.596f, 0.789f },
{ 0.559f, 0.861f, 0.639f },
{ 0.195f, 0.548f, 0.859f },
};

glBindBuffer(GL_ARRAY_BUFFER, Buffers[cBuffer]);
glBufferData(GL_ARRAY_BUFFER, sizeof(g_color_buffer_data), g_color_buffer_data, GL_STATIC_DRAW);

glVertexAttribPointer(colorPosition, 3, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(0));
glEnableVertexAttribArray(colorPosition);

glBindVertexArray(0);

ShaderInfo shaders[] =
{
{ GL_VERTEX_SHADER, "triangle.vert"},
{ GL_FRAGMENT_SHADER, "triangle.frag"},
{ GL_NONE, NULL}
};
}

void display()
{
glClearColor(1,0.5f,0,0);
glClear(GL_COLOR_BUFFER_BIT);

glBindVertexArray(VAOs[Triangles]); //?
glDrawArrays(GL_TRIANGLES, 0, NumVertices);

glFlush();
}

int main(int argc, char** argv)
{
const int width = 400, height = 400;

glutInit(&argc, argv);
glutInitWindowSize(width, height);
glutInitDisplayMode(GLUT_RGBA);
glutInitContextVersion(3, 3);
glutInitContextProfile(GLUT_CORE_PROFILE);
glutCreateWindow(argv[0]);

init();
glutDisplayFunc(display);
glutMainLoop();
return 0;
}

Here is my fragment shader:


#version 330 core
in vec3 fragmentColor;
out vec3 color;

void main()
{
color = fragmentColor;
}

Here is my vertex shader:


#version 330 core

layout(location = 0) in vec4 vPostion;
layout(location = 1) in vec3 vertexColor;
out vec3 fragmentColor;
void main()
{
gl_Position = vPosition;
fragmentColor = vertexColor;
}


This is the result I get on executing the program:

1192

Osbios
12-06-2013, 07:32 AM
I don't see loading or binding of any shaders.