Last active
December 17, 2015 00:29
-
-
Save sherief/5521281 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <cassert> | |
#define GLFW_INCLUDE_GL3 | |
#include <GL/glfw.h> | |
#include <lgsl/runtime.hpp> | |
#include <gfx/mat4.h> | |
using namespace std; | |
using namespace gfx; | |
const char* const VertexProgramSource = | |
"float4 main(float4 In : ATTRIBUTE_0, " | |
" uniform float4x4 Matrix) : POSITION " | |
"{" | |
" return Matrix * In;" | |
"}" | |
; | |
const char* const FragmentProgramSource = | |
"float4 main(uniform float4 C) : COLOR " | |
"{" | |
" return C;" | |
"}" | |
; | |
using namespace lgsl; | |
lgsl::opengl_3_runtime* Runtime; | |
lgsl::program VertexProgram; | |
lgsl::program FragmentProgram; | |
GLuint VAO; | |
GLuint VBO; | |
GLfloat Data[] = | |
{ -.5, -.5, 0, 1, | |
.5, -.5, 0, 1, | |
0, .5, 0, 1, | |
}; | |
float Rotation = 0.0; | |
const float RotationDelta = 0.6; | |
const float Color[] = { 0.0, 1.0, 0.0, 1.0 }; | |
void init() | |
{ | |
glClearColor(0.5, 0.5, 0.5, 1.0); | |
glEnable(GL_DEPTH_TEST); | |
glClear(GL_COLOR_BUFFER_BIT); | |
using namespace lgsl; | |
Runtime = new opengl_3_runtime(); | |
VertexProgram = Runtime->create_program(lgsl::VERTEX_PROGRAM, VertexProgramSource); | |
FragmentProgram = Runtime->create_program(lgsl::FRAGMENT_PROGRAM, FragmentProgramSource); | |
Runtime->bind_program(VertexProgram); | |
Runtime->bind_program(FragmentProgram); | |
auto CParam = Runtime->get_program_parameter(FragmentProgram, "C"); | |
Runtime->parameter_value(FragmentProgram, CParam, Color, 4); | |
Runtime->destroy_program_parameter(CParam); | |
// | |
glGenVertexArrays(1, &VAO); | |
glBindVertexArray(VAO); | |
glGenBuffers(1, &VBO); | |
glBindBuffer(GL_ARRAY_BUFFER, VBO); | |
glBufferData(GL_ARRAY_BUFFER, 16 * sizeof(GLfloat), Data, GL_STATIC_DRAW); | |
glVertexAttribPointer((GLuint)0, 4, GL_FLOAT, GL_FALSE, 0, 0); | |
glEnableVertexAttribArray(0); | |
} | |
int main(int argc, const char * argv[]) | |
{ | |
glfwInit(); | |
glfwOpenWindowHint(GLFW_OPENGL_VERSION_MAJOR, 3); | |
glfwOpenWindowHint(GLFW_OPENGL_VERSION_MINOR, 2); | |
glfwOpenWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); | |
glfwOpenWindow(500, 500, 8, 8, 8, 8, 24, 8, GLFW_WINDOW); | |
init(); | |
while(true) | |
{ | |
if(!glfwGetWindowParam(GLFW_OPENED)) | |
{ | |
break; | |
} | |
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); | |
// | |
auto Mat = rotation_matrix_deg(Rotation, Vec3(0, 1, 0)); | |
Rotation += RotationDelta; | |
float Values[16]; | |
double* M = Mat; | |
for(int i = 0; i < 16; ++i) | |
{ | |
Values[i] = M[i]; | |
} | |
auto MatrixParameter = Runtime->get_program_parameter(VertexProgram, "Matrix"); | |
Runtime->parameter_value(VertexProgram, MatrixParameter, Values, 16); | |
// | |
glBindVertexArray(VAO); | |
glDrawArrays(GL_TRIANGLES, 0, 3); // draw second object | |
glfwSwapBuffers(); | |
} | |
glfwTerminate(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment