Sign in to follow this  
tre

OpenGL Working with OGL3.x-

Recommended Posts

tre    168
Hi!
I've been setting up a new context for some time now. And I finally seem to get to grips with it. However, I can't render anything in my window, which is quite crippling...

I'm loading an OBJ into vectors and load that information into a couple of VBO's for texccords, vertex position, normals and a vector for indices.
I know for a fact that the code I'm using is functional under lower contexts and I just can't get it to work in the new one.

I'm thinking that it's got something to do with using my own matrices and how the shaders are set up... but I can't figure it out.

My render function:
void cOpenGLContext::renderScene(void) {
glViewport(0, 0, windowWidth, windowHeight); // set the viewport to fill the entire window
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); // clear required buffers

viewMatrix = glm::translate(glm::mat4(1.0f), glm::vec3(0.0f, 0.0f, -5.0f)); // create our view matrix which will translate us back 5 units
modelMatrix = glm::scale(glm::mat4(1.0f), glm::vec3(0.5f));// create our model matrix which will halve the size of our model

shader->bind();
int projectionMatrixLocation = glGetUniformLocation(shader->id(), "projectionMatrix"); // get the location of our projection matrix in the shader
int viewMatrixLocation = glGetUniformLocation(shader->id(), "viewMatrix"); // get the location of our view matrix in the shader
int modelMatrixLocation = glGetUniformLocation(shader->id(), "modelMatrix"); // get the location of our model matrix in the shader

glUniformMatrix4fv(projectionMatrixLocation, 1, GL_FALSE, &projectionMatrix[0][0]);// send our projection matrix to the shader
glUniformMatrix4fv(viewMatrixLocation, 1, GL_FALSE, &viewMatrix[0][0]); // send our view matrix to the shader
glUniformMatrix4fv(modelMatrixLocation, 1, GL_FALSE, &modelMatrix[0][0]); // send our model matrix to the shader

// rest of render function here
// VBO [start]
// drawing
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_NORMAL_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
// vertex buffer
glBindBuffer(GL_ARRAY_BUFFER, earth.vBufferObject);
glVertexPointer(3, GL_FLOAT, 0, 0);

// texture buffer
glBindBuffer(GL_ARRAY_BUFFER, earth.tBufferObject);
glTexCoordPointer(3, GL_FLOAT, 0, 0);

// normal buffer
glBindBuffer(GL_ARRAY_BUFFER, earth.nBufferObject);
glNormalPointer(GL_FLOAT, 0, 0);

// index buffer
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, earth.iBufferObject);

// draws it all
glDrawElements(GL_TRIANGLES, earth.vFaces.size(), GL_UNSIGNED_INT, 0);

// unbinds the buffers
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
glDisableClientState(GL_NORMAL_ARRAY);
glDisableClientState(GL_VERTEX_ARRAY);
// drawing end
shader->unbind();

SwapBuffers(hdc); // swap buffers so we can see our rendering
}





My shaders

// vert
#version 150 core

in vec3 in_Position;
in vec3 in_Color;
out vec3 pass_Color;

uniform mat4 projectionMatrix;
uniform mat4 viewMatrix;
uniform mat4 modelMatrix;

void main(void) {
gl_Position = projectionMatrix * viewMatrix * modelMatrix * vec4(in_Position, 1.0);
pass_Color = in_Color;
}



// frag
#version 150 core

in vec3 pass_Color;
out vec4 out_Color;

void main(void) {
out_Color = vec4(pass_Color, 1.0);
}





Can anyone see what I'm doing wrong? I'm going blind, staring at this code.

Thanks!
Marcus

Share this post


Link to post
Share on other sites
TheBuzzSaw    143
It looks like you need to study the 3.x specification a bit more closely. OpenGL 3 deprecates a lot of functionality. Just skimming over your code, I can see lots of deprecated code: glEnableClientState, glVertexPointer, etc. You need to learn how to use vertex attribute arrays.

If you are running OpenGL 3.2 or later, those functions simply will not work at all. Running 3.1 should enable that deprecated functionality.

Share this post


Link to post
Share on other sites
tre    168
Hi, TheBuzzSaw.
Do you mean something like:

// function for setting up the buffers
glGenBuffers(1, &vBufferObject);
glBindBuffer(GL_ARRAY_BUFFER, vBufferObject);
glBufferData(GL_ARRAY_BUFFER, ((int)(vertices.size()) * 3 * sizeof(float)), &(vertices.at(0)), GL_STATIC_DRAW);
glVertexAttribPointer((GLuint)0, 3, GL_FLOAT, GL_FALSE, 0, 0);

// ...

// draw function
glBindVertexArray(earth.vBufferObject);
glDrawArrays(GL_TRIANGLES, 0, 3);


Thanks for your previous answer.

Share this post


Link to post
Share on other sites
tre    168
Hm hm. Okay. I've got stuff to think about then. Crap. But thanks for your answer :)

If anyone else have anything to add, please do.

Thanks!
Marcus

Share this post


Link to post
Share on other sites
tre    168
Quote:
Original post by Trefall
Here's how I use VAO and VBO: Cube.cpp


Thanks for posting that. That's how I'm doing it right now. But I'm still not getting anything on the screen. Just my regular background.

// vao, vbo, ibo setup
vao = 0;

// vertex buffer
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);

glGenBuffers(1, &vBufferObject);
glBindBuffer(GL_ARRAY_BUFFER, vBufferObject);
glBufferData(GL_ARRAY_BUFFER, sizeof(float)*vertices.size(), NULL, GL_STATIC_DRAW);
glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(float)*vertices.size(), &vertices[0]);


//.....


// render function
glBindVertexArray(earth.vao);
glDrawElements(GL_TRIANGLES, earth.vFaces.size(), GL_UNSIGNED_INT, 0);
glBindVertexArray(0);


Thanks for the help, guys.

Share this post


Link to post
Share on other sites
Trefall    340
I assume you also bind and buffer your index list? :) Use this snippet to see if OpenGL is throwing any errors internally:


{
GLenum err = glGetError();
if(err != GL_NO_ERROR)
std::cout << "OpenGL prog error: " << gluErrorString(err) << std::endl;
std::cout.flush();
}

Share this post


Link to post
Share on other sites
cgrant    1825
You also may want to calculate your modelview * projection matrix on the CPU and uploading it once to the GPU instead of doing it per-vertex. The model matrix has to be passed up separately for each unique object. The only access you have to the GL default attributes is to the gl_* attributes in your shader. User defined attributes have to be bound and specified by the user...it doesn't happen automagically. See the GLSL spec for reference. With that said your shader is transforming some unknown attributes since the default vertex attribute that is bound to glVertexPointer is gl_Vertex which is not valid when version 150 is define.

Share this post


Link to post
Share on other sites
TheBuzzSaw    143
Ah yes. I definitely agree. Do as much of the work on the CPU as you can. If you know that the work needs to be done once, and it affects all the vertices you are about to draw, forcing every vertex to do the same work is wasteful.

Share this post


Link to post
Share on other sites
tre    168
Quote:
Original post by cgrant
You also may want to calculate your modelview * projection matrix on the CPU and uploading it once to the GPU instead of doing it per-vertex. The model matrix has to be passed up separately for each unique object. The only access you have to the GL default attributes is to the gl_* attributes in your shader. User defined attributes have to be bound and specified by the user...it doesn't happen automagically. See the GLSL spec for reference. With that said your shader is transforming some unknown attributes since the default vertex attribute that is bound to glVertexPointer is gl_Vertex which is not valid when version 150 is define.


Allright, seems like I've got quite a bit to rework, then.
- Calculate modelview * projection matrix on the CPU and upload it to the GPU once for each unique object
- Since I'm using GLSL version 150 I need some other way to point to my vertices.

Taking another look at the GLSL and OpenGL specifications.

Thanks!

Share this post


Link to post
Share on other sites
Trefall    340
Quote:
Original post by TheBuzzSaw
You don't wanna be using GLU in OpenGL 3.


You should correct that comment to: "you don't want to use GLU functionality that use deprecated OpenGL functionality" ;) gluErrorString is perfectly safe to use with OpenGL 3.x, and is quite convenient actually. Of course, you could always make your own string registry on GL error enums.

Quote:
Original post by tre
- Since I'm using GLSL version 150 I need some other way to point to my vertices.


Just send them in as attributes, with a buffer offset towards your VBO. Since you only have vertices in the vbo, that means you will point to the first element of your vbo. OpenGL will handle the rest for you :) If you refer back to my Cube.cpp you'll see how I've handled this.


#ifndef BUFFER_OFFSET
#define BUFFER_OFFSET(bytes) ((GLubyte*) NULL + bytes)
#endif

static void shaderAttrib(unsigned int prog, const char *attribName, int size, GLenum type, bool normalized, int stride, void* pointer)
{
int id = glGetAttribLocation(prog, attribName);
if(id < 0)
throw CL_Exception(cl_format("Failed to load attribute %1", attribName));

glVertexAttribPointer(id, size, type, normalized, stride, pointer);
glEnableVertexAttribArray(id);
}

//usage:
shaderAttrib(shader.getShaderProg(), "vVertex", 3, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(0));



Just make sure you use it while the VAO/VBO is still bound.

Share this post


Link to post
Share on other sites
TheBuzzSaw    143
Quote:
Original post by tre
Allright, seems like I've got quite a bit to rework, then.
- Calculate modelview * projection matrix on the CPU and upload it to the GPU once for each unique object
- Since I'm using GLSL version 150 I need some other way to point to my vertices.

Taking another look at the GLSL and OpenGL specifications.

Thanks!


Don't be afraid to transmit several matrices into the shaders either. For instance, some shader designs require access to just the model view or just the projection. However, you still don't want every single vertex doing unnecessary matrix math, so it is still smart to transmit the pre-multiplied model view projection matrix. One of my shaders, for instance, has uniform matrices "MVPM", "MVM", and "PM".

Quote:
Original post by Trefall
You should correct that comment to: "you don't want to use GLU functionality that use deprecated OpenGL functionality" ;) gluErrorString is perfectly safe to use with OpenGL 3.x, and is quite convenient actually. Of course, you could always make your own string registry on GL error enums.


Fair enough. The only stuff I was using from GLU back in the day was gluPerspective and gluUnProject. I just looked up how they were both calculated and rid myself my GLU entirely. It's also an annoying extra dependency in projects. XD

Share this post


Link to post
Share on other sites
tre    168
Allright. After some time I've got my program up and running with drawing happening.
The problem I'm facing now is that my model is not being drawn correctly. I've had this problem before and I think I know the cause - reading in an OBJ-file and only drawing the vertices messes it up when trying to draw anything else than GL_POINTS.
However, I don't know of any way to draw using face indices instead of the raw vertex data, using VAO's and VBO's.

The way I'm setting up my VAO+VBO:
// vertex buffer
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);

glGenBuffers(1, &vBufferObject);
glBindBuffer(GL_ARRAY_BUFFER, vBufferObject);
glBufferData(GL_ARRAY_BUFFER, sizeof(float)*vertices.size(), NULL, GL_STATIC_DRAW);
glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(float)*vertices.size(), &vertices[0]);



So, that's setting up my VAO+VBO using a vertex array. I'll have to change this so that the program reads and loads the indices. How?

Then the rendering part:
...
glBindVertexArray(vaoID[0]);
glDrawArrays(GL_TRIANGLE_STRIP, 0, earth.vertices.size());
glBindVertexArray(0);
...



How does this have to change to draw the model using the face indices? I just can't seem to find any good information on drawing using those instead. Maybe I'm stupid, but I just can't.

If anyone would like to explain it to me or point me in the right direction, it'd be great.

Thanks again, guys!
Marcus

Share this post


Link to post
Share on other sites
tre    168
Quote:
Original post by TheBuzzSaw
If you are trying to draw OBJ data, where are your indices? Look into using glDrawElements.


My indices are loaded when the model is loaded and set up last of the buffers for vertices, texture coordinates and normals.

Here:
// index buffer
glGenBuffers(1, &iBufferObject);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, iBufferObject);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, ((int)(vFaces.size()) * 3 * sizeof(float)), &(vFaces.at(0)), GL_STATIC_DRAW);


I were looking at glDrawElements... at the moment I'm loading all my vertex information from a Vertex Array Object. How is the process when using the indices? The same? As I understand it a VBO can be loaded with any information (vertex, normal, elements, texture coordinates) and a VAO can be loaded with several VBO's.

I don't know if you can tell, but I'm a little confused about how to go about this :)

Share this post


Link to post
Share on other sites
wolfscaptain    200
VBOs hold any sort of data in OpenGL, VAOs hold OpenGL state that should be applied for a single rendering call.

VAOs are there just for convenience, so you wont need to bind your buffers and make the pointers every single time you want to draw with your buffers.

Now, you can have as many (or there's probably a limit you will never get to) VBOs holding vertex data (GL_ARRAY_BUFFER) in a VAO, and one indices VBO (GL_ELEMENT_ARRAY_BUFFER).
If the VAO has an indices buffer, you can then use any rendering functions that use indices, glDraw*Elements*.

Here's a very good site teaching OpenGL 3.3 http://www.arcsynthesis.org/gltut/index.html

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this  

  • Similar Content

    • By Zaphyk
      I am developing my engine using the OpenGL 3.3 compatibility profile. It runs as expected on my NVIDIA card and on my Intel Card however when I tried it on an AMD setup it ran 3 times worse than on the other setups. Could this be a AMD driver thing or is this probably a problem with my OGL code? Could a different code standard create such bad performance?
    • By Kjell Andersson
      I'm trying to get some legacy OpenGL code to run with a shader pipeline,
      The legacy code uses glVertexPointer(), glColorPointer(), glNormalPointer() and glTexCoordPointer() to supply the vertex information.
      I know that it should be using setVertexAttribPointer() etc to clearly define the layout but that is not an option right now since the legacy code can't be modified to that extent.
      I've got a version 330 vertex shader to somewhat work:
      #version 330 uniform mat4 osg_ModelViewProjectionMatrix; uniform mat4 osg_ModelViewMatrix; layout(location = 0) in vec4 Vertex; layout(location = 2) in vec4 Normal; // Velocity layout(location = 3) in vec3 TexCoord; // TODO: is this the right layout location? out VertexData { vec4 color; vec3 velocity; float size; } VertexOut; void main(void) { vec4 p0 = Vertex; vec4 p1 = Vertex + vec4(Normal.x, Normal.y, Normal.z, 0.0f); vec3 velocity = (osg_ModelViewProjectionMatrix * p1 - osg_ModelViewProjectionMatrix * p0).xyz; VertexOut.velocity = velocity; VertexOut.size = TexCoord.y; gl_Position = osg_ModelViewMatrix * Vertex; } What works is the Vertex and Normal information that the legacy C++ OpenGL code seem to provide in layout location 0 and 2. This is fine.
      What I'm not getting to work is the TexCoord information that is supplied by a glTexCoordPointer() call in C++.
      Question:
      What layout location is the old standard pipeline using for glTexCoordPointer()? Or is this undefined?
       
      Side note: I'm trying to get an OpenSceneGraph 3.4.0 particle system to use custom vertex, geometry and fragment shaders for rendering the particles.
    • By markshaw001
      Hi i am new to this forum  i wanted to ask for help from all of you i want to generate real time terrain using a 32 bit heightmap i am good at c++ and have started learning Opengl as i am very interested in making landscapes in opengl i have looked around the internet for help about this topic but i am not getting the hang of the concepts and what they are doing can some here suggests me some good resources for making terrain engine please for example like tutorials,books etc so that i can understand the whole concept of terrain generation.
       
    • By KarimIO
      Hey guys. I'm trying to get my application to work on my Nvidia GTX 970 desktop. It currently works on my Intel HD 3000 laptop, but on the desktop, every bind textures specifically from framebuffers, I get half a second of lag. This is done 4 times as I have three RGBA textures and one depth 32F buffer. I tried to use debugging software for the first time - RenderDoc only shows SwapBuffers() and no OGL calls, while Nvidia Nsight crashes upon execution, so neither are helpful. Without binding it runs regularly. This does not happen with non-framebuffer binds.
      GLFramebuffer::GLFramebuffer(FramebufferCreateInfo createInfo) { glGenFramebuffers(1, &fbo); glBindFramebuffer(GL_FRAMEBUFFER, fbo); textures = new GLuint[createInfo.numColorTargets]; glGenTextures(createInfo.numColorTargets, textures); GLenum *DrawBuffers = new GLenum[createInfo.numColorTargets]; for (uint32_t i = 0; i < createInfo.numColorTargets; i++) { glBindTexture(GL_TEXTURE_2D, textures[i]); GLint internalFormat; GLenum format; TranslateFormats(createInfo.colorFormats[i], format, internalFormat); // returns GL_RGBA and GL_RGBA glTexImage2D(GL_TEXTURE_2D, 0, internalFormat, createInfo.width, createInfo.height, 0, format, GL_FLOAT, 0); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); DrawBuffers[i] = GL_COLOR_ATTACHMENT0 + i; glBindTexture(GL_TEXTURE_2D, 0); glFramebufferTexture(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + i, textures[i], 0); } if (createInfo.depthFormat != FORMAT_DEPTH_NONE) { GLenum depthFormat; switch (createInfo.depthFormat) { case FORMAT_DEPTH_16: depthFormat = GL_DEPTH_COMPONENT16; break; case FORMAT_DEPTH_24: depthFormat = GL_DEPTH_COMPONENT24; break; case FORMAT_DEPTH_32: depthFormat = GL_DEPTH_COMPONENT32; break; case FORMAT_DEPTH_24_STENCIL_8: depthFormat = GL_DEPTH24_STENCIL8; break; case FORMAT_DEPTH_32_STENCIL_8: depthFormat = GL_DEPTH32F_STENCIL8; break; } glGenTextures(1, &depthrenderbuffer); glBindTexture(GL_TEXTURE_2D, depthrenderbuffer); glTexImage2D(GL_TEXTURE_2D, 0, depthFormat, createInfo.width, createInfo.height, 0, GL_DEPTH_COMPONENT, GL_FLOAT, 0); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glBindTexture(GL_TEXTURE_2D, 0); glFramebufferTexture(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, depthrenderbuffer, 0); } if (createInfo.numColorTargets > 0) glDrawBuffers(createInfo.numColorTargets, DrawBuffers); else glDrawBuffer(GL_NONE); if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) std::cout << "Framebuffer Incomplete\n"; glBindFramebuffer(GL_FRAMEBUFFER, 0); width = createInfo.width; height = createInfo.height; } // ... // FBO Creation FramebufferCreateInfo gbufferCI; gbufferCI.colorFormats = gbufferCFs.data(); gbufferCI.depthFormat = FORMAT_DEPTH_32; gbufferCI.numColorTargets = gbufferCFs.size(); gbufferCI.width = engine.settings.resolutionX; gbufferCI.height = engine.settings.resolutionY; gbufferCI.renderPass = nullptr; gbuffer = graphicsWrapper->CreateFramebuffer(gbufferCI); // Bind glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbo); // Draw here... // Bind to textures glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, textures[0]); glActiveTexture(GL_TEXTURE1); glBindTexture(GL_TEXTURE_2D, textures[1]); glActiveTexture(GL_TEXTURE2); glBindTexture(GL_TEXTURE_2D, textures[2]); glActiveTexture(GL_TEXTURE3); glBindTexture(GL_TEXTURE_2D, depthrenderbuffer); Here is an extract of my code. I can't think of anything else to include. I've really been butting my head into a wall trying to think of a reason but I can think of none and all my research yields nothing. Thanks in advance!
    • By Adrianensis
      Hi everyone, I've shared my 2D Game Engine source code. It's the result of 4 years working on it (and I still continue improving features ) and I want to share with the community. You can see some videos on youtube and some demo gifs on my twitter account.
      This Engine has been developed as End-of-Degree Project and it is coded in Javascript, WebGL and GLSL. The engine is written from scratch.
      This is not a professional engine but it's for learning purposes, so anyone can review the code an learn basis about graphics, physics or game engine architecture. Source code on this GitHub repository.
      I'm available for a good conversation about Game Engine / Graphics Programming
  • Popular Now