GVKun编程网logo

react+babel2+fis3实现jsx报错Adjacent JSX elements must be wrapped in an enclosing tag (10:0)

11

本文将为您提供关于react+babel2+fis3实现jsx报错AdjacentJSXelementsmustbewrappedinanenclosingtag(10:0)的详细介绍,同时,我们还将

本文将为您提供关于react+babel2+fis3实现jsx报错Adjacent JSX elements must be wrapped in an enclosing tag (10:0)的详细介绍,同时,我们还将为您提供关于3D Computer Grapihcs Using OpenGL - 15 Draw Element Instanced、3D Computer Grapihcs Using OpenGL - 16 使用 DrawElementsInstanced 绘制立方体、Android Studio 报错 must either be declared abstract or implement abstract method、babel-loader jsx SyntaxError: Unexpected token的实用信息。

本文目录一览:

react+babel2+fis3实现jsx报错Adjacent JSX elements must be wrapped in an enclosing tag (10:0)

react+babel2+fis3实现jsx报错Adjacent JSX elements must be wrapped in an enclosing tag (10:0)

Adjacent JSX elements must be wrapped in an enclosing tag (10:0)

1.报错内容:Adjacent JSX elements must be wrapped in an enclosing tag (10:0)




2.解决方法


2.1注释{/* 注释 */}可能写在render中return语句前面


2.2jsx中在render中return中返回的html元素可能返回不止一个元素:



2.3正确写法:

3D Computer Grapihcs Using OpenGL - 15 Draw Element Instanced

3D Computer Grapihcs Using OpenGL - 15 Draw Element Instanced

友情提示:继续本节之前,需要保存此前的代码,本节为了试验,会对代码做一些修改,但后续的修改需要我们把代码返回之前的进度。

OpenGL 内置支持 Instancing,有专门的函数来处理这件事情。

为了方便,我们先使用最简单的三角形来学习

先修改 sendDataToOpenGL () 函数:

 1 void MyGlWindow::sendDataToOpenGL()
 2 {
 3     GLfloat tri[] = {
 4         -1.0f,+0.0f,
 5         -1.0f,+1.0f,
 6         -0.9f,+0.0f
 7     };
 8 
 9     GLuint vertexBufferID;
10     glGenBuffers(1, &vertexBufferID);
11     glBindBuffer(GL_ARRAY_BUFFER, vertexBufferID);
12     glBufferData(GL_ARRAY_BUFFER, sizeof(tri), tri, GL_STATIC_DRAW);
13     glEnableVertexAttribArray(0);
14     glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, 0);
15 
16     GLushort indices[] = { 0,1,2 };
17     GLuint indexArrayBufferID;
18     glGenBuffers(1, &indexArrayBufferID);
19     glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexArrayBufferID);
20     glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
21     numIndices = 3;
22 }

相应的修改 vertexshader

 1 #version 430                           
 2                                        
 3 in layout(location=0) vec2 position;   
 4                                       
 5 out vec3 passingColor;
 6                                        
 7 void main()                            
 8 {                                      
 9   
10   gl_Position = vec4(position.xy,0,1);
11   passingColor= vec3(1,0,0);           
12 }

以及 paintGL () 函数:

1 void MyGlWindow::paintGL()
2 {
3     glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
4     glViewport(0, 0, width(), height());
5 
6     glDrawElements(GL_TRIANGLES, numIndices, GL_UNSIGNED_SHORT, 0);
7 }

 

进行了上述修改之后,会在画布左上角绘制一个简单的红色三角形。

假如我们现在需要达到这样的目的:复制这个三角形多次,每次都横向偏移一定的量。

为了使用 OpenGL 提供的 Instancing 方法,避免每次都调用 glDrawElements,我们可以提供一个数组,作为偏移信息数组。

在 sendDataToOpenGL () 函数中定义一个 GLfloat 数组,把它绑定到 GL_ARRAY_BUFFER 上,开启一个新的通道,把数据传输进去。

最后在 paintGL 中使用 glDrawElementsInstanced 函数来绘制多个实例。

具体代码如下:

 1 void MyGlWindow::sendDataToOpenGL()
 2 {
 3     GLfloat tri[] = {
 4         -1.0f,+0.0f,
 5         -1.0f,+1.0f,
 6         -0.9f,+0.0f
 7     };
 8 
 9     GLuint vertexBufferID;
10     glGenBuffers(1, &vertexBufferID);
11     glBindBuffer(GL_ARRAY_BUFFER, vertexBufferID);
12     glBufferData(GL_ARRAY_BUFFER, sizeof(tri), tri, GL_STATIC_DRAW);
13     glEnableVertexAttribArray(0);
14     glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, 0);
15 
16     GLfloat offsets[] = {0.0f, 0.5f, 1.0f, 1.2f, 1.6f};
17     GLuint offsetsBufferID;
18     glGenBuffers(1, &offsetsBufferID);
19     glBindBuffer(GL_ARRAY_BUFFER, offsetsBufferID);
20     glBufferData(GL_ARRAY_BUFFER, sizeof(offsets), offsets, GL_STATIC_DRAW);
21     glEnableVertexAttribArray(1);
22     glVertexAttribPointer(1, 1, GL_FLOAT, GL_FALSE, 0, 0);
23     glVertexAttribDivisor(1, 1);
24 
25     GLushort indices[] = { 0,1,2 };
26     GLuint indexArrayBufferID;
27     glGenBuffers(1, &indexArrayBufferID);
28     glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexArrayBufferID);
29     glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
30     numIndices = 3;
31 }

16-23 行是新增内容。

16 行定义了一个 GLfloat 数组,有 5 个元素。

17-20 行我们已经很熟悉,不再解释了

21 行开启了通道 1

22 行设定了通道 1 的数据格式,第一个参数表示通道 1,第二个参数表示每 1 个数据作为一个单元

23 行 glVertexAttribDivsior 是个新函数,它是配合实例化绘制的。没有它的情况下,tri 数组每 2 个元素结合 offsets 数组的 1 个元素,也就是说 offsets 数组每个元素只用一次。有了这个函数之后,offsets 数组的每个元素都要做一次循环,和 tri 数组的每组元素结合一次,形成 5 个批次。

修改 VertexShader:

 1 #version 430                           
 2                                        
 3 in layout(location=0) vec2 position;   
 4 in layout(location=1) float offset;  
 5                                       
 6 out vec3 passingColor;
 7                                        
 8 void main()                            
 9 {                                      
10   
11   gl_Position = vec4(position.x + offset, + position.y,0,1);
12   passingColor= vec3(1,0,0);           
13 }

增加了第 4 行,对应 sendDataToOpenGL 的新增通道 1.

第 11 行中,我们把 position 的 x 坐标进行了 offset 的偏移。

最后修改 paintGL () 函数:

1 void MyGlWindow::paintGL()
2 {
3     glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
4     glViewport(0, 0, width(), height());
5     glDrawElementsInstanced(GL_TRIANGLES, numIndices, GL_UNSIGNED_SHORT, 0, 5);
6 }

第 5 行使用 glDrawElementsInstanced 函数代替 glDrawElements 函数,进行了批量绘制。

第五个参数表示绘制五个实例。

最后效果:

3D Computer Grapihcs Using OpenGL - 16 使用 DrawElementsInstanced 绘制立方体

3D Computer Grapihcs Using OpenGL - 16 使用 DrawElementsInstanced 绘制立方体

我们使用 15 节学到的知识来绘制 14 节的立方体。

在第 14 节我们使用了两次 glDrawElements 实现了 OpenGL 实例化,发现这样仍然不太方便,如果需要绘制成千上万的立方体,就需要手写成千上万次的 glDrawElements ().

而 15 节我们知道了 glDrawElementsInstanced 函数可以支持批量绘制。

我们需要绘制的多个立方体唯一不同的只是转换矩阵,为了达到这个目的,我们只需要定义一组不同的变换矩阵,采用 glDrawElementsInstanced 即可达到目的。

构建矩阵的更简便写法

在进行之前,先介绍一个创建平移和旋转矩阵的更简单的写法:

我们此前构建平移和旋转矩阵都是使用如下的语法:

glm::translate(glm::mat4(), glm::vec3(0.0f,0.0f,3.0f)); //平移矩阵

glm::rotate(glm::mat4(), 125.0f, glm::vec3(1.0f, 0.0f,0.0f)); //旋转矩阵

 

发现第一个参数都需要一个矩阵,这个写法不利于我们本节的实践。

有一个简化的方法:

引入头文件 <glm\gtx\transform.hpp>

然后上述两个矩阵的构建都可以省略掉第一个参数。

glm::translate(glm::vec3(0.0f,0.0f,3.0f)); //平移矩阵

glm::rotate(125.0f, glm::vec3(1.0f, 0.0f,0.0f)); //旋转矩阵

 

准备数据

先从修改 sendDataToOpenGL () 函数开始修改:

 1 void MyGlWindow::sendDataToOpenGL()
 2 {
 3     
 4     ShapeData shape = ShapeGenerator::makeCube();
 5 
 6     GLuint vertexBufferID;
 7     glGenBuffers(1, &vertexBufferID);
 8     glBindBuffer(GL_ARRAY_BUFFER, vertexBufferID);
 9     glBufferData(GL_ARRAY_BUFFER, shape.vertexBufferSize(), shape.vertices, GL_STATIC_DRAW);
10 
11     glEnableVertexAttribArray(0);
12     glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(GLfloat) * 6, 0);
13 
14 
15     GLuint indexBufferID;
16     glGenBuffers(1, &indexBufferID);
17     glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBufferID);
18     glBufferData(GL_ELEMENT_ARRAY_BUFFER, shape.indexBufferSize(), shape.indices, GL_STATIC_DRAW);
19 
20     glEnableVertexAttribArray(1);
21     glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(GLfloat) * 6, (char*)(sizeof(GLfloat) * 3));
22 
23     numIndices = shape.numIndices;
24     shape.cleanUp();
25 
26     //instancing
27     glm::mat4 projectionMatrix = glm::perspective(30.0f, ((float)width()) / height(), 0.1f, 10.0f);
28 
29     glm::mat4 fullTransforms[] =
30     {
31         projectionMatrix * glm::translate(glm::vec3(0.0f, 0.0f, -3.0f)) * glm::rotate(54.0f,glm::vec3(1.0f, 0.0f, 0.0f)),
32         projectionMatrix * glm::translate(glm::vec3(2.0f, 0.0f, -4.0f)) * glm::rotate(126.0f, glm::vec3(0.0f, 1.0f, 0.0f))
33     };
34 
35     GLuint transformMatrixBufferID;
36     glGenBuffers(1, &transformMatrixBufferID);
37     glBindBuffer(GL_ARRAY_BUFFER, transformMatrixBufferID);
38     glBufferData(GL_ARRAY_BUFFER, sizeof(fullTransforms), fullTransforms, GL_STATIC_DRAW);
39     glVertexAttribPointer(2, 4, GL_FLOAT, GL_FALSE, sizeof(glm::mat4), (void*)(sizeof(float) * 0));
40     glVertexAttribPointer(3, 4, GL_FLOAT, GL_FALSE, sizeof(glm::mat4), (void*)(sizeof(float) * 4));
41     glVertexAttribPointer(4, 4, GL_FLOAT, GL_FALSE, sizeof(glm::mat4), (void*)(sizeof(float) * 8));
42     glVertexAttribPointer(5, 4, GL_FLOAT, GL_FALSE, sizeof(glm::mat4), (void*)(sizeof(float) * 12));
43     glEnableVertexAttribArray(2);
44     glEnableVertexAttribArray(3);
45     glEnableVertexAttribArray(4);
46     glEnableVertexAttribArray(5);
47     glVertexAttribDivisor(2, 1);
48     glVertexAttribDivisor(3, 1);
49     glVertexAttribDivisor(4, 1);
50     glVertexAttribDivisor(5, 1);
51 }

从 27 行起是新增内容,27 行定义了一个 projectionMatrix。

29-33 行定义了一个 “变换矩阵数组”,包含两个元素。它们的形式都是 projectionMatrix * translationMatrix * rotationMatrix。注意这里的 tranlationMatrix 和 rotationMatrix 都使用了前面介绍的简化方法去定义。

37 行再次绑定 transformMatrixBuffer 到 GL_ARRAY_BUFFER 上,可能会有疑问:之前给 GL_ARRAY_BUFFER 绑定了 Vertex Buffer(8 行),这里又绑定其他的东西,会把之前绑定的数据破坏掉吗? 

答案是不会,只要在这之前使用了 glAttribPointer 函数(12 行),之前绑定的数据就是安全的。

39-50 行和上节学习的内容是相似的,但是这里用到了 4 组命令,原因是一个 mat4 要被当做四个 float 对待。

还要修改 paintGL ():

1 void MyGlWindow::paintGL()
2 {
3     glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
4     glViewport(0, 0, width(), height());
5     glDrawElementsInstanced(GL_TRIANGLES, numIndices, GL_UNSIGNED_SHORT, 0, 2);
6 }

我们看到 14 节中 paintGL () 中的很多代码都不见了,原因是这些工作都在 sendDataToOpenGL () 中准备了。这里只需要使用一个 glDrawElementsInstanced 函数去进行批量绘制即可。

再看修改后的 VertexShader:

 1 #version 430                           
 2                                        
 3 in layout(location=0) vec3 position;   
 4 in layout(location=1) vec3 color;  
 5 in layout(location=2) mat4 fullTransformMatrix;                                   
 6                                     
 7 out vec3 passingColor;
 8                                        
 9 void main()                            
10 {  
11   gl_Position = fullTransformMatrix * vec4(position,1);
12   passingColor= color;           
13 }

第五行虽然只有一个 location=2,但是对应的 sendDataToOpenGL () 中却有 2,3,4,5 四个通道,原因是我们也要把这里的 mat4 当做四个 float 对待,可以想象成这样的情景:

1 in layout(location=2) vec4 fullTransformMatrix_part1;    
2 in layout(location=3) vec4 fullTransformMatrix_part2;    
3 in layout(location=4) vec4 fullTransformMatrix_part3;    
4 in layout(location=5) vec4 fullTransformMatrix_part4;

编译运行后得到的结果和第 14 节是一样的。

这样做的好处是扩展起来非常容易,例如我们需要再多绘制几个立方体,只需要向 sendDataToOpenGL()函数中的 fullTransforms 数组中增加元素,并修改 paintLG () 函数中第 5 行的最后一个参数即可。

 

Android Studio 报错 must either be declared abstract or implement abstract method

Android Studio 报错 must either be declared abstract or implement abstract method

Android Studio 报错 must either be declared abstract or implement abstract method 解决方法
可以单击错误行,在行首出现红色电灯泡,点“implement methods“,就会自动补全缺失的代码

babel-loader jsx SyntaxError: Unexpected token

babel-loader jsx SyntaxError: Unexpected token

我是 React + Webpack 的初学者。

我在我的 hello world web 应用程序中发现了一个奇怪的错误。

我在 webpack 中使用 babel-loader 来帮助我将 jsx 转换为 js,但似乎 babel 无法理解 jsx 语法。

这是我的依赖项:

"devDependencies": {
  "babel-core": "^6.0.14","babel-loader": "^6.0.0","webpack": "^1.12.2","webpack-dev-server": "^1.12.1"
},"dependencies": {
  "react": "^0.14.1"
}

这是我的webpack.config.js

var path = require('path');
module.exports = {
  entry: ['webpack/hot/dev-server',path.resolve(__dirname,'app/main.js')],output: {
    path: path.resolve(__dirname,'build'),filename: 'bundle.js'
  },module: {
      loaders: [
          { test: /\.js$/,exclude: /node_modules/,loader: "babel-loader"}
      ]
  }
};

这是我的app/main.js

var React = require("react");
React.render(<h1>hello world</h1>,document.getElementById("app"));

这是错误信息

ERROR in ./app/main.js
Module build failed: SyntaxError: ~/**/app/main.js: Unexpected token (2:13)
  1 | var React = require("react");
> 2 | React.render(<h1>hello world</h1>,document.getElementById("app"));
    |              ^
at Parser.pp.raise (~/**/node_modules/babylon/lib/parser/location.js:24:13)

谢谢你们。

今天关于react+babel2+fis3实现jsx报错Adjacent JSX elements must be wrapped in an enclosing tag (10:0)的介绍到此结束,谢谢您的阅读,有关3D Computer Grapihcs Using OpenGL - 15 Draw Element Instanced、3D Computer Grapihcs Using OpenGL - 16 使用 DrawElementsInstanced 绘制立方体、Android Studio 报错 must either be declared abstract or implement abstract method、babel-loader jsx SyntaxError: Unexpected token等更多相关知识的信息可以在本站进行查询。

本文标签: