(I'm using the GLM library for all the matrix stuff)
opengl_4.cpp
#include "opengl_4.h"
/-snipped-/ Creating of rendering context and whatnot
void OpenGLContext::setupScene(void) {
glClearColor(0.3f,0.3f,0.3f,0.0f); //set the clear color to a dark grey
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LESS);
glEnable(GL_CULL_FACE);
glCullFace(GL_BACK);
glFrontFace(GL_CCW);
shader = new Shader("basic.vert","basic.frag");
item = new Object();
item->makeDefaultObject();
}
void OpenGLContext::reshapeWindow(int w, int h) {
windowWidth = w; //Set the window width
windowHeight = h; //Set thewindow height
glViewport(0,0, windowWidth, windowHeight); //Set the viewport size to fill the window
ProjectionMatrix = glm::perspective(
45.0f,
(float)windowWidth / windowHeight,
0.1f,
500.0f
);
}
void OpenGLContext::renderScene(void) {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); //Clear required buffers
ModelViewMatrix = glm::mat4(1.0f);//glm::translate(glm::mat4(1.0f), glm::vec3(0.0f,0.0f, -5.0f));
shader->bind();
item->bind();
glUniformMatrix4fv(shader->locations[0], 1, GL_FALSE, &ProjectionMatrix[0][0]);
glUniformMatrix4fv(shader->locations[1], 1, GL_FALSE, &ModelViewMatrix[0][0]);
glDrawElements(GL_TRIANGLES, item->polyCount*3, GL_UNSIGNED_INT, (GLvoid*)0);
shader->unbind();
item->unbind();
SwapBuffers(hdc); //swap buffers so we can see our rendering
}
object.cpp
//Object file
#include "object.h"
Object::Object()
{
makeDefaultObject();
}
Object::~Object()
{
}
void Object::makeDefaultObject()
{
vert Vertices[4] =
{
{{ 0.0f, 0.5f, 0.0f, 1.0f},{0.0f,0.0f,1.0f,1.0f}},
{{ 0.0f,-0.5f,-0.5f, 1.0f},{0.0f,1.0f,0.0f,1.0f}},
{{ 0.5f,-0.5f, 0.5f, 1.0f},{1.0f,0.0f,0.0f,1.0f}},
{{-0.5f,-0.5f, 0.5f, 1.0f},{1.0f,0.0f,0.0f,1.0f}}
};
vertCount = 4;
poly Polys[4] =
{
{0,1,2},
{2,0,3},
{3,1,0},
{1,3,2}
};
polyCount = 4;
glGenBuffers(1, &_vbo);
glBindBuffer(GL_ARRAY_BUFFER, _vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(Vertices), Vertices, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glGenBuffers(1, &_ibo);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _ibo);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(Polys), Polys, GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
glGenVertexArrays(1, &_vao);
glBindVertexArray(_vao);
glBindBuffer(GL_ARRAY_BUFFER, _vbo);
glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1);
glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, sizeof(Vertices[0]), 0);
glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, sizeof(Vertices[0]), (void*)sizeof(Vertices[0].position));
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _ibo);
glBindVertexArray(0);
}
void Object::bind()
{
glBindVertexArray(_vao);
}
void Object::unbind()
{
glBindVertexArray(0);
}
shader.cpp
#include "shader.h"
#include <string.h>
#include <iostream>
#include <stdlib.h>
using namespace std;
static char* textFileRead(const char *fileName) {
char* text = "NULL";
if (fileName != NULL) {
FILE *file = fopen(fileName, "rt");
if (file != NULL) {
fseek(file, 0, SEEK_END);
int count = ftell(file);
rewind(file);
if (count > 0) {
text = (char*)malloc(sizeof(char) * (count + 1));
count = fread(text, sizeof(char), count, file);
text[count] = '\0';
}
fclose(file);
}
}
return text;
}
Shader::Shader() {
}
Shader::Shader(const char *vsFile, const char *fsFile) {
init(vsFile, fsFile);
}
void Shader::init(const char *vsFile, const char *fsFile) {
shader_vp = glCreateShader(GL_VERTEX_SHADER);
shader_fp = glCreateShader(GL_FRAGMENT_SHADER);
const char* vsText = textFileRead(vsFile);
const char* fsText = textFileRead(fsFile);
if (vsText == NULL || fsText == NULL) {
cerr << "Either vertex shader or fragment shader file not found." << endl;
return;
}
glShaderSource(shader_vp, 1, &vsText, 0);
glShaderSource(shader_fp, 1, &fsText, 0);
glCompileShader(shader_vp);
glCompileShader(shader_fp);
shader_id = glCreateProgram();
glAttachShader(shader_id, shader_fp);
glAttachShader(shader_id, shader_vp);
glLinkProgram(shader_id);
locations[0] = glGetUniformLocation(shader_id, "ProjMat");
locations[1] = glGetUniformLocation(shader_id, "ModelViewMat");
glBindAttribLocation(shader_id, 0, "in_Position"); //Bind a constant attribute location for positions of vertices
glBindAttribLocation(shader_id, 1, "in_Color"); //Bind another constant attribute location, this time for color
}
Shader::~Shader() {
glDetachShader(shader_id, shader_fp);
glDetachShader(shader_id, shader_vp);
glDeleteShader(shader_fp);
glDeleteShader(shader_vp);
glDeleteProgram(shader_id);
}
unsigned int Shader::id() {
return shader_id;
}
void Shader::bind() {
glUseProgram(shader_id);
}
void Shader::unbind() {
glUseProgram(0);
}
Vertex shader
#version 330
layout(location = 0) in vec4 in_Position;
layout(location = 1) in vec4 in_Colour;
out vec4 mid_Colour;
uniform in mat4 ProjMat;
uniform in mat4 ModelViewMat;
void main(void)
{
gl_Position = ProjMat*ModelViewMat*in_Position;
mid_Colour = in_Colour;
}
Fragment shader
#version 330
in vec4 mid_Colour;
out vec4 out_Color;
void main(void)
{
out_Color = mid_Colour;
}
sorry about the huge page, I can't figure out how to shrink the code boxes.