Jump to content
  • Advertisement
Sign in to follow this  
zauron

strange crash

This topic is 5037 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am working on a small terrain engine, using heightmap. First i want to render a flat terrain. But i am getting a strange(to mee) access violation. After some debugging i have found out it happens after i render my scene. Actually the second time i call DrawIndexedPrimitive() (2.frame). CAnt figure out what i am doing wrong. The buffers are being filled correctly, i wrote the data to file and looked at it.
#include "terrain.h"

#include <stdio.h>   // sprintf
#include <fstream>   // filio
using namespace std;

bool Terrain::generate(){
  Vertex *p_vertex;
	int *p_index;
	int x, y;
  w = h = 3;  // num of squares not vertices
	float squaresize = 4.0f;

	// dreate the buffers
  if(D3D_OK != g_pd3dDevice->CreateIndexBuffer(w*h*6, 0, D3DFMT_INDEX32, D3DPOOL_DEFAULT, &ib, NULL)) return false;
  if(D3D_OK != g_pd3dDevice->CreateVertexBuffer((w+1)*(h+1)*sizeof(Vertex), 0, VERTEXFORMAT, D3DPOOL_DEFAULT, &vb, NULL)) return false;

  //fill vb
  vb->Lock(0,0,(void **)&p_vertex, 0);
  for (y = 0; y < w+1; y++) {
		for (x = 0; x < h+1; x++) {
			p_vertex[x+y*(w+1)].position.x = -w/2+x*squaresize;
			p_vertex[x+y*(w+1)].position.y = 2.0f;
			p_vertex[x+y*(w+1)].position.z = -h/2+y*squaresize;
		}
  }
  vb->Unlock();

	//fill ib
	int i = 0;
  ib->Lock(0,0,(void **)&p_index, 0);
  for (y = 0; y < w; y++) {
		for (x = 0; x < h; x++) {
			p_index[i++] = x+y*(w+1);
			p_index[i++] = x+y*(w+1)+1;
			p_index[i++] = x+y*(w+1)+(w+1);

			p_index[i++] = x+y*(w+1)+1;
			p_index[i++] = x+y*(w+1)+1+(w+1);
			p_index[i++] = x+y*(w+1)+(w+1);
		}
  }
  ib->Unlock();

  return true;
}

// draw
bool Terrain::draw(){

  g_pd3dDevice->SetStreamSource(0, vb, 0, sizeof(VERTEXFORMAT));
	g_pd3dDevice->SetIndices(ib);
	g_pd3dDevice->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 0, (w+1)*(h+1), 0, w*h*2);
  return true;
}


Share this post


Link to post
Share on other sites
Advertisement
From first glance, shouldn't your index buffer size be *sizeof(DWORD) as it should be in bytes not indices?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!