Deepest apologies for any formatting faux pax–It’s my first time here.
I have the following set of code (Xcode 5.02, OpenGL 4.1). I am trying to integrate numerically the lorenz differential equations and then pass that (once it’s all done) to OpenGL to make a pretty picture and maybe some math insight.
Symptoms: The same code works as is with the commented out GLfloat bufferData in place of the one returned from the instance Lorenz1 (of course the DrawArrays function at the bottom has to be substituted out). In this case I see three lines connected like I would like. Furthermore, in the present version, bufferData is not null. If you ask what it is near the bottom where it is drawn, you get a 1001 or 10001 entry vector with floats varying between 20 and -20. The program compiles as seen below without error or squabble, but it presents only a black box.
There are two classes not included for creating shaders and evaluating the diffeq but I didn’t think to post them because of space and I’m pretty sure they aren’t the problem due to successful runs mentioned above.
If you can help me and or point me in the right direction for how to debug things like VAO and VBO, you will be my hero of the week.
-Chris
#define GLFW_INCLUDE_GLCOREARB //to make glfw3.h include gl3.h instead of gl.h
#include <GLFW/glfw3.h>
//#include <glm/glm.hpp>
#include <stdlib.h>
#include <stdio.h>
#include <iostream>
#include "ShaderUtils.h"
#include "LorenzDiffEq.h"
void scroll(GLFWwindow* window,double x,double y){
std::cout<<x<<'
';
// glTranslatef(0.0, 0.0, -y);
}
int main(){
//Do Lorenz things
// std::vector<float> y0;
// y0.reserve(4);
// y0[0]=0.0f;y0[1]=5.0f;y0[2]=11.0f;y0[3]=7.0f;
float arr[]={0.0,5.0,11.0,7.0};
std::vector<float> y0(arr,arr+sizeof(arr)/sizeof(arr[0]));
DiffEq Lorenz1(y0);//initialize lorenz equation
Lorenz1.integrateSingleLorenz();
long int liVerticies;
Lorenz1.getNumberOfVerticies(liVerticies);
// GLuint nVerticies=bufferData.size()/3;
GLuint nVerticies=(GLuint)liVerticies;
//Create Window and start glfw
GLint glfwStatus=glfwInit();
if(!glfwStatus){
std::cout<<"window did not load"<<std::endl;
glfwTerminate();
}
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);//3
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 1);//2
GLFWwindow* window=glfwCreateWindow(640, 360, "OpenGL practice", NULL, NULL);
if (window==NULL) {
std::cout<<"did not create window"<<std::endl;
glfwTerminate();
return 1;
}
glfwMakeContextCurrent(window);
std::cout<<glGetString(GL_VERSION)<<std::endl;
glfwSetScrollCallback(window,scroll);
//Set VBO
GLuint myVBO;
glGenBuffers(1,&myVBO);
glBindBuffer(GL_ARRAY_BUFFER, myVBO);
std::vector<float> bufferData;
bufferData.reserve(nVerticies);
Lorenz1.makeBufferData(bufferData);//feeds data into here
glBufferData(GL_ARRAY_BUFFER, sizeof(bufferData),&bufferData, GL_STATIC_DRAW);
// GLfloat bufferData[]=
// {0.0,0.0,0.0,
// 1.0,0.0,0.0,
// 1.0,1.0,0.0,
// 1.0,1.0,1.0};
glBufferData(GL_ARRAY_BUFFER, sizeof(bufferData),&bufferData, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, NULL);
//Create Shaders and Program
GLuint vertexShader=shaderUtils::createShaderFromFile("/Users/christophersnyder/Documents/cppprojects/OpenGlTutorial2/vertShader.vs", GL_VERTEX_SHADER);
GLuint fragmentShader=shaderUtils::createShaderFromFile("/Users/christophersnyder/Documents/cppprojects/OpenGlTutorial2/fragsShader.fs", GL_FRAGMENT_SHADER);
GLuint shaderProgram=glCreateProgram();
glAttachShader(shaderProgram, vertexShader);
glAttachShader(shaderProgram, fragmentShader);
glLinkProgram(shaderProgram);
GLint linkStatus;
glGetProgramiv(shaderProgram, GL_LINK_STATUS, &linkStatus);
if (linkStatus!=GL_TRUE) {
std::cout<<"Shader program Failed to link"<<std::endl;
GLint infoLogLength;
glGetProgramiv(shaderProgram, GL_INFO_LOG_LENGTH, &infoLogLength);
GLchar* infoLog=new GLchar[infoLogLength+1];
glGetProgramInfoLog(shaderProgram, infoLogLength+1, NULL, infoLog);
delete infoLog;
return 0;
}
glUseProgram(shaderProgram);
//Create VAO
GLuint myVAO;
glGenVertexArrays(1, &myVAO);
glBindVertexArray(myVAO);
GLint positionLoc=glGetAttribLocation(shaderProgram, "position");
glEnableVertexAttribArray(positionLoc);//enables for use in VAO
glBindBuffer(GL_ARRAY_BUFFER,myVBO);
glVertexAttribPointer(positionLoc, 3, GL_FLOAT, GL_FALSE, 0, 0);
glBindBuffer(GL_ARRAY_BUFFER, NULL);
glBindVertexArray(NULL);
//make background black
glClearColor(0.0, 0.0, 0.0, 0.0);
//plotstuff
while( !glfwWindowShouldClose(window)){
GLint windowWidth,windowHeight;
glfwGetWindowSize(window,&windowWidth,&windowHeight);
glViewport(0, 0, windowWidth, windowHeight);
glClear(GL_COLOR_BUFFER_BIT);
glBindVertexArray(myVAO);
// std::cout<<myVBO;
// glDrawArrays(GL_LINES, 0, 2);
// std::cout<<nVerticies<<std::endl;
glDrawArrays(GL_LINE_STRIP, 0, nVerticies);
// glDrawArrays(GL_LINE_STRIP, 0, 5);
glBindVertexArray(NULL);
glfwPollEvents();
glfwSwapBuffers(window);
}
glDeleteVertexArrays(1, &myVAO);
glDeleteProgram(shaderProgram);
glDeleteShader(vertexShader);
glDeleteShader(fragmentShader);
glDeleteBuffers(1, &myVBO);
glfwDestroyWindow(window);
glfwTerminate();
return 0;
}