Hi Thokra:
Really appreciate if you took a look. I am working in Win7/Visual C++ 2010. The code compiles ok but I just get a blank screen. The triangle is not drawn.
Thanks,
Sam
Main program
////////////////////////////////////////////////////
// trianglePipeline.cpp
////////////////////////////////////////////////////
#include <iostream>
#include <fstream>
# include <GL/glew.h>
# include <GL/freeglut.h>
# include <GL/glext.h>
#pragma comment(lib, "glew32.lib")
struct Vertex
{
float coords[4];
float colors[4];
};
struct Matrix4x4
{
float entries[16];
};
static const Matrix4x4 IDENTITY_MATRIX4x4 =
{
{
1.0, 0.0, 0.0, 0.0,
0.0, 1.0, 0.0, 0.0,
0.0, 0.0, 1.0, 0.0,
0.0, 0.0, 0.0, 1.0
}
};
static Matrix4x4
MVmatrix,
projMatrix;
static unsigned int
vShaderProgId,
fShaderProgId,
MVmatrixUnifLoc,
projMatrixUnifLoc,
pipeline,
buffer,
vao;
Vertex triangleVertices[] = // Triangle vertex data: { {Coordinates 4-vector}, {Color 4-vector} }
{
{ { 20.0, 20.0, 0.0, 1.0 }, { 0.0, 0.0, 0.0, 1.0 } },
{ { 80.0, 20.0, 0.0, 1.0 }, { 0.0, 0.0, 0.0, 1.0 } },
{ { 20.0, 80.0, 0.0, 1.0 }, { 0.0, 0.0, 0.0, 1.0 } }
};
//Function to read a text file.
char* readTextFile(char* aTextFile)
{
FILE* filePointer = fopen(aTextFile, "rb");
char* content = NULL;
long numVal = 0;
fseek(filePointer, 0L, SEEK_END);
numVal = ftell(filePointer);
fseek(filePointer, 0L, SEEK_SET);
content = (char*) malloc((numVal+1) * sizeof(char));
fread(content, 1, numVal, filePointer);
content[numVal] = '\0';
fclose(filePointer);
return content;
}
// Initialization routine.
void setup(void)
{
glGenProgramPipelines(1, &pipeline);
char* vShaderSrc = readTextFile("vShader.glsl");
char* fShaderSrc = readTextFile("fShader.glsl");
vShaderProgId = glCreateShaderProgramv(GL_VERTEX_SHADER, 1, (const char**) &vShaderSrc);
fShaderProgId = glCreateShaderProgramv(GL_FRAGMENT_SHADER, 1, (const char**) &fShaderSrc);
glUseProgramStages(pipeline, GL_VERTEX_SHADER_BIT, vShaderProgId);
glUseProgramStages(pipeline, GL_FRAGMENT_SHADER_BIT, fShaderProgId);
glClearColor(1.0, 1.0, 1.0, 0.0);
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);
glGenBuffers(1, &buffer);
glBindBuffer(GL_ARRAY_BUFFER, buffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(triangleVertices), triangleVertices, GL_STATIC_DRAW);
glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, sizeof(triangleVertices[0]), 0);
glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, sizeof(triangleVertices[0]), (GLvoid*)sizeof(triangleVertices[0].coords));
glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1);
Matrix4x4 MVmatrix = IDENTITY_MATRIX4x4;
MVmatrixUnifLoc = glGetUniformLocation(vShaderProgId,"MVmatrix");
glProgramUniformMatrix4fv(vShaderProgId, MVmatrixUnifLoc, 1, GL_TRUE, MVmatrix.entries);
Matrix4x4 projMatrix =
{
{
0.02, 0.0, 0.0, -1.0,
0.0, 0.02, 0.0, -1.0,
0.0, 0.0, -1.0, 0.0,
0.0, 0.0, 0.0, 1.0
}
};
projMatrixUnifLoc = glGetUniformLocation(vShaderProgId,"projMatrix");
glProgramUniformMatrix4fv(vShaderProgId, projMatrixUnifLoc, 1, GL_TRUE, projMatrix.entries);
}
// Drawing routine.
void drawScene(void)
{
glBindProgramPipeline(pipeline);
glClear(GL_COLOR_BUFFER_BIT);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 3);
glFlush();
}
// OpenGL window reshape routine.
void resize(int w, int h)
{
glViewport(0, 0, w, h);
}
// Main routine.
int main(int argc, char* argv[])
{
glutInit(&argc, argv);
glutInitContextVersion(4, 3);
glutInitContextProfile(GLUT_CORE_PROFILE);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(500, 500);
glutInitWindowPosition(100, 100);
glutCreateWindow("trianglePipeline");
glutDisplayFunc(drawScene);
glutReshapeFunc(resize);
glewExperimental = GL_TRUE;
glewInit();
setup();
glutMainLoop();
}
Vertex shader:
#version 430 core
layout(location=0) in vec4 vertexCoordsIn;
layout(location=1) in vec4 vertexColorsIn;
out vec4 colorsExport;
uniform mat4 MVmatrix;
uniform mat4 projMatrix;
void main(void)
{
gl_Position = projMat * MVmatrix * vertexCoordsIn;
colorsExport = vertexColorsIn;
}
Fragment shader:
#version 430 core
in vec4 colorsExport;
out vec4 colorsOut;
void main(void)
{
colorsOut = colorsExport;
}