2010-10-03 111 views
0

我要让this code跑,但我还是太上℃的的n00b,以及OpenGL和VS 2008,以标识它们的是故障:为什么Visual Studio不允许我编译这个Bresenham代码?

#include <stdio.h> 
#include <stdlib.h> 
#include <GL/glut.h> 
#include<math.h> 


void init(void) 
{ 

//set display-window background color to white 
glClearColor(1.0,1.0,1.0,0.0); 
//set projection paramaters 
glMatrixMode(GL_PROJECTION); 
gluOrtho2D(0.0,300.0,0.0,300.0); 


} 


void setPixel(GLint xCoordinate, GLint yCoordinate) 
{ 
glBegin(GL_POINTS); 
glVertex2i(xCoordinate,yCoordinate); 
glEnd(); 
glFlush(); //executes all OpenGL functions as quickly as possible 
} 

//Bresenham line-drawing procedure for |m| < 1.0 

void lineBres(GLint x0, GLint y0, GLint xEnd, GLint yEnd) 
{ 
GLint dx = fabs(xEnd - x0); 
GLint dy = fabs(yEnd - y0); 
GLint p = 2 * dy - dx; 
GLint twoDy = 2 * dy; 
GLint twoDyMinusDx = 2 * (dy-dx); 
GLint x,y; 
// determine which endpoint to use as start position 


if (x0 > xEnd){ 
    x = xEnd; 
    y = yEnd; 
    xEnd = x; 
    }else{ 
    x = x0; 
    y = y0; 
    } 
    setPixel(x,y); 
     while(x<xEnd){ 
      x++; 
      if(p<0) 
      p += twoDy; 
      else{ 
       y++; 
       p += twoDyMinusDx; 
      } 
      setPixel(x,y); 
     } 
} 


void drawMyLine(void) 
{ 
    glClear(GL_COLOR_BUFFER_BIT); 
    glColor3f(1.0,0.0,0.0); 
    glPointSize(4.0); 
     GLint x0 = 100; 
     GLint y0 = 150; 
     GLint xEnd = 200; 
     GLint yEnd = 200; 
     lineBres(x0,y0,xEnd,yEnd); 
} 


void main(int argc, char*argv[]) 
{ 
//initialize GLUT 
    glutInit(&argc,argv); 
//initialize display mode 
    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); 
//set display-window width & height 
    glutInitWindowSize(400,400); 
//set display-window upper-left position 
    glutInitWindowPosition(0,0); 
//create display-window with a title 
    glutCreateWindow("Digital Differential Analyzer Algorithm: Programmed by Salha"); 
//initialze OpenGL 
    init(); 
//call graphics to be displayed on the window 
    glutDisplayFunc(drawMyLine); 
//display everything and wait 
    glutMainLoop(); 
} 

错误消息:

1>------ Build started: Project: test2, Configuration: Debug Win32 ------ 
1>Compiling... 
1>testing.c 
1>e:\documents and settings\administrator\my documents\visual studio 2008\projects\test2\test2\testing.c(68) : error C2275: 'GLint' : illegal use of this type as an expression 
1>  e:\program files\microsoft sdks\windows\v6.0a\include\gl\gl.h(48) : see declaration of 'GLint' 
1>e:\documents and settings\administrator\my documents\visual studio 2008\projects\test2\test2\testing.c(68) : error C2146: syntax error : missing ';' before identifier 'x0' 
1>e:\documents and settings\administrator\my documents\visual studio 2008\projects\test2\test2\testing.c(68) : error C2065: 'x0' : undeclared identifier 
1>e:\documents and settings\administrator\my documents\visual studio 2008\projects\test2\test2\testing.c(69) : error C2275: 'GLint' : illegal use of this type as an expression 
1>  e:\program files\microsoft sdks\windows\v6.0a\include\gl\gl.h(48) : see declaration of 'GLint' 
1>e:\documents and settings\administrator\my documents\visual studio 2008\projects\test2\test2\testing.c(69) : error C2146: syntax error : missing ';' before identifier 'y0' 
1>e:\documents and settings\administrator\my documents\visual studio 2008\projects\test2\test2\testing.c(69) : warning C4047: '=' : 'double (__cdecl *)(double)' differs in levels of indirection from 'int' 
1>e:\documents and settings\administrator\my documents\visual studio 2008\projects\test2\test2\testing.c(69) : error C2106: '=' : left operand must be l-value 
1>e:\documents and settings\administrator\my documents\visual studio 2008\projects\test2\test2\testing.c(69) : warning C4550: expression evaluates to a function which is missing an argument list 
1>e:\documents and settings\administrator\my documents\visual studio 2008\projects\test2\test2\testing.c(70) : error C2275: 'GLint' : illegal use of this type as an expression 
1>  e:\program files\microsoft sdks\windows\v6.0a\include\gl\gl.h(48) : see declaration of 'GLint' 
1>e:\documents and settings\administrator\my documents\visual studio 2008\projects\test2\test2\testing.c(70) : error C2146: syntax error : missing ';' before identifier 'xEnd' 
1>e:\documents and settings\administrator\my documents\visual studio 2008\projects\test2\test2\testing.c(70) : error C2065: 'xEnd' : undeclared identifier 
1>e:\documents and settings\administrator\my documents\visual studio 2008\projects\test2\test2\testing.c(71) : error C2275: 'GLint' : illegal use of this type as an expression 
1>  e:\program files\microsoft sdks\windows\v6.0a\include\gl\gl.h(48) : see declaration of 'GLint' 
1>e:\documents and settings\administrator\my documents\visual studio 2008\projects\test2\test2\testing.c(71) : error C2146: syntax error : missing ';' before identifier 'yEnd' 
1>e:\documents and settings\administrator\my documents\visual studio 2008\projects\test2\test2\testing.c(71) : error C2065: 'yEnd' : undeclared identifier 
1>e:\documents and settings\administrator\my documents\visual studio 2008\projects\test2\test2\testing.c(72) : error C2065: 'x0' : undeclared identifier 
1>e:\documents and settings\administrator\my documents\visual studio 2008\projects\test2\test2\testing.c(72) : warning C4047: 'function' : 'GLint' differs in levels of indirection from 'double (__cdecl *)(double)' 
1>e:\documents and settings\administrator\my documents\visual studio 2008\projects\test2\test2\testing.c(72) : warning C4024: 'lineBres' : different types for formal and actual parameter 2 
1>e:\documents and settings\administrator\my documents\visual studio 2008\projects\test2\test2\testing.c(72) : error C2065: 'xEnd' : undeclared identifier 
1>e:\documents and settings\administrator\my documents\visual studio 2008\projects\test2\test2\testing.c(72) : error C2065: 'yEnd' : undeclared identifier 
1>Build log was saved at "file://e:\Documents and Settings\Administrator\My Documents\Visual Studio 2008\Projects\test2\test2\Debug\BuildLog.htm" 
1>test2 - 15 error(s), 4 warning(s) 
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ========== 
+0

你能在这段代码中显示67行吗? – Rekin 2010-10-03 12:42:21

回答

4

开始在第一条消息

... testing.c(68):错误...非法使用...

转到您的源代码行68

void drawMyLine(void) 
{ 
    glClear(GL_COLOR_BUFFER_BIT); 
    glColor3f(1.0,0.0,0.0); 
    glPointSize(4.0); 
     GLint x0 = 100; /* THIS ONE? LINE 68? */ 
     GLint y0 = 150; 
     GLint xEnd = 200; 
     GLint yEnd = 200; 
     lineBres(x0,y0,xEnd,yEnd); 
} 

确定什么是错的,并改正它(显然你在C89模式下调用编译器:移动声明到函数的顶部)。重新编译和循环

void drawMyLine(void) 
{ 
     GLint x0 = 100; /* THIS ONE? LINE 68? */ 
     GLint y0 = 150; 
     GLint xEnd = 200; 
     GLint yEnd = 200; 
    glClear(GL_COLOR_BUFFER_BIT); 
    glColor3f(1.0,0.0,0.0); 
    glPointSize(4.0); 
     lineBres(x0,y0,xEnd,yEnd); 
} 
+0

您会注意到以下一些错误是由第一个错误引起的。纠正第一个错误,通常会清除编译消息中的数十个错误:-) – pmg 2010-10-03 13:16:23