Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stdafx.h"
- #include <iostream>
- #include <stdlib.h>
- #include <stdio.h>
- #include <windows.h>
- #include <GL/glew.h>
- #include <GLFW/glfw3.h>
- #include <GLFW/glfw3native.h>
- #include <SOIL.h>
- #include "Kernel/OVR_Math.h"
- #include <OVR_CAPI_GL.h>
- #include "OVR_CAPI.h"
- #include "CRender.h"
- #include "OvrShader.h" // Header with vertex and fragment shader
- using namespace OVR;
- static void error_callback(int error, const char* description)
- {
- fputs(description, stderr);
- }
- static void key_callback(GLFWwindow* glfwWnd, int key, int scancode, int action, int mods)
- {
- if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
- glfwSetWindowShouldClose(glfwWnd, GL_TRUE);
- }
- // Render class functions
- int oculusVr::ovrInit(int mode)
- {
- // initialise ovr framework
- ovr_Initialize();
- // create hmd handle
- switch (mode) {
- case 0:
- pHmd = ovrHmd_Create(0);
- break;
- case 1:
- pHmd = ovrHmd_CreateDebug(ovrHmd_DK1);
- break;
- case 2:
- pHmd = ovrHmd_CreateDebug(ovrHmd_DK2);
- break;
- default:
- // if no mode assume dummyDK2
- pHmd = ovrHmd_CreateDebug(ovrHmd_DK2);
- break;
- }
- if (!pHmd) {
- printf("ovrInit() failed");
- return NULL;
- }
- // Configure tracking
- ovrHmd_ConfigureTracking(pHmd, ovrTrackingCap_Orientation |
- ovrTrackingCap_MagYawCorrection | ovrTrackingCap_Position, 0);
- // Configure stereo settings
- OVR::Sizei recommenedTex0Size =
- ovrHmd_GetFovTextureSize(pHmd, ovrEye_Left, pHmd->DefaultEyeFov[0], 1.0f);
- OVR::Sizei recommenedTex1Size =
- ovrHmd_GetFovTextureSize(pHmd, ovrEye_Right, pHmd->DefaultEyeFov[1], 1.0f);
- // Get recomended resolution
- recomendedTargetSize.w = recommenedTex0Size.w + recommenedTex1Size.w;
- recomendedTargetSize.h = max(recommenedTex0Size.h, recommenedTex1Size.h);
- return 0;
- }
- int oculusVr::ovrSetParams(int width, int height)
- {
- // Use recomended resolution unless
- if(!(width & height)) {
- renderTargetSize.w = recomendedTargetSize.w;
- renderTargetSize.h = recomendedTargetSize.h;
- } else {
- renderTargetSize.w = width;
- renderTargetSize.h = height;
- }
- // Config eyeRenderDesc with default FOV only
- eyeRenderDesc[0] = ovrHmd_GetRenderDesc(pHmd, ovrEye_Left, pHmd->DefaultEyeFov[0]);
- eyeRenderDesc[1] = ovrHmd_GetRenderDesc(pHmd, ovrEye_Right, pHmd->DefaultEyeFov[1]);
- // Set the viewports based on the recomended resolution
- eyeRenderViewport[0].Pos = Vector2i(0, 0);
- eyeRenderViewport[0].Size = Sizei(renderTargetSize.w / 2, renderTargetSize.h);
- eyeRenderViewport[1].Pos = Vector2i((renderTargetSize.w + 1) / 2, 0);
- eyeRenderViewport[1].Size = eyeRenderViewport[0].Size;
- return 0;
- }
- int oculusVr::ovrMeshVbo()
- {
- // delete and generate 2 new VBOs
- glDeleteBuffers(2, vboId);
- glGenBuffers(2, &vboId[0]);
- // generate 2 IBOs
- glDeleteBuffers(2, iboId);
- glGenBuffers(2, &iboId[0]);
- // generate 1 VAO format is the same for 2 VBOs
- glDeleteVertexArrays(2, vaoId);
- glGenVertexArrays(2, &vaoId[0]);
- for (unsigned int eyeNum = 0; eyeNum < ovrEye_Count; eyeNum++) {
- // Generate distortion mesh vertices for eyeNum
- ovrDistortionMesh meshData;
- ovrHmd_CreateDistortionMesh(pHmd, eyeRenderDesc[eyeNum].Eye,
- eyeRenderDesc[eyeNum].Fov, ovrDistortionCap_Chromatic | ovrDistortionCap_Vignette,
- &meshData);
- // Get render scale and offset for this eyeNum
- ovrHmd_GetRenderScaleAndOffset(eyeRenderDesc[eyeNum].Fov,
- renderTargetSize, eyeRenderViewport[eyeNum], (ovrVector2f*)UVScaleOffset[eyeNum]);
- // VBO
- glBindBuffer(GL_ARRAY_BUFFER, vboId[eyeNum]);
- glBufferData(GL_ARRAY_BUFFER, sizeof(ovrDistortionVertex)*meshData.VertexCount,
- meshData.pVertexData, GL_STATIC_DRAW);
- // IBO
- glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, iboId[eyeNum]); // for indices
- glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(unsigned short)*meshData.IndexCount,
- meshData.pIndexData, GL_STATIC_DRAW);
- // VAO
- // Set vertex pointer to address of eyeNum's vertex data in mesh
- ovrDistortionVertex *pData = meshData.pVertexData;
- glBindVertexArray(vaoId[eyeNum]);
- // TimeWarpFactor and VignetteFade encoded into Pos.z and Pos.w
- glEnableVertexAttribArray(0);
- glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, sizeof(ovrDistortionVertex),
- (GLvoid*)offsetof(ovrDistortionVertex, ScreenPosNDC));
- glEnableVertexAttribArray(1);
- glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, sizeof(ovrDistortionVertex),
- (GLvoid*)offsetof(ovrDistortionVertex, TanEyeAnglesR));
- glEnableVertexAttribArray(2);
- glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, sizeof(ovrDistortionVertex),
- (GLvoid*)offsetof(ovrDistortionVertex, TanEyeAnglesG));
- glEnableVertexAttribArray(3);
- glVertexAttribPointer(3, 2, GL_FLOAT, GL_FALSE, sizeof(ovrDistortionVertex),
- (GLvoid*)offsetof(ovrDistortionVertex, TanEyeAnglesB));
- glBindVertexArray(0);
- // store total index count
- indexCount[eyeNum] = meshData.IndexCount;
- // destroy the mesh
- ovrHmd_DestroyDistortionMesh(&meshData);
- }
- return 0;
- }
- int oculusVr::ovrDeinit()
- {
- if (pHmd) {
- ovrHmd_Destroy(pHmd);
- }
- ovr_Shutdown();
- return 0;
- }
- int oculusVr::oglRender()
- {
- // use vertex and fragment shader
- glUseProgram(shaderProg);
- while (!glfwWindowShouldClose(glfwWindow) ) {
- // Perform operations for window 0
- glfwMakeContextCurrent(glfwWindow);
- glClear(GL_COLOR_BUFFER_BIT);
- // Draw left Eye
- glBindVertexArray(vaoId[0]);
- glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, iboId[0]);
- glUniform2f(uvScale, UVScaleOffset[0][0].x, UVScaleOffset[0][0].y);
- glUniform2f(uvOffset, UVScaleOffset[0][1].x, UVScaleOffset[0][1].y);
- glDrawElements(GL_LINES, indexCount[0], GL_UNSIGNED_SHORT, 0);
- // Draw right Eye
- glBindVertexArray(vaoId[1]);
- glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, iboId[1]);
- glUniform2f(uvScale, UVScaleOffset[1][0].x, UVScaleOffset[1][0].y);
- glUniform2f(uvOffset, UVScaleOffset[1][1].x, UVScaleOffset[1][1].y);
- glDrawElements(GL_TRIANGLES, indexCount[1], GL_UNSIGNED_SHORT, 0);
- glfwSwapBuffers(glfwWindow);
- // Check if 'esc' has been pressed
- glfwPollEvents();
- }
- return 0;
- }
- int oculusVr::oglInit()
- {
- glfwSetErrorCallback(error_callback);
- // Intialize GLFW
- if (!glfwInit()) {
- getchar();
- exit(EXIT_FAILURE);
- }
- // Make undercorated
- glfwWindowHint(GLFW_DECORATED, 0);
- glfwWindow = glfwCreateWindow(renderTargetSize.w, renderTargetSize.h, "OVR - Mesh debug", NULL, NULL);
- if (!glfwWindow) {
- glfwTerminate();
- getchar();
- exit(EXIT_FAILURE);
- }
- glfwMakeContextCurrent(glfwWindow);
- glfwSetKeyCallback(glfwWindow, key_callback);
- glewExperimental = true; // Needed for core profile
- ovrHmd_AttachToWindow(pHmd, glfwGetWin32Window(glfwWindow), NULL, NULL);
- // Initialize GLEW
- if (glewInit() != GLEW_OK) {
- fprintf(stderr, "Failed to initialize GLEW\n");
- getchar();
- return -1;
- }
- // get version info
- const GLubyte* renderer = glGetString(GL_RENDERER); // get renderer string
- const GLubyte* version = glGetString(GL_VERSION); // version as a string
- printf("Renderer: %s\n", renderer);
- printf("OpenGL version supported %s\n", version);
- return 0;
- }
- int oculusVr::oglDeinit()
- {
- // destroy both windows
- glfwDestroyWindow(glfwWindow);
- glfwTerminate();
- return 0;
- }
- int oculusVr::loadShader()
- {
- GLint length, result;
- GLuint vShaderId, fShaderId;
- shaderProg = glCreateProgram();
- // Bind the attributes
- glBindAttribLocation(shaderProg, 0, "Position");
- glBindAttribLocation(shaderProg, 1, "TexCoord0");
- glBindAttribLocation(shaderProg, 2, "TexCoord1");
- glBindAttribLocation(shaderProg, 3, "TexCoord2");
- // vertexShader
- vShaderId = glCreateShader(GL_VERTEX_SHADER);
- length = strlen(vertexShader);
- glShaderSource(vShaderId, 1, &vertexShader, &length);
- glCompileShader(vShaderId);
- // make sure the vertexShader compilation was successful
- glGetShaderiv(vShaderId, GL_COMPILE_STATUS, &result);
- if(result == GL_FALSE) {
- char *log;
- // get the shader info log
- glGetShaderiv(vShaderId, GL_INFO_LOG_LENGTH, &length);
- log = (char*)malloc(length);
- glGetShaderInfoLog(vShaderId, length, &result, log);
- // print an error message and the info log
- fprintf(stderr, "shaderCompileFromFile(): Unable to compile vShader: %s\n", log);
- free(log);
- glDeleteShader(vShaderId);
- return 0;
- }
- // Attach vertexShader
- glAttachShader(shaderProg, vShaderId);
- glDeleteShader(vShaderId);
- // fragmentShader
- fShaderId = glCreateShader(GL_FRAGMENT_SHADER);
- length = strlen(fragmentShader);
- glShaderSource(fShaderId, 1, &fragmentShader, &length);
- glCompileShader(fShaderId);
- // make sure the fragmentShader compilation was successful
- glGetShaderiv(fShaderId, GL_COMPILE_STATUS, &result);
- if(result == GL_FALSE) {
- char *log;
- // get the shader info log
- glGetShaderiv(vShaderId, GL_INFO_LOG_LENGTH, &length);
- log = (char*)malloc(length);
- glGetShaderInfoLog(fShaderId, length, &result, log);
- // print an error message and the info log
- fprintf(stderr, "shaderCompileFromFile(): Unable to compile fShader: %s\n", log);
- free(log);
- glDeleteShader(fShaderId);
- return 0;
- }
- // attach fragmentShader
- glAttachShader(shaderProg, fShaderId);
- glDeleteShader(fShaderId);
- // Link the program
- glLinkProgram(shaderProg);
- glGetProgramiv(shaderProg, GL_LINK_STATUS, &result);
- if(result == GL_FALSE) {
- GLint length;
- char *log;
- // get the program info log
- glGetProgramiv(shaderProg, GL_INFO_LOG_LENGTH, &length);
- log = (char*)malloc(length);
- glGetProgramInfoLog(shaderProg, length, &result, log);
- // print an error message and the info log
- fprintf(stderr, "sceneInit(): Program linking failed: %s\n", log);
- free(log);
- /* delete the program */
- glDeleteProgram(shaderProg);
- shaderProg = 0;
- return 0;
- }
- // setup uniforms
- uvScale = glGetUniformLocation(shaderProg, "EyeToSourceUVScale");
- uvOffset = glGetUniformLocation(shaderProg, "EyeToSourceUVOffset");
- return 0;
- };
- int oculusVr::loadTexture()
- {
- int width, height;
- unsigned char* image;
- // Enable context for window 0
- glfwMakeContextCurrent(glfwWindow);
- // Load texture
- GLuint tex1;
- glGenTextures(1, &tex1);
- glActiveTexture(GL_TEXTURE0);
- glBindTexture(GL_TEXTURE_2D, tex1);
- image = SOIL_load_image("rgb.png", &width, &height, 0, SOIL_LOAD_RGB);
- glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, image);
- SOIL_free_image_data(image);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment