Sign in to follow this  
zauron

strange crash

Recommended Posts

zauron    122
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

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