Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 2 of 2

Thread: Program not rendering in color

  1. #1
    Junior Member Newbie
    Join Date
    Nov 2013
    Posts
    9

    Unhappy Program not rendering in color

    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:

    Code :
    #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:

    Code :
    #version 330 core
    in vec3 fragmentColor;
    out vec3 color;
     
    void main()
    {
    	color = fragmentColor;
    }

    Here is my vertex shader:

    Code :
    #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:

    Click image for larger version. 

Name:	screenie.jpg 
Views:	215 
Size:	7.3 KB 
ID:	1192

  2. #2
    Intern Newbie
    Join Date
    May 2013
    Posts
    42
    I don't see loading or binding of any shaders.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •