Sign in to follow this  
disanti

OpenGL My first attempt with vertex pointers.

Recommended Posts

disanti    174
Hello, I'm trying to figure out how to use vertex pointers with no luck... I have verified that all the loaded vertex data, vertices, colors, normals, uvs, and indices are correctly sent to this code:
/////////////// drawTrianglePointer /////////////
void pss::CGraphics_OpenGL::drawTrianglePointer(const SVertex* vertices, const unsigned short* indices, const int triangleCount)
{
	if(! vertices || ! indices)
		return;

	glEnableClientState(GL_VERTEX_ARRAY);
	glEnableClientState(GL_TEXTURE_COORD_ARRAY);
	glEnableClientState(GL_NORMAL_ARRAY);
	glEnableClientState(GL_COLOR_ARRAY);
	glEnableClientState(GL_INDEX_ARRAY);

	glVertexPointer(3,GL_FLOAT,sizeof(SVertex),&vertices[0].pos);
	glNormalPointer(GL_FLOAT, sizeof(SVertex),&vertices[0].normals);
	glTexCoordPointer(2,GL_FLOAT,sizeof(SVertex),&vertices[0].uv);
	glColorPointer(4,GL_UNSIGNED_BYTE,sizeof(SVertex),&vertices[0].color);
	glIndexPointer(GL_UNSIGNED_SHORT,0,indices);

	glDrawElements(GL_TRIANGLES,triangleCount*3,GL_UNSIGNED_SHORT,indices);
	glFlush();
}

However nothing is drawn to the screen. When I draw my test triangle using glBegin and glEnd however, something is drawn. I have checked the size of the model and adjusted the camera view so that the data should be in view, culling is turned off. So why doesn't it draw anything? According to my time logger I've been debugging this for over three hours, and I'm getting pretty upset. Thanks~ John DiSanti

Share this post


Link to post
Share on other sites
bpoint    464
GL_INDEX_ARRAY and glIndexPointer are for color-indexed rendering (as in for 8-bit palette displays), not indicies into the vertex array. I agree -- the names are misleading :)

First try taking those two lines out and see what happens. Odds are since both the GL_COLOR_ARRAY and GL_INDEX_ARRAY are enabled, there's probably an error being thrown by glDrawElements.

Share this post


Link to post
Share on other sites
CRACK123    235
Shouldn't it be especially if pos, normals, uv and color are actually arrays. Notice the non-usage of reference.


glVertexPointer(3,GL_FLOAT,sizeof(SVertex),vertices[0].pos);
glNormalPointer(GL_FLOAT, sizeof(SVertex),vertices[0].normals);
glTexCoordPointer(2,GL_FLOAT,sizeof(SVertex),vertices[0].uv);
glColorPointer(4,GL_UNSIGNED_BYTE, sizeof(SVertex),vertices[0].color);



And you shouldn't be needing to use the glIndexPointer.

Share this post


Link to post
Share on other sites
ff8    134
i think u need the reference cause u are giving the function the beginning address to start with it .
try to use glVertexPointer w/o the rest as beginning and tell us what happens.

Share this post


Link to post
Share on other sites
disanti    174
Thank you all for the replies!

Quote:

GL_INDEX_ARRAY and glIndexPointer are for color-indexed rendering (as in for 8-bit palette displays), not indicies into the vertex array. I agree -- the names are misleading :)

First try taking those two lines out and see what happens. Odds are since both the GL_COLOR_ARRAY and GL_INDEX_ARRAY are enabled, there's probably an error being thrown by glDrawElements.


OK, so I removed the indexed color stuff, and it still isn't drawing anything. So then I tried commenting everything but the stuff for vertices and the draw elements function. Still no success.

This is my SVertex structure for those wondering:

typedef struct
{
struct {
float x,y,z;
}pos;
struct {
float x,y,z;
}normals;
struct {
float u,v;
}uv;
struct {
unsigned char r,g,b,a;
}color;
}SVertex;



Thanks! Rating++

Share this post


Link to post
Share on other sites
bpoint    464
Hrm, the code looks right... I can't think of any other reason why what you have shouldn't work.

The other thing you could try is using glArrayElement between a glBegin/glEnd pair, instead of the glDrawElements call, like so:

glBegin(GL_TRIANGLES);
for (int t = 0; t < triangleCount * 3; t++)
glArrayElement(indices[t]);
glEnd();


Trying this will at least determine if your problem is setting up the pointers to the data properly, or the indices being passed to glDrawElements. Another possibility is an error is being thrown somewhere. Stick in a few calls to glGetError() within the code there and make sure it's returning GL_NO_ERROR.

Share this post


Link to post
Share on other sites
MARS_999    1627
IMO you should redo the struct and not nest them.

Do something like this

struct Vertex
{
float vx;
float vy;
float vz;
float nx;
float ny;
float nz;
float s;
float t;
unsigned char r,g,b,a;
};



Keep it simple due to their is no need to nest struct inside of structs.

This way you can do
vertex.vx; ect... and you will be able to interleave them easier IMO... HTH

Share this post


Link to post
Share on other sites
disanti    174
OK, I've tried that, and I even tried writing a class that uses Direct3D. Neither draw the model so I'm getting convinced that something really isn't right here.

Edit: I figured I should do one last debug run tonight, and in the corner of my eye I say an error with the "DirectX debug output" that said "invalid number of vertices", so I debugged a little further and somehow I forgot to put the correct number of vertices into the mesh class I have. It draws in OpenGL now, however not in DirectX.

Thank you all for the help! My problem here is solved.

Share this post


Link to post
Share on other sites
bpoint    464
So glArrayElement with glBegin/glEnd didn't work? Are you getting any GL errors at all?

The only other thing I can think of is your vertex data is broken. You said in your first post that you verified it, but you might want to go in with a debugger and step through line by line double- and triple-checking your data.

As a final case, you can parse your data directly, use glBegin/glEnd and look up the indices yourself in a loop, sending the vertex data to glVertex3f and glColor4ub (and whatever is needed to draw your polygon).

Finally, it might be a good idea just to try vertices *only*, then when that's working, add color, test again, then add texture coords, etc. From what it looks like you're sending normal data but your lights may not be even setup properly. You could be getting a black triangle for all I know. :) (Actually, a good idea is to set your clear color to something other than black to check for black triangles.)

Good luck!

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 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
    • By C0dR
      I would like to introduce the first version of my physically based camera rendering library, written in C++, called PhysiCam.
      Physicam is an open source OpenGL C++ library, which provides physically based camera rendering and parameters. It is based on OpenGL and designed to be used as either static library or dynamic library and can be integrated in existing applications.
       
      The following features are implemented:
      Physically based sensor and focal length calculation Autoexposure Manual exposure Lense distortion Bloom (influenced by ISO, Shutter Speed, Sensor type etc.) Bokeh (influenced by Aperture, Sensor type and focal length) Tonemapping  
      You can find the repository at https://github.com/0x2A/physicam
       
      I would be happy about feedback, suggestions or contributions.

    • By altay
      Hi folks,
      Imagine we have 8 different light sources in our scene and want dynamic shadow map for each of them. The question is how do we generate shadow maps? Do we render the scene for each to get the depth data? If so, how about performance? Do we deal with the performance issues just by applying general methods (e.g. frustum culling)?
      Thanks,
       
  • Popular Now