PDA

View Full Version : Self taught/hobby programmer with first OpenGL project difficulties



Snyder
03-07-2014, 07:37 PM
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<<'\n';
// 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;
}

tonyo_au
03-07-2014, 08:33 PM
You say your vertices range from -20 - +20 but I do not see where your set a model-view matrix(and use in the shader). If you are not using one, the vertices x,y,z must be in the range -1 - +1.

arekkusu
03-07-2014, 09:35 PM
Show the shader text.

Snyder
03-07-2014, 10:46 PM
You say your vertices range from -20 - +20 but I do not see where your set a model-view matrix(and use in the shader). If you are not using one, the vertices x,y,z must be in the range -1 - +1.

Brilliant! Okay you've taken care of one error. I went back and make sure it was squarely between -1 and 1; I still get a black box. Such is my luck. At least there are only finitely many errors.

Can you find another one?

The values are not too small that I might just be zoomed out too far. I thought I should include my shader files and generator, just in case. But, I'm pretty sure there's no problem there.

-Chris


//Frag shader
#version 410 core

out vec4 fragData;
void main(){
fragData=vec4(1.0,1.0,1.0,1.0);
}

//vert shader
#version 410 core

in vec3 position;

void main(){
gl_Position=vec4(position,1.0);
}

//shaderutils.cpp
#include "ShaderUtils.h"
//#include <stdlib.h>
//#include <stdio.h>
//#include <iostream>
#include <fstream>
//#include <string>

GLuint shaderUtils::createShaderFromFile(const char* path,const GLenum shaderType){
GLuint shaderID=glCreateShader(shaderType);
std::ifstream fin;
fin.open(path);

if(!fin.is_open()){
fin.close();
std::cout<<"file not found '"<<path<<"'"<<std::endl;
return 0;
}

std::string source((std::istreambuf_iterator<GLchar>(fin)),std::istreambuf_iterator<GLchar>());
fin.close();
const GLchar* shaderSource=source.c_str();
glShaderSource(shaderID,1,&shaderSource,NULL);
glCompileShader(shaderID);
GLint compileStatus;
glGetShaderiv(shaderID, GL_COMPILE_STATUS, &compileStatus);

if (compileStatus!=GL_TRUE) {
std::cout<<"Shader Failed to Compile '"<<path<<"'"<<std::endl;
GLint infoLogLength;
glGetShaderiv(shaderID, GL_INFO_LOG_LENGTH, &infoLogLength);
GLchar* infoLog=new GLchar[infoLogLength+1];
glGetShaderInfoLog(shaderID, infoLogLength+1, NULL, infoLog);
std::cout<<infoLog<<std::endl;
delete infoLog;
return 0;
}

return shaderID;
}

//shaderutils.h
#ifndef __OpenGlTutorial2__ShaderUtils__
#define __OpenGlTutorial2__ShaderUtils__

#include <iostream>
#include <GLFW/glfw3.h>

class shaderUtils {

public:
static GLuint createShaderFromFile(const char* path,const GLenum shaderType);
};


#endif /* defined(__OpenGlTutorial2__ShaderUtils__) */

arekkusu
03-07-2014, 11:04 PM
It looks like you're mixing up (http://stackoverflow.com/questions/14234361/opengl-using-vbo-with-stdvector) C++ std::vector and the OpenGL C API.

Snyder
03-08-2014, 08:30 AM
Hot damn! It works! Funny thing is I had been to that site before, but I guess I came away with the wrong idea.

Thank you both so much.
Chris